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

BackgroundWorkStop.integration.test.ts

789 lines29.3 KB

1import { assert, it } from "@effect/vitest";
2import {
3 CommandId,
4 EventId,
5 MessageId,
6 NodeId,
7 ProjectId,
8 ProviderDriverKind,
9 ProviderInstanceId,
10 ProviderThreadId,
11 ProviderTurnId,
12 RunAttemptId,
13 RunId,
14 ThreadId,
15 TurnItemId,
16 type OrchestrationV2DomainEvent,
17} from "@t3tools/contracts";
18import * as DateTime from "effect/DateTime";
19import * as Effect from "effect/Effect";
20import * as Fiber from "effect/Fiber";
21import * as Queue from "effect/Queue";
22import * as Stream from "effect/Stream";
23import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
24import * as EffectWorker from "./EffectWorker.ts";
25import * as EventSink from "./EventSink.ts";
26import * as Orchestrator from "./Orchestrator.ts";
27import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
28import * as ProviderSessionManager from "./ProviderSessionManager.ts";
29import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
30import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
31import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
32​
33const driver = ProviderDriverKind.make("codex");
34const instanceId = ProviderInstanceId.make("codex");
35const modelSelection = { instanceId, model: "test-model" };
36​
37// Codex turns leave commands running, then the thread moves to another
38// provider thread (a provider switch). Stop must end all of the Codex work,
39// including when it selects an older resumed run on the other provider thread.
40const stopEarlierBackgroundWork = ({
41 failedStart = false,
42 stopWithQueue,
43 olderStart = false,
44 stalledRun,
45}: {
46 readonly failedStart?: boolean;
47 readonly stopWithQueue?: "thread.stop" | "run.interrupt";
48 readonly olderStart?: boolean;
49 readonly stalledRun?:
50 | "missing-session"
51 | "missing-session-terminal"
52 | "returned-interrupt"
53 | "returned-interrupt-terminal"
54 | "superseded-attempt";
55}) =>
56 Effect.scoped(
57 Effect.gen(function* () {
58 const cwd = yield* checkpointWorkspace("background-work-stop");
59 const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
60 const started: ProviderAdapter.ProviderAdapterV2TurnInput[] = [];
61 const interrupts: ProviderAdapter.ProviderAdapterV2InterruptInput[] = [];
62 const adapter: ProviderAdapter.ProviderAdapterV2["Service"] = {
63 instanceId,
64 driver,
65 getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2),
66 planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }),
67 openSession: (input) =>
68 Effect.gen(function* () {
69 const now = yield* DateTime.now;
70 return {
71 instanceId,
72 driver,
73 providerSessionId: input.providerSessionId,
74 providerSession: {
75 id: input.providerSessionId,
76 driver,
77 providerInstanceId: instanceId,
78 status: "ready",
79 cwd,
80 model: modelSelection.model,
81 capabilities: CodexProviderCapabilitiesV2,
82 createdAt: now,
83 updatedAt: now,
84 lastError: null,
85 },
86 events: Stream.fromQueue(events),
87 ensureThread: ({ threadId }) =>
88 Effect.succeed({
89 id: ProviderThreadId.make(`provider-thread:codex:${threadId}`),
90 driver,
91 providerInstanceId: instanceId,
92 providerSessionId: input.providerSessionId,
93 appThreadId: threadId,
94 ownerNodeId: null,
95 nativeThreadRef: { driver, nativeId: "native-thread", strength: "strong" },
96 nativeConversationHeadRef: null,
97 status: "idle",
98 firstRunOrdinal: null,
99 lastRunOrdinal: null,
100 handoffIds: [],
101 forkedFrom: null,
102 createdAt: now,
103 updatedAt: now,
104 }),
105 resumeThread: ({ providerThread }) => Effect.succeed(providerThread),
106 startTurn: (turn) =>
107 Effect.gen(function* () {
108 started.push(turn);
109 yield* Queue.offer(events, {
110 type: "provider_turn.updated",
111 driver,
112 providerTurn: {
113 id: ProviderTurnId.make(`provider-turn:${turn.attemptId}`),
114 providerThreadId: turn.providerThread.id,
115 nodeId: turn.rootNodeId,
116 runAttemptId: turn.attemptId,
117 nativeTurnRef: {
118 driver,
119 nativeId: `native:${turn.attemptId}`,
120 strength: "strong",
121 },
122 ordinal: turn.providerTurnOrdinal,
123 status: "running",
124 startedAt: now,
125 completedAt: null,
126 },
127 });
128 }),
129 steerTurn: () => Effect.die("unused"),
130 interruptTurn: (interrupt) =>
131 Effect.sync(() => {
132 interrupts.push(interrupt);
133 }),
134 respondToRuntimeRequest: () => Effect.die("unused"),
135 readThreadSnapshot: () => Effect.die("unused"),
136 rollbackThread: () => Effect.die("unused"),
137 forkThread: () => Effect.die("unused"),
138 };
139 }),
140 };
141 yield* Effect.gen(function* () {
142 const orchestrator = yield* Orchestrator.OrchestratorV2;
143 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
144 const sink = yield* EventSink.EventSinkV2;
145 const threadId = ThreadId.make("thread:background-work-stop");
146 const watch = (predicate: (event: OrchestrationV2DomainEvent) => boolean) =>
147 orchestrator.streamDomainEvents.pipe(
148 Stream.filter(predicate),
149 Stream.take(1),
150 Stream.runDrain,
151 Effect.forkScoped,
152 );
153 yield* orchestrator.dispatch({
154 type: "thread.create",
155 commandId: CommandId.make("create"),
156 threadId,
157 projectId: ProjectId.make("project:background-work-stop"),
158 title: "Background work stop",
159 modelSelection,
160 runtimeMode: "full-access",
161 interactionMode: "default",
162 branch: null,
163 worktreePath: cwd,
164 createdBy: "user",
165 creationSource: "web",
166 });
167 const running = yield* watch(
168 (event) => event.type === "provider-turn.updated" && event.payload.status === "running",
169 );
170 yield* orchestrator.dispatch({
171 type: "message.dispatch",
172 commandId: CommandId.make("start-dev-server"),
173 threadId,
174 messageId: MessageId.make("message:start-dev-server"),
175 text: "Start the dev server",
176 attachments: [],
177 dispatchMode: { type: "start_immediately" },
178 createdBy: "user",
179 creationSource: "web",
180 });
181 yield* worker.drain();
182 yield* Fiber.join(running);
183 const first = started[0]!;
184 const codexTurn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!;
185 const devServerId = TurnItemId.make("turn-item:dev-server");
186 const now = yield* DateTime.now;
187 yield* sink.write({
188 events: [
189 {
190 id: EventId.make("dev-server"),
191 type: "turn-item.updated",
192 threadId,
193 runId: first.runId,
194 occurredAt: now,
195 payload: {
196 id: devServerId,
197 threadId,
198 runId: first.runId,
199 nodeId: first.rootNodeId,
200 providerThreadId: codexTurn.providerThreadId,
201 providerTurnId: codexTurn.id,
202 nativeItemRef: null,
203 parentItemId: null,
204 ordinal: 100,
205 status: "running",
206 title: null,
207 startedAt: now,
208 completedAt: null,
209 updatedAt: now,
210 type: "command_execution",
211 input: "vp run dev --share",
212 },
213 },
214 ],
215 });
216 if (stalledRun !== undefined) {
217 const before = yield* orchestrator.getThreadProjection(threadId);
218 const run = before.runs[0]!;
219 const node = before.nodes.find((candidate) => candidate.id === run.rootNodeId)!;
220 const attempt = before.attempts[0]!;
221 if (stalledRun === "missing-session" || stalledRun === "missing-session-terminal") {
222 const sessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
223 const failed = yield* watch(
224 (event) => event.type === "run.updated" && event.payload.status === "failed",
225 );
226 yield* sessions.release({
227 providerSessionId: first.providerThread.providerSessionId!,
228 reason: "runtime_error",
229 });
230 yield* Fiber.join(failed);
231 // Disk exhaustion can lose these terminal writes. Restore that stale state.
232 yield* sink.write({
233 events: [
234 {
235 id: EventId.make("stale-run"),
236 type: "run.updated",
237 threadId,
238 occurredAt: now,
239 payload: run,
240 },
241 {
242 id: EventId.make("stale-node"),
243 type: "node.updated",
244 threadId,
245 occurredAt: now,
246 payload: node,
247 },
248 {
249 id: EventId.make("stale-attempt"),
250 type: "run-attempt.updated",
251 threadId,
252 occurredAt: now,
253 payload: attempt,
254 },
255 ],
256 });
257 }
258 const messageId = MessageId.make("partial-output");
259 const item = before.turnItems.find((candidate) => candidate.id === devServerId)!;
260 assert.ok(item.type === "command_execution");
261 const terminalProviderTurn = stalledRun.endsWith("-terminal");
262 if (terminalProviderTurn) {
263 yield* sink.write({
264 events: [
265 {
266 id: EventId.make("terminal-provider-turn"),
267 type: "provider-turn.updated",
268 threadId,
269 occurredAt: now,
270 payload: { ...codexTurn, status: "completed", completedAt: now },
271 },
272 ...(stalledRun === "missing-session-terminal"
273 ? [
274 {
275 id: EventId.make("completed-dev-server"),
276 type: "turn-item.updated" as const,
277 threadId,
278 occurredAt: now,
279 payload: { ...item, status: "completed" as const, completedAt: now },
280 },
281 ]
282 : []),
283 ],
284 });
285 }
286 yield* sink.write({
287 events: [
288 {
289 id: EventId.make("partial-message"),
290 type: "message.updated",
291 threadId,
292 runId: run.id,
293 occurredAt: now,
294 payload: {
295 id: messageId,
296 threadId,
297 runId: run.id,
298 nodeId: node.id,
299 role: "assistant",
300 text: "Partial output",
301 attachments: [],
302 streaming: true,
303 createdBy: "agent",
304 creationSource: "provider",
305 createdAt: now,
306 updatedAt: now,
307 },
308 },
309 {
310 id: EventId.make("partial-item"),
311 type: "turn-item.updated",
312 threadId,
313 runId: run.id,
314 occurredAt: now,
315 payload: {
316 ...item,
317 providerThreadId: codexTurn.providerThreadId,
318 providerTurnId: codexTurn.id,
319 id: TurnItemId.make("partial-output"),
320 type: "assistant_message",
321 messageId,
322 text: "Partial output",
323 streaming: true,
324 },
325 },
326 ],
327 });
328 if (stalledRun === "superseded-attempt") {
329 yield* sink.write({
330 events: [
331 {
332 id: EventId.make("new-attempt"),
333 type: "run.updated",
334 threadId,
335 occurredAt: now,
336 payload: { ...run, activeAttemptId: RunAttemptId.make("new-attempt") },
337 },
338 ],
339 });
340 }
341 yield* orchestrator.dispatch(
342 stalledRun === "superseded-attempt"
343 ? {
344 type: "thread.background-work.settle",
345 commandId: CommandId.make("late-settle"),
346 threadId,
347 providerThreadId: codexTurn.providerThreadId,
348 providerTurnId: codexTurn.id,
349 }
350 : {
351 type: "thread.stop",
352 commandId: CommandId.make("stop-stalled-run"),
353 threadId,
354 },
355 );
356 yield* worker.drain();
357 const after = yield* orchestrator.getThreadProjection(threadId);
358 const interrupted = stalledRun !== "superseded-attempt";
359 assert.equal(after.runs[0]?.status, interrupted ? "interrupted" : "running");
360 assert.equal(after.attempts[0]?.status, interrupted ? "interrupted" : "running");
361 assert.equal(
362 after.providerTurns[0]?.status,
363 terminalProviderTurn ? "completed" : interrupted ? "interrupted" : "running",
364 );
365 assert.equal(
366 after.nodes.find((candidate) => candidate.id === node.id)?.status,
367 interrupted ? "interrupted" : "running",
368 );
369 const output = after.messages.find((message) => message.id === messageId)!;
370 assert.equal(output.streaming, !interrupted);
371 assert.equal(output.text, "Partial output");
372 assert.equal(
373 after.turnItems.find((candidate) => candidate.id === devServerId)?.status,
374 stalledRun === "missing-session-terminal"
375 ? "completed"
376 : interrupted
377 ? "interrupted"
378 : "running",
379 );
380 assert.equal(
381 after.turnItems.find((candidate) => candidate.type === "assistant_message")?.status,
382 interrupted ? "interrupted" : "running",
383 );
384 assert.equal(
385 after.turnItems.filter((candidate) => candidate.type === "run_interrupt_result").length,
386 interrupted ? 1 : 0,
387 );
388 assert.isEmpty(after.runs.filter((candidate) => candidate.status === "waiting"));
389 return;
390 }
391 const settled = yield* watch(
392 (event) =>
393 event.type === "run.updated" &&
394 event.payload.id === first.runId &&
395 event.payload.status === "waiting",
396 );
397 yield* Queue.offer(events, {
398 type: "provider_turn.updated",
399 driver,
400 providerTurn: { ...codexTurn, status: "completed", completedAt: now },
401 });
402 yield* Queue.offer(events, {
403 type: "turn.terminal",
404 driver,
405 providerThreadId: codexTurn.providerThreadId,
406 providerTurnId: codexTurn.id,
407 runOrdinal: first.runOrdinal,
408 status: "completed",
409 failure: null,
410 threadDisposition: "reusable",
411 });
412 yield* Fiber.join(settled);
413 yield* worker.drain();
414​
415 const codexProviderThread = (yield* orchestrator.getThreadProjection(threadId))
416 .providerThreads[0]!;
417 // A settled run with its root node, attempt, provider turn and,
418 // optionally, a command or native subagent it left running.
419 const settledRun = (input: {
420 readonly ordinal: number;
421 readonly providerThreadId: ProviderThreadId;
422 readonly runningItem?: {
423 readonly id: TurnItemId;
424 readonly kind: "command" | "subagent";
425 };
426 }) => {
427 const runId = RunId.make(`run:${input.ordinal}`);
428 const attemptId = RunAttemptId.make(`attempt:${input.ordinal}`);
429 const nodeId = NodeId.make(`node:${input.ordinal}`);
430 const providerTurnId = ProviderTurnId.make(`provider-turn:${input.ordinal}`);
431 const events: Array<OrchestrationV2DomainEvent> = [
432 {
433 id: EventId.make(`run:${input.ordinal}`),
434 type: "run.created",
435 threadId,
436 occurredAt: now,
437 payload: {
438 id: runId,
439 threadId,
440 ordinal: input.ordinal,
441 providerInstanceId: instanceId,
442 modelSelection,
443 providerThreadId: input.providerThreadId,
444 userMessageId: MessageId.make(`message:${input.ordinal}`),
445 rootNodeId: nodeId,
446 activeAttemptId: attemptId,
447 status: "completed",
448 requestedAt: now,
449 startedAt: now,
450 completedAt: now,
451 checkpointId: null,
452 contextHandoffId: null,
453 },
454 },
455 {
456 id: EventId.make(`node:${input.ordinal}`),
457 type: "node.updated",
458 threadId,
459 runId,
460 occurredAt: now,
461 payload: {
462 id: nodeId,
463 threadId,
464 runId,
465 parentNodeId: null,
466 rootNodeId: nodeId,
467 kind: "root_turn",
468 status: "completed",
469 countsForRun: true,
470 providerThreadId: input.providerThreadId,
471 providerTurnId,
472 nativeItemRef: null,
473 runtimeRequestId: null,
474 checkpointScopeId: null,
475 startedAt: now,
476 completedAt: now,
477 },
478 },
479 {
480 id: EventId.make(`attempt:${input.ordinal}`),
481 type: "run-attempt.created",
482 threadId,
483 occurredAt: now,
484 payload: {
485 id: attemptId,
486 runId,
487 attemptOrdinal: 1,
488 rootNodeId: nodeId,
489 providerInstanceId: instanceId,
490 providerThreadId: input.providerThreadId,
491 providerTurnId,
492 reason: "initial",
493 status: "completed",
494 startedAt: now,
495 completedAt: now,
496 },
497 },
498 {
499 id: EventId.make(`provider-turn:${input.ordinal}`),
500 type: "provider-turn.updated",
501 threadId,
502 occurredAt: now,
503 payload: {
504 id: providerTurnId,
505 providerThreadId: input.providerThreadId,
506 nodeId,
507 runAttemptId: attemptId,
508 nativeTurnRef: null,
509 ordinal: input.ordinal,
510 status: "completed",
511 startedAt: now,
512 completedAt: now,
513 },
514 },
515 ];
516 const item = input.runningItem;
517 if (item !== undefined) {
518 const base = {
519 id: item.id,
520 threadId,
521 runId,
522 nodeId,
523 providerThreadId: input.providerThreadId,
524 providerTurnId,
525 nativeItemRef: null,
526 parentItemId: null,
527 ordinal: input.ordinal * 100,
528 status: "running",
529 title: null,
530 startedAt: now,
531 completedAt: null,
532 updatedAt: now,
533 } as const;
534 events.push({
535 id: EventId.make(`item:${input.ordinal}`),
536 type: "turn-item.updated",
537 threadId,
538 runId,
539 occurredAt: now,
540 payload:
541 item.kind === "command"
542 ? { ...base, type: "command_execution", input: "vp run test --watch" }
543 : {
544 ...base,
545 // A native subagent item names its own provider thread
546 // but its parent's provider turn.
547 providerThreadId: ProviderThreadId.make("provider-thread:codex-subagent"),
548 type: "subagent",
549 subagentId: NodeId.make(`subagent:${input.ordinal}`),
550 origin: "provider_native",
551 driver,
552 providerInstanceId: instanceId,
553 childThreadId: null,
554 prompt: "Review the change",
555 result: null,
556 },
557 });
558 }
559 return { runId, providerTurnId, events };
560 };
561​
562 // Later Codex runs leave a second command and a native subagent. Then
563 // the thread moves on to another provider thread, which also has a
564 // live session.
565 const watcherId = TurnItemId.make("turn-item:watcher");
566 const otherProviderThreadId = ProviderThreadId.make("provider-thread:other");
567 const watcherRun = settledRun({
568 ordinal: 2,
569 providerThreadId: olderStart ? otherProviderThreadId : codexProviderThread.id,
570 ...(olderStart ? {} : { runningItem: { id: watcherId, kind: "command" as const } }),
571 });
572 const reviewerId = TurnItemId.make("turn-item:reviewer");
573 const reviewerRun = settledRun({
574 ordinal: 3,
575 providerThreadId: codexProviderThread.id,
576 runningItem: { id: reviewerId, kind: "subagent" },
577 });
578 const latestRun = settledRun({ ordinal: 4, providerThreadId: otherProviderThreadId });
579 yield* sink.write({
580 events: [
581 ...watcherRun.events,
582 ...reviewerRun.events,
583 {
584 id: EventId.make("provider-thread:other"),
585 type: "provider-thread.updated",
586 threadId,
587 occurredAt: now,
588 payload: {
589 ...codexProviderThread,
590 id: otherProviderThreadId,
591 firstRunOrdinal: 4,
592 lastRunOrdinal: 4,
593 },
594 },
595 ...latestRun.events,
596 ],
597 });
598​
599 const failedRun = settledRun({ ordinal: 5, providerThreadId: otherProviderThreadId });
600 if (failedStart) {
601 yield* sink.write({
602 events: failedRun.events.flatMap((event): Array<OrchestrationV2DomainEvent> => {
603 switch (event.type) {
604 case "provider-turn.updated":
605 return [];
606 case "run.created":
607 return [{ ...event, payload: { ...event.payload, status: "failed" } }];
608 case "node.updated":
609 return [
610 {
611 ...event,
612 payload: { ...event.payload, status: "failed", providerTurnId: null },
613 },
614 ];
615 case "run-attempt.created":
616 return [
617 {
618 ...event,
619 payload: { ...event.payload, status: "failed", providerTurnId: null },
620 },
621 ];
622 default:
623 return [event];
624 }
625 }),
626 });
627 }
628​
629 if (stopWithQueue !== undefined) {
630 const owner = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
631 (run) => run.id === latestRun.runId,
632 )!;
633 // A message queues while checkpointing finishes, then the user holds the queue.
634 yield* sink.write({
635 events: [
636 {
637 id: EventId.make("latest-run-waiting"),
638 type: "run.updated",
639 threadId,
640 runId: owner.id,
641 occurredAt: now,
642 payload: { ...owner, status: "waiting", completedAt: null },
643 },
644 ],
645 });
646 yield* orchestrator.dispatch({
647 type: "message.dispatch",
648 commandId: CommandId.make("queue-follow-up"),
649 threadId,
650 messageId: MessageId.make("message:queue-follow-up"),
651 text: "Follow up after the background work",
652 attachments: [],
653 dispatchMode: { type: "queue_after_active" },
654 createdBy: "user",
655 creationSource: "web",
656 });
657 const queued = (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)!;
658 assert.equal(queued.status, "queued");
659 yield* sink.write({
660 events: [
661 {
662 id: EventId.make("queue-held"),
663 type: "run.updated",
664 threadId,
665 runId: queued.id,
666 occurredAt: now,
667 payload: { ...queued, queueHeld: true },
668 },
669 {
670 id: EventId.make("latest-run-settled"),
671 type: "run.updated",
672 threadId,
673 runId: owner.id,
674 occurredAt: now,
675 payload: owner,
676 },
677 ],
678 });
679 }
680​
681 if (olderStart) {
682 const older = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
683 (run) => run.id === watcherRun.runId,
684 )!;
685 // A resumed queue can start a lower-ordinal run after later runs have ended.
686 yield* sink.write({
687 events: [
688 {
689 id: EventId.make("older-run-starting"),
690 type: "run.updated",
691 threadId,
692 runId: older.id,
693 occurredAt: now,
694 payload: { ...older, status: "starting", startedAt: null, completedAt: null },
695 },
696 ],
697 });
698 }
699​
700 yield* orchestrator.dispatch(
701 stopWithQueue === "thread.stop"
702 ? {
703 type: "thread.stop",
704 commandId: CommandId.make("stop-background-work"),
705 threadId,
706 }
707 : {
708 type: "run.interrupt",
709 commandId: CommandId.make("stop-background-work"),
710 threadId,
711 runId: olderStart
712 ? watcherRun.runId
713 : failedStart
714 ? failedRun.runId
715 : latestRun.runId,
716 ...(stopWithQueue === undefined ? {} : { holdQueue: true }),
717 },
718 );
719 yield* worker.drain();
720​
721 // The Codex interrupt targets its latest pending work, the subagent's parent turn,
722 // so its settlement covers the Codex background work.
723 assert.sameDeepMembers(
724 interrupts.map((interrupt) => [interrupt.providerThread.id, interrupt.providerTurnId]),
725 [
726 ...(olderStart ? [] : [[otherProviderThreadId, latestRun.providerTurnId]]),
727 [codexProviderThread.id, reviewerRun.providerTurnId],
728 ],
729 );
730 const after = yield* orchestrator.getThreadProjection(threadId);
731 if (olderStart) {
732 assert.equal(
733 after.runs.find((run) => run.id === watcherRun.runId)?.status,
734 "interrupted",
735 );
736 }
737 if (stopWithQueue !== undefined) {
738 assert.equal(after.runs.at(-1)?.status, "queued");
739 assert.equal(after.runs.at(-1)?.queueHeld, true);
740 assert.lengthOf(started, 1);
741 }
742 assert.deepEqual(
743 [devServerId, ...(olderStart ? [] : [watcherId]), reviewerId].map(
744 (id) => after.turnItems.find((item) => item.id === id)?.status,
745 ),
746 olderStart
747 ? ["interrupted", "interrupted"]
748 : ["interrupted", "interrupted", "interrupted"],
749 );
750 }).pipe(
751 Effect.provide(
752 ProviderReplayHarness.layerWithRegistry(
753 { name: "background-work-stop" },
754 ProviderAdapterRegistry.layerSingle(adapter),
755 { runEffectWorker: false },
756 ),
757 ),
758 );
759 }),
760 );
761​
762it.effect("Stop reaches background work an earlier provider thread still runs", () =>
763 stopEarlierBackgroundWork({}),
764);
765​
766it.effect(
767 "Stop reaches earlier background work after the newest run fails before provider start",
768 () => stopEarlierBackgroundWork({ failedStart: true }),
769);
770​
771it.effect.each(["thread.stop", "run.interrupt"] as const)(
772 "%s stops background work when a later message is queued",
773 (stopType) => stopEarlierBackgroundWork({ stopWithQueue: stopType }),
774);
775​
776it.effect.each(["thread.stop", "run.interrupt"] as const)(
777 "%s reaches later background work when an older run is starting",
778 (stopType) => stopEarlierBackgroundWork({ stopWithQueue: stopType, olderStart: true }),
779);
780​
781it.effect.each([
782 "missing-session",
783 "missing-session-terminal",
784 "returned-interrupt",
785 "returned-interrupt-terminal",
786 "superseded-attempt",
787] as const)("Stop recovers a stalled run after %s without changing a newer attempt", (stalledRun) =>
788 stopEarlierBackgroundWork({ stalledRun }),
789);