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

ProviderRuntimeRecoveryService.test.ts

1,274 lines46.4 KB

1import { assert, it, vi } from "@effect/vitest";
2import {
3 MessageId,
4 NodeId,
5 ProviderDriverKind,
6 ProviderInstanceId,
7 ProviderSessionId,
8 ProviderThreadId,
9 ProviderTurnId,
10 RunAttemptId,
11 RunId,
12 RuntimeRequestId,
13 ThreadId,
14 TurnItemId,
15 type OrchestrationV2ThreadProjection,
16} from "@t3tools/contracts";
17import * as Effect from "effect/Effect";
18import * as Layer from "effect/Layer";
19import * as Ref from "effect/Ref";
20​
21import * as EffectWorker from "./EffectWorker.ts";
22import * as EffectOutbox from "./EffectOutbox.ts";
23import * as EventSink from "./EventSink.ts";
24import * as IdAllocator from "./IdAllocator.ts";
25import * as ProjectionStore from "./ProjectionStore.ts";
26import * as ProviderRuntimeRecovery from "./ProviderRuntimeRecoveryService.ts";
27import * as ServerSettings from "../serverSettings.ts";
28​
29it.effect("leaves durable effects for the worker after runtime reconciliation", () =>
30 Effect.gen(function* () {
31 const runs = yield* Ref.make(0);
32 const layer = ProviderRuntimeRecovery.layer.pipe(
33 Layer.provide(ServerSettings.layerTest()),
34 Layer.provide(
35 Layer.mergeAll(
36 Layer.mock(ProjectionStore.ProjectionStoreV2)({
37 getRecoveryThreadIds: () => Effect.succeed([]),
38 }),
39 Layer.mock(EventSink.EventSinkV2)({}),
40 IdAllocator.layer,
41 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
42 runRecoveryOnce: Ref.getAndUpdate(runs, (count) => count + 1).pipe(
43 Effect.map((count) => count < 2),
44 ),
45 }),
46 Layer.mock(EffectOutbox.EffectOutboxV2)({
47 reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
48 }),
49 ),
50 ),
51 );
52 const summary = yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService.pipe(
53 Effect.flatMap((recovery) => recovery.recover),
54 Effect.provide(layer),
55 );
56 assert.deepEqual(summary, {
57 terminalizedRuns: 0,
58 stoppedSessions: 0,
59 closedRequests: 0,
60 retiredEffects: 0,
61 requeuedEffects: 0,
62 });
63 assert.equal(yield* Ref.get(runs), 0);
64 }),
65);
66​
67it.effect("reads recovery projections only for threads that need runtime recovery", () => {
68 const settledThreadIds = Array.from({ length: 1_000 }, (_, index) =>
69 ThreadId.make(`thread_recovery_settled_${index}`),
70 );
71 const recoveryThreadId = ThreadId.make("thread_recovery_candidate");
72 const projectionReads = vi.fn<(threadId: ThreadId) => void>();
73 const layer = ProviderRuntimeRecovery.layer.pipe(
74 Layer.provide(ServerSettings.layerTest()),
75 Layer.provide(
76 Layer.mergeAll(
77 Layer.mock(ProjectionStore.ProjectionStoreV2)({
78 getShellSnapshot: () =>
79 Effect.succeed({
80 schemaVersion: 2,
81 snapshotSequence: 0,
82 threads: [...settledThreadIds, recoveryThreadId].map((id) => ({ id })),
83 archivedThreads: [],
84 } as never),
85 getRecoveryThreadIds: () => Effect.succeed([recoveryThreadId]),
86 getRuntimeRecoveryProjection: (threadId) => {
87 projectionReads(threadId);
88 return Effect.succeed({
89 thread: { id: threadId },
90 runtimeRequests: [],
91 providerSessions: [],
92 providerThreads: [],
93 providerTurns: [],
94 runs: [],
95 attempts: [],
96 nodes: [],
97 subagents: [],
98 messages: [],
99 turnItems: [],
100 } as unknown as OrchestrationV2ThreadProjection);
101 },
102 }),
103 Layer.mock(EventSink.EventSinkV2)({}),
104 IdAllocator.layer,
105 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
106 runRecoveryOnce: Effect.succeed(false),
107 }),
108 Layer.mock(EffectOutbox.EffectOutboxV2)({
109 cancelUnsettled: () => Effect.succeed([]),
110 signalCancellations: () => Effect.void,
111 reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
112 }),
113 ),
114 ),
115 );
116​
117 return Effect.gen(function* () {
118 yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).recover;
119 assert.deepEqual(
120 projectionReads.mock.calls.map(([threadId]) => threadId),
121 [recoveryThreadId],
122 );
123 }).pipe(Effect.provide(layer));
124});
125​
126it.effect("expires orphaned runtime requests before command readiness", () => {
127 const threadId = ThreadId.make("thread_recovery_requests");
128 let committedInput: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0] | null =
129 null;
130 const committed = vi.fn(
131 (input: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0]) => {
132 committedInput = input;
133 return Effect.succeed({ committed: true, cancelledEffectCount: 0 } as never);
134 },
135 );
136 const projection = {
137 thread: { id: threadId },
138 runtimeRequests: [
139 {
140 id: RuntimeRequestId.make("request_orphaned"),
141 nodeId: NodeId.make("node_orphaned"),
142 status: "pending",
143 responseCapability: { type: "not_resumable", reason: "old process" },
144 },
145 ],
146 providerSessions: [],
147 providerThreads: [],
148 runs: [],
149 nodes: [],
150 } as unknown as OrchestrationV2ThreadProjection;
151 const layer = ProviderRuntimeRecovery.layer.pipe(
152 Layer.provide(ServerSettings.layerTest()),
153 Layer.provide(
154 Layer.mergeAll(
155 Layer.mock(ProjectionStore.ProjectionStoreV2)({
156 getRecoveryThreadIds: () => Effect.succeed([threadId]),
157 getRuntimeRecoveryProjection: () => Effect.succeed(projection),
158 }),
159 Layer.mock(EventSink.EventSinkV2)({ commitCommand: committed }),
160 IdAllocator.layer,
161 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
162 runRecoveryOnce: Effect.succeed(false),
163 }),
164 Layer.mock(EffectOutbox.EffectOutboxV2)({
165 reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
166 }),
167 ),
168 ),
169 );
170 return Effect.gen(function* () {
171 yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).recover;
172 const command = committedInput;
173 assert.isNotNull(command);
174 if (command === null) return;
175 assert.equal(command?.events[0]?.type, "runtime-request.updated");
176 if (command?.events[0]?.type === "runtime-request.updated") {
177 assert.equal(command.events[0].payload.status, "expired");
178 assert.equal(command.events[0].payload.responseCapability.type, "not_resumable");
179 }
180 }).pipe(Effect.provide(layer));
181});
182​
183it.effect("preserves async questions across startup and shutdown", () => {
184 const threadId = ThreadId.make("async-recovery-thread");
185 const nodeId = NodeId.make("async-recovery-node");
186 const projection = {
187 thread: { id: threadId },
188 runtimeRequests: [
189 {
190 id: RuntimeRequestId.make("async-recovery-request"),
191 nodeId,
192 status: "pending",
193 responseCapability: { type: "message" },
194 },
195 ],
196 providerSessions: [],
197 providerThreads: [],
198 runs: [],
199 nodes: [],
200 turnItems: [],
201 } as unknown as OrchestrationV2ThreadProjection;
202 const commitCommand = vi.fn(() => Effect.die("an async question needs no process-loss write"));
203 const layer = ProviderRuntimeRecovery.layer.pipe(
204 Layer.provide(ServerSettings.layerTest()),
205 Layer.provide(
206 Layer.mergeAll(
207 Layer.mock(ProjectionStore.ProjectionStoreV2)({
208 getRecoveryThreadIds: () => Effect.succeed([threadId]),
209 getRuntimeRecoveryProjection: () => Effect.succeed(projection),
210 }),
211 Layer.mock(EventSink.EventSinkV2)({ commitCommand }),
212 IdAllocator.layer,
213 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
214 runRecoveryOnce: Effect.succeed(false),
215 }),
216 Layer.mock(EffectOutbox.EffectOutboxV2)({
217 reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
218 cancelUnsettled: () => Effect.succeed([]),
219 signalCancellations: () => Effect.void,
220 }),
221 ),
222 ),
223 );
224 return Effect.gen(function* () {
225 const recovery = yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService;
226 assert.equal((yield* recovery.reconcile("startup")).closedRequests, 0);
227 assert.equal((yield* recovery.reconcile("startup")).closedRequests, 0);
228 assert.isFalse(commitCommand.mock.calls.length > 0);
229 }).pipe(Effect.provide(layer));
230});
231​
232it.effect("uses the same reconciliation path to cancel runtime requests during shutdown", () => {
233 const threadId = ThreadId.make("thread_shutdown_requests");
234 let committedInput: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0] | null =
235 null;
236 const projection = {
237 thread: { id: threadId },
238 runtimeRequests: [
239 {
240 id: RuntimeRequestId.make("request_shutdown"),
241 nodeId: NodeId.make("node_shutdown"),
242 status: "pending",
243 responseCapability: { type: "live" },
244 },
245 ],
246 providerSessions: [],
247 providerThreads: [],
248 runs: [],
249 nodes: [],
250 } as unknown as OrchestrationV2ThreadProjection;
251 const layer = ProviderRuntimeRecovery.layer.pipe(
252 Layer.provide(ServerSettings.layerTest()),
253 Layer.provide(
254 Layer.mergeAll(
255 Layer.mock(ProjectionStore.ProjectionStoreV2)({
256 getRecoveryThreadIds: () => Effect.succeed([threadId]),
257 getRuntimeRecoveryProjection: () => Effect.succeed(projection),
258 }),
259 Layer.mock(EventSink.EventSinkV2)({
260 commitCommand: (input) => {
261 committedInput = input;
262 return Effect.succeed({ committed: true, cancelledEffectCount: 1 } as never);
263 },
264 }),
265 IdAllocator.layer,
266 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
267 runRecoveryOnce: Effect.succeed(false),
268 }),
269 Layer.mock(EffectOutbox.EffectOutboxV2)({
270 reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
271 }),
272 ),
273 ),
274 );
275​
276 return Effect.gen(function* () {
277 const summary =
278 yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).reconcile("shutdown");
279 assert.equal(summary.closedRequests, 1);
280 assert.equal(summary.retiredEffects, 1);
281 const requestEvent = committedInput?.events[0];
282 assert.equal(requestEvent?.type, "runtime-request.updated");
283 if (requestEvent?.type === "runtime-request.updated") {
284 assert.equal(requestEvent.payload.status, "cancelled");
285 assert.equal(requestEvent.payload.responseCapability.type, "not_resumable");
286 if (requestEvent.payload.responseCapability.type === "not_resumable") {
287 assert.match(requestEvent.payload.responseCapability.reason, /shut down/);
288 }
289 }
290 }).pipe(Effect.provide(layer));
291});
292​
293it.effect(
294 "preserves a replayable waiting run while cancelling its process-bound background work",
295 () => {
296 const threadId = ThreadId.make("thread_waiting_checkpoint");
297 const runId = RunId.make("run_waiting_checkpoint");
298 const providerThreadId = ProviderThreadId.make("provider_thread_waiting_checkpoint");
299 const providerInstanceId = ProviderInstanceId.make("claude");
300 const backgroundItemId = TurnItemId.make("turn_item_waiting_checkpoint");
301 let committedInput: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0] | null =
302 null;
303 const projection = {
304 thread: { id: threadId },
305 runtimeRequests: [],
306 providerSessions: [],
307 providerThreads: [
308 {
309 id: providerThreadId,
310 driver: ProviderDriverKind.make("claude"),
311 providerInstanceId,
312 status: "idle",
313 pendingBackgroundTasks: [
314 { taskId: String(backgroundItemId), description: "Finish background task" },
315 ],
316 },
317 ],
318 runs: [{ id: runId, status: "waiting", providerInstanceId, providerThreadId }],
319 attempts: [],
320 nodes: [],
321 subagents: [],
322 messages: [],
323 turnItems: [
324 {
325 id: backgroundItemId,
326 runId,
327 nodeId: null,
328 providerThreadId,
329 type: "command_execution",
330 status: "running",
331 },
332 ],
333 } as unknown as OrchestrationV2ThreadProjection;
334 const layer = ProviderRuntimeRecovery.layer.pipe(
335 Layer.provide(ServerSettings.layerTest()),
336 Layer.provide(
337 Layer.mergeAll(
338 Layer.mock(ProjectionStore.ProjectionStoreV2)({
339 getRecoveryThreadIds: () => Effect.succeed([threadId]),
340 getRuntimeRecoveryProjection: () => Effect.succeed(projection),
341 }),
342 Layer.mock(EventSink.EventSinkV2)({
343 commitCommand: (input) => {
344 committedInput = input;
345 return Effect.succeed({
346 committed: true,
347 cancelledEffectCount: 0,
348 } as never);
349 },
350 }),
351 IdAllocator.layer,
352 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
353 runRecoveryOnce: Effect.succeed(false),
354 }),
355 Layer.mock(EffectOutbox.EffectOutboxV2)({
356 listByCommandId: () =>
357 Effect.succeed([
358 {
359 request: { type: "checkpoint.capture", runId },
360 status: "running",
361 },
362 ] as never),
363 reconcileAfterProcessLoss: Effect.succeed({ requeued: 1, cancelled: 0 }),
364 }),
365 ),
366 ),
367 );
368​
369 return Effect.gen(function* () {
370 const summary =
371 yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).reconcile("startup");
372 assert.equal(summary.terminalizedRuns, 0);
373 assert.equal(summary.requeuedEffects, 1);
374​
375 const command = committedInput;
376 assert.isNotNull(command);
377 if (command === null) return;
378 // The run stays waiting for its checkpoint; it only records the work
379 // the restart cancelled so the next provider turn can be told.
380 assert.isFalse(command.events.some((event) => event.type === "run.updated"));
381 const recorded = command.events.flatMap((event) =>
382 event.type === "run.background-work-cancelled" ? [event.payload] : [],
383 );
384 // The roster entry is the same task as the item, so it is listed once.
385 assert.deepEqual(
386 recorded.map((entry) => ({
387 runId: entry.runId,
388 kinds: entry.restartCancelledBackgroundWork.map((work) => work.kind),
389 })),
390 [{ runId, kinds: ["shell"] }],
391 );
392 assert.isTrue(
393 command.events.some(
394 (event) =>
395 event.type === "turn-item.updated" &&
396 event.payload.id === backgroundItemId &&
397 event.payload.status === "cancelled",
398 ),
399 );
400 assert.isTrue(
401 command.events.some(
402 (event) =>
403 event.type === "provider-thread.updated" &&
404 event.payload.id === providerThreadId &&
405 event.payload.pendingBackgroundTasks?.length === 0,
406 ),
407 );
408 }).pipe(Effect.provide(layer));
409 },
410);
411​
412it.effect(
413 "records cancelled work on the provider thread that lost it after a provider switch",
414 () => {
415 const threadId = ThreadId.make("thread_switch_note");
416 const claudeRunId = RunId.make("run_switch_note_claude");
417 const codexRunId = RunId.make("run_switch_note_codex");
418 const claudeThreadId = ProviderThreadId.make("provider_thread_switch_note_claude");
419 const codexThreadId = ProviderThreadId.make("provider_thread_switch_note_codex");
420 const claude = ProviderInstanceId.make("claude");
421 const codex = ProviderInstanceId.make("codex");
422 let committedInput: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0] | null =
423 null;
424 // Claude launched a background subagent, then the thread switched to Codex.
425 const projection = {
426 thread: { id: threadId },
427 runtimeRequests: [],
428 providerSessions: [],
429 providerThreads: [
430 {
431 id: claudeThreadId,
432 ownerNodeId: null,
433 driver: ProviderDriverKind.make("claudeAgent"),
434 providerInstanceId: claude,
435 status: "idle",
436 pendingBackgroundTasks: [{ taskId: "task-claude", description: "Watch the build" }],
437 },
438 {
439 id: codexThreadId,
440 ownerNodeId: null,
441 driver: ProviderDriverKind.make("codex"),
442 providerInstanceId: codex,
443 status: "idle",
444 },
445 ],
446 providerTurns: [],
447 runs: [
448 {
449 id: claudeRunId,
450 ordinal: 1,
451 status: "completed",
452 providerInstanceId: claude,
453 providerThreadId: claudeThreadId,
454 },
455 {
456 id: codexRunId,
457 ordinal: 2,
458 status: "completed",
459 providerInstanceId: codex,
460 providerThreadId: codexThreadId,
461 },
462 ],
463 attempts: [],
464 nodes: [],
465 subagents: [],
466 messages: [],
467 turnItems: [
468 {
469 id: TurnItemId.make("turn_item_switch_note_subagent"),
470 runId: claudeRunId,
471 nodeId: null,
472 providerThreadId: claudeThreadId,
473 nativeItemRef: null,
474 type: "subagent",
475 title: "Background subagent",
476 status: "running",
477 },
478 ],
479 } as unknown as OrchestrationV2ThreadProjection;
480 const layer = ProviderRuntimeRecovery.layer.pipe(
481 Layer.provide(ServerSettings.layerTest()),
482 Layer.provide(
483 Layer.mergeAll(
484 Layer.mock(ProjectionStore.ProjectionStoreV2)({
485 getRecoveryThreadIds: () => Effect.succeed([threadId]),
486 getRuntimeRecoveryProjection: () => Effect.succeed(projection),
487 }),
488 Layer.mock(EventSink.EventSinkV2)({
489 commitCommand: (input) => {
490 committedInput = input;
491 return Effect.succeed({ committed: true, cancelledEffectCount: 0 } as never);
492 },
493 }),
494 IdAllocator.layer,
495 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
496 runRecoveryOnce: Effect.succeed(false),
497 }),
498 Layer.mock(EffectOutbox.EffectOutboxV2)({
499 reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
500 }),
501 ),
502 ),
503 );
504​
505 return Effect.gen(function* () {
506 yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).reconcile("startup");
507 const recorded = (committedInput?.events ?? []).flatMap((event) =>
508 event.type === "run.background-work-cancelled" ? [event.payload] : [],
509 );
510 // The Codex run is later, but only Claude's turns may be told about it.
511 assert.deepEqual(
512 recorded.map((entry) => ({
513 runId: entry.runId,
514 kinds: entry.restartCancelledBackgroundWork.map((work) => work.kind),
515 })),
516 [{ runId: claudeRunId, kinds: ["subagent", "task"] }],
517 );
518 }).pipe(Effect.provide(layer));
519 },
520);
521​
522it.effect("cancels a stale waiting run when no checkpoint capture can finish it", () => {
523 const threadId = ThreadId.make("thread_stale_waiting");
524 const runId = RunId.make("run_stale_waiting");
525 let committedInput: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0] | null =
526 null;
527 const projection = {
528 thread: { id: threadId },
529 runtimeRequests: [],
530 providerSessions: [],
531 providerThreads: [],
532 providerTurns: [],
533 runs: [
534 {
535 id: runId,
536 status: "waiting",
537 providerInstanceId: ProviderInstanceId.make("codex"),
538 },
539 ],
540 attempts: [],
541 nodes: [],
542 subagents: [],
543 messages: [],
544 turnItems: [],
545 } as unknown as OrchestrationV2ThreadProjection;
546 const layer = ProviderRuntimeRecovery.layer.pipe(
547 Layer.provide(ServerSettings.layerTest()),
548 Layer.provide(
549 Layer.mergeAll(
550 Layer.mock(ProjectionStore.ProjectionStoreV2)({
551 getRecoveryThreadIds: () => Effect.succeed([threadId]),
552 getRuntimeRecoveryProjection: () => Effect.succeed(projection),
553 }),
554 Layer.mock(EventSink.EventSinkV2)({
555 commitCommand: (input) => {
556 committedInput = input;
557 return Effect.succeed({ committed: true, cancelledEffectCount: 0 } as never);
558 },
559 }),
560 IdAllocator.layer,
561 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
562 runRecoveryOnce: Effect.succeed(false),
563 }),
564 Layer.mock(EffectOutbox.EffectOutboxV2)({
565 listByCommandId: () => Effect.succeed([]),
566 reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
567 }),
568 ),
569 ),
570 );
571​
572 return Effect.gen(function* () {
573 const summary =
574 yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).reconcile("startup");
575 assert.equal(summary.terminalizedRuns, 1);
576 const runEvent = committedInput?.events.find((event) => event.type === "run.updated");
577 assert.equal(runEvent?.type === "run.updated" ? runEvent.payload.status : null, "cancelled");
578 }).pipe(Effect.provide(layer));
579});
580​
581it.effect("holds accepted queued work without cancelling its execution state after restart", () => {
582 const threadId = ThreadId.make("thread_queued_restart");
583 const runId = RunId.make("run_queued_restart");
584 const attemptId = RunAttemptId.make("attempt_queued_restart");
585 const rootNodeId = NodeId.make("node_queued_restart");
586 let committedInput: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0] | null =
587 null;
588 const projection = {
589 thread: { id: threadId },
590 runtimeRequests: [],
591 providerSessions: [],
592 providerThreads: [],
593 providerTurns: [],
594 runs: [
595 {
596 id: runId,
597 status: "queued",
598 queuePosition: 1,
599 providerInstanceId: ProviderInstanceId.make("codex"),
600 },
601 ],
602 attempts: [
603 {
604 id: attemptId,
605 runId,
606 rootNodeId,
607 status: "pending",
608 },
609 ],
610 nodes: [
611 {
612 id: rootNodeId,
613 runId,
614 status: "pending",
615 },
616 ],
617 subagents: [],
618 messages: [],
619 turnItems: [],
620 } as unknown as OrchestrationV2ThreadProjection;
621 const layer = ProviderRuntimeRecovery.layer.pipe(
622 Layer.provide(ServerSettings.layerTest()),
623 Layer.provide(
624 Layer.mergeAll(
625 Layer.mock(ProjectionStore.ProjectionStoreV2)({
626 getRecoveryThreadIds: () => Effect.succeed([threadId]),
627 getRuntimeRecoveryProjection: () => Effect.succeed(projection),
628 }),
629 Layer.mock(EventSink.EventSinkV2)({
630 commitCommand: (input) => {
631 committedInput = input;
632 return Effect.succeed({ committed: true, cancelledEffectCount: 0 } as never);
633 },
634 }),
635 IdAllocator.layer,
636 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
637 runRecoveryOnce: Effect.succeed(false),
638 }),
639 Layer.mock(EffectOutbox.EffectOutboxV2)({
640 reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
641 }),
642 ),
643 ),
644 );
645​
646 return Effect.gen(function* () {
647 const summary =
648 yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).reconcile("startup");
649 assert.equal(summary.terminalizedRuns, 0);
650 const command = committedInput;
651 assert.isNotNull(command);
652 if (command === null) return;
653 const runEvent = command.events.find((event) => event.type === "run.updated");
654 const attemptEvent = command.events.find((event) => event.type === "run-attempt.updated");
655 const nodeEvent = command.events.find((event) => event.type === "node.updated");
656 assert.equal(runEvent?.type === "run.updated" ? runEvent.payload.status : null, "queued");
657 assert.equal(runEvent?.type === "run.updated" ? runEvent.payload.queuePosition : null, 1);
658 assert.equal(runEvent?.type === "run.updated" ? runEvent.payload.queueHeld : false, true);
659 assert.isUndefined(attemptEvent);
660 assert.isUndefined(nodeEvent);
661 }).pipe(Effect.provide(layer));
662});
663​
664it.effect(
665 "cancels the complete in-flight subtree and stops its persisted session without reopening it",
666 () => {
667 const threadId = ThreadId.make("thread_recovery_cancel");
668 const runId = RunId.make("run_recovery_cancel");
669 const attemptId = RunAttemptId.make("attempt_recovery_cancel");
670 const rootNodeId = NodeId.make("node_recovery_cancel");
671 const providerThreadId = ProviderThreadId.make("provider_thread_recovery_cancel");
672 const providerTurnId = ProviderTurnId.make("provider_turn_recovery_cancel");
673 const providerSessionId = ProviderSessionId.make("provider_session_recovery_cancel");
674 let committedInput: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0] | null =
675 null;
676 const projection = {
677 thread: { id: threadId },
678 runtimeRequests: [],
679 providerSessions: [
680 {
681 id: providerSessionId,
682 driver: ProviderDriverKind.make("codex"),
683 providerInstanceId: ProviderInstanceId.make("codex"),
684 status: "ready",
685 },
686 ],
687 providerThreads: [
688 {
689 id: providerThreadId,
690 driver: ProviderDriverKind.make("codex"),
691 providerInstanceId: ProviderInstanceId.make("codex"),
692 status: "active",
693 },
694 ],
695 providerTurns: [
696 {
697 id: providerTurnId,
698 runAttemptId: attemptId,
699 nodeId: rootNodeId,
700 status: "running",
701 },
702 ],
703 runs: [
704 {
705 id: runId,
706 status: "starting",
707 providerThreadId,
708 providerInstanceId: ProviderInstanceId.make("codex"),
709 },
710 ],
711 attempts: [
712 {
713 id: attemptId,
714 runId,
715 rootNodeId,
716 status: "running",
717 },
718 ],
719 nodes: [{ id: rootNodeId, runId, status: "running" }],
720 subagents: [],
721 messages: [{ id: MessageId.make("message_recovery_cancel"), runId, streaming: true }],
722 turnItems: [
723 {
724 id: TurnItemId.make("turn_item_recovery_cancel"),
725 runId,
726 nodeId: rootNodeId,
727 status: "running",
728 },
729 ],
730 } as unknown as OrchestrationV2ThreadProjection;
731 const layer = ProviderRuntimeRecovery.layer.pipe(
732 Layer.provide(ServerSettings.layerTest()),
733 Layer.provide(
734 Layer.mergeAll(
735 Layer.mock(ProjectionStore.ProjectionStoreV2)({
736 getRecoveryThreadIds: () => Effect.succeed([threadId]),
737 getRuntimeRecoveryProjection: () => Effect.succeed(projection),
738 }),
739 Layer.mock(EventSink.EventSinkV2)({
740 commitCommand: (input) => {
741 committedInput = input;
742 return Effect.succeed({ committed: true, cancelledEffectCount: 2 } as never);
743 },
744 }),
745 IdAllocator.layer,
746 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
747 runRecoveryOnce: Effect.succeed(false),
748 }),
749 Layer.mock(EffectOutbox.EffectOutboxV2)({
750 reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
751 }),
752 ),
753 ),
754 );
755​
756 return Effect.gen(function* () {
757 const summary = yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService)
758 .recover;
759 assert.equal(summary.terminalizedRuns, 1);
760 assert.equal(summary.stoppedSessions, 1);
761 assert.equal(summary.retiredEffects, 2);
762 assert.deepEqual(committedInput?.cancelUnsettledEffects?.effectTypes, [
763 "provider-turn.start",
764 "provider-turn.interrupt",
765 "provider-turn.steer",
766 "provider-turn.restart",
767 "runtime-request.respond",
768 ]);
769 const events = committedInput?.events ?? [];
770 assert.deepEqual(
771 events.map((event) => [
772 event.type,
773 "status" in event.payload ? event.payload.status : null,
774 ]),
775 [
776 ["run.updated", "cancelled"],
777 ["run-attempt.updated", "cancelled"],
778 ["node.updated", "cancelled"],
779 ["provider-turn.updated", "cancelled"],
780 ["message.updated", null],
781 ["turn-item.updated", "cancelled"],
782 ["provider-thread.updated", "idle"],
783 ["provider-session.updated", "stopped"],
784 ],
785 );
786 const messageEvent = events.find((event) => event.type === "message.updated");
787 assert.isFalse(messageEvent?.type === "message.updated" && messageEvent.payload.streaming);
788 }).pipe(Effect.provide(layer));
789 },
790);
791​
792it.effect(
793 "clears persisted pendingBackgroundTasks and terminalizes stale background items on settled runs",
794 () => {
795 const threadId = ThreadId.make("thread_recovery_background");
796 const settledRunId = RunId.make("run_recovery_background_settled");
797 const activeRunId = RunId.make("run_recovery_background_active");
798 const activeAttemptId = RunAttemptId.make("attempt_recovery_background_active");
799 const activeRootNodeId = NodeId.make("node_recovery_background_active");
800 const idleProviderThreadId = ProviderThreadId.make("provider_thread_recovery_background_idle");
801 const activeProviderThreadId = ProviderThreadId.make(
802 "provider_thread_recovery_background_active",
803 );
804 const secondaryProviderThreadId = ProviderThreadId.make(
805 "provider_thread_recovery_background_secondary",
806 );
807 const providerSessionId = ProviderSessionId.make("provider_session_recovery_background");
808 const settledStaleItemId = TurnItemId.make("turn_item_recovery_background_stale");
809 const activeRunItemId = TurnItemId.make("turn_item_recovery_background_active");
810 const nullRunCommandItemId = TurnItemId.make("turn_item_recovery_background_null_run");
811 const nullRunSubagentItemId = TurnItemId.make("turn_item_recovery_background_null_subagent");
812 const claudeInstanceId = ProviderInstanceId.make("claude");
813 const secondaryInstanceId = ProviderInstanceId.make("claude-secondary");
814 const subagentInstanceId = ProviderInstanceId.make("claude-subagent");
815 let committedInput: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0] | null =
816 null;
817 const projection = {
818 thread: { id: threadId },
819 runtimeRequests: [],
820 providerSessions: [
821 {
822 id: providerSessionId,
823 driver: ProviderDriverKind.make("claude"),
824 providerInstanceId: claudeInstanceId,
825 status: "ready",
826 },
827 ],
828 providerThreads: [
829 {
830 id: idleProviderThreadId,
831 driver: ProviderDriverKind.make("claude"),
832 // Index-0 is intentionally a different instance so misattribution
833 // to providerThreads[0] fails the assertions below.
834 providerInstanceId: claudeInstanceId,
835 status: "idle",
836 pendingBackgroundTasks: [{ taskId: "bg-settled", description: "sleep 30" }],
837 },
838 {
839 id: activeProviderThreadId,
840 driver: ProviderDriverKind.make("claude"),
841 providerInstanceId: claudeInstanceId,
842 status: "active",
843 pendingBackgroundTasks: [{ taskId: "bg-active", description: "npm test" }],
844 },
845 {
846 id: secondaryProviderThreadId,
847 driver: ProviderDriverKind.make("claude"),
848 providerInstanceId: secondaryInstanceId,
849 status: "idle",
850 pendingBackgroundTasks: [],
851 },
852 ],
853 providerTurns: [],
854 runs: [
855 {
856 id: settledRunId,
857 status: "completed",
858 providerInstanceId: claudeInstanceId,
859 },
860 {
861 id: activeRunId,
862 status: "running",
863 providerInstanceId: claudeInstanceId,
864 },
865 ],
866 attempts: [
867 {
868 id: activeAttemptId,
869 runId: activeRunId,
870 rootNodeId: activeRootNodeId,
871 status: "running",
872 },
873 ],
874 nodes: [{ id: activeRootNodeId, runId: activeRunId, status: "running" }],
875 subagents: [],
876 messages: [],
877 turnItems: [
878 {
879 id: settledStaleItemId,
880 runId: settledRunId,
881 nodeId: null,
882 providerThreadId: idleProviderThreadId,
883 type: "command_execution",
884 status: "running",
885 },
886 {
887 id: activeRunItemId,
888 runId: activeRunId,
889 nodeId: activeRootNodeId,
890 providerThreadId: activeProviderThreadId,
891 type: "dynamic_tool",
892 status: "running",
893 },
894 {
895 // Missing run: must attribute via providerThreadId, not index 0.
896 id: nullRunCommandItemId,
897 runId: null,
898 nodeId: null,
899 providerThreadId: secondaryProviderThreadId,
900 type: "command_execution",
901 status: "running",
902 },
903 {
904 // Missing run with a real matching provider thread whose instance
905 // differs from the subagent's own: own providerInstanceId must win.
906 id: nullRunSubagentItemId,
907 runId: null,
908 nodeId: null,
909 providerThreadId: secondaryProviderThreadId,
910 type: "subagent",
911 status: "running",
912 providerInstanceId: subagentInstanceId,
913 },
914 ],
915 } as unknown as OrchestrationV2ThreadProjection;
916 const layer = ProviderRuntimeRecovery.layer.pipe(
917 Layer.provide(ServerSettings.layerTest()),
918 Layer.provide(
919 Layer.mergeAll(
920 Layer.mock(ProjectionStore.ProjectionStoreV2)({
921 getRecoveryThreadIds: () => Effect.succeed([threadId]),
922 getRuntimeRecoveryProjection: () => Effect.succeed(projection),
923 }),
924 Layer.mock(EventSink.EventSinkV2)({
925 commitCommand: (input) => {
926 committedInput = input;
927 return Effect.succeed({ committed: true, cancelledEffectCount: 0 } as never);
928 },
929 }),
930 IdAllocator.layer,
931 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
932 runRecoveryOnce: Effect.succeed(false),
933 }),
934 Layer.mock(EffectOutbox.EffectOutboxV2)({
935 listByCommandId: () => Effect.succeed([]),
936 reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
937 }),
938 ),
939 ),
940 );
941​
942 return Effect.gen(function* () {
943 const summary =
944 yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).reconcile("startup");
945 assert.equal(summary.terminalizedRuns, 1);
946 const events = committedInput?.events ?? [];
947​
948 const turnItemCancels = events.filter(
949 (event) => event.type === "turn-item.updated" && event.payload.status === "cancelled",
950 );
951 // Active-run item + settled-run stale + null-run command + null-run subagent.
952 assert.equal(turnItemCancels.length, 4);
953 assert.deepEqual(
954 turnItemCancels
955 .map((event) => event.type === "turn-item.updated" && event.payload.id)
956 .sort(),
957 [activeRunItemId, nullRunCommandItemId, nullRunSubagentItemId, settledStaleItemId].sort(),
958 );
959​
960 const cancelById = (id: TurnItemId) =>
961 turnItemCancels.find(
962 (event) => event.type === "turn-item.updated" && event.payload.id === id,
963 );
964 assert.equal(cancelById(nullRunCommandItemId)?.providerInstanceId, secondaryInstanceId);
965 // Subagent own instance wins over the matching thread's secondary instance.
966 assert.notEqual(subagentInstanceId, secondaryInstanceId);
967 assert.equal(cancelById(nullRunSubagentItemId)?.providerInstanceId, subagentInstanceId);
968 // Settled-run item still prefers the run's provider instance when present.
969 assert.equal(cancelById(settledStaleItemId)?.providerInstanceId, claudeInstanceId);
970​
971 const providerThreadEvents = events.filter(
972 (event) => event.type === "provider-thread.updated",
973 );
974 // Only threads with active status or nonempty rosters are rewritten.
975 assert.equal(providerThreadEvents.length, 2);
976 for (const event of providerThreadEvents) {
977 if (event.type !== "provider-thread.updated") continue;
978 assert.deepEqual(event.payload.pendingBackgroundTasks ?? [], []);
979 }
980 const idleThreadEvent = providerThreadEvents.find(
981 (event) =>
982 event.type === "provider-thread.updated" && event.payload.id === idleProviderThreadId,
983 );
984 assert.equal(
985 idleThreadEvent?.type === "provider-thread.updated" ? idleThreadEvent.payload.status : null,
986 "idle",
987 );
988 const activeThreadEvent = providerThreadEvents.find(
989 (event) =>
990 event.type === "provider-thread.updated" && event.payload.id === activeProviderThreadId,
991 );
992 assert.equal(
993 activeThreadEvent?.type === "provider-thread.updated"
994 ? activeThreadEvent.payload.status
995 : null,
996 "idle",
997 );
998 }).pipe(Effect.provide(layer));
999 },
1001​
1002it.effect(
1003 "terminalizes a leftover nonpersistent dynamic_tool on a settled run after process loss",
1004 () => {
1005 const threadId = ThreadId.make("thread_recovery_orphan_wait");
1006 const settledRunId = RunId.make("run_recovery_orphan_wait_settled");
1007 const providerThreadId = ProviderThreadId.make("provider_thread_recovery_orphan_wait");
1008 const orphanWaitItemId = TurnItemId.make(
1009 "turn-item:provider:codex:native-item:exec-4669f3bb-78c9-4af1-b44e-daa340d2c538",
1010 );
1011 const persistentMonitorItemId = TurnItemId.make(
1012 "turn-item:provider:codex:native-item:exec-persistent-monitor",
1013 );
1014 const orphanWaitNodeId = NodeId.make("node_recovery_orphan_wait");
1015 const persistentMonitorNodeId = NodeId.make("node_recovery_persistent_monitor");
1016 const codexInstanceId = ProviderInstanceId.make("codex");
1017 let committedInput: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0] | null =
1018 null;
1019 const projection = {
1020 thread: { id: threadId, providerInstanceId: codexInstanceId },
1021 runtimeRequests: [],
1022 providerSessions: [],
1023 providerThreads: [
1025 id: providerThreadId,
1026 driver: ProviderDriverKind.make("codex"),
1027 providerInstanceId: codexInstanceId,
1028 status: "idle",
1029 pendingBackgroundTasks: [],
1030 },
1031 ],
1032 providerTurns: [],
1033 runs: [{ id: settledRunId, status: "completed", providerInstanceId: codexInstanceId }],
1034 attempts: [],
1035 nodes: [
1036 { id: orphanWaitNodeId, runId: settledRunId, status: "running", kind: "tool_call" },
1038 id: persistentMonitorNodeId,
1039 runId: settledRunId,
1040 status: "running",
1041 kind: "tool_call",
1042 },
1043 ],
1044 subagents: [],
1045 messages: [],
1046 turnItems: [
1048 id: orphanWaitItemId,
1049 runId: settledRunId,
1050 nodeId: orphanWaitNodeId,
1051 providerThreadId,
1052 type: "dynamic_tool",
1053 status: "running",
1054 toolName: "t3-code.t3_thread_wait",
1055 input: {
1056 threadId:
1057 "thread:delegated-task:command%3Amcp%3Aaafffab1-e811-458a-ae83-558e542c61ff%3Adelegate-task%3Areview-mobile-reconnect-opus-20260815",
1058 timeoutMs: 30000,
1059 },
1060 },
1062 id: persistentMonitorItemId,
1063 runId: settledRunId,
1064 nodeId: persistentMonitorNodeId,
1065 providerThreadId,
1066 type: "dynamic_tool",
1067 status: "running",
1068 toolName: "grok.monitor",
1069 input: { persistent: true, command: "tail -f" },
1070 },
1071 ],
1072 } as unknown as OrchestrationV2ThreadProjection;
1073 const layer = ProviderRuntimeRecovery.layer.pipe(
1074 Layer.provide(ServerSettings.layerTest()),
1075 Layer.provide(
1076 Layer.mergeAll(
1077 Layer.mock(ProjectionStore.ProjectionStoreV2)({
1078 getRecoveryThreadIds: () => Effect.succeed([threadId]),
1079 getRuntimeRecoveryProjection: () => Effect.succeed(projection),
1080 }),
1081 Layer.mock(EventSink.EventSinkV2)({
1082 commitCommand: (input) => {
1083 committedInput = input;
1084 return Effect.succeed({ committed: true, cancelledEffectCount: 0 } as never);
1085 },
1086 }),
1087 IdAllocator.layer,
1088 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
1089 runRecoveryOnce: Effect.succeed(false),
1090 }),
1091 Layer.mock(EffectOutbox.EffectOutboxV2)({
1092 listByCommandId: () => Effect.succeed([]),
1093 reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
1094 }),
1095 ),
1096 ),
1097 );
1098​
1099 return Effect.gen(function* () {
1100 const summary =
1101 yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).reconcile("startup");
1102 assert.equal(summary.terminalizedRuns, 0);
1103 const turnItemCancels = (committedInput?.events ?? []).filter(
1104 (event) => event.type === "turn-item.updated" && event.payload.status === "cancelled",
1105 );
1106 // Process loss means the provider is gone, so even a persistent monitor
1107 // cannot still be alive. The leftover wait and the monitor both close.
1108 assert.equal(turnItemCancels.length, 2);
1109 assert.deepEqual(
1110 turnItemCancels
1111 .map((event) => event.type === "turn-item.updated" && event.payload.id)
1112 .sort(),
1113 [orphanWaitItemId, persistentMonitorItemId].sort(),
1114 );
1115 const nodeCancels = (committedInput?.events ?? []).filter(
1116 (event) => event.type === "node.updated" && event.payload.status === "cancelled",
1117 );
1118 assert.equal(nodeCancels.length, 2);
1119 assert.deepEqual(
1120 nodeCancels.map((event) => event.type === "node.updated" && event.payload.id).sort(),
1121 [orphanWaitNodeId, persistentMonitorNodeId].sort(),
1122 );
1123 }).pipe(Effect.provide(layer));
1124 },
1126​
1127it.effect(
1128 "terminalizes the linked subagent and node for a stale subagent item on a settled run",
1129 () => {
1130 const threadId = ThreadId.make("thread_recovery_subagent");
1131 const settledRunId = RunId.make("run_recovery_subagent_settled");
1132 const providerThreadId = ProviderThreadId.make("provider_thread_recovery_subagent");
1133 const staleSubagentNodeId = NodeId.make("node_recovery_subagent_stale");
1134 const doneSubagentNodeId = NodeId.make("node_recovery_subagent_done");
1135 const staleItemId = TurnItemId.make("turn_item_recovery_subagent_stale");
1136 const doneItemId = TurnItemId.make("turn_item_recovery_subagent_done");
1137 const claudeInstanceId = ProviderInstanceId.make("claude");
1138 let committedInput: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0] | null =
1139 null;
1140 const projection = {
1141 thread: { id: threadId },
1142 runtimeRequests: [],
1143 providerSessions: [],
1144 providerThreads: [
1146 id: providerThreadId,
1147 driver: ProviderDriverKind.make("claude"),
1148 providerInstanceId: claudeInstanceId,
1149 status: "idle",
1150 pendingBackgroundTasks: [],
1151 },
1152 ],
1153 providerTurns: [],
1154 // Settled run: the stale-item loop owns it, not the nonterminal loop.
1155 runs: [{ id: settledRunId, status: "completed", providerInstanceId: claudeInstanceId }],
1156 attempts: [],
1157 nodes: [
1158 { id: staleSubagentNodeId, runId: settledRunId, status: "running" },
1159 { id: doneSubagentNodeId, runId: settledRunId, status: "completed" },
1160 ],
1161 subagents: [
1163 id: staleSubagentNodeId,
1164 runId: settledRunId,
1165 driver: ProviderDriverKind.make("claude"),
1166 providerInstanceId: claudeInstanceId,
1167 status: "running",
1168 },
1170 // Already finished with a real result: must never be overwritten.
1171 id: doneSubagentNodeId,
1172 runId: settledRunId,
1173 driver: ProviderDriverKind.make("claude"),
1174 providerInstanceId: claudeInstanceId,
1175 status: "completed",
1176 result: "done",
1177 },
1178 ],
1179 messages: [],
1180 turnItems: [
1182 id: staleItemId,
1183 runId: settledRunId,
1184 nodeId: staleSubagentNodeId,
1185 providerThreadId,
1186 type: "subagent",
1187 status: "running",
1188 subagentId: staleSubagentNodeId,
1189 providerInstanceId: claudeInstanceId,
1190 },
1192 id: doneItemId,
1193 runId: settledRunId,
1194 nodeId: doneSubagentNodeId,
1195 providerThreadId,
1196 type: "subagent",
1197 status: "completed",
1198 subagentId: doneSubagentNodeId,
1199 providerInstanceId: claudeInstanceId,
1200 },
1201 ],
1202 } as unknown as OrchestrationV2ThreadProjection;
1203 const layer = ProviderRuntimeRecovery.layer.pipe(
1204 Layer.provide(ServerSettings.layerTest()),
1205 Layer.provide(
1206 Layer.mergeAll(
1207 Layer.mock(ProjectionStore.ProjectionStoreV2)({
1208 getRecoveryThreadIds: () => Effect.succeed([threadId]),
1209 getRuntimeRecoveryProjection: () => Effect.succeed(projection),
1210 }),
1211 Layer.mock(EventSink.EventSinkV2)({
1212 commitCommand: (input) => {
1213 committedInput = input;
1214 return Effect.succeed({ committed: true, cancelledEffectCount: 0 } as never);
1215 },
1216 }),
1217 IdAllocator.layer,
1218 Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
1219 runRecoveryOnce: Effect.succeed(false),
1220 }),
1221 Layer.mock(EffectOutbox.EffectOutboxV2)({
1222 listByCommandId: () => Effect.succeed([]),
1223 reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
1224 }),
1225 ),
1226 ),
1227 );
1228​
1229 return Effect.gen(function* () {
1230 yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).reconcile("startup");
1231 const events = committedInput?.events ?? [];
1232​
1233 // Only the nonterminal subagent item is cancelled.
1234 const turnItemCancels = events.filter(
1235 (event) => event.type === "turn-item.updated" && event.payload.status === "cancelled",
1236 );
1237 assert.equal(turnItemCancels.length, 1);
1238​
1239 // The linked subagent entity is terminalized alongside its turn item.
1240 const subagentCancels = events.filter((event) => event.type === "subagent.updated");
1241 assert.equal(subagentCancels.length, 1);
1242 const subagentCancel = subagentCancels[0];
1243 assert.equal(
1244 subagentCancel?.type === "subagent.updated" ? subagentCancel.payload.id : null,
1245 staleSubagentNodeId,
1246 );
1247 assert.equal(
1248 subagentCancel?.type === "subagent.updated" ? subagentCancel.payload.status : null,
1249 "cancelled",
1250 );
1251​
1252 // So is its execution node, which no live process can terminalize.
1253 const nodeCancels = events.filter((event) => event.type === "node.updated");
1254 assert.equal(nodeCancels.length, 1);
1255 const nodeCancel = nodeCancels[0];
1256 assert.equal(
1257 nodeCancel?.type === "node.updated" ? nodeCancel.payload.id : null,
1258 staleSubagentNodeId,
1259 );
1260​
1261 // The already-completed subagent and node are left untouched.
1262 assert.isFalse(
1263 events.some(
1264 (event) => event.type === "subagent.updated" && event.payload.id === doneSubagentNodeId,
1265 ),
1266 );
1267 assert.isFalse(
1268 events.some(
1269 (event) => event.type === "node.updated" && event.payload.id === doneSubagentNodeId,
1270 ),
1271 );
1272 }).pipe(Effect.provide(layer));
1273 },