@devries-cameron20 / t3code-1 / commits / 5374e2d19c
Fork 0

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

fix(server): a wake stuck on one thread's parent lock no longer holds up other threads' wakes (#37416)

Julius Marminge committed Nov 15, 2026 at 16:41 UTC 5374e2d19cef8650ec222a0d079b5610860cf5b1
2 files changed +51 -1
apps/server/src/orchestration-v2/ProviderContinuationService.test.ts
+44 -1
@@ -11,6 +11,7 @@ import {
11 type OrchestrationV2ThreadProjection,
12 type OrchestrationV2Notification,
13 } from "@t3tools/contracts";
14 +import * as Clock from "effect/Clock";
15 import * as Deferred from "effect/Deferred";
16 import * as Effect from "effect/Effect";
17 import * as Exit from "effect/Exit";
@@ -95,10 +96,15 @@ const makeGuard = Effect.fnUntraced(function* (completed?: Deferred.Deferred<voi
96 function layerTest(input: {
97 readonly dispatched: Queue.Queue<unknown>;
98 readonly getThreadRecords: () => Effect.Effect<OrchestrationV2ThreadProjection>;
99 + readonly beforeDispatch?: (threadId: ThreadId) => Effect.Effect<void>;
100 }) {
101 const layerThreads = Layer.mock(ThreadManagementService.ThreadManagementService)({
102 getThreadRecords: input.getThreadRecords,
101 - dispatch: (command) => Queue.offer(input.dispatched, command).pipe(Effect.as({} as never)),
103 + dispatch: (command) =>
104 + (
105 + ("threadId" in command ? input.beforeDispatch?.(command.threadId) : undefined) ??
106 + Effect.void
107 + ).pipe(Effect.andThen(Queue.offer(input.dispatched, command)), Effect.as({} as never)),
108 });
109 const layerWorker = ProviderContinuationService.layer.pipe(
110 Layer.provide(
@@ -618,4 +624,41 @@ describe("ProviderContinuationService", () => {
624 );
625 });
626 });
627 + it.effect(
628 + "a wake held by another thread's parent lock does not delay other threads' wakes",
629 + () => {
630 + return Effect.gen(function* () {
631 + const dispatched = yield* Queue.unbounded<unknown>();
632 + const blockedChildId = ThreadId.make("thread-provider-continuation-blocked-child");
633 + const dispatchedAt = new Map<ThreadId, number>();
634 + yield* Effect.gen(function* () {
635 + const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
636 + yield* requests.offer({ ...request(), threadId: blockedChildId });
637 + yield* requests.offer(request());
638 + yield* TestClock.adjust("10 minutes");
639 + yield* Queue.take(dispatched);
640 + yield* Queue.take(dispatched);
641 + assert.equal(dispatchedAt.get(blockedChildId), 10 * 60 * 1000);
642 + assert.equal(dispatchedAt.get(threadId), 0);
643 + }).pipe(
644 + Effect.provide(
645 + layerTest({
646 + dispatched,
647 + getThreadRecords: () => Effect.succeed(projection),
648 + // Models the child's message intake waiting on its coordinator's dispatch lock.
649 + beforeDispatch: (dispatchThreadId) =>
650 + (dispatchThreadId === blockedChildId
651 + ? Effect.sleep("10 minutes")
652 + : Effect.void
653 + ).pipe(
654 + Effect.andThen(Clock.currentTimeMillis),
655 + Effect.map((now) => void dispatchedAt.set(dispatchThreadId, now)),
656 + ),
657 + }),
658 + ),
659 + Effect.scoped,
660 + );
661 + });
662 + },
663 + );
664 });
apps/server/src/orchestration-v2/ProviderContinuationService.ts
+7
@@ -2,6 +2,7 @@ import {
2 CommandId,
3 type MessageId,
4 type OrchestrationV2ThreadProjection,
5 + type ThreadId,
6 } from "@t3tools/contracts";
7 import * as Effect from "effect/Effect";
8 import * as Layer from "effect/Layer";
@@ -11,6 +12,7 @@ import * as Stream from "effect/Stream";
12
13 import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
14 import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
15 +import * as KeyedLock from "@t3tools/shared/KeyedLock";
16 import type * as Orchestrator from "./Orchestrator.ts";
17 import * as ThreadManagementService from "./ThreadManagementService.ts";
18 import { isUndeliveredMailboxSteer } from "./NotificationMailbox.ts";
@@ -63,6 +65,7 @@ export const layer = Layer.effectDiscard(
65 const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
66 const threads = yield* ThreadManagementService.ThreadManagementService;
67 const retryAttempts = yield* Ref.make(new Map<string, number>());
68 + const threadLocks = yield* KeyedLock.make<ThreadId>();
69
70 const clearRetryAttempt = (key: string) =>
71 Ref.update(retryAttempts, (current) => {
@@ -336,6 +339,10 @@ export const layer = Layer.effectDiscard(
339 }
340 }),
341 ),
342 + // A dispatch can wait on its parent's lock, so only its own thread's later requests queue behind it.
343 + (handled) => threadLocks.withLock(request.threadId, handled),
344 + // Starting immediately joins the thread's lock queue before the next take, keeping per-thread order.
345 + Effect.forkScoped({ startImmediately: true }),
346 ),
347 ),
348 Effect.forever,