@devries-cameron20 / t3code-1 / commits / c7ce74fce6
Fork 0

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

perf(server): batch provider commits without blocking unrelated writes (#38544)

Co-authored-by: Julius Marminge <julius0216@outlook.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Cursor <cursoragent@cursor.com>

Yash Singh committed Nov 18, 2026 at 15:41 UTC c7ce74fce68c716c1a02051fa7c003e3802351ce
7 files changed +672 -150
apps/server/src/orchestration-v2/EventSink.ts
+76 -23
@@ -72,12 +72,22 @@ export type EventSinkV2Error = typeof EventSinkV2Error.Type;
72
73 interface ProviderBatchPublication {
74 readonly publish: Array<Effect.Effect<void>>;
75 + readonly projection: ProjectionStore.ProviderBatchProjection;
76 + /** The projection high-water mark, recorded once before the batch commits. */
77 + projectedSequence: number | undefined;
78 eventCount: number;
79 }
80 const CurrentProviderBatch = Context.Reference<ProviderBatchPublication | undefined>(
81 "t3/orchestration-v2/EventSink/CurrentProviderBatch",
82 { defaultValue: () => undefined },
83 );
84 +// Projecting `provider-thread.updated` also rewrites its app thread's row.
85 +const projectedThreadIds = (events: ReadonlyArray<OrchestrationV2DomainEvent>) =>
86 + events.flatMap((event) =>
87 + event.type === "provider-thread.updated" && event.payload.appThreadId !== null
88 + ? [event.threadId, event.payload.appThreadId]
89 + : [event.threadId],
90 + );
91
92 export interface EventSinkV2Shape {
93 /** Items that are not provider snapshots, such as a consumer's own control items, never batch. */
@@ -409,6 +419,30 @@ const layerBase: Layer.Layer<
419 );
420 };
421
422 + // The enclosing write or commit span already traces this projection bookkeeping.
423 + const recordProjectedSequence = Effect.fnUntraced(function* (sequence: number) {
424 + const now = DateTime.formatIso(yield* DateTime.now);
425 + yield* sql`
426 + INSERT INTO orchestration_v2_projection_metadata (
427 + projection_name,
428 + schema_version,
429 + last_sequence,
430 + updated_at
431 + )
432 + VALUES (
433 + 'thread-projections',
434 + ${ProjectionStore.ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION},
435 + ${sequence},
436 + ${now}
437 + )
438 + ON CONFLICT(projection_name)
439 + DO UPDATE SET
440 + schema_version = excluded.schema_version,
441 + last_sequence = excluded.last_sequence,
442 + updated_at = excluded.updated_at
443 + `;
444 + });
445 +
446 const applyStoredEvents =
447 /* This helper runs for individual orchestration events; the enclosing operation owns the span. */
448 Effect.fnUntraced(function* (storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>) {
@@ -417,26 +451,9 @@ const layerBase: Layer.Layer<
451 });
452 const sequence = storedEvents.at(-1)?.sequence;
453 if (sequence !== undefined) {
420 - const now = DateTime.formatIso(yield* DateTime.now);
421 - yield* sql`
422 - INSERT INTO orchestration_v2_projection_metadata (
423 - projection_name,
424 - schema_version,
425 - last_sequence,
426 - updated_at
427 - )
428 - VALUES (
429 - 'thread-projections',
430 - ${ProjectionStore.ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION},
431 - ${sequence},
432 - ${now}
433 - )
434 - ON CONFLICT(projection_name)
435 - DO UPDATE SET
436 - schema_version = excluded.schema_version,
437 - last_sequence = excluded.last_sequence,
438 - updated_at = excluded.updated_at
439 - `;
454 + const batch = yield* CurrentProviderBatch;
455 + if (batch !== undefined) batch.projectedSequence = sequence;
456 + else yield* recordProjectedSequence(sequence);
457 }
458 // Read lineage here, on the connection this transaction holds; see makeRunOutcomeRecorder.
459 return yield* runOutcomes.prepare(storedEvents);
@@ -476,6 +493,7 @@ const layerBase: Layer.Layer<
493 const batch = yield* CurrentProviderBatch;
494 if (batch !== undefined) batch.eventCount += input.events.length;
495 const { committed } = yield* commitThenPublish(
496 + projectedThreadIds(input.events),
497 Effect.gen(function* () {
498 // Check ownership in the same transaction that claims the native session.
499 const imported = input.unlessNativeThreadOwned;
@@ -530,6 +548,7 @@ const layerBase: Layer.Layer<
548 });
549
550 return yield* commitThenPublish(
551 + [input.threadId, ...projectedThreadIds(input.events)],
552 Effect.gen(function* () {
553 const rows = yield* sql<{
554 readonly status: string;
@@ -611,6 +630,7 @@ const layerBase: Layer.Layer<
630 });
631
632 return yield* commitThenPublish(
633 + projectedThreadIds(input.events),
634 Effect.gen(function* () {
635 const rows = yield* sql<{
636 readonly active_attempt_id: string | null;
@@ -664,6 +684,7 @@ const layerBase: Layer.Layer<
684 events: ReadonlyArray<OrchestrationV2DomainEvent>,
685 ) =>
686 commitThenPublish(
687 + projectedThreadIds(events),
688 Effect.gen(function* () {
689 const rows = yield* readStoredPayload;
690 if (rows[0]?.payload_json !== expectedJson) {
@@ -764,6 +785,7 @@ const layerBase: Layer.Layer<
785 input: Parameters<EventSinkV2Shape["commitCommand"]>[0],
786 ) {
787 const result = yield* commitThenPublish(
788 + [input.threadId, ...projectedThreadIds(input.events)],
789 Effect.gen(function* () {
790 const reserved = yield* commandReceipts.insertIfAbsent({
791 commandId: input.commandId,
@@ -909,6 +931,7 @@ const layerBase: Layer.Layer<
931 const commitProjectCommandEffect = Effect.fn("orchestrationV2.EventSink.commitProjectCommand")(
932 function* (input: Parameters<EventSinkV2Shape["commitProjectCommand"]>[0]) {
933 const result = yield* commitThenPublish(
934 + [],
935 Effect.gen(function* () {
936 const reserved: CommandReceiptStore.ProjectCommandReceiptV2 = {
937 commandId: input.commandId,
@@ -1037,7 +1060,9 @@ const layerBase: Layer.Layer<
1060 );
1061 };
1062
1063 + // Waits only for queued provider snapshots of `threadIds`, the threads this write touches.
1064 const commitThenPublish = <A, E, R>(
1065 + threadIds: ReadonlyArray<ThreadId>,
1066 transaction: Effect.Effect<A, E, R>,
1067 publish: (committed: A) => Effect.Effect<void>,
1068 ) =>
@@ -1046,7 +1071,7 @@ const layerBase: Layer.Layer<
1071 ? Effect.flatMap(Effect.serviceOption(sql.transactionService), (existing) =>
1072 Option.isSome(existing)
1073 ? commitThenPublishNow(transaction, publish)
1049 - : coordinator.barrier(commitThenPublishNow(transaction, publish)),
1074 + : coordinator.barrier(commitThenPublishNow(transaction, publish), threadIds),
1075 )
1076 : transaction.pipe(
1077 Effect.tap((committed) =>
@@ -1061,12 +1086,33 @@ const layerBase: Layer.Layer<
1086 "orchestrationV2.EventSink.providerBatch",
1087 )(function* <A, E, R>(effect: Effect.Effect<A, E, R>) {
1088 if ((yield* CurrentProviderBatch) !== undefined) return yield* effect;
1064 - const batch: ProviderBatchPublication = { publish: [], eventCount: 0 };
1089 + const batch: ProviderBatchPublication = {
1090 + publish: [],
1091 + eventCount: 0,
1092 + projection: { nodeReads: new Map(), threadTouches: new Map() },
1093 + projectedSequence: undefined,
1094 + };
1095 let started = false;
1096 const result = yield* commitThenPublishNow(
1097 Effect.sync(() => {
1098 started = true;
1069 - }).pipe(Effect.andThen(Effect.provideService(effect, CurrentProviderBatch, batch))),
1099 + }).pipe(
1100 + Effect.andThen(
1101 + effect.pipe(
1102 + Effect.provideService(CurrentProviderBatch, batch),
1103 + Effect.provideService(ProjectionStore.ProviderBatchProjectionState, batch.projection),
1104 + Effect.tap(() =>
1105 + ProjectionStore.writeThreadTouches(sql, batch.projection.threadTouches).pipe(
1106 + Effect.andThen(
1107 + batch.projectedSequence === undefined
1108 + ? Effect.void
1109 + : recordProjectedSequence(batch.projectedSequence),
1110 + ),
1111 + ),
1112 + ),
1113 + ),
1114 + ),
1115 + ),
1116 () => Effect.forEach(batch.publish, (publish) => publish, { discard: true }),
1117 ).pipe(
1118 // Normalization mutates run-local state, so only retry before its transaction body starts.
@@ -1105,12 +1151,19 @@ const layerBase: Layer.Layer<
1151 const batch = yield* CurrentProviderBatch;
1152 const publicationCount = batch?.publish.length ?? 0;
1153 const eventCount = batch?.eventCount ?? 0;
1154 + const projectedSequence = batch?.projectedSequence;
1155 + const threadTouches = new Map(batch?.projection.threadTouches);
1156 return yield* sql.withTransaction(work).pipe(
1157 Effect.onError(() =>
1158 Effect.sync(() => {
1159 if (batch !== undefined) {
1160 batch.publish.length = publicationCount;
1161 batch.eventCount = eventCount;
1162 + batch.projectedSequence = projectedSequence;
1163 + batch.projection.nodeReads.clear();
1164 + batch.projection.threadTouches.clear();
1165 + for (const [threadId, updatedAt] of threadTouches)
1166 + batch.projection.threadTouches.set(threadId, updatedAt);
1167 }
1168 }),
1169 ),
apps/server/src/orchestration-v2/FoundationPersistence.test.ts
+246 -17
@@ -419,6 +419,209 @@ it.effect(
419 }).pipe(Effect.provide(Layer.fresh(layerTest))),
420 );
421
422 +function nodeComparisonEvent(thread: OrchestrationV2AppThread, now: DateTime.Utc) {
423 + const nodeId = NodeId.make(`node:${thread.id}`);
424 + return {
425 + id: EventId.make(`node-cache:${thread.id}`),
426 + type: "node.updated",
427 + threadId: thread.id,
428 + occurredAt: now,
429 + payload: {
430 + id: nodeId,
431 + threadId: thread.id,
432 + runId: null,
433 + parentNodeId: null,
434 + rootNodeId: nodeId,
435 + kind: "assistant_message",
436 + status: "running",
437 + countsForRun: false,
438 + providerThreadId: null,
439 + providerTurnId: null,
440 + nativeItemRef: null,
441 + runtimeRequestId: null,
442 + checkpointScopeId: null,
443 + startedAt: now,
444 + completedAt: null,
445 + },
446 + } satisfies OrchestrationV2DomainEvent;
447 +}
448 +
449 +it.effect("commits other threads' writes ahead of a thread's queued provider snapshots", () =>
450 + Effect.gen(function* () {
451 + const sink = yield* EventSink.EventSinkV2;
452 + const now = yield* DateTime.now;
453 + const [busy, independent] = ["busy", "independent"].map((id) =>
454 + makeThread(ThreadId.make(id), now),
455 + );
456 + yield* sink.write({
457 + events: [busy!, independent!].map((thread) =>
458 + threadCreatedEvent({ id: `created:${thread.id}`, thread, now }),
459 + ),
460 + });
461 + const highWater = yield* sink.latestSequence();
462 + const busyNode = nodeComparisonEvent(busy!, now);
463 + const snapshot = {
464 + type: "node.updated" as const,
465 + driver: providerDriver,
466 + node: busyNode.payload,
467 + };
468 + const started = yield* Deferred.make<void>();
469 + const release = yield* Deferred.make<void>();
470 + const pulled = yield* Deferred.make<void>();
471 + let writes = 0;
472 + let pulls = 0;
473 + const input = yield* Queue.unbounded<typeof snapshot>();
474 + const provider = yield* sink
475 + .consumeProviderEvents(
476 + Stream.fromQueue(input).pipe(
477 + Stream.take(3),
478 + Stream.tap(() => (++pulls === 3 ? Deferred.succeed(pulled, undefined) : Effect.void)),
479 + ),
480 + () =>
481 + Effect.gen(function* () {
482 + const id = EventId.make(`busy-provider-${++writes}`);
483 + yield* sink.write({ events: [{ ...busyNode, id }] });
484 + if (writes > 1) return;
485 + yield* Deferred.succeed(started, undefined);
486 + yield* Deferred.await(release);
487 + }),
488 + Effect.succeed(false),
489 + Effect.succeed(true),
490 + )
491 + .pipe(Effect.forkScoped({ startImmediately: true }));
492 + yield* Queue.offer(input, snapshot);
493 + yield* Deferred.await(started);
494 + yield* Queue.offerAll(input, [snapshot, snapshot]);
495 + yield* Deferred.await(pulled);
496 + yield* TestClock.adjust(0);
497 + const writeTo = (event: OrchestrationV2DomainEvent, id: string) =>
498 + sink
499 + .write({ events: [{ ...event, id: EventId.make(id) }] })
500 + .pipe(Effect.forkScoped({ startImmediately: true }));
501 + const sameThread = yield* writeTo(busyNode, "busy-command");
502 + const otherThread = yield* writeTo(nodeComparisonEvent(independent!, now), "independent");
503 + const childOfBusy = yield* writeTo(
504 + {
505 + id: EventId.make("unused"),
506 + type: "provider-thread.updated",
507 + threadId: independent!.id,
508 + driver: providerDriver,
509 + providerInstanceId,
510 + occurredAt: now,
511 + payload: {
512 + id: ProviderThreadId.make("provider-thread:busy-child"),
513 + driver: providerDriver,
514 + providerInstanceId,
515 + providerSessionId: null,
516 + appThreadId: busy!.id,
517 + ownerNodeId: null,
518 + nativeThreadRef: null,
519 + nativeConversationHeadRef: null,
520 + status: "idle",
521 + firstRunOrdinal: 1,
522 + lastRunOrdinal: 1,
523 + handoffIds: [],
524 + forkedFrom: null,
525 + createdAt: now,
526 + updatedAt: now,
527 + },
528 + },
529 + "busy-child",
530 + );
531 + yield* TestClock.adjust(0);
532 + yield* Deferred.succeed(release, undefined);
533 + yield* Fiber.join(provider);
534 + yield* Fiber.join(sameThread);
535 + yield* Fiber.join(otherThread);
536 + yield* Fiber.join(childOfBusy);
537 + const store = yield* EventStore.EventStoreV2;
538 + const events = yield* store.read({ afterSequence: highWater }).pipe(Stream.runCollect);
539 + assert.deepEqual(
540 + events.map((stored) => stored.event.id),
541 + [
542 + "busy-provider-1",
543 + "independent",
544 + "busy-provider-2",
545 + "busy-provider-3",
546 + "busy-command",
547 + "busy-child",
548 + ],
549 + );
550 + }).pipe(Effect.scoped, Effect.provide(Layer.fresh(layerTest))),
551 +);
552 +
553 +it.effect(
554 + "reuses node comparisons and writes thread bookkeeping once per provider transaction",
555 + () =>
556 + Effect.gen(function* () {
557 + const sink = yield* EventSink.EventSinkV2;
558 + const projections = yield* ProjectionStore.ProjectionStoreV2;
559 + const sql = yield* SqlClient.SqlClient;
560 + const now = yield* DateTime.now;
561 + const thread = makeThread(ThreadId.make("node-comparison-cache"), now);
562 + yield* sink.write({
563 + events: [threadCreatedEvent({ id: "node-cache-created", thread, now })],
564 + });
565 + const event = nodeComparisonEvent(thread, now);
566 + const statements = { nodeReads: 0, threadTouches: 0, highWaterWrites: 0 };
567 + const observe: Statement.Transformer = (statement) => {
568 + const text = statement.compile()[0];
569 + if (
570 + text.includes("SELECT payload_json FROM orchestration_v2_projection_nodes WHERE node_id")
571 + )
572 + statements.nodeReads++;
573 + if (text.includes("UPDATE orchestration_v2_projection_threads")) statements.threadTouches++;
574 + if (text.includes("INSERT INTO orchestration_v2_projection_metadata"))
575 + statements.highWaterWrites++;
576 + return Effect.succeed(statement);
577 + };
578 + const lastOccurredAt = DateTime.add(now, { minutes: 3 });
579 + yield* sink
580 + .withProviderBatch(
581 + Effect.gen(function* () {
582 + for (let n = 0; n < 21; n++)
583 + assert.isFalse(yield* projections.isNodeStored(event.payload));
584 + assert.equal(statements.nodeReads, 1);
585 + yield* sink.write({ events: [event] });
586 + assert.isTrue(yield* projections.isNodeStored(event.payload));
587 + for (const minutes of [1, 2, 3]) {
588 + yield* sink.write({
589 + events: [
590 + {
591 + ...event,
592 + id: EventId.make(`node-cache-touch:${minutes}`),
593 + occurredAt: DateTime.add(now, { minutes }),
594 + },
595 + ],
596 + });
597 + }
598 + assert.deepEqual([statements.threadTouches, statements.highWaterWrites], [0, 0]);
599 + }),
600 + )
601 + .pipe(Effect.provideService(Statement.CurrentTransformer, observe));
602 + assert.deepEqual([statements.threadTouches, statements.highWaterWrites], [1, 1]);
603 + assert.deepEqual(
604 + DateTime.toEpochMillis((yield* projections.getThread(thread.id)).updatedAt),
605 + DateTime.toEpochMillis(lastOccurredAt),
606 + );
607 + assert.deepEqual(
608 + yield* sql`SELECT last_sequence FROM orchestration_v2_projection_metadata
609 + WHERE projection_name = 'thread-projections'`,
610 + [{ last_sequence: yield* sink.latestSequence() }],
611 + );
612 + const completed = { ...event.payload, status: "completed" as const, completedAt: now };
613 + yield* sink.write({
614 + events: [{ ...event, id: EventId.make("node-cache-completed"), payload: completed }],
615 + });
616 + yield* sink.withProviderBatch(
617 + Effect.gen(function* () {
618 + assert.isFalse(yield* projections.isNodeStored(event.payload));
619 + assert.isTrue(yield* projections.isNodeStored(completed));
620 + }),
621 + );
622 + }).pipe(Effect.scoped, Effect.provide(Layer.fresh(layerTest))),
623 +);
624 +
625 it.effect.each(
626 (["none", "duplicate", "after-write"] as const).flatMap((failure) =>
627 [1, 10].map((streams) => ({ failure, streams })),
@@ -427,7 +630,7 @@ it.effect.each(
630 "shares the writer and isolates failure=$failure across $streams independent consumers",
631 ({ failure, streams }) => {
632 const rollback = failure !== "none";
430 - const failedIndex = streams - 1;
633 + const failedIndex = failure === "after-write" ? 0 : streams - 1;
634 const jobError = { _tag: "InvalidUpdate" } as const;
635 const writes: number[] = [];
636 const tracer = Tracer.make({
@@ -455,6 +658,8 @@ it.effect.each(
658 threadCreatedEvent({ id: `created:${index}`, thread, now }),
659 ),
660 });
661 + const node = nodeComparisonEvent(threads[0]!, now);
662 + yield* sink.write({ events: [node] });
663 const highWater = yield* sink.latestSequence();
664 const store = yield* EventStore.EventStoreV2;
665 const publications = yield* Queue.unbounded<EventId>();
@@ -465,7 +670,7 @@ it.effect.each(
670 const pulled = yield* Deferred.make<void>();
671 let arrivals = 0;
672 const inputs = yield* Effect.forEach(threads, () =>
468 - Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>(),
673 + Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event, Cause.Done>(),
674 );
675 const consumers = yield* Effect.forEach(inputs, (input, index) =>
676 sink
@@ -477,8 +682,11 @@ it.effect.each(
682 ),
683 (raw) =>
684 raw.type === "message.updated"
480 - ? sink
481 - .write({
685 + ? Effect.gen(function* () {
686 + if (failure === "after-write" && index !== failedIndex) {
687 + assert.isTrue(yield* projections.isNodeStored(node.payload));
688 + }
689 + yield* sink.write({
690 events: [
691 {
692 id: EventId.make(
@@ -487,19 +695,29 @@ it.effect.each(
695 : `shared-event:${index}`,
696 ),
697 threadId: raw.message.threadId,
490 - occurredAt: now,
698 + occurredAt: DateTime.add(now, { minutes: index + 1 }),
699 type: "message.updated",
700 payload: raw.message,
701 },
702 + ...(failure === "after-write" && index === failedIndex
703 + ? [
704 + {
705 + ...node,
706 + id: EventId.make("rolled-back-node"),
707 + occurredAt: DateTime.add(now, { hours: 1 }),
708 + payload: {
709 + ...node.payload,
710 + status: "completed" as const,
711 + completedAt: now,
712 + },
713 + },
714 + ]
715 + : []),
716 ],
495 - })
496 - .pipe(
497 - Effect.andThen(
498 - failure === "after-write" && index === failedIndex
499 - ? Effect.fail(jobError)
500 - : Effect.void,
501 - ),
502 - )
717 + });
718 + if (failure === "after-write" && index === failedIndex)
719 + return yield* Effect.fail(jobError);
720 + })
721 : Effect.void,
722 Effect.succeed(false),
723 Effect.succeed(true),
@@ -527,10 +745,10 @@ it.effect.each(
745 });
746 }
747 yield* Deferred.await(pulled);
530 - yield* TestClock.adjust(0);
531 - yield* sink.write({ events: [] });
748 + yield* Effect.forEach(inputs, Queue.end, { discard: true });
749 + const exits = yield* Effect.forEach(consumers, Fiber.join);
750 if (rollback) {
533 - const exit = yield* Fiber.join(consumers[failedIndex]!);
751 + const exit = exits[failedIndex]!;
752 assert.isTrue(Exit.isFailure(exit));
753 if (failure === "after-write" && Exit.isFailure(exit))
754 assert.strictEqual(Cause.squash(exit.cause), jobError);
@@ -544,10 +762,21 @@ it.effect.each(
762 assert.equal(yield* Queue.size(publications), 0);
763 assert.deepEqual(writes, rollback && streams === 1 ? [] : [streams - (rollback ? 1 : 0)]);
764 assert.equal(yield* sink.latestSequence(), highWater + streams - (rollback ? 1 : 0));
765 + const sql = yield* SqlClient.SqlClient;
766 + assert.deepEqual(
767 + yield* sql`SELECT last_sequence FROM orchestration_v2_projection_metadata
768 + WHERE projection_name = 'thread-projections'`,
769 + [{ last_sequence: yield* sink.latestSequence() }],
770 + );
771 for (let index = 0; index < threads.length; index++) {
772 + const failed = rollback && index === failedIndex;
773 assert.lengthOf(
774 (yield* projections.getThreadRecords(threads[index]!.id, ["messages"])).messages,
550 - rollback && index === failedIndex ? 0 : 1,
775 + failed ? 0 : 1,
776 + );
777 + assert.equal(
778 + DateTime.toEpochMillis((yield* projections.getThread(threads[index]!.id)).updatedAt),
779 + DateTime.toEpochMillis(failed ? now : DateTime.add(now, { minutes: index + 1 })),
780 );
781 }
782 }).pipe(
apps/server/src/orchestration-v2/ProjectionStore.ts
+51 -7
@@ -838,6 +838,34 @@ export class ProjectionStoreV2 extends Context.Service<ProjectionStoreV2, Projec
838 "t3/orchestration-v2/ProjectionStore/ProjectionStoreV2",
839 ) {}
840
841 +// The provider batch owns the writer, so no other write can change these rows before it commits.
842 +export interface ProviderBatchProjection {
843 + readonly nodeReads: Map<NodeId, string | undefined>;
844 + /** Thread `updated_at` bumps, written once by `writeThreadTouches` before the batch commits. */
845 + readonly threadTouches: Map<ThreadId, string>;
846 +}
847 +export class ProviderBatchProjectionState extends Context.Reference<
848 + ProviderBatchProjection | undefined
849 +>("t3/orchestration-v2/ProjectionStore/ProviderBatchProjectionState", {
850 + defaultValue: () => undefined,
851 +}) {}
852 +
853 +// Runs per projected event or once per batch; the projection or batch commit span traces it.
854 +export const writeThreadTouches = Effect.fnUntraced(function* (
855 + sql: SqlClient.SqlClient,
856 + touches: ReadonlyMap<ThreadId, string>,
857 +) {
858 + for (const [threadId, updatedAt] of touches) {
859 + yield* sql`
860 + UPDATE orchestration_v2_projection_threads
861 + SET
862 + updated_at = ${updatedAt},
863 + payload_json = json_set(payload_json, '$.updatedAt', ${updatedAt})
864 + WHERE thread_id = ${threadId}
865 + `;
866 + }
867 +});
868 +
869 export const ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION = 2;
870
871 function upsertById<T extends { readonly id: string }>(items: ReadonlyArray<T>, next: T): Array<T> {
@@ -2347,6 +2375,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2375 for (const [key, cached] of timelineIndexes) {
2376 if (cached.index.threadIds.includes(event.threadId)) timelineIndexes.delete(key);
2377 }
2378 + const batch = yield* ProviderBatchProjectionState;
2379 + // These rewrite the thread row from what they read, so a deferred bump must land first.
2380 + const rewrittenThreadId = event.type.startsWith("thread.")
2381 + ? event.threadId
2382 + : event.type === "provider-thread.updated"
2383 + ? event.payload.appThreadId
2384 + : null;
2385 + const deferredUpdatedAt =
2386 + rewrittenThreadId === null ? undefined : batch?.threadTouches.get(rewrittenThreadId);
2387 + if (
2388 + batch !== undefined &&
2389 + rewrittenThreadId !== null &&
2390 + deferredUpdatedAt !== undefined
2391 + ) {
2392 + batch.threadTouches.delete(rewrittenThreadId);
2393 + yield* writeThreadTouches(sql, new Map([[rewrittenThreadId, deferredUpdatedAt]]));
2394 + }
2395 switch (event.type) {
2396 case "thread.pull-request-link-updated": {
2397 const thread = yield* getThread(event.threadId);
@@ -2593,6 +2638,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2638 completed_at = excluded.completed_at,
2639 payload_json = excluded.payload_json
2640 `;
2641 + batch?.nodeReads.set(event.payload.id, payloadJson);
2642 break;
2643 }
2644 case "subagent.updated": {
@@ -3234,13 +3280,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3280 event.type !== "thread.guest-share-updated"
3281 ) {
3282 const updatedAt = yield* encodeThreadUpdatedAt(event.occurredAt);
3237 - yield* sql`
3238 - UPDATE orchestration_v2_projection_threads
3239 - SET
3240 - updated_at = ${updatedAt},
3241 - payload_json = json_set(payload_json, '$.updatedAt', ${updatedAt})
3242 - WHERE thread_id = ${event.threadId}
3243 - `;
3283 + if (batch !== undefined) batch.threadTouches.set(event.threadId, updatedAt);
3284 + else yield* writeThreadTouches(sql, new Map([[event.threadId, updatedAt]]));
3285 }
3286 },
3287 (effect, event) =>
@@ -5818,9 +5859,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5859 )(
5860 function* (node) {
5861 const payloadJson = yield* encodeNodePayload(node);
5862 + const cache = (yield* ProviderBatchProjectionState)?.nodeReads;
5863 + if (cache?.has(node.id)) return cache.get(node.id) === payloadJson;
5864 const rows = yield* sql<PayloadRow>`
5865 SELECT payload_json FROM orchestration_v2_projection_nodes WHERE node_id = ${node.id}
5866 `;
5867 + cache?.set(node.id, rows[0]?.payload_json);
5868 return rows[0]?.payload_json === payloadJson;
5869 },
5870 (effect, node) => effect.pipe(Effect.mapError(controlReadError(node.threadId))),
apps/server/src/orchestration-v2/ProviderCommitBatch.test.ts
+59
@@ -404,6 +404,65 @@ it.effect.each([false, true])(
404 }),
405 );
406
407 +it.effect.each(["none", "writer", "snapshots"] as const)(
408 + "runs another stream's control during a busy commit; splits slow backlogs for a write waiting on %s",
409 + (waitingOn) =>
410 + Effect.gen(function* () {
411 + const started = yield* Deferred.make<void>();
412 + const release = yield* Deferred.make<void>();
413 + let first = true;
414 + const recorder = yield* recordBatches(
415 + Effect.gen(function* () {
416 + if (!first) return;
417 + first = false;
418 + yield* Deferred.succeed(started, undefined);
419 + yield* Deferred.await(release);
420 + }),
421 + );
422 + const leading = yield* recorder
423 + .consume(Stream.make(event(-1)), recorder.process, Effect.succeed(false))
424 + .pipe(Effect.forkScoped({ startImmediately: true }));
425 + yield* Deferred.await(started);
426 + yield* recorder.consume(Stream.make(control), recorder.process, Effect.succeed(true));
427 + assert.deepEqual(yield* Queue.takeAll(recorder.output), [[control]]);
428 +
429 + const pulled = yield* Deferred.make<void>();
430 + const backlog = [event(0), event(1), event(2)];
431 + const slow = yield* recorder
432 + .consume(
433 + Stream.fromArray(backlog).pipe(
434 + Stream.tap((raw) =>
435 + raw === backlog.at(-1) ? Deferred.succeed(pulled, undefined) : Effect.void,
436 + ),
437 + ),
438 + (raw) => recorder.process(raw).pipe(Effect.andThen(TestClock.adjust("100 millis"))),
439 + Effect.succeed(false),
440 + )
441 + .pipe(Effect.forkScoped({ startImmediately: true }));
442 + yield* Deferred.await(pulled);
443 + yield* TestClock.adjust(0);
444 + // An unrelated write stands in for one waiting on the writer the batch holds.
445 + const writerFree = yield* Deferred.make<void>();
446 + const write = yield* recorder
447 + .barrier(
448 + waitingOn === "writer" ? Deferred.await(writerFree) : Effect.void,
449 + waitingOn === "snapshots" ? ["single-thread"] : ["other-thread"],
450 + )
451 + .pipe(Effect.forkScoped({ startImmediately: true }));
452 + yield* Deferred.succeed(release, undefined);
453 + yield* Fiber.join(leading);
454 + yield* Fiber.join(slow);
455 + yield* Deferred.succeed(writerFree, undefined);
456 + yield* Fiber.join(write);
457 + assert.deepEqual(
458 + yield* Queue.takeAll(recorder.output),
459 + waitingOn === "none"
460 + ? [[event(-1)], backlog]
461 + : [[event(-1)], ...backlog.map((raw) => [raw])],
462 + );
463 + }),
464 +);
465 +
466 it.effect.each(["failure", "stop"] as const)(
467 "flushes the prefix and respects a source %s barrier",
468 (mode) =>
apps/server/src/orchestration-v2/ProviderCommitBatch.ts
+148 -95
@@ -11,6 +11,8 @@ import * as Stream from "effect/Stream";
11
12 const MAX_EVENTS = 64;
13 const MAX_BYTES = 1024 * 1024;
14 +// While another write waits, a shared transaction stops taking jobs after this long.
15 +const MAX_HOLD_NANOS = 100_000_000n;
16 const encodeQueue = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
17
18 export const enabled = Config.Boolean("T3CODE_PROVIDER_COMMIT_BATCHING").pipe(
@@ -18,11 +20,28 @@ export const enabled = Config.Boolean("T3CODE_PROVIDER_COMMIT_BATCHING").pipe(
20 Effect.orDie,
21 );
22
21 -const isSnapshot = (event: { readonly type: string }) =>
23 +type Snapshot =
24 + | { readonly type: "node.updated"; readonly node: { readonly threadId: string } }
25 + | { readonly type: "message.updated"; readonly message: { readonly threadId: string } }
26 + | { readonly type: "turn_item.updated"; readonly turnItem: { readonly threadId: string } }
27 + | { readonly type: "plan.updated"; readonly plan: { readonly threadId: string } };
28 +const isSnapshot = (event: { readonly type: string }): event is Snapshot =>
29 event.type === "node.updated" ||
30 event.type === "message.updated" ||
31 event.type === "turn_item.updated" ||
32 event.type === "plan.updated";
33 +const snapshotThreadId = (event: Snapshot) => {
34 + switch (event.type) {
35 + case "node.updated":
36 + return event.node.threadId;
37 + case "message.updated":
38 + return event.message.threadId;
39 + case "turn_item.updated":
40 + return event.turnItem.threadId;
41 + case "plan.updated":
42 + return event.plan.threadId;
43 + }
44 +};
45
46 export class CommitBatchError extends Schema.TaggedError<CommitBatchError>()("CommitBatchError", {
47 cause: Schema.Defect(),
@@ -31,8 +50,8 @@ export const isCommitBatchError = Schema.is(CommitBatchError);
50
51 interface Job {
52 readonly owner: { active: boolean };
53 + readonly threadId: string;
54 readonly queuedAt: number;
35 - readonly snapshot: boolean;
55 readonly bytes: number;
56 readonly execute: Effect.Effect<void, CommitBatchError>;
57 readonly settle: (
@@ -40,11 +59,11 @@ interface Job {
59 preserveFailure: boolean,
60 ) => Effect.Effect<void>;
61 readonly cancel: Effect.Effect<void>;
62 + readonly settled: Effect.Effect<void>;
63 }
64 interface Batch {
65 readonly events: ReadonlyArray<Job>;
46 - readonly bytes: number;
47 - readonly reason: "ready" | "limit" | "control";
66 + readonly reason: "ready" | "limit";
67 }
68
69 function collect(queue: Queue.Queue<Job>) {
@@ -54,20 +73,15 @@ function collect(queue: Queue.Queue<Job>) {
73 let events: Array<Job> = [];
74 let bytes = 0;
75 const flush = (reason: Batch["reason"]) => {
57 - if (events.length > 0) batches.push({ events, bytes, reason });
76 + if (events.length > 0) batches.push({ events, reason });
77 events = [];
78 bytes = 0;
79 };
80 for (const job of ready) {
62 - if (!job.snapshot) {
63 - flush("control");
64 - batches.push({ events: [job], bytes: 0, reason: "control" });
65 - } else {
66 - if (events.length > 0 && bytes + job.bytes > MAX_BYTES) flush("limit");
67 - events.push(job);
68 - bytes += job.bytes;
69 - if (events.length >= MAX_EVENTS || bytes >= MAX_BYTES) flush("limit");
70 - }
81 + if (events.length > 0 && bytes + job.bytes > MAX_BYTES) flush("limit");
82 + events.push(job);
83 + bytes += job.bytes;
84 + if (events.length >= MAX_EVENTS || bytes >= MAX_BYTES) flush("limit");
85 }
86 flush("ready");
87 return Stream.fromArray(batches);
@@ -92,6 +106,8 @@ export const make = Effect.fn("ProviderCommitBatch.make")(function* (
106 const queue = yield* Queue.bounded<Job>(MAX_EVENTS);
107 const pending = new Set<Job>();
108 let closed = false;
109 + // Writes in `barrier` that have not finished; they wait on the batch or the writer it holds.
110 + let waitingWriters = 0;
111 const shutdown = Effect.gen(function* () {
112 closed = true;
113 yield* Queue.shutdown(queue);
@@ -102,7 +118,7 @@ export const make = Effect.fn("ProviderCommitBatch.make")(function* (
118 const enqueue = Effect.fnUntraced(function* <A, E, R>(
119 effect: Effect.Effect<A, E, R>,
120 token: { active: boolean },
105 - snapshot: boolean,
121 + threadId: string,
122 bytes: number,
123 ) {
124 const caller = yield* Effect.context<R>();
@@ -111,8 +127,8 @@ export const make = Effect.fn("ProviderCommitBatch.make")(function* (
127 let result: A;
128 const job: Job = {
129 owner: token,
130 + threadId,
131 queuedAt: yield* Clock.currentTimeMillis,
115 - snapshot,
132 bytes,
133 execute: effect.pipe(
134 Effect.tapCause((cause) =>
@@ -146,6 +162,7 @@ export const make = Effect.fn("ProviderCommitBatch.make")(function* (
162 pending.delete(job);
163 yield* Deferred.interrupt(reply);
164 }),
165 + settled: Deferred.await(reply).pipe(Effect.exit, Effect.asVoid),
166 };
167 yield* Effect.uninterruptibleMask((restore) =>
168 Effect.gen(function* () {
@@ -158,77 +175,111 @@ export const make = Effect.fn("ProviderCommitBatch.make")(function* (
175 );
176 return Deferred.await(reply);
177 });
178 + // Commits a prefix of `jobs` in one transaction and returns how many it took.
179 + const commitPrefix = (jobs: ReadonlyArray<Job>, reason: Batch["reason"]) =>
180 + Effect.uninterruptibleMask((restore) =>
181 + Effect.gen(function* () {
182 + const outcomes = new Map<Job, Exit.Exit<void, CommitBatchError>>();
183 + let taken = 0;
184 + const work = Effect.gen(function* () {
185 + // A retried transaction runs this again from the first job.
186 + outcomes.clear();
187 + taken = 0;
188 + const startedAt = yield* Clock.monotonicTimeNanos;
189 + for (const job of jobs) {
190 + if (
191 + taken > 0 &&
192 + waitingWriters > 0 &&
193 + (yield* Clock.monotonicTimeNanos) - startedAt >= MAX_HOLD_NANOS
194 + )
195 + return;
196 + taken++;
197 + if (!job.owner.active) {
198 + yield* job.cancel;
199 + continue;
200 + }
201 + const outcome = yield* (jobs.length > 1 ? isolate(job.execute) : job.execute).pipe(
202 + Effect.exit,
203 + );
204 + outcomes.set(job, outcome);
205 + if (Exit.isFailure(outcome)) {
206 + job.owner.active = false;
207 + if (jobs.length === 1) return yield* Effect.failCause(outcome.cause);
208 + }
209 + }
210 + });
211 + const exit = yield* restore(
212 + commit(work).pipe(Effect.provideService(CurrentWorker, yield* Effect.fiberId)),
213 + ).pipe(Effect.exit);
214 + const committed = taken === 0 ? jobs : jobs.slice(0, taken);
215 + yield* Effect.annotateCurrentSpan({
216 + "orchestration_v2.work_item_count": committed.length,
217 + "orchestration_v2.deferred_work_item_count": jobs.length - committed.length,
218 + "orchestration_v2.consumer_count": new Set(committed.map((job) => job.owner)).size,
219 + "orchestration_v2.raw_json_bytes": committed.reduce((bytes, job) => bytes + job.bytes, 0),
220 + "orchestration_v2.batch_flush_reason": reason,
221 + "orchestration_v2.backlog_batching": true,
222 + "orchestration_v2.batch_wait_ms":
223 + (yield* Clock.currentTimeMillis) - Math.min(...committed.map((job) => job.queuedAt)),
224 + });
225 + for (const job of committed) {
226 + const outcome = outcomes.get(job) ?? Exit.void;
227 + yield* job.settle(
228 + Exit.isSuccess(exit) ? outcome : exit,
229 + Exit.isSuccess(exit) ||
230 + (Exit.isFailure(exit) &&
231 + Exit.isFailure(outcome) &&
232 + Cause.squash(exit.cause) === Cause.squash(outcome.cause)),
233 + );
234 + }
235 + return committed.length;
236 + }),
237 + ).pipe(Effect.withSpan("orchestrationV2.ProviderCommitBatch.flush"));
238 if (enabled) {
239 yield* collect(queue).pipe(
163 - Stream.runForEach((batch) =>
164 - Effect.uninterruptibleMask((restore) =>
165 - Effect.gen(function* () {
166 - const jobs = batch.events.filter((job) => job.owner.active);
167 - for (const job of batch.events) if (!job.owner.active) yield* job.cancel;
240 + Stream.runForEach(
241 + Effect.fnUntraced(function* (batch: Batch) {
242 + let jobs = batch.events;
243 + while (jobs.length > 0) {
244 + for (const job of jobs) if (!job.owner.active) yield* job.cancel;
245 + jobs = jobs.filter((job) => job.owner.active);
246 if (jobs.length === 0) return;
169 - yield* Effect.annotateCurrentSpan({
170 - "orchestration_v2.work_item_count": jobs.length,
171 - "orchestration_v2.consumer_count": new Set(jobs.map((job) => job.owner)).size,
172 - "orchestration_v2.raw_json_bytes": batch.bytes,
173 - "orchestration_v2.batch_flush_reason": batch.reason,
174 - "orchestration_v2.backlog_batching": true,
175 - "orchestration_v2.batch_wait_ms":
176 - (yield* Clock.currentTimeMillis) - Math.min(...jobs.map((job) => job.queuedAt)),
177 - });
178 - const outcomes = new Map<Job, Exit.Exit<void, CommitBatchError>>();
179 - const work = Effect.forEach(
180 - jobs,
181 - (job) =>
182 - Effect.gen(function* () {
183 - if (!job.owner.active) return yield* job.cancel;
184 - const outcome = yield* (
185 - jobs.length > 1 && job.bytes > 0 ? isolate(job.execute) : job.execute
186 - ).pipe(Effect.exit);
187 - outcomes.set(job, outcome);
188 - if (Exit.isFailure(outcome)) {
189 - job.owner.active = false;
190 - if (jobs.length === 1) return yield* Effect.failCause(outcome.cause);
191 - }
192 - }),
193 - { discard: true },
194 - );
195 - const exit = yield* restore(
196 - (jobs.every((job) => job.bytes === 0) ? work : commit(work)).pipe(
197 - Effect.provideService(CurrentWorker, yield* Effect.fiberId),
198 - ),
199 - ).pipe(Effect.exit);
200 - for (const job of jobs) {
201 - const outcome = outcomes.get(job) ?? Exit.void;
202 - yield* job.settle(
203 - Exit.isSuccess(exit) ? outcome : exit,
204 - Exit.isSuccess(exit) ||
205 - (Exit.isFailure(exit) &&
206 - Exit.isFailure(outcome) &&
207 - Cause.squash(exit.cause) === Cause.squash(outcome.cause)),
208 - );
209 - }
210 - }),
211 - ).pipe(Effect.withSpan("orchestrationV2.ProviderCommitBatch.flush")),
247 + jobs = jobs.slice(yield* commitPrefix(jobs, batch.reason));
248 + }
249 + }),
250 ),
251 Effect.ensuring(shutdown),
252 Effect.forkScoped,
253 );
254 }
217 - const barrier = Effect.fnUntraced(function* <A, E, R>(effect: Effect.Effect<A, E, R>) {
255 + /**
256 + * Runs `effect` after queued snapshots for `threadIds` commit; omitted, after every queued
257 + * snapshot. Other threads' snapshots never delay it.
258 + */
259 + const barrier = Effect.fnUntraced(function* <A, E, R>(
260 + effect: Effect.Effect<A, E, R>,
261 + threadIds?: ReadonlyArray<string>,
262 + ) {
263 if (closed) return yield* Effect.interrupt;
219 - if (!enabled || pending.size === 0 || (yield* CurrentWorker) === (yield* Effect.fiberId))
220 - return yield* effect;
221 - const token = { active: true };
222 - return yield* enqueue(Effect.exit(effect), token, false, 0).pipe(
223 - Effect.flatten,
224 - Effect.orDie,
225 - Effect.flatten,
226 - Effect.ensuring(
227 - Effect.sync(() => {
228 - token.active = false;
264 + if (!enabled || (yield* CurrentWorker) === (yield* Effect.fiberId)) return yield* effect;
265 + return yield* Effect.uninterruptibleMask((restore) => {
266 + waitingWriters++;
267 + return restore(
268 + Effect.gen(function* () {
269 + for (const job of Array.from(pending)) {
270 + if (threadIds === undefined || threadIds.includes(job.threadId)) yield* job.settled;
271 + }
272 + if (closed) return yield* Effect.interrupt;
273 + return yield* effect;
274 }),
230 - ),
231 - );
275 + ).pipe(
276 + Effect.ensuring(
277 + Effect.sync(() => {
278 + waitingWriters--;
279 + }),
280 + ),
281 + );
282 + });
283 });
284 const consume = Effect.fnUntraced(function* <A extends { readonly type: string }, E, R, E2, R2>(
285 source: Stream.Stream<A, E, R>,
@@ -249,29 +300,31 @@ export const make = Effect.fn("ProviderCommitBatch.make")(function* (
300 Stream.concat(Stream.succeed({ _tag: "End" as const })),
301 );
302 let resume: Effect.Effect<void> = Effect.void;
303 + // Replies run in order, so EOF, failures and controls follow this stream's earlier snapshots
304 + // without waiting for other streams' work.
305 yield* signals.pipe(
306 Stream.mapEffect((signal) =>
307 Effect.gen(function* () {
308 yield* resume;
309 const processed = yield* Deferred.make<void>();
257 - const event = signal._tag === "Event" ? signal.event : undefined;
258 - // EOF only acknowledges prior work; it can share a commit with other consumers.
259 - const snapshot =
260 - signal._tag === "End" ||
261 - (event !== undefined && isSnapshot(event) && (yield* canBatch));
262 - const bytes =
263 - snapshot && event !== undefined ? Buffer.byteLength(yield* encodeQueue(event)) : 0;
264 - const work: Effect.Effect<boolean, E | E2, R2> =
265 - signal._tag === "Failure"
266 - ? Effect.failCause(signal.cause)
267 - : event === undefined
268 - ? Effect.succeed(true)
269 - : process(event).pipe(Effect.as(false));
270 - const reply = snapshot
271 - ? yield* enqueue(work, token, true, bytes)
272 - : (yield* enqueue(Effect.void, token, false, 0)).pipe(Effect.andThen(work));
273 - if (!snapshot) resume = Deferred.await(processed);
274 - return { reply, processed, check: !snapshot };
310 + let reply: Effect.Effect<boolean, E | E2 | CommitBatchError, R2>;
311 + let check = false;
312 + if (signal._tag === "End") reply = Effect.succeed(true);
313 + else if (signal._tag === "Failure") reply = Effect.failCause(signal.cause);
314 + else if (isSnapshot(signal.event) && (yield* canBatch)) {
315 + const bytes = Buffer.byteLength(yield* encodeQueue(signal.event));
316 + reply = yield* enqueue(
317 + process(signal.event).pipe(Effect.as(false)),
318 + token,
319 + snapshotThreadId(signal.event),
320 + bytes,
321 + );
322 + } else {
323 + reply = process(signal.event).pipe(Effect.as(false));
324 + check = true;
325 + resume = Deferred.await(processed);
326 + }
327 + return { reply, processed, check };
328 }),
329 ),
330 Stream.buffer({ capacity: MAX_EVENTS }),
packages/shared/src/nodeSqliteClient.test.ts
+55
@@ -230,6 +230,61 @@ it.effect.each([false, true])("batches worker results without losing rows (reade
230 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
231 );
232
233 +it.effect("coalesces savepoints with queries and preserves nested rollback", () =>
234 + Effect.gen(function* () {
235 + const { filename } = yield* makeTempDatabase;
236 + yield* Effect.gen(function* () {
237 + const sql = yield* SqlClient.SqlClient;
238 + const worker = startedWorkers.filter((entry) => !checkpointWorkers.includes(entry)).at(-1)!;
239 + using messages = vi.spyOn(worker, "postMessage");
240 + yield* sql.withTransaction(
241 + Effect.forEach(Array.from({ length: 53 }), () => sql.withTransaction(Effect.void)),
242 + );
243 + assert.equal(messages.mock.calls.length, 2);
244 +
245 + yield* sql.withTransaction(
246 + Effect.gen(function* () {
247 + yield* sql`UPDATE counters SET value = 1 WHERE id = 1`;
248 + const failure = yield* sql
249 + .withTransaction(
250 + sql`UPDATE counters SET value = 2 WHERE id = 1`.pipe(
251 + Effect.andThen(Effect.fail("bad update")),
252 + ),
253 + )
254 + .pipe(Effect.flip);
255 + assert.equal(failure, "bad update");
256 + const sent = messages.mock.calls.length;
257 + assert.equal(
258 + yield* sql.withTransaction(Effect.fail("empty update")).pipe(Effect.flip),
259 + "empty update",
260 + );
261 + assert.equal(messages.mock.calls.length, sent);
262 + assert.deepEqual(yield* sql`SELECT value FROM counters WHERE id = 1`, [{ value: 1 }]);
263 + assert.deepEqual(messages.mock.calls.at(-1)?.[0].savepoints, []);
264 + yield* sql.withTransaction(
265 + sql
266 + .withTransaction(
267 + sql`UPDATE counters SET value = 3 WHERE id = 1`.pipe(
268 + Effect.andThen(Effect.fail("nested update")),
269 + ),
270 + )
271 + .pipe(Effect.flip),
272 + );
273 + assert.deepEqual(yield* sql`SELECT value FROM counters WHERE id = 1`, [{ value: 1 }]);
274 +
275 + yield* sql.withTransaction(sql`UPDATE counters SET value = 4 WHERE id = 1`);
276 + yield* sql`UPDATE counters SET value = 5 WHERE id = 1`;
277 + const unsent = yield* sql
278 + .withTransaction(sql.unsafe("SELECT ?", [Symbol("uncloneable")]))
279 + .pipe(Effect.exit);
280 + assert.isTrue(Exit.isFailure(unsent));
281 + }),
282 + );
283 + assert.deepEqual(yield* sql`SELECT value FROM counters WHERE id = 1`, [{ value: 5 }]);
284 + }).pipe(Effect.provide(SqliteClient.layer({ filename, writerWorker: true })));
285 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
286 +);
287 +
288 it.effect("reuses a worker query after an iterator constraint failure", () =>
289 Effect.gen(function* () {
290 const { filename } = yield* makeTempDatabase;
packages/shared/src/nodeSqliteClient.ts
+37 -8
@@ -247,11 +247,13 @@ parentPort.on("message", (request) => {
247 parentPort.close();
248 return;
249 }
250 - const { id, sql, params, cached, values, raw, safeIntegers, received } = request;
250 + const { id, sql, params, cached, values, raw, safeIntegers, received, savepoints } = request;
251 let operation = "open";
252 let iterator;
253 try {
254 if (db === undefined) throw new Error("database connection is unavailable");
255 + operation = "execute";
256 + for (const savepoint of savepoints) db.exec(savepoint);
257 operation = "prepare";
258 const statement = prepare(sql, cached);
259 operation = "execute";
@@ -370,6 +372,8 @@ const makeWorkerConnection = Effect.fnUntraced(function* (options: {
372 }
373 | undefined;
374 let nextRequestId = 0;
375 + // Materialize nested savepoints with the next statement; empty jobs need no worker round trip.
376 + const pendingSavepoints: string[] = [];
377 // A replacement writer would lose the pragmas callers set on it, such as foreign_keys.
378 let writerStopped: string | undefined;
379
@@ -467,8 +471,18 @@ const makeWorkerConnection = Effect.fnUntraced(function* (options: {
471 });
472 if (pending.size === 1) worker.ref();
473 const cached = input.cached && !hasLargeBinding(input.params);
474 + const message = {
475 + id,
476 + safeIntegers,
477 + ...input,
478 + cached,
479 + received,
480 + savepoints: [...pendingSavepoints],
481 + };
482 // oxlint-disable-next-line unicorn/require-post-message-target-origin -- Node workers do not accept a target origin.
471 - worker.postMessage({ id, safeIntegers, ...input, cached, received });
483 + worker.postMessage(message);
484 + // An unsent savepoint must stay queued, or its rollback would target an older sibling's.
485 + pendingSavepoints.length = 0;
486 });
487 // A posted write runs to the end, as it did on the event loop; the caller and its lock
488 // must not move on while the worker is still applying it. Reads stay cancellable.
@@ -492,12 +506,27 @@ const makeWorkerConnection = Effect.fnUntraced(function* (options: {
506 executeUnprepared(sql, params, rowTransform) {
507 // Read snapshots need deferred BEGIN; readonly connections reject IMMEDIATE and EXCLUSIVE.
508 const statement = readOnly && /^\s*BEGIN\b/i.test(sql) ? "BEGIN" : sql;
495 - const effect = request({
496 - sql: statement,
497 - params: params ?? [],
498 - cached: false,
499 - values: false,
500 - raw: false,
509 + const effect = Effect.suspend(() => {
510 + if (!readOnly && /^SAVEPOINT effect_sql_\d+$/.test(statement)) {
511 + pendingSavepoints.push(statement);
512 + return Effect.succeed([]);
513 + }
514 + const rollback = /^ROLLBACK TO SAVEPOINT (effect_sql_\d+)$/.exec(statement);
515 + if (rollback !== null) {
516 + const pending = pendingSavepoints.lastIndexOf(`SAVEPOINT ${rollback[1]}`);
517 + if (pending !== -1) {
518 + pendingSavepoints.splice(pending);
519 + return Effect.succeed([]);
520 + }
521 + }
522 + if (statement === "COMMIT" || statement === "ROLLBACK") pendingSavepoints.length = 0;
523 + return request({
524 + sql: statement,
525 + params: params ?? [],
526 + cached: false,
527 + values: false,
528 + raw: false,
529 + });
530 });
531 return rowTransform ? Effect.map(effect, rowTransform) : effect;
532 },