Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderContinuationService.test.ts

804 lines28.7 KB

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