Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import {
3 MessageId,
4 ProviderDriverKind,
5 ProviderThreadId,
6 RunId,
7 ThreadId,
8 type OrchestrationV2ThreadProjection,
9} from "@t3tools/contracts";
10import * as Deferred from "effect/Deferred";
11import * as DateTime from "effect/DateTime";
12import * as Effect from "effect/Effect";
13import * as Layer from "effect/Layer";
14import * as Option from "effect/Option";
15import * as Queue from "effect/Queue";
16import * as Ref from "effect/Ref";
17import * as Semaphore from "effect/Semaphore";
18import * as TestClock from "effect/testing/TestClock";
19
20import { layer as idAllocatorLayer } from "./IdAllocator.ts";
21import {
22 type ProviderContinuationRequest,
23 ProviderContinuationRequests,
24 layer as continuationRequestsLayer,
25} from "./ProviderContinuationRequests.ts";
26import { workerLive } from "./ProviderContinuationService.ts";
27import { ThreadManagementService } from "./ThreadManagementService.ts";
28
29const threadId = ThreadId.make("thread-provider-continuation");
30const providerThreadId = ProviderThreadId.make("provider-thread-continuation");
31const driver = ProviderDriverKind.make("continuation-test");
32const parentRunId = RunId.make("run-provider-continuation-parent");
33const delegatedMessageId = MessageId.make("message-provider-continuation-delegated");
34const projection = {
35 thread: { archivedAt: null, deletedAt: null },
36 messages: [],
37} as unknown as OrchestrationV2ThreadProjection;
38
39const request = (
40 dispatchIfCurrent?: ProviderContinuationRequest["dispatchIfCurrent"],
41 detail: string | null = null,
42): ProviderContinuationRequest => ({
43 threadId,
44 providerThreadId,
45 driver,
46 detail,
47 ...(dispatchIfCurrent === undefined ? {} : { dispatchIfCurrent }),
48});
49
50const delegatedProjection = (disposition: "open" | "stopped" | "disposed" = "open") =>
51 ({
52 ...projection,
53 runs: [
54 {
55 id: parentRunId,
56 delegatedCompletion: {
57 disposition,
58 nextGeneration: 2,
59 delivery: {
60 generation: 1,
61 messageId: delegatedMessageId,
62 taskIds: ["node-provider-continuation-first", "node-provider-continuation-second"],
63 },
64 },
65 },
66 ],
67 }) as unknown as OrchestrationV2ThreadProjection;
68
69const makeGuard = Effect.fnUntraced(function* (completed?: Deferred.Deferred<void>) {
70 const generation = yield* Ref.make(0);
71 const permit = yield* Semaphore.make(1);
72 const capture = Effect.gen(function* () {
73 const captured = yield* Ref.updateAndGet(generation, (value) => value + 1);
74 return <A, E, R>(effect: Effect.Effect<A, E, R>) =>
75 permit
76 .withPermit(
77 Effect.gen(function* () {
78 if ((yield* Ref.get(generation)) !== captured) return Option.none();
79 return Option.some(yield* effect);
80 }),
81 )
82 .pipe(
83 completed === undefined
84 ? (effect) => effect
85 : Effect.ensuring(Deferred.succeed(completed, undefined)),
86 );
87 });
88 return {
89 capture,
90 invalidate: permit.withPermit(Ref.update(generation, (value) => value + 1)),
91 };
92});
93
94function testLayer(input: {
95 readonly dispatched: Queue.Queue<unknown>;
96 readonly getThreadProjection: () => Effect.Effect<OrchestrationV2ThreadProjection>;
97}) {
98 const threads = Layer.mock(ThreadManagementService)({
99 getThreadProjection: input.getThreadProjection,
100 dispatch: (command) => Queue.offer(input.dispatched, command).pipe(Effect.as({} as never)),
101 });
102 const worker = workerLive.pipe(
103 Layer.provide(Layer.mergeAll(idAllocatorLayer, continuationRequestsLayer, threads)),
104 );
105 return Layer.merge(continuationRequestsLayer, worker);
106}
107
108describe("ProviderContinuationService", () => {
109 it.effect("marks an adapter-buffered wake with the provider creation source", () => {
110 return Effect.gen(function* () {
111 const dispatched = yield* Queue.unbounded<unknown>();
112 yield* Effect.gen(function* () {
113 const requests = yield* ProviderContinuationRequests;
114 yield* requests.offer(request());
115 const command = (yield* Queue.take(dispatched)) as { readonly creationSource: string };
116 // ClaudeAdapterV2 keys on this to attach buffered CLI output.
117 assert.equal(command.creationSource, "provider");
118 }).pipe(
119 Effect.provide(
120 testLayer({ dispatched, getThreadProjection: () => Effect.succeed(projection) }),
121 ),
122 Effect.scoped,
123 );
124 });
125 });
126
127 it.effect("delivers a message_text wake as a real prompt, not a buffered wake", () => {
128 return Effect.gen(function* () {
129 const dispatched = yield* Queue.unbounded<unknown>();
130 yield* Effect.gen(function* () {
131 const requests = yield* ProviderContinuationRequests;
132 yield* requests.offer({
133 threadId,
134 providerThreadId,
135 driver,
136 detail: "Delegated task completed.",
137 delivery: "message_text",
138 });
139 const command = (yield* Queue.take(dispatched)) as {
140 readonly creationSource: string;
141 readonly text: string;
142 };
143 // An app-owned child buffers nothing in the adapter, so this text is
144 // the whole wake. Marking it "provider" would make ClaudeAdapterV2
145 // drop it and settle the turn having prompted nothing.
146 assert.notEqual(command.creationSource, "provider");
147 assert.equal(command.creationSource, "server");
148 assert.equal(command.text, "Delegated task completed.");
149 }).pipe(
150 Effect.provide(
151 testLayer({ dispatched, getThreadProjection: () => Effect.succeed(projection) }),
152 ),
153 Effect.scoped,
154 );
155 });
156 });
157
158 it.effect("dispatches a current delegated completion as one server-owned queued message", () => {
159 return Effect.gen(function* () {
160 const dispatched = yield* Queue.unbounded<unknown>();
161 yield* Effect.gen(function* () {
162 const requests = yield* ProviderContinuationRequests;
163 yield* requests.offer({
164 threadId,
165 providerThreadId,
166 driver,
167 detail: null,
168 delivery: "message_text",
169 delegatedCompletion: {
170 parentRunId,
171 generation: 1,
172 messageId: delegatedMessageId,
173 },
174 });
175 const command = (yield* Queue.take(dispatched)) as {
176 readonly createdBy: string;
177 readonly creationSource: string;
178 readonly delegatedCompletion: {
179 readonly generation: number;
180 readonly parentRunId: RunId;
181 readonly taskIds: ReadonlyArray<string>;
182 };
183 readonly dispatchMode: { readonly type: string };
184 readonly messageId: MessageId;
185 readonly text: string;
186 };
187 assert.equal(command.createdBy, "agent");
188 assert.equal(command.creationSource, "server");
189 assert.deepEqual(command.dispatchMode, { type: "queue_after_active" });
190 assert.equal(command.messageId, delegatedMessageId);
191 assert.equal(command.delegatedCompletion.parentRunId, parentRunId);
192 assert.equal(command.delegatedCompletion.generation, 1);
193 assert.deepEqual(command.delegatedCompletion.taskIds, [
194 "node-provider-continuation-first",
195 "node-provider-continuation-second",
196 ]);
197 assert.include(command.text, "task_status");
198 }).pipe(
199 Effect.provide(
200 testLayer({
201 dispatched,
202 getThreadProjection: () => Effect.succeed(delegatedProjection()),
203 }),
204 ),
205 Effect.scoped,
206 );
207 });
208 });
209
210 it.effect("retries a delegated completion after a transient dispatch failure", () => {
211 return Effect.gen(function* () {
212 const attempts = yield* Ref.make(0);
213 const dispatched = yield* Queue.unbounded<unknown>();
214 const threads = Layer.mock(ThreadManagementService)({
215 getThreadProjection: () => Effect.succeed(delegatedProjection()),
216 dispatch: (command) =>
217 Ref.getAndUpdate(attempts, (count) => count + 1).pipe(
218 Effect.flatMap((attempt) =>
219 attempt === 0
220 ? Effect.fail(new Error("simulated transient dispatch failure") as never)
221 : Queue.offer(dispatched, command).pipe(Effect.as({} as never)),
222 ),
223 ),
224 });
225 const worker = workerLive.pipe(
226 Layer.provide(Layer.mergeAll(idAllocatorLayer, continuationRequestsLayer, threads)),
227 );
228
229 yield* Effect.gen(function* () {
230 const requests = yield* ProviderContinuationRequests;
231 yield* requests.offer({
232 threadId,
233 providerThreadId,
234 driver,
235 detail: null,
236 delivery: "message_text",
237 delegatedCompletion: {
238 parentRunId,
239 generation: 1,
240 messageId: delegatedMessageId,
241 },
242 });
243 yield* Effect.yieldNow;
244 assert.equal(yield* Ref.get(attempts), 1);
245 assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
246
247 yield* TestClock.adjust("100 millis");
248 const command = (yield* Queue.take(dispatched)) as {
249 readonly messageId: MessageId;
250 };
251 assert.equal(command.messageId, delegatedMessageId);
252 assert.equal(yield* Ref.get(attempts), 2);
253 }).pipe(Effect.provide(Layer.merge(continuationRequestsLayer, worker)), Effect.scoped);
254 });
255 });
256
257 it.effect("backs off repeated delegated completion dispatch failures", () => {
258 return Effect.gen(function* () {
259 const attempts = yield* Ref.make(0);
260 const disposition = yield* Ref.make<"open" | "disposed">("open");
261 const threads = Layer.mock(ThreadManagementService)({
262 getThreadProjection: () =>
263 Ref.get(disposition).pipe(Effect.map((state) => delegatedProjection(state))),
264 dispatch: () =>
265 Ref.update(attempts, (count) => count + 1).pipe(
266 Effect.andThen(Effect.fail(new Error("simulated persistent failure") as never)),
267 ),
268 });
269 const worker = workerLive.pipe(
270 Layer.provide(Layer.mergeAll(idAllocatorLayer, continuationRequestsLayer, threads)),
271 );
272
273 yield* Effect.gen(function* () {
274 const requests = yield* ProviderContinuationRequests;
275 yield* requests.offer({
276 threadId,
277 providerThreadId,
278 driver,
279 detail: null,
280 delivery: "message_text",
281 delegatedCompletion: {
282 parentRunId,
283 generation: 1,
284 messageId: delegatedMessageId,
285 },
286 });
287 yield* Effect.yieldNow;
288 assert.equal(yield* Ref.get(attempts), 1);
289
290 yield* TestClock.adjust("100 millis");
291 yield* Effect.yieldNow;
292 assert.equal(yield* Ref.get(attempts), 2);
293 yield* TestClock.adjust("199 millis");
294 yield* Effect.yieldNow;
295 assert.equal(yield* Ref.get(attempts), 2);
296 yield* TestClock.adjust("1 millis");
297 yield* Effect.yieldNow;
298 assert.equal(yield* Ref.get(attempts), 3);
299
300 yield* Ref.set(disposition, "disposed");
301 yield* TestClock.adjust("400 millis");
302 }).pipe(Effect.provide(Layer.merge(continuationRequestsLayer, worker)), Effect.scoped);
303 });
304 });
305
306 it.effect("resets delegated completion retry backoff after archive or deletion", () => {
307 return Effect.gen(function* () {
308 for (const barrier of ["archivedAt", "deletedAt"] as const) {
309 const attempts = yield* Ref.make(0);
310 const blockedProjectionReads = yield* Ref.make(0);
311 const blockedRequestDropped = yield* Deferred.make<void>();
312 const state = yield* Ref.make<"open" | "blocked" | "disposed">("open");
313 const threads = Layer.mock(ThreadManagementService)({
314 getThreadProjection: () =>
315 Effect.gen(function* () {
316 const currentState = yield* Ref.get(state);
317 if (currentState === "blocked") {
318 const reads = yield* Ref.updateAndGet(blockedProjectionReads, (count) => count + 1);
319 if (reads === 2) {
320 yield* Deferred.succeed(blockedRequestDropped, undefined);
321 }
322 }
323 return {
324 ...delegatedProjection(currentState === "disposed" ? "disposed" : "open"),
325 thread: {
326 ...projection.thread,
327 [barrier]:
328 currentState === "blocked"
329 ? DateTime.makeUnsafe("2026-08-05T00:00:00.000Z")
330 : null,
331 },
332 };
333 }),
334 dispatch: () =>
335 Ref.update(attempts, (count) => count + 1).pipe(
336 Effect.andThen(Effect.fail(new Error("simulated persistent failure") as never)),
337 ),
338 });
339 const worker = workerLive.pipe(
340 Layer.provide(Layer.mergeAll(idAllocatorLayer, continuationRequestsLayer, threads)),
341 );
342 const completionRequest = {
343 threadId,
344 providerThreadId,
345 driver,
346 detail: null,
347 delivery: "message_text" as const,
348 delegatedCompletion: {
349 parentRunId,
350 generation: 1,
351 messageId: delegatedMessageId,
352 },
353 };
354
355 yield* Effect.gen(function* () {
356 const requests = yield* ProviderContinuationRequests;
357 yield* requests.offer(completionRequest);
358 yield* Effect.yieldNow;
359 assert.equal(yield* Ref.get(attempts), 1);
360
361 yield* Ref.set(state, "blocked");
362 yield* TestClock.adjust("100 millis");
363 yield* Deferred.await(blockedRequestDropped);
364 yield* Effect.yieldNow;
365 assert.equal(yield* Ref.get(attempts), 1);
366
367 yield* Ref.set(state, "open");
368 yield* requests.offer(completionRequest);
369 yield* Effect.yieldNow;
370 assert.equal(yield* Ref.get(attempts), 2);
371 yield* TestClock.adjust("99 millis");
372 yield* Effect.yieldNow;
373 assert.equal(yield* Ref.get(attempts), 2);
374 yield* TestClock.adjust("1 millis");
375 yield* Effect.yieldNow;
376 assert.equal(yield* Ref.get(attempts), 3);
377
378 yield* Ref.set(state, "disposed");
379 yield* TestClock.adjust("200 millis");
380 }).pipe(Effect.provide(Layer.merge(continuationRequestsLayer, worker)), Effect.scoped);
381 }
382 });
383 });
384
385 it.effect("drops a delegated completion retry after its delivery closes", () => {
386 return Effect.gen(function* () {
387 const attempts = yield* Ref.make(0);
388 const disposition = yield* Ref.make<"open" | "disposed">("open");
389 const threads = Layer.mock(ThreadManagementService)({
390 getThreadProjection: () =>
391 Ref.get(disposition).pipe(Effect.map((state) => delegatedProjection(state))),
392 dispatch: () =>
393 Ref.update(attempts, (count) => count + 1).pipe(
394 Effect.andThen(Effect.fail(new Error("simulated dispatch failure") as never)),
395 ),
396 });
397 const worker = workerLive.pipe(
398 Layer.provide(Layer.mergeAll(idAllocatorLayer, continuationRequestsLayer, threads)),
399 );
400
401 yield* Effect.gen(function* () {
402 const requests = yield* ProviderContinuationRequests;
403 yield* requests.offer({
404 threadId,
405 providerThreadId,
406 driver,
407 detail: null,
408 delivery: "message_text",
409 delegatedCompletion: {
410 parentRunId,
411 generation: 1,
412 messageId: delegatedMessageId,
413 },
414 });
415 yield* Effect.yieldNow;
416 assert.equal(yield* Ref.get(attempts), 1);
417
418 yield* Ref.set(disposition, "disposed");
419 yield* TestClock.adjust("100 millis");
420 yield* Effect.yieldNow;
421 assert.equal(yield* Ref.get(attempts), 1);
422 }).pipe(Effect.provide(Layer.merge(continuationRequestsLayer, worker)), Effect.scoped);
423 });
424 });
425
426 it.effect("keeps a Grok delegated completion queued instead of restarting active work", () => {
427 return Effect.gen(function* () {
428 const dispatched = yield* Queue.unbounded<unknown>();
429 yield* Effect.gen(function* () {
430 const requests = yield* ProviderContinuationRequests;
431 yield* requests.offer({
432 threadId,
433 providerThreadId,
434 driver: ProviderDriverKind.make("grok"),
435 detail: null,
436 delivery: "message_text",
437 delegatedCompletion: {
438 parentRunId,
439 generation: 1,
440 messageId: delegatedMessageId,
441 },
442 });
443 const command = (yield* Queue.take(dispatched)) as {
444 readonly dispatchMode: { readonly type: string };
445 };
446 assert.deepEqual(command.dispatchMode, { type: "queue_after_active" });
447 }).pipe(
448 Effect.provide(
449 testLayer({
450 dispatched,
451 getThreadProjection: () => Effect.succeed(delegatedProjection()),
452 }),
453 ),
454 Effect.scoped,
455 );
456 });
457 });
458
459 it.effect(
460 "drops stopped and disposed delegated completions instead of reviving them after recovery",
461 () => {
462 return Effect.gen(function* () {
463 for (const disposition of ["stopped", "disposed"] as const) {
464 const dispatched = yield* Queue.unbounded<unknown>();
465 yield* Effect.gen(function* () {
466 const requests = yield* ProviderContinuationRequests;
467 yield* requests.offer({
468 threadId,
469 providerThreadId,
470 driver,
471 detail: null,
472 delivery: "message_text",
473 delegatedCompletion: {
474 parentRunId,
475 generation: 1,
476 messageId: delegatedMessageId,
477 },
478 });
479 yield* Effect.yieldNow;
480 yield* Effect.yieldNow;
481 assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
482 }).pipe(
483 Effect.provide(
484 testLayer({
485 dispatched,
486 getThreadProjection: () => Effect.succeed(delegatedProjection(disposition)),
487 }),
488 ),
489 Effect.scoped,
490 );
491 }
492 });
493 },
494 );
495
496 it.effect("does not redispatch a persisted delegated completion message during recovery", () => {
497 return Effect.gen(function* () {
498 const dispatched = yield* Queue.unbounded<unknown>();
499 yield* Effect.gen(function* () {
500 const requests = yield* ProviderContinuationRequests;
501 yield* requests.offer({
502 threadId,
503 providerThreadId,
504 driver,
505 detail: null,
506 delivery: "message_text",
507 delegatedCompletion: {
508 parentRunId,
509 generation: 1,
510 messageId: delegatedMessageId,
511 },
512 });
513 yield* Effect.yieldNow;
514 yield* Effect.yieldNow;
515 assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
516 }).pipe(
517 Effect.provide(
518 testLayer({
519 dispatched,
520 getThreadProjection: () =>
521 Effect.succeed({
522 ...delegatedProjection(),
523 messages: [{ id: delegatedMessageId }],
524 } as unknown as OrchestrationV2ThreadProjection),
525 }),
526 ),
527 Effect.scoped,
528 );
529 });
530 });
531
532 it.effect("drops archived and deleted delegated completions during recovery", () => {
533 return Effect.gen(function* () {
534 for (const barrier of ["archivedAt", "deletedAt"] as const) {
535 const dispatched = yield* Queue.unbounded<unknown>();
536 yield* Effect.gen(function* () {
537 const requests = yield* ProviderContinuationRequests;
538 yield* requests.offer({
539 threadId,
540 providerThreadId,
541 driver,
542 detail: null,
543 delivery: "message_text",
544 delegatedCompletion: {
545 parentRunId,
546 generation: 1,
547 messageId: delegatedMessageId,
548 },
549 });
550 yield* Effect.yieldNow;
551 yield* Effect.yieldNow;
552 assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
553 }).pipe(
554 Effect.provide(
555 testLayer({
556 dispatched,
557 getThreadProjection: () =>
558 Effect.succeed({
559 ...delegatedProjection(),
560 thread: {
561 ...projection.thread,
562 [barrier]: DateTime.makeUnsafe("2026-08-03T00:00:00.000Z"),
563 },
564 } as unknown as OrchestrationV2ThreadProjection),
565 }),
566 ),
567 Effect.scoped,
568 );
569 }
570 });
571 });
572
573 it.effect("dispatches a current request exactly once", () => {
574 return Effect.gen(function* () {
575 const dispatched = yield* Queue.unbounded<unknown>();
576 yield* Effect.gen(function* () {
577 const requests = yield* ProviderContinuationRequests;
578 yield* requests.offer(request());
579 yield* Queue.take(dispatched);
580 yield* Effect.yieldNow;
581 assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
582 }).pipe(
583 Effect.provide(
584 testLayer({ dispatched, getThreadProjection: () => Effect.succeed(projection) }),
585 ),
586 Effect.scoped,
587 );
588 });
589 });
590
591 it.effect("queues a continuation behind an active user run", () => {
592 return Effect.gen(function* () {
593 const dispatched = yield* Queue.unbounded<unknown>();
594 yield* Effect.gen(function* () {
595 const requests = yield* ProviderContinuationRequests;
596 yield* requests.offer(request());
597 const command = yield* Queue.take(dispatched);
598 assert.deepEqual((command as { readonly dispatchMode?: unknown }).dispatchMode, {
599 type: "queue_after_active",
600 });
601 }).pipe(
602 Effect.provide(
603 testLayer({ dispatched, getThreadProjection: () => Effect.succeed(projection) }),
604 ),
605 Effect.scoped,
606 );
607 });
608 });
609
610 it.effect("drops a request invalidated before dispatch", () => {
611 return Effect.gen(function* () {
612 const dispatched = yield* Queue.unbounded<unknown>();
613 yield* Effect.gen(function* () {
614 const requests = yield* ProviderContinuationRequests;
615 const guard = yield* makeGuard();
616 const dispatchIfCurrent = yield* guard.capture;
617 yield* guard.invalidate;
618 yield* requests.offer(request(dispatchIfCurrent));
619 yield* Effect.yieldNow;
620 yield* Effect.yieldNow;
621 assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
622 }).pipe(
623 Effect.provide(
624 testLayer({ dispatched, getThreadProjection: () => Effect.succeed(projection) }),
625 ),
626 Effect.scoped,
627 );
628 });
629 });
630
631 it.effect("drops a request invalidated while projection is blocked", () => {
632 return Effect.gen(function* () {
633 const dispatched = yield* Queue.unbounded<unknown>();
634 const projectionEntered = yield* Deferred.make<void>();
635 const releaseProjection = yield* Deferred.make<void>();
636 const guardCompleted = yield* Deferred.make<void>();
637 yield* Effect.gen(function* () {
638 const requests = yield* ProviderContinuationRequests;
639 const guard = yield* makeGuard(guardCompleted);
640 const dispatchIfCurrent = yield* guard.capture;
641 yield* requests.offer(request(dispatchIfCurrent));
642 yield* Deferred.await(projectionEntered);
643 yield* guard.invalidate;
644 yield* Deferred.succeed(releaseProjection, undefined);
645 yield* Deferred.await(guardCompleted);
646 assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
647 }).pipe(
648 Effect.provide(
649 testLayer({
650 dispatched,
651 getThreadProjection: () =>
652 Deferred.succeed(projectionEntered, undefined).pipe(
653 Effect.andThen(Deferred.await(releaseProjection)),
654 Effect.as(projection),
655 ),
656 }),
657 ),
658 Effect.scoped,
659 );
660 });
661 });
662
663 it.effect("does not revive an old request when a later generation is current", () => {
664 return Effect.gen(function* () {
665 const dispatched = yield* Queue.unbounded<unknown>();
666 const firstProjectionEntered = yield* Deferred.make<void>();
667 const releaseFirstProjection = yield* Deferred.make<void>();
668 let projectionCalls = 0;
669 yield* Effect.gen(function* () {
670 const requests = yield* ProviderContinuationRequests;
671 const guard = yield* makeGuard();
672 const first = yield* guard.capture;
673 yield* requests.offer(request(first, "A"));
674 yield* Deferred.await(firstProjectionEntered);
675 const second = yield* guard.capture;
676 yield* requests.offer(request(second, "B"));
677 yield* Deferred.succeed(releaseFirstProjection, undefined);
678 const command = yield* Queue.take(dispatched);
679 assert.equal((command as { readonly text?: unknown }).text, "B");
680 yield* Effect.yieldNow;
681 assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
682 }).pipe(
683 Effect.provide(
684 testLayer({
685 dispatched,
686 getThreadProjection: () => {
687 projectionCalls += 1;
688 return projectionCalls === 1
689 ? Deferred.succeed(firstProjectionEntered, undefined).pipe(
690 Effect.andThen(Deferred.await(releaseFirstProjection)),
691 Effect.as(projection),
692 )
693 : Effect.succeed(projection);
694 },
695 }),
696 ),
697 Effect.scoped,
698 );
699 });
700 });
701
702 it.effect("clears a sticky continuation offer when its thread is archived", () => {
703 return Effect.gen(function* () {
704 const dispatched = yield* Queue.unbounded<unknown>();
705 const cleared = yield* Ref.make(false);
706 yield* Effect.gen(function* () {
707 const requests = yield* ProviderContinuationRequests;
708 yield* requests.offer({
709 ...request(),
710 clearIfCurrent: () => Ref.set(cleared, true),
711 });
712 for (let attempt = 0; attempt < 20 && !(yield* Ref.get(cleared)); attempt += 1) {
713 yield* Effect.yieldNow;
714 }
715 assert.isTrue(yield* Ref.get(cleared));
716 assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
717 }).pipe(
718 Effect.provide(
719 testLayer({
720 dispatched,
721 getThreadProjection: () =>
722 Effect.succeed({
723 ...projection,
724 thread: {
725 ...projection.thread,
726 archivedAt: DateTime.makeUnsafe("2026-07-20T00:00:00.000Z"),
727 },
728 }),
729 }),
730 ),
731 Effect.scoped,
732 );
733 });
734 });
735});