Open History to see the last commit
1import { expect, it } from "@effect/vitest";
2import {
3 CommandId,
4 EnvironmentId,
5 MessageId,
6 type OrchestrationV2Command,
7 type OrchestrationV2Run,
8 type OrchestrationV2ProviderThread,
9 ProviderThreadId,
10 ProviderDriverKind,
11 ProviderSessionId,
12 type OrchestrationV2StoredEvent,
13 type OrchestrationV2ThreadProjection,
14 ProjectId,
15 ProviderInstanceId,
16 RunId,
17 ThreadId,
18} from "@t3tools/contracts";
19import * as Deferred from "effect/Deferred";
20import * as DateTime from "effect/DateTime";
21import * as Duration from "effect/Duration";
22import * as Option from "effect/Option";
23import * as Effect from "effect/Effect";
24import * as Fiber from "effect/Fiber";
25import * as Layer from "effect/Layer";
26import * as Queue from "effect/Queue";
27import * as Stream from "effect/Stream";
28import * as TestClock from "effect/testing/TestClock";
29
30import { AgentScope } from "@t3tools/shared/AgentScope";
31
32import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
33import * as ServerSettings from "../serverSettings.ts";
34import * as SqlitePersistence from "../persistence/Sqlite.ts";
35import * as CommandReceiptStore from "./CommandReceiptStore.ts";
36import * as Orchestrator from "./Orchestrator.ts";
37import * as ProviderSessionManager from "./ProviderSessionManager.ts";
38import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
39import * as ThreadManagementService from "./ThreadManagementService.ts";
40import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
41import * as LegacyV1ThreadImporter from "./legacy/LegacyV1ThreadImporter.ts";
42import {
43 ThreadWorktreeRestore,
44 WorktreeRecreateError,
45} from "../workspace/restoreMissingWorktree.ts";
46
47it("stamps authoritative provenance on commands that create threads or messages", () => {
48 const command: OrchestrationV2Command = {
49 type: "thread.create",
50 createdBy: "agent",
51 creationSource: "mcp",
52 commandId: CommandId.make("command:thread-management:create"),
53 threadId: ThreadId.make("thread:thread-management:create"),
54 projectId: ProjectId.make("project:thread-management"),
55 title: "Thread management",
56 modelSelection: {
57 instanceId: ProviderInstanceId.make("codex"),
58 model: "gpt-5-codex",
59 },
60 runtimeMode: "full-access",
61 interactionMode: "default",
62 branch: null,
63 worktreePath: null,
64 // A client cannot claim a linked environment's origin, or a parent there.
65 linkOrigin: { sessionId: "forged", label: "Somewhere else" },
66 delegatedFrom: {
67 environmentId: EnvironmentId.make("environment:elsewhere"),
68 threadId: ThreadId.make("thread:elsewhere"),
69 title: "Somewhere else",
70 },
71 // A client's message goes through intake; only the server folds one into the creation.
72 initialMessage: {
73 messageId: MessageId.make("message:thread-management:forged"),
74 text: "Forged",
75 attachments: [],
76 },
77 };
78
79 const stamped = ThreadManagementService.withCreationProvenance(command, {
80 createdBy: "user",
81 creationSource: "web",
82 });
83 expect(stamped).toMatchObject({
84 createdBy: "user",
85 creationSource: "web",
86 });
87 expect(stamped).not.toHaveProperty("linkOrigin");
88 expect(stamped).not.toHaveProperty("delegatedFrom");
89 expect(stamped).not.toHaveProperty("initialMessage");
90});
91
92const layerReceiptsTest = CommandReceiptStore.layer.pipe(
93 Layer.provide(SqlitePersistence.layerMemory),
94);
95
96const layerDispatchChokepointTest =
97 (settingsLayer: ReturnType<typeof ServerSettings.layerTest>) => (dispatched: Array<string>) =>
98 ThreadManagementService.layer.pipe(
99 Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
100 Layer.provide(
101 Layer.mock(Orchestrator.OrchestratorV2)({
102 getThreadRecords: () =>
103 Effect.succeed({
104 thread: {
105 id: ThreadId.make("thread:thread-management:chokepoint"),
106 projectId: ProjectId.make("project:thread-management:chokepoint"),
107 deletedAt: null,
108 modelSelection: {
109 instanceId: ProviderInstanceId.make("codex_work"),
110 model: "test",
111 },
112 },
113 } as unknown as OrchestrationV2ThreadProjection),
114 dispatch: (command) =>
115 Effect.sync(() => {
116 dispatched.push(command.type);
117 return { sequence: 1, storedEvents: [] } as never;
118 }),
119 }),
120 ),
121 Layer.provide(settingsLayer),
122 Layer.provideMerge(layerReceiptsTest),
123 );
124
125it.effect(
126 "the dispatch chokepoint refuses a continuation message.dispatch on a project-disabled instance before reaching the orchestrator",
127 () => {
128 const projectId = ProjectId.make("project:thread-management:chokepoint");
129 const threadId = ThreadId.make("thread:thread-management:chokepoint");
130 const instanceId = ProviderInstanceId.make("codex_work");
131 const commandId = CommandId.make("command:thread-management:chokepoint-disabled");
132 const dispatched: Array<string> = [];
133 const layerTest = layerDispatchChokepointTest(
134 ServerSettings.layerTest({
135 providerInstances: { [instanceId]: { driver: "codex", enabled: true } },
136 projectSettingsOverrides: {
137 [projectId]: { providerInstances: { [instanceId]: { enabled: false } } },
138 },
139 }),
140 )(dispatched);
141
142 return Effect.gen(function* () {
143 const threads = yield* ThreadManagementService.ThreadManagementService;
144 const error = yield* Effect.flip(
145 threads.dispatch({
146 type: "message.dispatch",
147 commandId,
148 threadId,
149 messageId: MessageId.make("message:thread-management:chokepoint-disabled"),
150 text: "Background task completed.",
151 attachments: [],
152 dispatchMode: { type: "queue_after_active" },
153 createdBy: "agent",
154 creationSource: "server",
155 }),
156 );
157
158 expect(error).toBeInstanceOf(
159 ThreadManagementService.ThreadManagementProviderInstanceDisabledError,
160 );
161 expect(error).toMatchObject({ projectId, instanceId });
162 expect(dispatched).toEqual([]);
163 }).pipe(Effect.provide(layerTest));
164 },
165);
166
167it.effect(
168 "a replay of an already-accepted command id dispatches despite the instance now being disabled",
169 () => {
170 const projectId = ProjectId.make("project:thread-management:chokepoint");
171 const threadId = ThreadId.make("thread:thread-management:chokepoint");
172 const instanceId = ProviderInstanceId.make("codex_work");
173 const commandId = CommandId.make("command:thread-management:chokepoint-replay");
174 const dispatched: Array<string> = [];
175 const layerTest = layerDispatchChokepointTest(
176 ServerSettings.layerTest({
177 providerInstances: { [instanceId]: { driver: "codex", enabled: true } },
178 projectSettingsOverrides: {
179 [projectId]: { providerInstances: { [instanceId]: { enabled: false } } },
180 },
181 }),
182 )(dispatched);
183
184 return Effect.gen(function* () {
185 const receipts = yield* CommandReceiptStore.CommandReceiptStoreV2;
186 yield* receipts.insertIfAbsent({
187 commandId,
188 threadId,
189 commandType: "message.dispatch",
190 acceptedAt: yield* DateTime.now,
191 resultSequence: 1,
192 status: "accepted",
193 error: null,
194 });
195
196 const threads = yield* ThreadManagementService.ThreadManagementService;
197 yield* threads.dispatch({
198 type: "message.dispatch",
199 commandId,
200 threadId,
201 messageId: MessageId.make("message:thread-management:chokepoint-replay"),
202 text: "Background task completed.",
203 attachments: [],
204 dispatchMode: { type: "queue_after_active" },
205 createdBy: "agent",
206 creationSource: "server",
207 });
208
209 expect(dispatched).toEqual(["message.dispatch"]);
210 }).pipe(Effect.provide(layerTest));
211 },
212);
213
214it.effect.each([
215 { label: "fails a send that starts a turn", running: false },
216 { label: "still steers a running turn", running: true },
217])("sendToThread with a missing worktree that cannot be recreated $label", ({ running }) => {
218 const projectId = ProjectId.make("project:thread-management:worktree-gone");
219 const threadId = ThreadId.make("thread:thread-management:worktree-gone");
220 const dispatched: Array<string> = [];
221 const layerTest = ThreadManagementService.layerWithLegacyImporter.pipe(
222 Layer.provide(
223 Layer.succeed(ThreadWorktreeRestore, {
224 restore: () =>
225 Effect.fail(
226 new WorktreeRecreateError({
227 worktreePath: "/worktrees/gone",
228 branch: "t3/gone",
229 cause: new Error("branch_checked_out_in_worktree"),
230 }),
231 ),
232 }),
233 ),
234 Layer.provide(
235 Layer.mock(LegacyV1ThreadImporter.LegacyV1ThreadImporter)({
236 ensureTranscript: () => Effect.succeed({ importedThreadCount: 0, importedMessageCount: 0 }),
237 }),
238 ),
239 Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
240 Layer.provide(
241 Layer.mock(Orchestrator.OrchestratorV2)({
242 getThreadRecords: () =>
243 Effect.succeed({
244 thread: {
245 id: threadId,
246 projectId,
247 deletedAt: null,
248 archivedAt: null,
249 worktreePath: "/worktrees/gone",
250 branch: "t3/gone",
251 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "test" },
252 },
253 runs: running
254 ? [
255 {
256 id: RunId.make("run:thread-management:worktree-gone"),
257 status: "running",
258 activeAttemptId: "attempt:thread-management:worktree-gone",
259 },
260 ]
261 : [],
262 providerTurns: running
263 ? [
264 {
265 runAttemptId: "attempt:thread-management:worktree-gone",
266 status: "running",
267 },
268 ]
269 : [],
270 messages: [],
271 turnItems: [],
272 } as unknown as OrchestrationV2ThreadProjection),
273 dispatch: (command) =>
274 Effect.sync(() => {
275 dispatched.push(command.type);
276 return { sequence: 1, storedEvents: [] } as never;
277 }),
278 }),
279 ),
280 Layer.provide(ServerSettings.layerTest()),
281 Layer.provideMerge(layerReceiptsTest),
282 );
283
284 return Effect.gen(function* () {
285 const threads = yield* ThreadManagementService.ThreadManagementService;
286 const error = yield* Effect.flip(
287 threads.sendToThread({
288 projectId,
289 threadId,
290 commandId: CommandId.make("command:thread-management:worktree-gone"),
291 messageId: MessageId.make("message:thread-management:worktree-gone"),
292 text: "Reply with ok.",
293 attachments: [],
294 mode: "auto",
295 createdBy: "agent",
296 creationSource: "mcp",
297 }),
298 );
299 // The mock records no message, so a dispatched send ends in the projection read.
300 expect(error._tag).toBe(
301 running
302 ? "ThreadManagementDurableRunProjectionError"
303 : "ThreadManagementWorktreeRestoreError",
304 );
305 expect(dispatched).toEqual(running ? ["message.dispatch"] : []);
306 }).pipe(Effect.provide(layerTest));
307});
308
309it.effect("waitForThread follows a restart continuation instead of the cut run", () =>
310 Effect.gen(function* () {
311 const projectId = ProjectId.make("project:thread-management:wait-restart");
312 const threadId = ThreadId.make("thread:thread-management:wait-restart");
313 const runId = RunId.make("run:thread-management:wait-restart");
314 const continuationId = RunId.make("run:thread-management:wait-restart:2");
315 const subscribed = yield* Deferred.make<void>();
316 const events = yield* Queue.unbounded<OrchestrationV2StoredEvent>();
317 let runs: Array<{
318 id: RunId;
319 status: OrchestrationV2Run["status"];
320 restartContinuationOfRunId?: RunId;
321 }> = [{ id: runId, status: "cancelled" }];
322 let pending = true;
323 const stored = (sequence: number, event: object) =>
324 ({ sequence, event: { threadId, ...event } }) as unknown as OrchestrationV2StoredEvent;
325 const layerTest = ThreadManagementService.layer.pipe(
326 Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
327 Layer.provide(
328 Layer.mock(Orchestrator.OrchestratorV2)({
329 getThreadEventSequence: () => Effect.succeed(0),
330 restartContinuationPending: (run) => Effect.succeed(pending && run.id === runId),
331 awaitRestartContinuation: () => Effect.never,
332 streamStoredEventsFrom: (input) =>
333 input?.eventType === "run.updated"
334 ? Stream.fromEffect(Deferred.succeed(subscribed, undefined)).pipe(
335 Stream.drain,
336 Stream.concat(Stream.fromQueue(events)),
337 )
338 : Stream.never,
339 getThreadRecords: () =>
340 Effect.sync(
341 () =>
342 ({
343 thread: { id: threadId, projectId, deletedAt: null },
344 runs,
345 }) as unknown as OrchestrationV2ThreadProjection,
346 ),
347 }),
348 ),
349 Layer.provide(ServerSettings.layerTest()),
350 Layer.provide(layerReceiptsTest),
351 );
352 const service = yield* ThreadManagementService.ThreadManagementService.pipe(
353 Effect.provide(layerTest),
354 );
355 const fiber = yield* service
356 .waitForThread({ projectId, threadId, runId, timeoutMs: 60 * 60 * 1_000 })
357 .pipe(Effect.forkChild);
358 yield* Deferred.await(subscribed);
359 const continuation = { id: continuationId, restartContinuationOfRunId: runId };
360 runs = [...runs, { ...continuation, status: "running" }];
361 pending = false;
362 yield* Queue.offer(
363 events,
364 stored(1, { type: "run.updated", payload: { ...continuation, status: "running" } }),
365 );
366 runs = [runs[0]!, { ...continuation, status: "completed" }];
367 yield* Queue.offer(
368 events,
369 stored(2, { type: "run.updated", payload: { ...continuation, status: "completed" } }),
370 );
371 const result = yield* Fiber.join(fiber);
372
373 expect(result).toMatchObject({
374 timedOut: false,
375 run: { id: continuationId, status: "completed" },
376 });
377 }),
378);
379
380it.effect("waitForThread can wait past an ended run for the thread's delegated work", () =>
381 Effect.gen(function* () {
382 const projectId = ProjectId.make("project:thread-management:wait-delegated");
383 const threadId = ThreadId.make("thread:thread-management:wait-delegated");
384 const runId = RunId.make("run:thread-management:wait-delegated");
385 const wakeRunId = RunId.make("run:thread-management:wait-delegated:wake");
386 const subscribed = yield* Deferred.make<void>();
387 const events = yield* Queue.unbounded<OrchestrationV2StoredEvent>();
388 const at = (seconds: number) => DateTime.makeUnsafe(seconds * 1_000);
389 const run = (id: RunId, ordinal: number, status: OrchestrationV2Run["status"], end: number) =>
390 ({
391 id,
392 ordinal,
393 status,
394 startedAt: at(end - 1),
395 completedAt: status === "running" ? null : at(end),
396 }) as unknown as OrchestrationV2Run;
397 // The run ended, but a task it delegated still runs and will wake the thread.
398 let runs = [run(runId, 1, "completed", 1)];
399 let subagents = [{ status: "running" }];
400 const stored = (sequence: number, event: object) =>
401 ({ sequence, event: { threadId, ...event } }) as unknown as OrchestrationV2StoredEvent;
402 const layerTest = ThreadManagementService.layer.pipe(
403 Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
404 Layer.provide(
405 Layer.mock(Orchestrator.OrchestratorV2)({
406 getThreadEventSequence: () => Effect.succeed(0),
407 restartContinuationPending: () => Effect.succeed(false),
408 delegatedTaskResultPending: () => Effect.succeed(false),
409 streamStoredEventsFrom: (input) =>
410 input?.eventType === "subagent.updated"
411 ? Stream.fromEffect(Deferred.succeed(subscribed, undefined)).pipe(
412 Stream.drain,
413 Stream.concat(Stream.fromQueue(events)),
414 )
415 : Stream.never,
416 getThreadRecords: () =>
417 Effect.sync(
418 () =>
419 ({
420 thread: { id: threadId, projectId, deletedAt: null },
421 runs,
422 messages: [],
423 subagents,
424 providerThreads: [],
425 }) as unknown as OrchestrationV2ThreadProjection,
426 ),
427 }),
428 ),
429 Layer.provide(ServerSettings.layerTest()),
430 Layer.provide(layerReceiptsTest),
431 );
432 const service = yield* ThreadManagementService.ThreadManagementService.pipe(
433 Effect.provide(layerTest),
434 );
435 // Without the option, the ended run is the answer, as before.
436 expect(
437 yield* service.waitForThread({ projectId, threadId, runId, timeoutMs: 60 * 60 * 1_000 }),
438 ).toMatchObject({ timedOut: false, run: { id: runId } });
439 expect(
440 yield* service.waitForThread({ projectId, threadId, runId, timeoutMs: 60 * 60 * 1_000 }),
441 ).not.toHaveProperty("resultRun");
442
443 const fiber = yield* service
444 .waitForThread({
445 projectId,
446 threadId,
447 runId,
448 timeoutMs: 60 * 60 * 1_000,
449 untilDelegatedWorkSettled: true,
450 })
451 .pipe(Effect.forkChild);
452 // A wait that returns early fails below instead of hanging here.
453 yield* Effect.raceFirst(Deferred.await(subscribed), Fiber.await(fiber));
454 // The task finishes; its delivery wakes the thread, whose next run answers.
455 subagents = [{ status: "completed" }];
456 runs = [...runs, run(wakeRunId, 2, "completed", 3)];
457 yield* Queue.offer(events, stored(1, { type: "subagent.updated", payload: {} }));
458 const result = yield* Fiber.join(fiber);
459
460 expect(result).toMatchObject({
461 timedOut: false,
462 run: { id: runId, status: "completed" },
463 resultRun: { id: wakeRunId, status: "completed" },
464 });
465 }),
466);
467
468it.effect("waitForThread settles delegated work on a thread rolled back past its only run", () =>
469 Effect.gen(function* () {
470 const projectId = ProjectId.make("project:thread-management:wait-rolled-back");
471 const threadId = ThreadId.make("thread:thread-management:wait-rolled-back");
472 const runId = RunId.make("run:thread-management:wait-rolled-back");
473 const rolledBack = {
474 id: runId,
475 ordinal: 1,
476 status: "rolled_back",
477 startedAt: DateTime.makeUnsafe(0),
478 completedAt: DateTime.makeUnsafe(1_000),
479 } as unknown as OrchestrationV2Run;
480 const layerTest = ThreadManagementService.layer.pipe(
481 Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
482 Layer.provide(
483 Layer.mock(Orchestrator.OrchestratorV2)({
484 getThreadEventSequence: () => Effect.succeed(0),
485 restartContinuationPending: () => Effect.succeed(false),
486 delegatedTaskResultPending: () => Effect.succeed(false),
487 streamStoredEventsFrom: () => Stream.never,
488 getThreadRecords: () =>
489 Effect.succeed({
490 thread: { id: threadId, projectId, deletedAt: null },
491 runs: [rolledBack],
492 messages: [],
493 subagents: [],
494 providerThreads: [],
495 } as unknown as OrchestrationV2ThreadProjection),
496 }),
497 ),
498 Layer.provide(ServerSettings.layerTest()),
499 Layer.provide(layerReceiptsTest),
500 );
501 const service = yield* ThreadManagementService.ThreadManagementService.pipe(
502 Effect.provide(layerTest),
503 );
504 const fiber = yield* service
505 .waitForThread({
506 projectId,
507 threadId,
508 runId,
509 timeoutMs: 60_000,
510 untilDelegatedWorkSettled: true,
511 })
512 .pipe(Effect.forkChild);
513 // Nothing will ever run there again, so a wait still open runs out here.
514 yield* TestClock.adjust(Duration.millis(60_000));
515 const result = yield* Fiber.join(fiber);
516
517 expect(result).toMatchObject({
518 timedOut: false,
519 run: { id: runId, status: "rolled_back" },
520 resultRun: { id: runId, status: "rolled_back" },
521 });
522 }),
523);
524
525it.effect("waitForThread returns the cut run once its continuation is declined", () =>
526 Effect.gen(function* () {
527 const projectId = ProjectId.make("project:thread-management:wait-declined");
528 const threadId = ThreadId.make("thread:thread-management:wait-declined");
529 const runId = RunId.make("run:thread-management:wait-declined");
530 const subscribed = yield* Deferred.make<void>();
531 const declined = yield* Deferred.make<void>();
532 let pending = true;
533 const layerTest = ThreadManagementService.layer.pipe(
534 Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
535 Layer.provide(
536 Layer.mock(Orchestrator.OrchestratorV2)({
537 getThreadEventSequence: () => Effect.succeed(0),
538 restartContinuationPending: () => Effect.succeed(pending),
539 awaitRestartContinuation: () => Deferred.await(declined),
540 streamStoredEventsFrom: (input) =>
541 input?.eventType === "run.updated"
542 ? Stream.fromEffect(Deferred.succeed(subscribed, undefined)).pipe(
543 Stream.drain,
544 Stream.concat(Stream.never),
545 )
546 : Stream.never,
547 getThreadRecords: () =>
548 Effect.succeed({
549 thread: { id: threadId, projectId, deletedAt: null },
550 runs: [{ id: runId, status: "cancelled" }],
551 } as unknown as OrchestrationV2ThreadProjection),
552 }),
553 ),
554 Layer.provide(ServerSettings.layerTest()),
555 Layer.provide(layerReceiptsTest),
556 );
557 const service = yield* ThreadManagementService.ThreadManagementService.pipe(
558 Effect.provide(layerTest),
559 );
560 const fiber = yield* service
561 .waitForThread({ projectId, threadId, runId, timeoutMs: 60 * 60 * 1_000 })
562 .pipe(Effect.forkChild);
563 yield* Deferred.await(subscribed);
564 pending = false;
565 yield* Deferred.succeed(declined, undefined);
566 const result = yield* Fiber.join(fiber);
567
568 expect(result).toMatchObject({ timedOut: false, run: { id: runId, status: "cancelled" } });
569 }),
570);
571
572it.effect.each([
573 "live",
574 "no-match",
575 "no-match-clear",
576 "no-session",
577 "unsupported-runtime",
578] as const)(
579 "marks background work awaited on the thread's active provider thread with state %s",
580 (state) =>
581 Effect.gen(function* () {
582 const threadId = ThreadId.make("thread:background-wait");
583 const providerThreadId = ProviderThreadId.make("provider-thread:background-wait");
584 const providerSessionId = ProviderSessionId.make("provider-session:background-wait");
585 const driver = ProviderDriverKind.make("claudeAgent");
586 const now = yield* DateTime.now;
587 const providerThread: OrchestrationV2ProviderThread = {
588 id: providerThreadId,
589 driver,
590 providerInstanceId: ProviderInstanceId.make("claudeAgent"),
591 providerSessionId,
592 appThreadId: threadId,
593 ownerNodeId: null,
594 nativeThreadRef: { driver, nativeId: "native-thread", strength: "strong" },
595 nativeConversationHeadRef: null,
596 status: "idle",
597 firstRunOrdinal: 1,
598 lastRunOrdinal: 1,
599 handoffIds: [],
600 forkedFrom: null,
601 pendingBackgroundTasks: [{ taskId: "tests", kind: "command" }],
602 contextUsage: null,
603 nativeMetadata: null,
604 createdAt: now,
605 updatedAt: now,
606 };
607 const calls: Array<{ providerThreadId: ProviderThreadId; awaited: boolean }> = [];
608 const runtime = {
609 driver,
610 providerSessionId,
611 ...(state === "unsupported-runtime"
612 ? {}
613 : {
614 awaitBackgroundTasks: (input: {
615 readonly providerThread: OrchestrationV2ProviderThread;
616 readonly taskIds: ReadonlyArray<string>;
617 readonly awaited: boolean;
618 }) =>
619 Effect.sync(() => {
620 calls.push({
621 providerThreadId: input.providerThread.id,
622 awaited: input.awaited,
623 });
624 return { matched: input.taskIds.filter((taskId) => taskId === "tests") };
625 }),
626 }),
627 } as unknown as ProviderAdapter.ProviderAdapterV2SessionRuntime;
628 const layerTest = ThreadManagementService.layer.pipe(
629 Layer.provide(
630 Layer.mergeAll(
631 Layer.mock(Orchestrator.OrchestratorV2)({
632 getThreadRecords: () =>
633 Effect.succeed({
634 thread: {
635 id: threadId,
636 deletedAt: null,
637 activeProviderThreadId: providerThreadId,
638 },
639 providerThreads: [providerThread],
640 } as unknown as OrchestrationV2ThreadProjection),
641 }),
642 Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
643 get: () =>
644 Effect.succeed(state === "no-session" ? Option.none() : Option.some(runtime)),
645 }),
646 ),
647 ),
648 Layer.provide(ServerSettings.layerTest()),
649 Layer.provide(layerReceiptsTest),
650 );
651 const result = yield* Effect.gen(function* () {
652 const service = yield* ThreadManagementService.ThreadManagementService;
653 return yield* service
654 .awaitBackgroundTasks({
655 threadId,
656 taskIds: state.startsWith("no-match") ? ["finished"] : ["tests", "finished"],
657 awaited: state !== "no-match-clear",
658 })
659 .pipe(Effect.result);
660 }).pipe(Effect.provide(Layer.merge(layerTest, ThreadCommandExecutor.layer)));
661
662 if (state === "live") {
663 expect(result).toMatchObject({
664 success: { matched: ["tests"], unmatched: ["finished"] },
665 });
666 expect(calls).toEqual([{ providerThreadId, awaited: true }]);
667 } else if (state === "no-match") {
668 expect(result).toMatchObject({
669 failure: { _tag: "ThreadManagementBackgroundWaitError", reason: "unmatched" },
670 });
671 expect(calls).toEqual([{ providerThreadId, awaited: true }]);
672 } else if (state === "no-match-clear") {
673 expect(result).toMatchObject({ success: { matched: [], unmatched: ["finished"] } });
674 expect(calls).toEqual([{ providerThreadId, awaited: false }]);
675 } else {
676 expect(result).toMatchObject({
677 failure: {
678 _tag: "ThreadManagementBackgroundWaitError",
679 reason: state === "no-session" ? "unavailable" : "unsupported",
680 },
681 });
682 expect(calls).toEqual([]);
683 }
684 }),
685);
686
687it.effect.each([
688 { status: "completed" as const, followUp: "none", settles: true },
689 { status: "failed" as const, followUp: "none", settles: false },
690 { status: "interrupted" as const, followUp: "none", settles: false },
691 { status: "completed" as const, followUp: "notification", settles: true },
692 { status: "completed" as const, followUp: "replayed-notification", settles: true },
693 { status: "completed" as const, followUp: "delegated", settles: true },
694 { status: "completed" as const, followUp: "failed-notification", settles: false },
695 { status: "completed" as const, followUp: "held-notification", settles: true },
696 { status: "completed" as const, followUp: "user", settles: false },
697 { status: "completed" as const, followUp: "steered-notification", settles: false },
698])("settleAfterRun handles $status with $followUp follow-up", ({ status, followUp, settles }) =>
699 Effect.gen(function* () {
700 const projectId = ProjectId.make("project:thread-management:settle-after-run");
701 const threadId = ThreadId.make("thread:thread-management:settle-after-run");
702 const runId = RunId.make("run:thread-management:settle-after-run");
703 const followUpId = RunId.make(`${runId}:follow-up`);
704 const subscribed = yield* Deferred.make<void>();
705 const events = yield* Queue.unbounded<OrchestrationV2StoredEvent>();
706 const replayRejected = yield* Deferred.make<void>();
707 let sequence = 0;
708 let sequenceReads = 0;
709 let rejectedCommandId: CommandId | undefined;
710 const runs = [{ id: runId, ordinal: 1, status, userMessageId: MessageId.make("original") }];
711 const nextRun = {
712 id: followUpId,
713 ordinal: 2,
714 status: "running" as OrchestrationV2Run["status"],
715 userMessageId: MessageId.make("follow-up"),
716 };
717 const dispatched: Array<string> = [];
718 // The notification starts after the completed run was observed, before settlement commits.
719 let raced = false;
720 const layerTest = ThreadManagementService.layer.pipe(
721 Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
722 Layer.provide(
723 Layer.mock(Orchestrator.OrchestratorV2)({
724 getThreadEventSequence: () =>
725 Effect.sync(() => {
726 if (followUp === "replayed-notification" && sequenceReads++ === 2) sequence = 1;
727 return sequence;
728 }),
729 restartContinuationPending: () => Effect.succeed(false),
730 streamStoredEventsFrom: () =>
731 Stream.fromEffect(Deferred.succeed(subscribed, undefined)).pipe(
732 Stream.drain,
733 Stream.concat(Stream.fromQueue(events)),
734 ),
735 getThreadRecords: (_threadId, _fields, filter) =>
736 Effect.sync(
737 () =>
738 ({
739 thread: { id: threadId, projectId, deletedAt: null },
740 runs: raced ? [...runs, nextRun] : runs,
741 messages: [
742 { id: MessageId.make("original"), runId, role: "user" as const },
743 {
744 id: nextRun.userMessageId,
745 runId: followUpId,
746 role: "user" as const,
747 ...(followUp.endsWith("notification") ? { notification: {} } : {}),
748 ...(followUp === "delegated" ? { delegatedCompletion: {} } : {}),
749 },
750 { id: MessageId.make("reply"), runId: followUpId, role: "assistant" as const },
751 ...(followUp === "steered-notification"
752 ? [
753 {
754 id: MessageId.make("user-steer"),
755 runId: followUpId,
756 role: "user" as const,
757 },
758 ]
759 : []),
760 ].filter(
761 (message) =>
762 (filter?.messageIds === undefined ||
763 filter.messageIds.includes(message.id)) &&
764 (filter?.messageRunIds === undefined ||
765 filter.messageRunIds.includes(message.runId)) &&
766 (filter?.messageRoles === undefined ||
767 filter.messageRoles.includes(message.role)),
768 ),
769 }) as unknown as OrchestrationV2ThreadProjection,
770 ),
771 dispatch: (command) =>
772 Effect.gen(function* () {
773 if (followUp !== "none" && !raced) {
774 raced = true;
775 rejectedCommandId = command.commandId;
776 return yield* new Orchestrator.OrchestratorDispatchError({
777 commandId: command.commandId,
778 commandType: command.type,
779 cause: "Thread is still running",
780 });
781 }
782 if (followUp === "replayed-notification" && command.commandId === rejectedCommandId) {
783 yield* Deferred.succeed(replayRejected, undefined);
784 return yield* new Orchestrator.OrchestratorCommandPreviouslyRejectedError({
785 commandId: command.commandId,
786 commandType: command.type,
787 detail: "Thread is still running",
788 });
789 }
790 dispatched.push(command.type);
791 return { sequence: 1, storedEvents: [] } as never;
792 }),
793 }),
794 ),
795 Layer.provide(ServerSettings.layerTest()),
796 Layer.provide(layerReceiptsTest),
797 );
798 const service = yield* ThreadManagementService.ThreadManagementService.pipe(
799 Effect.provide(layerTest),
800 );
801 const fiber = yield* service
802 .settleAfterRun({ projectId, threadId, runId })
803 .pipe(Effect.forkChild);
804 if (followUp !== "none") {
805 yield* Deferred.await(subscribed);
806 expect(dispatched).toEqual([]);
807 nextRun.status =
808 followUp === "failed-notification"
809 ? "failed"
810 : followUp === "held-notification"
811 ? "queued"
812 : "completed";
813 yield* Queue.offer(events, {
814 sequence: 1,
815 event: { type: "run.updated", threadId, payload: nextRun },
816 } as unknown as OrchestrationV2StoredEvent);
817 }
818 if (followUp === "replayed-notification") {
819 yield* Deferred.await(replayRejected);
820 expect(dispatched).toEqual([]);
821 sequence = 2;
822 yield* Queue.offer(events, {
823 sequence,
824 event: { type: "run.updated", threadId, payload: nextRun },
825 } as unknown as OrchestrationV2StoredEvent);
826 }
827 yield* Fiber.join(fiber);
828 expect(dispatched).toEqual(settles ? ["thread.settle"] : []);
829 }),
830);
831
832it.effect.each([
833 {
834 name: "kills the thread's own agent and ends its turn",
835 others: [] as ReadonlyArray<string>,
836 killed: true,
837 expected: { outcome: "killed", sharedWithThreadIds: [] },
838 teardown: ["release"],
839 },
840 {
841 name: "keeps a process another working thread shares and only ends this turn",
842 others: ["thread:thread-management:force-stop:other"],
843 killed: false,
844 expected: {
845 outcome: "shared",
846 sharedWithThreadIds: ["thread:thread-management:force-stop:other"],
847 },
848 teardown: ["detach"],
849 },
850 {
851 name: "still settles a finished run's background work in a shared process",
852 others: ["thread:thread-management:force-stop:other"],
853 killed: false,
854 settledRun: true,
855 expected: {
856 outcome: "shared",
857 sharedWithThreadIds: ["thread:thread-management:force-stop:other"],
858 },
859 teardown: ["detach"],
860 },
861 {
862 name: "detaches when a sibling starts a turn just before the session is released",
863 others: [] as ReadonlyArray<string>,
864 killed: false,
865 siblingBusy: true,
866 expected: { outcome: "shared", sharedWithThreadIds: [] },
867 teardown: ["release", "detach"],
868 },
869 {
870 name: "reports a teardown that has not finished in time as still releasing",
871 others: [] as ReadonlyArray<string>,
872 killed: false,
873 releaseHangs: true,
874 expected: { outcome: "releasing", sharedWithThreadIds: [] },
875 teardown: ["release"],
876 },
877])("Force stop $name", ({ others, killed, expected, teardown, ...row }) =>
878 Effect.gen(function* () {
879 const threadId = ThreadId.make("thread:thread-management:force-stop");
880 const providerSessionId = ProviderSessionId.make("provider-session:force-stop");
881 const providerThreadId = ProviderThreadId.make("provider-thread:force-stop");
882 const runId = RunId.make("run:thread-management:force-stop");
883 const dispatched: Array<OrchestrationV2Command | { readonly type: string }> = [];
884 const teardowns: Array<string> = [];
885 const tornDown = yield* Deferred.make<void>();
886 const layerTest = ThreadManagementService.layer.pipe(
887 Layer.provide(
888 Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
889 isLive: () => Effect.succeed(true),
890 otherWorkingThreads: () => Effect.succeed(others.map((id) => ThreadId.make(id))),
891 releaseUnlessBusy: () =>
892 Effect.sync(() => teardowns.push("release")).pipe(
893 Effect.andThen(
894 "releaseHangs" in row ? Effect.never : Effect.succeed(!("siblingBusy" in row)),
895 ),
896 ),
897 detach: () =>
898 Effect.sync(() => teardowns.push("detach")).pipe(
899 Effect.andThen(Deferred.succeed(tornDown, undefined)),
900 Effect.asVoid,
901 ),
902 }),
903 ),
904 Layer.provide(
905 Layer.mock(Orchestrator.OrchestratorV2)({
906 getThreadRecords: () =>
907 Effect.succeed({
908 thread: { id: threadId, deletedAt: null },
909 runs: [
910 {
911 id: runId,
912 ordinal: 1,
913 status: "settledRun" in row ? "completed" : "running",
914 activeAttemptId: "attempt",
915 },
916 // A queued follow-up neither supersedes the request nor becomes the target.
917 { id: RunId.make("run:queued-follow-up"), ordinal: 2, status: "queued" },
918 ],
919 providerThreads: [{ id: providerThreadId, appThreadId: threadId, providerSessionId }],
920 providerTurns: [
921 {
922 id: "provider-turn:force-stop",
923 providerThreadId,
924 runAttemptId: "attempt",
925 status: "settledRun" in row ? "completed" : "running",
926 },
927 ],
928 } as unknown as OrchestrationV2ThreadProjection),
929 dispatch: (command) =>
930 Effect.sync(() => {
931 dispatched.push(command);
932 return { sequence: 1, storedEvents: [] } as never;
933 }),
934 }),
935 ),
936 Layer.provide(
937 Layer.succeed(AgentScope, {
938 ...AgentScope.defaultValue(),
939 kill: () => Effect.succeed(killed),
940 }),
941 ),
942 Layer.provide(ServerSettings.layerTest()),
943 Layer.provide(layerReceiptsTest),
944 );
945 yield* Effect.gen(function* () {
946 const service = yield* ThreadManagementService.ThreadManagementService;
947 const stopping = yield* service.forceStopThread({ threadId, runId }).pipe(Effect.forkChild);
948 yield* TestClock.adjust("10 seconds");
949 expect(yield* Fiber.join(stopping)).toEqual(expected);
950 expect(dispatched).toMatchObject([
951 {
952 type: "thread.background-work.settle",
953 providerThreadId,
954 reason: "Force stopped by user",
955 },
956 ]);
957 if (teardown.at(-1) === "detach") yield* Deferred.await(tornDown);
958 expect(teardowns).toEqual(teardown);
959 }).pipe(Effect.provide(layerTest));
960 }),
961);
962
963it.effect("Force stop refuses once a newer run has started, before killing anything", () =>
964 Effect.gen(function* () {
965 const threadId = ThreadId.make("thread:thread-management:force-stop-superseded");
966 let killed = false;
967 const layerTest = ThreadManagementService.layer.pipe(
968 Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
969 Layer.provide(
970 Layer.mock(Orchestrator.OrchestratorV2)({
971 getThreadRecords: () =>
972 Effect.succeed({
973 thread: { id: threadId, deletedAt: null },
974 runs: [
975 { id: RunId.make("run:stopped"), ordinal: 1, status: "running" },
976 { id: RunId.make("run:newer"), ordinal: 2, status: "running" },
977 ],
978 providerThreads: [],
979 providerTurns: [],
980 } as unknown as OrchestrationV2ThreadProjection),
981 }),
982 ),
983 Layer.provide(
984 Layer.succeed(AgentScope, {
985 ...AgentScope.defaultValue(),
986 kill: () => Effect.sync(() => (killed = true)),
987 }),
988 ),
989 Layer.provide(ServerSettings.layerTest()),
990 Layer.provide(layerReceiptsTest),
991 );
992 const error = yield* Effect.gen(function* () {
993 const service = yield* ThreadManagementService.ThreadManagementService;
994 return yield* Effect.flip(
995 service.forceStopThread({ threadId, runId: RunId.make("run:stopped") }),
996 );
997 }).pipe(Effect.provide(layerTest));
998 expect(error.reason).toBe("superseded");
999 expect(killed).toBe(false);
1000 }),
1001);
1002
1003it.effect(
1004 "admits two transcript exports at a time and frees a slot on failure or interruption",
1005 () =>
1006 Effect.gen(function* () {
1007 const started = yield* Queue.unbounded<string>();
1008 const gates = new Map(
1009 ["a", "b", "c", "d", "e", "f"].map((id) => [
1010 id,
1011 Deferred.makeUnsafe<void, Orchestrator.OrchestratorV2Error>(),
1012 ]),
1013 );
1014 const layerTest = ThreadManagementService.layer.pipe(
1015 Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
1016 Layer.provide(
1017 Layer.mock(Orchestrator.OrchestratorV2)({
1018 getTranscriptByteFloor: () => Effect.succeed(0),
1019 getThreadSnapshot: (threadId) =>
1020 Queue.offer(started, threadId).pipe(
1021 Effect.andThen(Deferred.await(gates.get(threadId)!)),
1022 Effect.as({
1023 schemaVersion: 1,
1024 snapshotSequence: 1,
1025 projection: {
1026 thread: { id: threadId, title: threadId },
1027 updatedAt: DateTime.makeUnsafe(0),
1028 visibleTurnItems: [],
1029 } as unknown as OrchestrationV2ThreadProjection,
1030 }),
1031 ),
1032 }),
1033 ),
1034 Layer.provide(ServerSettings.layerTest()),
1035 Layer.provide(layerReceiptsTest),
1036 );
1037
1038 yield* Effect.gen(function* () {
1039 const threads = yield* ThreadManagementService.ThreadManagementService;
1040 const exportThread = (id: string) =>
1041 threads.getThreadTranscript(ThreadId.make(id)).pipe(
1042 Effect.provideService(ServerEnvironment.ServerEnvironmentIdentity, {
1043 getEnvironmentId: Effect.succeed(EnvironmentId.make("environment:export")),
1044 }),
1045 Effect.forkChild({ startImmediately: true }),
1046 );
1047 const a = yield* exportThread("a");
1048 const b = yield* exportThread("b");
1049 const c = yield* exportThread("c");
1050 const d = yield* exportThread("d");
1051 const e = yield* exportThread("e");
1052 const f = yield* exportThread("f");
1053 expect(yield* Queue.takeAll(started)).toEqual(["a", "b"]);
1054
1055 yield* Fiber.interrupt(c);
1056 yield* Deferred.fail(
1057 gates.get("a")!,
1058 new Orchestrator.OrchestratorProjectionError({ threadId: ThreadId.make("a"), cause: "" }),
1059 );
1060 expect(yield* Queue.take(started)).toBe("d");
1061 yield* Fiber.interrupt(b);
1062 expect(yield* Queue.take(started)).toBe("e");
1063 yield* Deferred.succeed(gates.get("d")!, undefined);
1064 expect(yield* Queue.take(started)).toBe("f");
1065 yield* Deferred.succeed(gates.get("e")!, undefined);
1066 yield* Deferred.succeed(gates.get("f")!, undefined);
1067
1068 expect((yield* Fiber.await(a))._tag).toBe("Failure");
1069 expect((yield* Fiber.join(d)).threadId).toBe("d");
1070 expect((yield* Fiber.join(e)).threadId).toBe("e");
1071 expect((yield* Fiber.join(f)).threadId).toBe("f");
1072 expect(yield* Queue.size(started)).toBe(0);
1073 }).pipe(Effect.provide(layerTest));
1074 }),
1075);