Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import {
3 CommandId,
4 ContextTransferId,
5 EventId,
6 MessageId,
7 NodeId,
8 type OrchestrationV2DomainEvent,
9 ProjectId,
10 ProviderDriverKind,
11 ProviderInstanceId,
12 ProviderSessionId,
13 ProviderTurnId,
14 RuntimeRequestId,
15 ThreadId,
16 TurnItemId,
17} from "@t3tools/contracts";
18import * as DateTime from "effect/DateTime";
19import * as Effect from "effect/Effect";
20import * as Exit from "effect/Exit";
21import * as Layer from "effect/Layer";
22import * as Schema from "effect/Schema";
23import * as SqlClient from "effect/sql/SqlClient";
24import * as Stream from "effect/Stream";
25import * as EventStore from "./EventStore.ts";
26import * as TestClock from "effect/testing/TestClock";
27
28import * as SqlitePersistence from "../persistence/Sqlite.ts";
29import * as ServerSettings from "../serverSettings.ts";
30import { CLAUDE_PROVIDER } from "./Adapters/ClaudeAdapterV2.ts";
31import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
32import * as CommandReceiptStore from "./CommandReceiptStore.ts";
33import { OrchestrationEffectRequestV2 } from "./EffectOutbox.ts";
34import * as Orchestrator from "./Orchestrator.ts";
35import * as EffectWorker from "./EffectWorker.ts";
36import * as EffectOutbox from "./EffectOutbox.ts";
37import * as ProjectionStore from "./ProjectionStore.ts";
38import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
39import * as ThreadManagementService from "./ThreadManagementService.ts";
40import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
41import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
42
43const instanceId = ProviderInstanceId.make("codex");
44const modelSelection = { instanceId, model: "gpt-5.1-codex" };
45const adapter = {
46 instanceId,
47 driver: ProviderDriverKind.make("codex"),
48 getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2),
49 planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" as const }),
50 openSession: () => Effect.die("Runs here never reach a provider"),
51} as ProviderAdapter.ProviderAdapterV2["Service"];
52const layerDatabase = SqlitePersistence.layerMemory;
53const layerTest = ThreadManagementService.layer.pipe(
54 Layer.provideMerge(
55 Layer.mergeAll(
56 layerDatabase,
57 ProjectionStore.layer.pipe(Layer.provide(layerDatabase)),
58 EventStore.layer.pipe(Layer.provide(layerDatabase)),
59 EffectOutbox.layer.pipe(Layer.provide(layerDatabase)),
60 ProviderReplayHarness.layerWithRegistry(
61 { name: "thread-stop" },
62 ProviderAdapterRegistry.layerFromAdapters([adapter]),
63 { databaseLayer: layerDatabase, runEffectWorker: false },
64 ),
65 ),
66 ),
67 Layer.provideMerge(ServerSettings.layerTest()),
68 Layer.provideMerge(CommandReceiptStore.layer.pipe(Layer.provide(layerDatabase))),
69);
70
71const encodeEffectRequest = Schema.encodeSync(Schema.fromJsonString(OrchestrationEffectRequestV2));
72
73const pullRequest = (number: number) => ({
74 host: "github.com",
75 repository: "pingdotgg/t3code",
76 number,
77});
78
79const createWatchingThread = (threadId: ThreadId, number: number) =>
80 Effect.gen(function* () {
81 const orchestrator = yield* Orchestrator.OrchestratorV2;
82 yield* orchestrator.dispatch({
83 type: "thread.create",
84 commandId: CommandId.make(`create:${threadId}`),
85 threadId,
86 projectId: ProjectId.make("project:thread-stop"),
87 title: threadId,
88 modelSelection,
89 runtimeMode: "full-access",
90 interactionMode: "default",
91 branch: null,
92 worktreePath: null,
93 createdBy: "user",
94 creationSource: "web",
95 });
96 yield* watch(threadId, number);
97 });
98
99const watch = (threadId: ThreadId, number: number) =>
100 Effect.gen(function* () {
101 const orchestrator = yield* Orchestrator.OrchestratorV2;
102 yield* orchestrator.dispatch({
103 type: "thread.pull-request.watch",
104 commandId: CommandId.make(`watch:${threadId}:${number}`),
105 threadId,
106 ...pullRequest(number),
107 watching: true,
108 link: { url: `https://github.com/pingdotgg/t3code/pull/${number}`, source: "agent" },
109 });
110 });
111
112const send = (threadId: ThreadId, text: string, type: "start_immediately" | "queue_after_active") =>
113 Effect.gen(function* () {
114 const orchestrator = yield* Orchestrator.OrchestratorV2;
115 yield* orchestrator.dispatch({
116 type: "message.dispatch",
117 commandId: CommandId.make(`send:${threadId}:${text}`),
118 threadId,
119 messageId: MessageId.make(`message:${threadId}:${text}`),
120 text,
121 attachments: [],
122 dispatchMode: { type },
123 createdBy: "user",
124 creationSource: "web",
125 });
126 });
127
128const delegate = (parentThreadId: ThreadId, task: string) =>
129 Effect.gen(function* () {
130 const orchestrator = yield* Orchestrator.OrchestratorV2;
131 const parentRun = (yield* orchestrator.getThreadProjection(parentThreadId)).runs.at(-1)!;
132 yield* orchestrator.dispatch({
133 type: "delegated_task.request",
134 commandId: CommandId.make(`delegate:${task}`),
135 parentThreadId,
136 parentRunId: parentRun.id,
137 parentNodeId: parentRun.rootNodeId!,
138 task,
139 modelSelection,
140 runtimeMode: "full-access",
141 interactionMode: "default",
142 completionWake: "always",
143 createdBy: "agent",
144 creationSource: "mcp",
145 });
146 const projection = yield* orchestrator.getThreadProjection(parentThreadId);
147 return projection.subagents.find((candidate) => candidate.prompt === task)!.childThreadId!;
148 });
149
150const threadState = (threadId: ThreadId) =>
151 Effect.gen(function* () {
152 const orchestrator = yield* Orchestrator.OrchestratorV2;
153 const projection = yield* orchestrator.getThreadProjection(threadId);
154 return {
155 runs: projection.runs.map((run) => `${run.status}${run.queueHeld === true ? ":held" : ""}`),
156 watched: (projection.thread.pullRequests ?? [])
157 .filter((link) => link.watch !== undefined)
158 .map((link) => link.number),
159 };
160 });
161
162it.effect.each([
163 "idle",
164 "archived",
165 "missing",
166 "running",
167 "queued",
168 "waiting",
169 "correlated",
170 "provider-active",
171 "roster",
172 "background",
173 "question",
174 "restart",
175] as const)(
176 "Stop clears an orphaned follow-up only when its child is finished (%s)",
177 (childState) =>
178 Effect.gen(function* () {
179 const orchestrator = yield* Orchestrator.OrchestratorV2;
180 const projections = yield* ProjectionStore.ProjectionStoreV2;
181 const parentThreadId = ThreadId.make(`thread:orphan-parent:${childState}`);
182 yield* createWatchingThread(parentThreadId, 1);
183 yield* send(parentThreadId, "parent", "start_immediately");
184 const childThreadId = yield* delegate(parentThreadId, "original task");
185 const parent = yield* orchestrator.getThreadProjection(parentThreadId);
186 const child = yield* orchestrator.getThreadProjection(childThreadId);
187 const original = parent.subagents[0]!;
188 const originalItem = parent.turnItems.find((item) => item.type === "subagent")!;
189 const originalNode = parent.nodes.find((node) => node.id === original.id)!;
190 const taskId = NodeId.make(`node:orphan-followup:${childState}`);
191 const itemId = TurnItemId.make(`item:orphan-followup:${childState}`);
192 const followupChildThreadId =
193 childState === "missing" ? ThreadId.make("thread:missing-child") : childThreadId;
194 const now = yield* DateTime.now;
195 let sequence = 0;
196 const apply = (event: Omit<OrchestrationV2DomainEvent, "id">) =>
197 projections.apply({
198 ...event,
199 id: EventId.make(`event:orphan:${childState}:${sequence++}`),
200 } as OrchestrationV2DomainEvent);
201 yield* apply({
202 type: "subagent.updated",
203 threadId: parentThreadId,
204 occurredAt: now,
205 payload: {
206 ...original,
207 status: "completed",
208 result: "Original result",
209 completedAt: now,
210 completionDelivery: { state: "acknowledged", observedByRunId: null },
211 },
212 });
213 yield* apply({
214 type: "context-transfer.created",
215 threadId: parentThreadId,
216 occurredAt: now,
217 payload: {
218 ...child.contextTransfers[0]!,
219 id: ContextTransferId.make(`transfer:original-result:${childState}`),
220 type: "subagent_result",
221 sourceThreadId: childThreadId,
222 targetThreadId: parentThreadId,
223 sourcePoint: { threadId: childThreadId, runId: child.runs[0]!.id },
224 targetRunId: parent.runs[0]!.id,
225 },
226 });
227 yield* apply({
228 type: "node.updated",
229 threadId: parentThreadId,
230 occurredAt: now,
231 payload: { ...originalNode, status: "completed", completedAt: now },
232 });
233 yield* apply({
234 type: "turn-item.updated",
235 threadId: parentThreadId,
236 occurredAt: now,
237 payload: { ...originalItem, status: "completed", completedAt: now },
238 });
239 yield* apply({
240 type: "subagent.updated",
241 threadId: parentThreadId,
242 occurredAt: now,
243 payload: {
244 ...original,
245 id: taskId,
246 childThreadId: followupChildThreadId,
247 status: "running",
248 result: null,
249 completedAt: null,
250 },
251 });
252 yield* apply({
253 type: "node.updated",
254 threadId: parentThreadId,
255 occurredAt: now,
256 payload: { ...originalNode, id: taskId, status: "running" },
257 });
258 assert.equal(originalItem.type, "subagent");
259 if (originalItem.type !== "subagent") return;
260 yield* apply({
261 type: "turn-item.updated",
262 threadId: parentThreadId,
263 occurredAt: now,
264 payload: {
265 ...originalItem,
266 id: itemId,
267 nodeId: taskId,
268 subagentId: taskId,
269 childThreadId: followupChildThreadId,
270 status: "running",
271 },
272 });
273 const runAtStart = child.runs[0]!;
274 const childRunStatus =
275 childState === "running" || childState === "queued" || childState === "waiting"
276 ? childState
277 : "completed";
278 yield* apply({
279 type: "run.updated",
280 threadId: childThreadId,
281 occurredAt: now,
282 payload: { ...runAtStart, status: "running", delegatedTaskId: taskId },
283 });
284 assert.equal(
285 (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]?.delegatedTaskId,
286 taskId,
287 );
288 // A terminal write based on the attempt's start snapshot loses a later steer binding.
289 yield* apply({
290 type: "run.updated",
291 threadId: childThreadId,
292 occurredAt: now,
293 payload: {
294 ...runAtStart,
295 status: childRunStatus,
296 completedAt: childRunStatus === "completed" ? now : null,
297 ...(childState === "correlated" ? { delegatedTaskId: taskId } : {}),
298 },
299 });
300 assert.equal(
301 (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]?.delegatedTaskId,
302 childState === "correlated" ? taskId : undefined,
303 );
304 if (childState === "provider-active" || childState === "roster") {
305 yield* apply({
306 type: "provider-thread.updated",
307 threadId: childThreadId,
308 occurredAt: now,
309 payload: {
310 ...child.providerThreads[0]!,
311 status: childState === "provider-active" ? "active" : "idle",
312 pendingBackgroundTasks:
313 childState === "roster" ? [{ taskId: "background-task", kind: "command" }] : [],
314 },
315 });
316 }
317 if (childState === "background") {
318 yield* apply({
319 type: "turn-item.updated",
320 threadId: childThreadId,
321 occurredAt: now,
322 payload: {
323 ...originalItem,
324 id: TurnItemId.make("item:child-background"),
325 threadId: childThreadId,
326 runId: runAtStart.id,
327 nodeId: runAtStart.rootNodeId,
328 type: "command_execution",
329 status: "running",
330 input: "test background work",
331 },
332 });
333 }
334 if (childState === "question") {
335 yield* apply({
336 type: "runtime-request.updated",
337 threadId: childThreadId,
338 occurredAt: now,
339 payload: {
340 id: RuntimeRequestId.make("request:child-question"),
341 nodeId: runAtStart.rootNodeId!,
342 providerTurnId: null,
343 nativeRequestRef: null,
344 kind: "user_input",
345 status: "pending",
346 responseCapability: { type: "message" },
347 createdAt: now,
348 resolvedAt: null,
349 },
350 });
351 }
352 if (childState === "restart") {
353 const outbox = yield* EffectOutbox.EffectOutboxV2;
354 yield* outbox.enqueue([
355 {
356 id: `effect:restart-continuation:${runAtStart.id}`,
357 commandId: CommandId.make("command:child-restart"),
358 threadId: childThreadId,
359 request: { type: "provider-runtime.continue", sourceRunId: runAtStart.id },
360 },
361 ]);
362 }
363 yield* apply({
364 type: "run.updated",
365 threadId: parentThreadId,
366 occurredAt: now,
367 payload: { ...parent.runs[0]!, status: "completed", completedAt: now },
368 });
369 if (childState === "archived") {
370 yield* apply({
371 type: "thread.archived",
372 threadId: childThreadId,
373 occurredAt: now,
374 payload: { ...child.thread, archivedAt: now },
375 });
376 }
377 const childBeforeStop = yield* orchestrator.getThreadProjection(childThreadId);
378 assert.lengthOf(
379 ((yield* orchestrator.getThreadShell(parentThreadId))?.pendingBackgroundTasks ?? []).filter(
380 (task) => task.kind === "subagent",
381 ),
382 1,
383 );
384 yield* orchestrator.dispatch({
385 type: "thread.stop",
386 commandId: CommandId.make(`stop-orphan:${childState}`),
387 threadId: parentThreadId,
388 });
389 const after = yield* orchestrator.getThreadProjection(parentThreadId);
390 assert.isEmpty((after.thread.pullRequests ?? []).filter((link) => link.watch !== undefined));
391 const expected = childState === "idle" || childState === "archived" ? "cancelled" : "running";
392 assert.equal(after.subagents.find((task) => task.id === taskId)?.status, expected);
393 assert.equal(after.nodes.find((node) => node.id === taskId)?.status, expected);
394 assert.equal(after.turnItems.find((item) => item.id === itemId)?.status, expected);
395 assert.equal(
396 after.subagents.find((task) => task.id === taskId)?.completionDelivery?.state,
397 "disposed",
398 );
399 assert.equal(
400 after.subagents.find((task) => task.id === original.id)?.result,
401 "Original result",
402 );
403 assert.deepEqual(yield* orchestrator.getThreadProjection(childThreadId), childBeforeStop);
404 if (expected === "cancelled") {
405 assert.lengthOf(
406 (yield* orchestrator.getThreadShell(parentThreadId))?.pendingBackgroundTasks ?? [],
407 0,
408 );
409 yield* orchestrator.recoverDelegatedTasks;
410 yield* orchestrator.dispatch({
411 type: "thread.stop",
412 commandId: CommandId.make(`stop-orphan-again:${childState}`),
413 threadId: parentThreadId,
414 });
415 const repeated = yield* orchestrator.getThreadProjection(parentThreadId);
416 assert.equal(repeated.subagents.find((task) => task.id === taskId)?.status, "cancelled");
417 assert.deepEqual(repeated.messages, after.messages);
418 assert.deepEqual(repeated.contextTransfers, after.contextTransfers);
419 }
420 }).pipe(Effect.provide(layerTest)),
421);
422
423it.effect("Stop ends watches, holds queues, and stops the delegated tasks under the thread", () =>
424 Effect.gen(function* () {
425 const orchestrator = yield* Orchestrator.OrchestratorV2;
426 const projections = yield* ProjectionStore.ProjectionStoreV2;
427 const threads = yield* ThreadManagementService.ThreadManagementService;
428 const sql = yield* SqlClient.SqlClient;
429 const parentThreadId = ThreadId.make("thread:stop-parent");
430 yield* createWatchingThread(parentThreadId, 1);
431 yield* send(parentThreadId, "first", "start_immediately");
432 const childThreadId = yield* delegate(parentThreadId, "child task");
433 assert.isTrue(Exit.isFailure(yield* Effect.exit(watch(childThreadId, 2))));
434 const grandchildThreadId = yield* delegate(childThreadId, "grandchild task");
435 yield* send(childThreadId, "child follow-up", "queue_after_active");
436
437 const now = yield* DateTime.now;
438 const firstRun = (yield* orchestrator.getThreadProjection(parentThreadId)).runs[0]!;
439 yield* projections.apply({
440 id: EventId.make("event:stop-parent:first-run-completed"),
441 type: "run.updated",
442 threadId: parentThreadId,
443 runId: firstRun.id,
444 occurredAt: now,
445 payload: { ...firstRun, status: "completed", completedAt: now },
446 });
447 yield* send(parentThreadId, "second", "start_immediately");
448 const secondRun = (yield* orchestrator.getThreadProjection(parentThreadId)).runs.at(-1)!;
449
450 const stopCommandId = CommandId.make("stop-parent");
451 yield* orchestrator.dispatch({
452 type: "run.interrupt",
453 commandId: stopCommandId,
454 threadId: parentThreadId,
455 runId: secondRun.id,
456 holdQueue: true,
457 });
458
459 assert.deepEqual(yield* threadState(parentThreadId), {
460 runs: ["completed", "interrupted"],
461 watched: [],
462 });
463 const parent = yield* orchestrator.getThreadProjection(parentThreadId);
464 assert.equal(parent.subagents[0]?.completionDelivery?.state, "disposed");
465 const effects = yield* sql<{ readonly effect_type: string }>`
466 SELECT effect_type FROM orchestration_v2_effect_outbox WHERE command_id = ${stopCommandId}
467 `;
468 assert.include(
469 effects.map((row) => row.effect_type),
470 "delegated-tasks.stop",
471 );
472
473 yield* threads.stopDelegatedTasks({ threadId: parentThreadId, commandId: stopCommandId });
474 assert.deepEqual(yield* threadState(childThreadId), {
475 runs: ["interrupted", "queued:held"],
476 watched: [],
477 });
478 assert.deepEqual((yield* threadState(grandchildThreadId)).runs, ["interrupted"]);
479
480 yield* threads.stopDelegatedTasks({ threadId: parentThreadId, commandId: stopCommandId });
481 assert.deepEqual((yield* threadState(childThreadId)).runs, ["interrupted", "queued:held"]);
482 }).pipe(Effect.provide(layerTest)),
483);
484
485it.effect.each(["thread.stop", "run.interrupt"] as const)(
486 "%s cancels buffered wakes and preserves durable queued messages",
487 (type) =>
488 Effect.gen(function* () {
489 const orchestrator = yield* Orchestrator.OrchestratorV2;
490 const threadId = ThreadId.make(`thread:stop-buffered:${type}`);
491 yield* createWatchingThread(threadId, 5);
492 yield* send(threadId, "active", "start_immediately");
493 yield* send(threadId, "user follow-up", "queue_after_active");
494 for (const creationSource of ["provider", "server"] as const) {
495 yield* orchestrator.dispatch({
496 type: "message.dispatch",
497 commandId: CommandId.make(`notification:${type}:${creationSource}`),
498 threadId,
499 messageId: MessageId.make(`notification:${type}:${creationSource}`),
500 text: "Background activity updated",
501 attachments: [],
502 createdBy: "agent",
503 creationSource,
504 notification: {
505 source: { kind: "background_task" },
506 outcome: "updated",
507 summary: "Background activity updated",
508 },
509 dispatchMode: { type: "queue_after_active" },
510 });
511 }
512 const before = yield* orchestrator.getThreadProjection(threadId);
513 const buffered = before.runs.find((run) =>
514 before.messages.some(
515 (message) => message.id === run.userMessageId && message.creationSource === "provider",
516 ),
517 )!;
518 const stop = { commandId: CommandId.make(`stop:${type}`), threadId };
519 yield* orchestrator.dispatch(
520 type === "run.interrupt"
521 ? { ...stop, type, runId: before.runs[0]!.id, holdQueue: true }
522 : { ...stop, type },
523 );
524 const stopped = yield* orchestrator.getThreadProjection(threadId);
525 assert.equal(stopped.runs.find((run) => run.id === buffered.id)?.status, "cancelled");
526 assert.equal(
527 stopped.attempts.find((attempt) => attempt.runId === buffered.id)?.status,
528 "cancelled",
529 );
530 assert.equal(
531 stopped.nodes.find((node) => node.id === buffered.rootNodeId)?.status,
532 "cancelled",
533 );
534 assert.deepEqual(
535 stopped.runs.filter((run) => run.status === "queued").map((run) => run.queueHeld),
536 [true, true],
537 );
538 }).pipe(Effect.provide(layerTest)),
539);
540
541it.effect(
542 "thread.stop ends an idle thread's watches and accepts a thread with nothing to stop",
543 () =>
544 Effect.gen(function* () {
545 const orchestrator = yield* Orchestrator.OrchestratorV2;
546 const threadId = ThreadId.make("thread:stop-idle");
547 yield* createWatchingThread(threadId, 3);
548
549 yield* orchestrator.dispatch({
550 type: "thread.stop",
551 commandId: CommandId.make("stop-idle"),
552 threadId,
553 });
554 assert.deepEqual(yield* threadState(threadId), { runs: [], watched: [] });
555
556 const again = yield* orchestrator.dispatch({
557 type: "thread.stop",
558 commandId: CommandId.make("stop-idle-again"),
559 threadId,
560 });
561 assert.lengthOf(again.storedEvents, 0);
562 }).pipe(Effect.provide(layerTest)),
563);
564
565it.effect("a run Stop reached cannot delegate or start a watch, even after it ends", () =>
566 Effect.gen(function* () {
567 const orchestrator = yield* Orchestrator.OrchestratorV2;
568 const projections = yield* ProjectionStore.ProjectionStoreV2;
569 const threadId = ThreadId.make("thread:stop-barrier");
570 yield* createWatchingThread(threadId, 4);
571 yield* send(threadId, "work", "start_immediately");
572 const now = yield* DateTime.now;
573 const run = { ...(yield* orchestrator.getThreadProjection(threadId)).runs[0]!, startedAt: now };
574 yield* projections.apply({
575 id: EventId.make("event:stop-barrier:running"),
576 type: "run.updated",
577 threadId,
578 runId: run.id,
579 occurredAt: now,
580 payload: { ...run, status: "running" },
581 });
582 yield* send(threadId, "queued", "queue_after_active");
583 const queued = (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)!;
584 yield* orchestrator.dispatch({
585 type: "queued-run.cancel",
586 commandId: CommandId.make("cancel-queued"),
587 threadId,
588 runId: queued.id,
589 });
590 yield* projections.apply({
591 id: EventId.make("event:stop-barrier:interrupt-request"),
592 type: "turn-item.updated",
593 threadId,
594 runId: run.id,
595 occurredAt: now,
596 payload: {
597 id: TurnItemId.make("turn-item:stop-barrier:interrupt-request"),
598 threadId,
599 runId: run.id,
600 nodeId: run.rootNodeId!,
601 providerThreadId: run.providerThreadId!,
602 providerTurnId: null,
603 nativeItemRef: null,
604 parentItemId: null,
605 ordinal: 1_000,
606 status: "completed",
607 title: "Interrupt requested",
608 startedAt: now,
609 completedAt: now,
610 updatedAt: now,
611 type: "run_interrupt_request",
612 message: "Stop",
613 },
614 });
615
616 assert.isTrue(Exit.isFailure(yield* Effect.exit(delegate(threadId, "late task"))));
617 assert.isTrue(Exit.isFailure(yield* Effect.exit(watch(threadId, 5))));
618
619 yield* projections.apply({
620 id: EventId.make("event:stop-barrier:interrupted"),
621 type: "run.updated",
622 threadId,
623 runId: run.id,
624 occurredAt: now,
625 payload: { ...run, status: "interrupted", completedAt: now },
626 });
627 assert.isTrue(Exit.isFailure(yield* Effect.exit(watch(threadId, 5))));
628 assert.deepEqual(yield* threadState(threadId), {
629 runs: ["interrupted", "cancelled"],
630 watched: [4],
631 });
632
633 for (const watching of [false, true]) {
634 yield* orchestrator.dispatch({
635 type: "thread.pull-request.watch",
636 commandId: CommandId.make(`manual-watch:${watching}`),
637 threadId,
638 ...pullRequest(4),
639 watching,
640 });
641 }
642 assert.deepEqual((yield* threadState(threadId)).watched, [4]);
643 }).pipe(Effect.provide(layerTest)),
644);
645
646it.effect("thread.stop keeps a restart continuation of the stopped run from starting", () =>
647 Effect.gen(function* () {
648 const orchestrator = yield* Orchestrator.OrchestratorV2;
649 const projections = yield* ProjectionStore.ProjectionStoreV2;
650 const sql = yield* SqlClient.SqlClient;
651 const threadId = ThreadId.make("thread:stop-restart");
652 yield* createWatchingThread(threadId, 8);
653 yield* send(threadId, "work", "start_immediately");
654 const now = yield* DateTime.now;
655 const run = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
656 yield* projections.apply({
657 id: EventId.make("event:stop-restart:cancelled"),
658 type: "run.updated",
659 threadId,
660 runId: run.id,
661 occurredAt: now,
662 payload: { ...run, status: "cancelled", startedAt: null, completedAt: now },
663 });
664 const at = DateTime.formatIso(now);
665 yield* sql`
666 INSERT INTO orchestration_v2_effect_outbox (
667 effect_id, command_id, thread_id, effect_type, payload_json, status,
668 attempt_count, available_at, created_at, updated_at
669 ) VALUES (
670 ${`effect:restart-continuation:${run.id}`}, 'command:restart', ${threadId},
671 'provider-runtime.continue',
672 ${encodeEffectRequest({ type: "provider-runtime.continue", sourceRunId: run.id })},
673 'pending', 0, ${at}, ${at}, ${at}
674 )
675 `;
676
677 yield* orchestrator.dispatch({
678 type: "thread.stop",
679 commandId: CommandId.make("stop-restart"),
680 threadId,
681 });
682 yield* orchestrator.dispatch({
683 type: "message.dispatch",
684 commandId: CommandId.make("restart-continuation"),
685 threadId,
686 messageId: MessageId.make("message:restart-continuation"),
687 text: "Continue.",
688 attachments: [],
689 dispatchMode: { type: "start_immediately" },
690 createdBy: "agent",
691 creationSource: "server",
692 restartContinuationOfRunId: run.id,
693 });
694 assert.deepEqual(yield* threadState(threadId), { runs: ["cancelled"], watched: [] });
695 }).pipe(Effect.provide(layerTest)),
696);
697
698it.effect.each(["thread.stop", "run.interrupt"] as const)(
699 "a successful %s blocks a newer restart continuation when completion timestamps tie",
700 (stopType) =>
701 Effect.gen(function* () {
702 const orchestrator = yield* Orchestrator.OrchestratorV2;
703 const projections = yield* ProjectionStore.ProjectionStoreV2;
704 const sql = yield* SqlClient.SqlClient;
705 const threadId = ThreadId.make("thread:stop-resumed-queue");
706 yield* createWatchingThread(threadId, 12);
707 yield* send(threadId, "first", "start_immediately");
708 yield* send(threadId, "queued", "queue_after_active");
709 const now = yield* DateTime.now;
710 const [first, queued] = (yield* orchestrator.getThreadProjection(threadId)).runs;
711 assert.isDefined(first);
712 assert.isDefined(queued);
713 for (const run of [first!, queued!]) {
714 yield* projections.apply({
715 id: EventId.make(`event:stop-resumed-queue:hold:${run.id}`),
716 type: "run.updated",
717 threadId,
718 runId: run.id,
719 occurredAt: now,
720 payload:
721 run.id === first!.id
722 ? { ...run, status: "completed", completedAt: now }
723 : { ...run, queueHeld: true },
724 });
725 }
726 yield* send(threadId, "restart source", "start_immediately");
727 const source = (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)!;
728 yield* projections.apply({
729 id: EventId.make("event:stop-resumed-queue:restart-cancelled"),
730 type: "run.updated",
731 threadId,
732 runId: source.id,
733 occurredAt: now,
734 payload: { ...source, status: "cancelled", startedAt: now, completedAt: now },
735 });
736 const at = DateTime.formatIso(now);
737 yield* sql`
738 INSERT INTO orchestration_v2_effect_outbox (
739 effect_id, command_id, thread_id, effect_type, payload_json, status,
740 attempt_count, available_at, created_at, updated_at
741 ) VALUES (
742 ${`effect:restart-continuation:${source.id}`}, 'command:restart', ${threadId},
743 'provider-runtime.continue',
744 ${encodeEffectRequest({ type: "provider-runtime.continue", sourceRunId: source.id })},
745 'pending', 0, ${at}, ${at}, ${at}
746 )
747 `;
748 yield* orchestrator.dispatch({
749 type: "queue.resume",
750 commandId: CommandId.make("resume-older-queue"),
751 threadId,
752 });
753 assert.equal(
754 (yield* orchestrator.getThreadProjection(threadId)).runs.find(
755 (run) => run.id === queued!.id,
756 )?.status,
757 "starting",
758 );
759 yield* orchestrator.dispatch(
760 stopType === "thread.stop"
761 ? { type: "thread.stop", commandId: CommandId.make("stop-resumed-queue"), threadId }
762 : {
763 type: "run.interrupt",
764 commandId: CommandId.make("stop-resumed-queue"),
765 threadId,
766 runId: queued!.id,
767 holdQueue: true,
768 },
769 );
770 const stopped = yield* orchestrator.getThreadProjection(threadId);
771 const interrupted = stopped.runs.find((run) => run.id === queued!.id)!;
772 assert.equal(interrupted.status, "interrupted");
773 assert.equal(DateTime.toEpochMillis(interrupted.completedAt!), DateTime.toEpochMillis(now));
774 yield* orchestrator.dispatch({
775 type: "message.dispatch",
776 commandId: CommandId.make("restart-after-stop"),
777 threadId,
778 messageId: MessageId.make("message:restart-after-stop"),
779 text: "Continue.",
780 attachments: [],
781 dispatchMode: { type: "start_immediately" },
782 createdBy: "agent",
783 creationSource: "server",
784 restartContinuationOfRunId: source.id,
785 });
786 // The held run resumed after the cancelled source ran, so it sorts after it.
787 assert.deepEqual(yield* threadState(threadId), {
788 runs: ["completed", "cancelled", "interrupted"],
789 watched: [],
790 });
791 }).pipe(Effect.provide(layerTest.pipe(Layer.provideMerge(TestClock.layer())))),
792);
793
794it.effect.each([
795 { ending: "cancelled", question: "resolved" },
796 { ending: "interrupted", question: "resolved" },
797 { ending: "completed", question: "pending" },
798 { ending: "failed", question: "pending" },
799] as const)(
800 "a $ending delegated task leaves its async question $question",
801 ({ ending, question }) =>
802 Effect.gen(function* () {
803 const orchestrator = yield* Orchestrator.OrchestratorV2;
804 const projections = yield* ProjectionStore.ProjectionStoreV2;
805 const parentThreadId = ThreadId.make(`thread:stop-question-parent:${ending}`);
806 yield* createWatchingThread(parentThreadId, 20);
807 yield* send(parentThreadId, "first", "start_immediately");
808 const childThreadId = yield* delegate(parentThreadId, `question task ${ending}`);
809 const childRun = (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]!;
810 assert.equal(
811 (yield* orchestrator.getThreadProjection(childThreadId)).nodes.find(
812 (node) => node.id === childRun.rootNodeId,
813 )?.runId,
814 childRun.id,
815 );
816 const now = yield* DateTime.now;
817 yield* projections.apply({
818 id: EventId.make("event:stop-question:request"),
819 type: "runtime-request.updated",
820 threadId: childThreadId,
821 runId: childRun.id,
822 occurredAt: now,
823 payload: {
824 id: RuntimeRequestId.make("request:stop-question"),
825 nodeId: childRun.rootNodeId!,
826 providerTurnId: null,
827 nativeRequestRef: null,
828 kind: "user_input",
829 status: "pending",
830 responseCapability: { type: "message" },
831 createdAt: now,
832 resolvedAt: null,
833 },
834 });
835 const request = Effect.map(
836 orchestrator.getThreadProjection(childThreadId),
837 (projection) => projection.runtimeRequests[0],
838 );
839
840 yield* orchestrator.recoverDelegatedTask(childThreadId, childRun.id);
841 assert.equal((yield* request)?.status, "pending");
842 if (ending === "interrupted") {
843 yield* orchestrator.dispatch({
844 type: "thread.stop",
845 commandId: CommandId.make("stop-question-child"),
846 threadId: childThreadId,
847 });
848 } else {
849 yield* projections.apply({
850 id: EventId.make(`event:stop-question:${ending}`),
851 type: "run.updated",
852 threadId: childThreadId,
853 runId: childRun.id,
854 occurredAt: now,
855 payload: { ...childRun, status: ending, startedAt: now, completedAt: now },
856 });
857 }
858 yield* orchestrator.recoverDelegatedTask(childThreadId, childRun.id);
859 assert.equal((yield* request)?.status, question);
860 assert.equal((yield* request)?.decision, question === "resolved" ? "cancel" : undefined);
861 }).pipe(Effect.provide(layerTest)),
862);
863
864it.effect("a delegated task that cannot be stopped fails the walk after its siblings stop", () =>
865 Effect.gen(function* () {
866 const orchestrator = yield* Orchestrator.OrchestratorV2;
867 const projections = yield* ProjectionStore.ProjectionStoreV2;
868 const threads = yield* ThreadManagementService.ThreadManagementService;
869 const parentThreadId = ThreadId.make("thread:stop-partial");
870 yield* createWatchingThread(parentThreadId, 6);
871 yield* send(parentThreadId, "work", "start_immediately");
872 const childThreadId = yield* delegate(parentThreadId, "stoppable task");
873 const task = (yield* orchestrator.getThreadProjection(parentThreadId)).subagents[0]!;
874 yield* projections.apply({
875 id: EventId.make("event:stop-partial:missing-task"),
876 type: "subagent.updated",
877 threadId: parentThreadId,
878 runId: task.runId!,
879 nodeId: NodeId.make("node:stop-partial:missing"),
880 providerInstanceId: instanceId,
881 occurredAt: yield* DateTime.now,
882 payload: {
883 ...task,
884 id: NodeId.make("node:stop-partial:missing"),
885 childThreadId: ThreadId.make("thread:stop-partial:missing"),
886 startedAt: DateTime.subtract(task.startedAt!, { hours: 1 }),
887 },
888 });
889
890 const walked = yield* Effect.exit(
891 threads.stopDelegatedTasks({
892 threadId: parentThreadId,
893 commandId: CommandId.make("stop-partial"),
894 }),
895 );
896 assert.isTrue(Exit.isFailure(walked));
897 assert.deepEqual((yield* threadState(childThreadId)).runs, ["interrupted"]);
898 }).pipe(Effect.provide(layerTest)),
899);
900
901it.effect("thread.stop on a finished thread refuses a late agent watch", () =>
902 Effect.gen(function* () {
903 const orchestrator = yield* Orchestrator.OrchestratorV2;
904 const projections = yield* ProjectionStore.ProjectionStoreV2;
905 const threadId = ThreadId.make("thread:stop-finished");
906 yield* createWatchingThread(threadId, 9);
907 yield* send(threadId, "work", "start_immediately");
908 const now = yield* DateTime.now;
909 const run = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
910 yield* projections.apply({
911 id: EventId.make("event:stop-finished:completed"),
912 type: "run.updated",
913 threadId,
914 runId: run.id,
915 occurredAt: now,
916 payload: { ...run, status: "completed", startedAt: now, completedAt: now },
917 });
918
919 yield* orchestrator.dispatch({
920 type: "thread.stop",
921 commandId: CommandId.make("stop-finished"),
922 threadId,
923 });
924 assert.isTrue(Exit.isFailure(yield* Effect.exit(watch(threadId, 10))));
925 assert.deepEqual(yield* threadState(threadId), { runs: ["completed"], watched: [] });
926 }).pipe(Effect.provide(layerTest)),
927);
928
929it.effect("thread.stop marks a turn it cannot interrupt so a late agent watch is refused", () =>
930 Effect.gen(function* () {
931 const orchestrator = yield* Orchestrator.OrchestratorV2;
932 const projections = yield* ProjectionStore.ProjectionStoreV2;
933 const threadId = ThreadId.make("thread:stop-lost-session");
934 yield* createWatchingThread(threadId, 11);
935 yield* send(threadId, "work", "start_immediately");
936 const now = yield* DateTime.now;
937 const run = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
938 yield* projections.apply({
939 id: EventId.make("event:stop-lost-session:running"),
940 type: "run.updated",
941 threadId,
942 runId: run.id,
943 occurredAt: now,
944 payload: { ...run, status: "running", startedAt: now },
945 });
946 yield* projections.apply({
947 id: EventId.make("event:stop-lost-session:turn"),
948 type: "provider-turn.updated",
949 threadId,
950 occurredAt: now,
951 payload: {
952 id: ProviderTurnId.make("provider-turn:stop-lost-session"),
953 providerThreadId: run.providerThreadId!,
954 nodeId: run.rootNodeId!,
955 runAttemptId: run.activeAttemptId!,
956 nativeTurnRef: null,
957 ordinal: 1,
958 status: "running",
959 startedAt: now,
960 completedAt: null,
961 },
962 });
963
964 yield* orchestrator.dispatch({
965 type: "thread.stop",
966 commandId: CommandId.make("stop-lost-session"),
967 threadId,
968 });
969 assert.isTrue(Exit.isFailure(yield* Effect.exit(watch(threadId, 12))));
970 assert.deepEqual(yield* threadState(threadId), { runs: ["running"], watched: [] });
971 }).pipe(Effect.provide(layerTest)),
972);
973
974it.effect.each(["missing session", "completed", "failed", "cancelled"] as const)(
975 "a native child Stop only settles a confirmed %s outcome and preserves its owner and sibling",
976 (providerState) =>
977 Effect.gen(function* () {
978 const orchestrator = yield* Orchestrator.OrchestratorV2;
979 const projections = yield* ProjectionStore.ProjectionStoreV2;
980 const sql = yield* SqlClient.SqlClient;
981 const threadId = ThreadId.make("thread:native-child-stop");
982 yield* createWatchingThread(threadId, 20);
983 yield* send(threadId, "owner", "start_immediately");
984 yield* delegate(threadId, "native-child-fixture");
985 const before = yield* orchestrator.getThreadProjection(threadId);
986 const run = before.runs[0]!;
987 const now = yield* DateTime.now;
988 const subagent = {
989 ...before.subagents[0]!,
990 origin: "provider_native" as const,
991 driver: CLAUDE_PROVIDER,
992 providerInstanceId: ProviderInstanceId.make("claudeAgent"),
993 nativeTaskRef: {
994 driver: CLAUDE_PROVIDER,
995 nativeId: "claude-task",
996 strength: "strong" as const,
997 },
998 status: "running" as const,
999 startedAt: now,
1000 };
1001 yield* projections.apply({
1002 id: EventId.make("event:native-child-stop:agent"),
1003 type: "subagent.updated",
1004 threadId,
1005 runId: run.id,
1006 nodeId: subagent.id,
1007 occurredAt: now,
1008 payload: subagent,
1009 });
1010 const sibling = {
1011 ...subagent,
1012 id: NodeId.make("node:native-stop-sibling"),
1013 childThreadId: null,
1014 };
1015 yield* projections.apply({
1016 id: EventId.make("event:native-child-stop:sibling"),
1017 type: "subagent.updated",
1018 threadId,
1019 runId: run.id,
1020 nodeId: sibling.id,
1021 occurredAt: now,
1022 payload: sibling,
1023 });
1024 const item = before.turnItems.find(
1025 (item) => item.type === "subagent" && item.subagentId === subagent.id,
1026 )!;
1027 yield* projections.apply({
1028 id: EventId.make("event:native-child-stop:item"),
1029 type: "turn-item.updated",
1030 threadId,
1031 runId: run.id,
1032 occurredAt: now,
1033 payload: { ...item, status: "running", completedAt: null },
1034 });
1035 const childRoot = {
1036 ...before.nodes.find((node) => node.id === run.rootNodeId)!,
1037 id: NodeId.make("node:native-stop-child-root"),
1038 threadId: subagent.childThreadId!,
1039 runId: null,
1040 parentNodeId: null,
1041 status: "running" as const,
1042 countsForRun: false,
1043 completedAt: null,
1044 };
1045 yield* projections.apply({
1046 id: EventId.make("event:native-child-stop:child-root"),
1047 type: "node.updated",
1048 threadId: childRoot.threadId,
1049 nodeId: childRoot.id,
1050 occurredAt: now,
1051 payload: childRoot,
1052 });
1053 const providerThread = before.providerThreads.find(
1054 (thread) => thread.id === run.providerThreadId,
1055 )!;
1056 yield* projections.apply({
1057 id: EventId.make("event:native-child-stop:provider-thread"),
1058 type: "provider-thread.updated",
1059 threadId,
1060 occurredAt: now,
1061 payload: {
1062 ...providerThread,
1063 driver: CLAUDE_PROVIDER,
1064 providerInstanceId: ProviderInstanceId.make("claudeAgent"),
1065 providerSessionId: ProviderSessionId.make("session:native-child-stop"),
1066 },
1067 });
1068 const providerTurnId = ProviderTurnId.make("turn:native-child-stop");
1069 yield* projections.apply({
1070 id: EventId.make("event:native-child-stop:provider-turn"),
1071 type: "provider-turn.updated",
1072 threadId,
1073 occurredAt: now,
1074 payload: {
1075 id: providerTurnId,
1076 providerThreadId: providerThread.id,
1077 nodeId: run.rootNodeId!,
1078 runAttemptId: run.activeAttemptId!,
1079 nativeTurnRef: null,
1080 ordinal: 1,
1081 status: "completed",
1082 startedAt: now,
1083 completedAt: now,
1084 },
1085 });
1086 yield* projections.apply({
1087 id: EventId.make("event:native-child-stop:owner-completed"),
1088 type: "run.updated",
1089 threadId,
1090 runId: run.id,
1091 occurredAt: now,
1092 payload: { ...run, status: "completed", startedAt: now, completedAt: now },
1093 });
1094 yield* send(threadId, "queued", "queue_after_active");
1095 const stateBefore = yield* threadState(threadId);
1096 const eventStore = yield* EventStore.EventStoreV2;
1097 const afterSequence = yield* eventStore.latestSequence({ threadId });
1098 const confirmedStatus =
1099 providerState === "completed" || providerState === "failed" || providerState === "cancelled"
1100 ? providerState
1101 : null;
1102 const confirmedOutcome =
1103 confirmedStatus === null
1104 ? undefined
1105 : { status: confirmedStatus, completedAt: now, result: "Actual provider outcome" };
1106 const commandId = CommandId.make("stop-native-child");
1107 yield* orchestrator.dispatch({
1108 type: "subagent.stop",
1109 commandId,
1110 threadId,
1111 runId: run.id,
1112 subagentId: subagent.id,
1113 });
1114 assert.deepEqual(yield* threadState(threadId), stateBefore);
1115 const effects = yield* sql<{ payload_json: string }>`
1116 SELECT payload_json FROM orchestration_v2_effect_outbox WHERE command_id = ${commandId}
1117 `;
1118 assert.equal(effects.length, 1);
1119 assert.deepEqual(JSON.parse(effects[0]!.payload_json), {
1120 type: "provider-turn.interrupt",
1121 providerSessionId: "session:native-child-stop",
1122 providerThreadId: providerThread.id,
1123 providerTurnId,
1124 subagent: { id: subagent.id, nativeTaskId: "claude-task" },
1125 });
1126 assert.equal(
1127 (yield* orchestrator.getThreadProjection(threadId)).subagents[0]?.status,
1128 "running",
1129 );
1130 const unrelated = yield* Effect.exit(
1131 orchestrator.dispatch({
1132 type: "subagent.stop",
1133 commandId: CommandId.make("stop-unknown-child"),
1134 threadId,
1135 runId: run.id,
1136 subagentId: NodeId.make("unknown-child"),
1137 }),
1138 );
1139 assert.isTrue(Exit.isFailure(unrelated));
1140 yield* TestClock.adjust("1 second");
1141 yield* sql`DELETE FROM orchestration_v2_effect_outbox WHERE command_id != ${commandId}`;
1142 if (providerState === "missing session") {
1143 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1144 yield* worker.drain();
1145 } else {
1146 yield* orchestrator.dispatch({
1147 type: "thread.background-work.settle",
1148 commandId: CommandId.make("repair-native-child"),
1149 threadId,
1150 providerThreadId: providerThread.id,
1151 providerTurnId,
1152 subagent: { id: subagent.id, stoppedAt: now, outcome: confirmedOutcome! },
1153 });
1154 }
1155 const settled = yield* orchestrator.getThreadProjection(threadId);
1156 const cancelled = settled.subagents.find((task) => task.id === subagent.id)!;
1157 if (confirmedOutcome === undefined) {
1158 const events = yield* eventStore.read({ threadId, afterSequence }).pipe(Stream.runCollect);
1159 assert.isFalse(
1160 events.some(
1161 ({ event }) =>
1162 (event.type === "subagent.updated" ||
1163 event.type === "node.updated" ||
1164 event.type === "turn-item.updated") &&
1165 ["completed", "failed", "cancelled", "interrupted"].includes(event.payload.status),
1166 ),
1167 );
1168 assert.equal(cancelled.status, "running");
1169 assert.equal(
1170 (yield* orchestrator.getThreadProjection(childRoot.threadId)).nodes.find(
1171 (node) => node.id === childRoot.id,
1172 )?.status,
1173 "running",
1174 );
1175 assert.deepEqual(yield* threadState(threadId), stateBefore);
1176 return;
1177 }
1178
1179 const repaired = yield* eventStore.read({ threadId, afterSequence }).pipe(Stream.runCollect);
1180 const terminal = repaired.flatMap(({ event }) =>
1181 (event.type === "subagent.updated" ||
1182 event.type === "node.updated" ||
1183 event.type === "turn-item.updated") &&
1184 event.payload.status === confirmedStatus
1185 ? [event]
1186 : [],
1187 );
1188 assert.deepEqual(terminal.map((event) => event.type).sort(), [
1189 "node.updated",
1190 "subagent.updated",
1191 "turn-item.updated",
1192 ]);
1193 for (const event of terminal) {
1194 assert.equal(event.nodeId, subagent.id);
1195 assert.equal(
1196 DateTime.toEpochMillis(event.payload.completedAt!),
1197 DateTime.toEpochMillis(confirmedOutcome!.completedAt),
1198 );
1199 if (event.type === "subagent.updated" || event.type === "turn-item.updated") {
1200 assert.isDefined(event.payload.updatedAt);
1201 }
1202 }
1203
1204 assert.equal(cancelled.status, confirmedStatus);
1205 assert.isNotNull(cancelled.completedAt);
1206 assert.equal(
1207 DateTime.toEpochMillis(cancelled.completedAt!),
1208 DateTime.toEpochMillis(confirmedOutcome!.completedAt),
1209 );
1210 assert.equal(cancelled.result, confirmedOutcome!.result);
1211 assert.equal(
1212 settled.turnItems.find((candidate) => candidate.id === item.id)?.status,
1213 confirmedStatus,
1214 );
1215 const child = yield* orchestrator.getThreadProjection(childRoot.threadId);
1216 assert.equal(child.nodes.find((node) => node.id === childRoot.id)?.status, confirmedStatus);
1217 assert.isNotNull(child.nodes.find((node) => node.id === childRoot.id)?.completedAt);
1218 assert.equal(settled.subagents.find((task) => task.id === sibling.id)?.status, "running");
1219 assert.deepEqual(yield* threadState(threadId), stateBefore);
1220 yield* TestClock.adjust("1 second");
1221 const resumedAt = yield* DateTime.now;
1222 yield* projections.apply({
1223 id: EventId.make("event:native-child-stop:resume"),
1224 type: "subagent.updated",
1225 threadId,
1226 runId: run.id,
1227 nodeId: subagent.id,
1228 occurredAt: resumedAt,
1229 payload: { ...cancelled, status: "running", startedAt: resumedAt, completedAt: null },
1230 });
1231 yield* orchestrator.dispatch({
1232 type: "thread.background-work.settle",
1233 commandId: CommandId.make("old-stop-after-resume"),
1234 threadId,
1235 providerThreadId: providerThread.id,
1236 providerTurnId,
1237 subagent: { id: subagent.id, stoppedAt: now, outcome: confirmedOutcome },
1238 });
1239 assert.equal(
1240 (yield* orchestrator.getThreadProjection(threadId)).subagents.find(
1241 (task) => task.id === subagent.id,
1242 )?.status,
1243 "running",
1244 );
1245 }).pipe(Effect.provide(layerTest)),
1246);