The best way to control Claude Code, Codex and more from any computer or phone

main
ts 1,098 lines 40.9 KB
Raw
1 import { assert, it } from "@effect/vitest";
2 import {
3 CommandId,
4 EventId,
5 MessageId,
6 type ModelSelection,
7 type OrchestrationV2ProviderCapabilities,
8 type OrchestrationV2ThreadProjection,
9 type OrchestrationV2ProviderSession,
10 type OrchestrationV2ProviderThread,
11 ProjectId,
12 ProviderDriverKind,
13 ProviderInstanceId,
14 ProviderSessionId,
15 ProviderThreadId,
16 ProviderTurnId,
17 type RunId,
18 ThreadId,
19 TurnItemId,
20 } from "@t3tools/contracts";
21 import * as DateTime from "effect/DateTime";
22 import * as Effect from "effect/Effect";
23 import * as Fiber from "effect/Fiber";
24 import * as Layer from "effect/Layer";
25 import * as Queue from "effect/Queue";
26 import * as Ref from "effect/Ref";
27 import * as Stream from "effect/Stream";
28
29 import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
30 import * as EffectWorker from "./EffectWorker.ts";
31 import * as EventSink from "./EventSink.ts";
32 import * as Orchestrator from "./Orchestrator.ts";
33 import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
34 import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
35 import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
36 import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
37
38 const driver = ProviderDriverKind.make("codex");
39 const providerInstanceId = ProviderInstanceId.make("codex-restart-test");
40 const initialSelection = {
41 instanceId: providerInstanceId,
42 model: "restart-model-a",
43 } satisfies ModelSelection;
44 const replacementSelection = {
45 instanceId: providerInstanceId,
46 model: "restart-model-b",
47 } satisfies ModelSelection;
48 const seedSelection = {
49 instanceId: providerInstanceId,
50 model: "seed-model",
51 } satisfies ModelSelection;
52 const modelSwitches = (projection: OrchestrationV2ThreadProjection) =>
53 projection.turnItems.flatMap((item) =>
54 item.type === "user_message" && item.modelSwitch !== undefined ? [item.modelSwitch] : [],
55 );
56 const pooledCapabilities: OrchestrationV2ProviderCapabilities = CodexProviderCapabilitiesV2;
57 const exclusiveCapabilities: OrchestrationV2ProviderCapabilities = {
58 ...CodexProviderCapabilitiesV2,
59 sessions: {
60 ...CodexProviderCapabilitiesV2.sessions,
61 supportsMultipleProviderThreadsPerSession: false,
62 supportsModelSwitchInSession: false,
63 },
64 };
65
66 interface ActiveTurn {
67 readonly input: ProviderAdapter.ProviderAdapterV2TurnInput;
68 readonly providerTurnId: ProviderTurnId;
69 }
70
71 interface RestartAdapterState {
72 readonly activeTurn: ActiveTurn | null;
73 readonly opened: ReadonlyArray<{
74 readonly model: string | null;
75 readonly cwd: string | null;
76 }>;
77 readonly started: ReadonlyArray<{
78 readonly model: string;
79 readonly cwd: string | null;
80 readonly attemptId: string;
81 }>;
82 readonly closedSessionCount: number;
83 readonly failedReplacementOpen: boolean;
84 }
85
86 function makeRestartAdapter(
87 state: Ref.Ref<RestartAdapterState>,
88 sessionCapabilities: OrchestrationV2ProviderCapabilities = pooledCapabilities,
89 providerInstanceId = initialSelection.instanceId,
90 ): ProviderAdapter.ProviderAdapterV2["Service"] {
91 return {
92 instanceId: providerInstanceId,
93 driver,
94 getCapabilities: () => Effect.succeed(sessionCapabilities),
95 planSelectionTransition: ({ current, target }) =>
96 Effect.succeed(
97 current.model === target.model
98 ? ({ type: "apply_on_next_turn" } as const)
99 : ({ type: "restart_session" } as const),
100 ),
101 openSession: (sessionInput) =>
102 Effect.gen(function* () {
103 const failThisOpen = yield* Ref.modify(state, (current) => {
104 const shouldFail =
105 sessionInput.modelSelection.model === replacementSelection.model &&
106 !current.failedReplacementOpen;
107 return [
108 shouldFail,
109 {
110 ...current,
111 failedReplacementOpen: current.failedReplacementOpen || shouldFail,
112 opened: [
113 ...current.opened,
114 {
115 model: sessionInput.modelSelection.model,
116 cwd: sessionInput.runtimePolicy.cwd,
117 },
118 ],
119 },
120 ] as const;
121 });
122 if (failThisOpen) {
123 return yield* new ProviderAdapter.ProviderAdapterOpenSessionError({
124 driver,
125 providerSessionId: sessionInput.providerSessionId,
126 cause: "simulated replacement open failure",
127 });
128 }
129
130 const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
131 const now = yield* DateTime.now;
132 const providerSession: OrchestrationV2ProviderSession = {
133 id: sessionInput.providerSessionId,
134 driver,
135 providerInstanceId,
136 status: "ready",
137 cwd: sessionInput.runtimePolicy.cwd ?? "/fallback",
138 model: sessionInput.modelSelection.model,
139 capabilities: sessionCapabilities,
140 createdAt: now,
141 updatedAt: now,
142 lastError: null,
143 };
144 yield* Effect.addFinalizer(() =>
145 Ref.update(state, (current) => ({
146 ...current,
147 closedSessionCount: current.closedSessionCount + 1,
148 })),
149 );
150
151 const publishTerminal = (active: ActiveTurn, status: "completed" | "interrupted") =>
152 Effect.gen(function* () {
153 const occurredAt = yield* DateTime.now;
154 yield* Queue.offer(events, {
155 type: "provider_turn.updated",
156 driver,
157 providerTurn: {
158 id: active.providerTurnId,
159 providerThreadId: active.input.providerThread.id,
160 nodeId: active.input.rootNodeId,
161 runAttemptId: active.input.attemptId,
162 nativeTurnRef: {
163 driver,
164 nativeId: `native:${active.providerTurnId}`,
165 strength: "strong",
166 },
167 ordinal: active.input.providerTurnOrdinal,
168 status,
169 startedAt: occurredAt,
170 completedAt: occurredAt,
171 },
172 });
173 yield* Queue.offer(events, {
174 type: "turn.terminal",
175 driver,
176 providerThreadId: active.input.providerThread.id,
177 providerTurnId: active.providerTurnId,
178 runOrdinal: active.input.runOrdinal,
179 status,
180 failure: null,
181 threadDisposition: "reusable",
182 });
183 });
184
185 return {
186 instanceId: providerInstanceId,
187 driver,
188 providerSessionId: sessionInput.providerSessionId,
189 providerSession,
190 events: Stream.fromQueue(events),
191 ensureThread: (threadInput) =>
192 Effect.gen(function* () {
193 const createdAt = yield* DateTime.now;
194 return {
195 id: ProviderThreadId.make(`provider-thread:${threadInput.threadId}`),
196 driver,
197 providerInstanceId,
198 providerSessionId: sessionInput.providerSessionId,
199 appThreadId: threadInput.threadId,
200 ownerNodeId: null,
201 nativeThreadRef: {
202 driver,
203 nativeId: `native-thread:${threadInput.threadId}`,
204 strength: "strong",
205 },
206 nativeConversationHeadRef: null,
207 status: "idle",
208 firstRunOrdinal: null,
209 lastRunOrdinal: null,
210 handoffIds: [],
211 forkedFrom: null,
212 createdAt,
213 updatedAt: createdAt,
214 } satisfies OrchestrationV2ProviderThread;
215 }),
216 resumeThread: ({ providerThread }) => Effect.succeed(providerThread),
217 startTurn: (input) =>
218 Effect.gen(function* () {
219 yield* Ref.update(state, (current) => ({
220 ...current,
221 started: [
222 ...current.started,
223 {
224 model: input.modelSelection.model,
225 cwd: input.runtimePolicy.cwd,
226 attemptId: input.attemptId,
227 },
228 ],
229 }));
230 const active = {
231 input,
232 providerTurnId: ProviderTurnId.make(`provider-turn:${input.attemptId}`),
233 } satisfies ActiveTurn;
234 if (input.modelSelection.model === initialSelection.model) {
235 const occurredAt = yield* DateTime.now;
236 yield* Ref.update(state, (current) => ({ ...current, activeTurn: active }));
237 yield* Queue.offer(events, {
238 type: "provider_turn.updated",
239 driver,
240 providerTurn: {
241 id: active.providerTurnId,
242 providerThreadId: input.providerThread.id,
243 nodeId: input.rootNodeId,
244 runAttemptId: input.attemptId,
245 nativeTurnRef: {
246 driver,
247 nativeId: `native:${active.providerTurnId}`,
248 strength: "strong",
249 },
250 ordinal: input.providerTurnOrdinal,
251 status: "running",
252 startedAt: occurredAt,
253 completedAt: null,
254 },
255 });
256 return;
257 }
258 yield* publishTerminal(active, "completed");
259 }),
260 steerTurn: () => Effect.void,
261 interruptTurn: () =>
262 Effect.gen(function* () {
263 const active = (yield* Ref.get(state)).activeTurn;
264 if (active !== null) {
265 const updatedAt = yield* DateTime.now;
266 yield* Queue.offer(events, {
267 type: "provider_thread.updated",
268 driver,
269 providerThread: {
270 ...active.input.providerThread,
271 status: "idle",
272 updatedAt,
273 },
274 });
275 yield* publishTerminal(active, "interrupted");
276 yield* Ref.update(state, (current) => ({ ...current, activeTurn: null }));
277 }
278 }),
279 respondToRuntimeRequest: () => Effect.void,
280 readThreadSnapshot: () => Effect.die("unused readThreadSnapshot"),
281 rollbackThread: () => Effect.die("unused rollbackThread"),
282 forkThread: () => Effect.die("unused forkThread"),
283 };
284 }),
285 };
286 }
287
288 it.live("retries a failed selection restart session open before completing", () =>
289 Effect.gen(function* () {
290 const cwd = yield* checkpointWorkspace("selection-restart-lifecycle");
291 const threadId = ThreadId.make("thread:selection-restart-lifecycle");
292 const state = yield* Ref.make<RestartAdapterState>({
293 activeTurn: null,
294 opened: [],
295 started: [],
296 closedSessionCount: 0,
297 failedReplacementOpen: false,
298 });
299 const layerRegistry = ProviderAdapterRegistry.layerSingle(makeRestartAdapter(state));
300
301 const result = yield* Effect.gen(function* () {
302 const orchestrator = yield* Orchestrator.OrchestratorV2;
303 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
304 const eventSink = yield* EventSink.EventSinkV2;
305
306 yield* orchestrator.dispatch({
307 type: "thread.create",
308 createdBy: "user",
309 creationSource: "web",
310 commandId: CommandId.make("command:selection-restart:create"),
311 threadId,
312 projectId: ProjectId.make("project:selection-restart"),
313 title: "Selection restart",
314 modelSelection: initialSelection,
315 runtimeMode: "full-access",
316 interactionMode: "default",
317 branch: null,
318 worktreePath: cwd,
319 });
320 const running = yield* eventSink
321 .stream({
322 threadId,
323 afterSequence: yield* eventSink.latestSequence({ threadId }),
324 })
325 .pipe(
326 Stream.map((stored) => stored.event),
327 Stream.filter(
328 (event) => event.type === "provider-turn.updated" && event.payload.status === "running",
329 ),
330 Stream.take(1),
331 Stream.runDrain,
332 Effect.forkScoped,
333 );
334 yield* orchestrator.dispatch({
335 type: "message.dispatch",
336 createdBy: "user",
337 creationSource: "web",
338 commandId: CommandId.make("command:selection-restart:first"),
339 threadId,
340 messageId: MessageId.make("message:selection-restart:first"),
341 text: "first",
342 attachments: [],
343 modelSelection: initialSelection,
344 dispatchMode: { type: "start_immediately" },
345 });
346 yield* Fiber.join(running);
347 yield* worker.drain();
348 const activeProjection = yield* orchestrator.getThreadProjection(threadId);
349 assert.isTrue(activeProjection.providerTurns.some((turn) => turn.status === "running"));
350 const activeRunId = activeProjection.runs[0]?.id;
351 if (activeRunId === undefined) {
352 return yield* Effect.die("active restart test run is missing");
353 }
354
355 const completed = yield* eventSink
356 .stream({
357 threadId,
358 afterSequence: yield* eventSink.latestSequence({ threadId }),
359 })
360 .pipe(
361 Stream.map((stored) => stored.event),
362 Stream.filter(
363 (event) => event.type === "run.updated" && event.payload.status === "completed",
364 ),
365 Stream.take(1),
366 Stream.runDrain,
367 Effect.forkScoped,
368 );
369 yield* orchestrator.dispatch({
370 type: "message.dispatch",
371 createdBy: "user",
372 creationSource: "web",
373 commandId: CommandId.make("command:selection-restart:second"),
374 threadId,
375 messageId: MessageId.make("message:selection-restart:second"),
376 text: "second",
377 attachments: [],
378 modelSelection: replacementSelection,
379 dispatchMode: { type: "restart_active", targetRunId: activeRunId },
380 });
381 yield* Fiber.join(completed);
382 yield* worker.drain();
383 return {
384 projection: yield* orchestrator.getThreadProjection(threadId),
385 captured: yield* Ref.get(state),
386 };
387 }).pipe(
388 Effect.provide(
389 ProviderReplayHarness.layerWithRegistry(
390 { name: "selection-restart-lifecycle" },
391 layerRegistry,
392 ),
393 ),
394 );
395 const { projection, captured } = result;
396
397 assert.lengthOf(projection.runs, 1);
398 assert.lengthOf(projection.attempts, 2);
399 assert.deepEqual(
400 projection.attempts.map((attempt) => attempt.status),
401 ["superseded", "completed"],
402 );
403 assert.deepEqual(
404 projection.providerTurns.map((turn) => turn.status),
405 ["interrupted", "completed"],
406 );
407 assert.isFalse(
408 projection.turnItems.some(
409 (item) => item.type === "run_interrupt_request" || item.type === "run_interrupt_result",
410 ),
411 "selection restart supersede must not project hard-Stop interrupt items",
412 );
413 assert.equal(projection.runs.at(-1)?.modelSelection.model, replacementSelection.model);
414 assert.deepEqual(modelSwitches(projection), [
415 { from: initialSelection, to: replacementSelection },
416 ]);
417 assert.equal(projection.runs.at(-1)?.status, "completed");
418 assert.isTrue(captured.failedReplacementOpen);
419 assert.equal(captured.closedSessionCount, 0);
420 assert.deepEqual(
421 captured.opened.map((open) => [open.model, open.cwd]),
422 [
423 [initialSelection.model, cwd],
424 [replacementSelection.model, cwd],
425 [replacementSelection.model, cwd],
426 ],
427 );
428 assert.deepEqual(
429 captured.started.map((turn) => [turn.model, turn.cwd]),
430 [
431 [initialSelection.model, cwd],
432 [replacementSelection.model, cwd],
433 ],
434 );
435 assert.notEqual(
436 projection.providerSessions[0]?.id,
437 projection.providerThreads[0]?.providerSessionId,
438 );
439 assert.equal(
440 projection.providerThreads[0]?.providerSessionId,
441 projection.providerSessions.find((session) => session.model === replacementSelection.model)
442 ?.id,
443 );
444 }),
445 );
446
447 // regression: #22971 an exclusive session closes on detach; that failed the run and held the queue
448 it.live.each(["pooled", "exclusive"] as const)(
449 "a workspace handoff on a %s session completes its run and starts the queued continuation",
450 (mode) =>
451 Effect.gen(function* () {
452 const cwd = yield* checkpointWorkspace(`workspace-handoff-${mode}`);
453 const handoffCwd = yield* checkpointWorkspace(`workspace-handoff-${mode}-target`);
454 const threadId = ThreadId.make(`thread:workspace-handoff:${mode}`);
455 const state = yield* Ref.make<RestartAdapterState>({
456 activeTurn: null,
457 opened: [],
458 started: [],
459 closedSessionCount: 0,
460 failedReplacementOpen: false,
461 });
462 const base = makeRestartAdapter(
463 state,
464 mode === "pooled" ? pooledCapabilities : exclusiveCapabilities,
465 );
466 const handoffToolId = TurnItemId.make(`turn-item:workspace-handoff:${mode}:tool`);
467 // A session that closes on detach is never asked to interrupt: Claude's cannot answer.
468 // Its turn is inside the handoff's own tool call, which the closed session never ends.
469 const adapter: ProviderAdapter.ProviderAdapterV2["Service"] =
470 mode === "pooled"
471 ? base
472 : {
473 ...base,
474 openSession: (input) =>
475 Effect.gen(function* () {
476 const session = yield* base.openSession(input);
477 const toolEvents =
478 yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
479 return {
480 ...session,
481 events: Stream.merge(session.events, Stream.fromQueue(toolEvents)),
482 startTurn: (turn) =>
483 Effect.gen(function* () {
484 yield* session.startTurn(turn);
485 const now = yield* DateTime.now;
486 yield* Queue.offer(toolEvents, {
487 type: "turn_item.updated",
488 driver,
489 turnItem: {
490 id: handoffToolId,
491 type: "dynamic_tool",
492 threadId: turn.threadId,
493 runId: turn.runId,
494 nodeId: turn.rootNodeId,
495 providerThreadId: turn.providerThread.id,
496 providerTurnId: ProviderTurnId.make(`provider-turn:${turn.attemptId}`),
497 nativeItemRef: null,
498 parentItemId: null,
499 ordinal: 1,
500 status: "running",
501 title: "t3_worktree_handoff",
502 toolName: "t3-code.t3_worktree_handoff",
503 input: {},
504 output: null,
505 startedAt: now,
506 completedAt: null,
507 updatedAt: now,
508 },
509 });
510 }),
511 interruptTurn: () => Effect.die("an exclusive handoff must not interrupt"),
512 };
513 }),
514 };
515 const layerRegistry = ProviderAdapterRegistry.layerSingle(adapter);
516
517 const projection = yield* Effect.gen(function* () {
518 const orchestrator = yield* Orchestrator.OrchestratorV2;
519 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
520 const eventSink = yield* EventSink.EventSinkV2;
521
522 yield* orchestrator.dispatch({
523 type: "thread.create",
524 createdBy: "user",
525 creationSource: "web",
526 commandId: CommandId.make("command:workspace-handoff:create"),
527 threadId,
528 projectId: ProjectId.make("project:workspace-handoff"),
529 title: "Workspace handoff",
530 modelSelection: initialSelection,
531 runtimeMode: "full-access",
532 interactionMode: "default",
533 branch: null,
534 worktreePath: cwd,
535 });
536 const running = yield* eventSink
537 .stream({
538 threadId,
539 afterSequence: yield* eventSink.latestSequence({ threadId }),
540 })
541 .pipe(
542 Stream.map((stored) => stored.event),
543 Stream.filter(
544 (event) =>
545 event.type === "provider-turn.updated" && event.payload.status === "running",
546 ),
547 Stream.take(1),
548 Stream.runDrain,
549 Effect.forkScoped,
550 );
551 yield* orchestrator.dispatch({
552 type: "message.dispatch",
553 createdBy: "user",
554 creationSource: "web",
555 commandId: CommandId.make("command:workspace-handoff:first"),
556 threadId,
557 messageId: MessageId.make("message:workspace-handoff:first"),
558 text: "move to a worktree",
559 attachments: [],
560 modelSelection: initialSelection,
561 dispatchMode: { type: "start_immediately" },
562 });
563 yield* Fiber.join(running);
564 yield* worker.drain();
565
566 const settled = yield* eventSink
567 .stream({
568 threadId,
569 afterSequence: yield* eventSink.latestSequence({ threadId }),
570 })
571 .pipe(
572 Stream.map((stored) => stored.event),
573 Stream.filter(
574 (event) =>
575 event.type === "run.updated" &&
576 event.payload.status !== "running" &&
577 event.payload.status !== "queued" &&
578 event.payload.status !== "starting",
579 ),
580 Stream.take(1),
581 Stream.runDrain,
582 Effect.forkScoped,
583 );
584 // What t3_worktree_handoff dispatches while the calling turn is still running.
585 const bound = yield* orchestrator.dispatch({
586 type: "thread.metadata.update",
587 commandId: CommandId.make("command:workspace-handoff:rebind"),
588 threadId,
589 branch: "repro-handoff",
590 worktreePath: handoffCwd,
591 continuation: {
592 messageId: MessageId.make("message:workspace-handoff:continuation"),
593 text: "continue in the worktree",
594 },
595 });
596 // The detach can settle the run at once, so its follow-up must exist in the same commit.
597 assert.deepEqual(
598 bound.storedEvents.flatMap(({ event }) =>
599 event.type === "run.created" ? [event.payload.status] : [],
600 ),
601 ["queued"],
602 );
603 yield* Fiber.join(settled);
604 yield* worker.drain();
605 return yield* orchestrator.getThreadProjection(threadId);
606 }).pipe(
607 Effect.provide(
608 ProviderReplayHarness.layerWithRegistry(
609 { name: `workspace-handoff-${mode}` },
610 layerRegistry,
611 ),
612 ),
613 );
614
615 assert.equal(projection.thread.worktreePath, handoffCwd);
616 assert.equal(projection.runs[0]?.status, "completed");
617 assert.isNotNull(projection.runs[0]?.checkpointId ?? null);
618 assert.isFalse(
619 projection.turnItems.some(
620 (item) => item.type === "run_interrupt_result" || item.type === "error",
621 ),
622 "an intentional workspace handoff must not project an interrupt result or a failure",
623 );
624 if (mode === "exclusive") {
625 // regression: #39410 the handoff's tool call stayed running after its session closed.
626 assert.equal(
627 projection.turnItems.find((item) => item.id === handoffToolId)?.status,
628 "interrupted",
629 );
630 }
631 assert.deepEqual(
632 (yield* Ref.get(state)).started.map((turn) => turn.cwd),
633 [cwd, handoffCwd],
634 );
635 }),
636 );
637
638 it.live("Stop during a restart interrupts the turn the restart could not stop", () =>
639 Effect.gen(function* () {
640 const cwd = yield* checkpointWorkspace("selection-restart-stop");
641 const threadId = ThreadId.make("thread:selection-restart-stop");
642 const state = yield* Ref.make<RestartAdapterState>({
643 activeTurn: null,
644 opened: [],
645 started: [],
646 closedSessionCount: 0,
647 failedReplacementOpen: false,
648 });
649 const base = makeRestartAdapter(state);
650 // A wedged provider ignores the restart's soft cancel; only Stop ends its turn.
651 const adapter: ProviderAdapter.ProviderAdapterV2["Service"] = {
652 ...base,
653 openSession: (input) =>
654 base.openSession(input).pipe(
655 Effect.map((session) => ({
656 ...session,
657 interruptTurn: (interrupt: ProviderAdapter.ProviderAdapterV2InterruptInput) =>
658 interrupt.requestRuntimeRestart === true
659 ? session.interruptTurn(interrupt)
660 : Effect.never,
661 })),
662 ),
663 };
664
665 const result = yield* Effect.gen(function* () {
666 const orchestrator = yield* Orchestrator.OrchestratorV2;
667 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
668 const running = yield* orchestrator.streamDomainEvents.pipe(
669 Stream.filter(
670 (event) => event.type === "provider-turn.updated" && event.payload.status === "running",
671 ),
672 Stream.take(1),
673 Stream.runDrain,
674 Effect.forkScoped({ startImmediately: true }),
675 );
676 yield* orchestrator.dispatch({
677 type: "thread.create",
678 createdBy: "user",
679 creationSource: "web",
680 commandId: CommandId.make("command:selection-restart-stop:create"),
681 threadId,
682 projectId: ProjectId.make("project:selection-restart-stop"),
683 title: "Stop during restart",
684 modelSelection: initialSelection,
685 runtimeMode: "full-access",
686 interactionMode: "default",
687 branch: null,
688 worktreePath: cwd,
689 });
690 yield* orchestrator.dispatch({
691 type: "message.dispatch",
692 createdBy: "user",
693 creationSource: "web",
694 commandId: CommandId.make("command:selection-restart-stop:first"),
695 threadId,
696 messageId: MessageId.make("message:selection-restart-stop:first"),
697 text: "first",
698 attachments: [],
699 modelSelection: initialSelection,
700 dispatchMode: { type: "start_immediately" },
701 });
702 yield* worker.drain();
703 yield* Fiber.join(running);
704 const runId = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!.id;
705
706 yield* orchestrator.dispatch({
707 type: "message.dispatch",
708 createdBy: "user",
709 creationSource: "web",
710 commandId: CommandId.make("command:selection-restart-stop:steer"),
711 threadId,
712 messageId: MessageId.make("message:selection-restart-stop:steer"),
713 text: "Are you still going?",
714 attachments: [],
715 modelSelection: initialSelection,
716 dispatchMode: { type: "restart_active", targetRunId: runId },
717 });
718 yield* orchestrator.dispatch({
719 type: "run.interrupt",
720 commandId: CommandId.make("command:selection-restart-stop:stop"),
721 threadId,
722 runId,
723 });
724 yield* worker.drain();
725 return {
726 projection: yield* orchestrator.getThreadProjection(threadId),
727 captured: yield* Ref.get(state),
728 };
729 }).pipe(
730 Effect.provide(
731 ProviderReplayHarness.layerWithRegistry(
732 { name: "selection-restart-stop" },
733 ProviderAdapterRegistry.layerSingle(adapter),
734 ),
735 ),
736 );
737
738 assert.isNull(result.captured.activeTurn);
739 assert.lengthOf(result.captured.started, 1);
740 assert.equal(result.projection.runs[0]?.status, "interrupted");
741 assert.deepEqual(
742 result.projection.providerTurns.map((turn) => turn.status),
743 ["interrupted"],
744 );
745 }),
746 );
747
748 it.live(
749 "restarts the live session on a model change when a newer stopped session record exists",
750 () =>
751 Effect.gen(function* () {
752 const name = "selection-restart-dead-stopped";
753 const cwd = yield* checkpointWorkspace(name);
754 const threadId = ThreadId.make(`thread:${name}`);
755 const state = yield* Ref.make<RestartAdapterState>({
756 activeTurn: null,
757 opened: [],
758 started: [],
759 closedSessionCount: 0,
760 failedReplacementOpen: true,
761 });
762 const layerRegistry = ProviderAdapterRegistry.layerSingle(
763 makeRestartAdapter(state, exclusiveCapabilities),
764 );
765
766 const result = yield* Effect.gen(function* () {
767 const orchestrator = yield* Orchestrator.OrchestratorV2;
768 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
769 const eventSink = yield* EventSink.EventSinkV2;
770 const dispatch = (step: string, modelSelection: ModelSelection) =>
771 Effect.gen(function* () {
772 const terminal = yield* orchestrator.streamDomainEvents.pipe(
773 Stream.filter(
774 (event) => event.type === "run.updated" && event.payload.status === "completed",
775 ),
776 Stream.take(1),
777 Stream.runDrain,
778 Effect.forkScoped,
779 );
780 yield* orchestrator.dispatch({
781 type: "message.dispatch",
782 createdBy: "user",
783 creationSource: "web",
784 commandId: CommandId.make(`${name}:${step}`),
785 threadId,
786 messageId: MessageId.make(`${name}:${step}`),
787 text: step,
788 attachments: [],
789 modelSelection,
790 dispatchMode: { type: "start_immediately" },
791 });
792 yield* worker.drain();
793 yield* Fiber.join(terminal);
794 yield* worker.drain();
795 return yield* orchestrator.getThreadProjection(threadId);
796 });
797 yield* orchestrator.dispatch({
798 type: "thread.create",
799 createdBy: "user",
800 creationSource: "web",
801 commandId: CommandId.make(`${name}:create`),
802 threadId,
803 projectId: ProjectId.make(`project:${name}`),
804 title: name,
805 modelSelection: seedSelection,
806 runtimeMode: "full-access",
807 interactionMode: "default",
808 branch: null,
809 worktreePath: cwd,
810 });
811 const first = yield* dispatch("first", seedSelection);
812 const liveSession = first.providerSessions.find(
813 (session) => session.status !== "stopped" && session.status !== "error",
814 );
815 assert.isDefined(liveSession);
816
817 const deadAt = yield* DateTime.now;
818 const deadSession: OrchestrationV2ProviderSession = {
819 id: ProviderSessionId.make(`session:${name}:dead`),
820 driver,
821 providerInstanceId,
822 status: "ready",
823 cwd,
824 model: seedSelection.model,
825 capabilities: exclusiveCapabilities,
826 createdAt: deadAt,
827 updatedAt: deadAt,
828 lastError: null,
829 };
830 yield* eventSink.write({
831 events: [
832 {
833 id: EventId.make(`event:${name}:dead-attached`),
834 type: "provider-session.attached",
835 threadId,
836 driver,
837 providerInstanceId,
838 occurredAt: deadAt,
839 payload: deadSession,
840 },
841 {
842 id: EventId.make(`event:${name}:dead-updated`),
843 type: "provider-session.updated",
844 threadId,
845 driver,
846 providerInstanceId,
847 occurredAt: deadAt,
848 payload: {
849 ...deadSession,
850 status: "stopped",
851 updatedAt: deadAt,
852 lastError: null,
853 },
854 },
855 ],
856 });
857
858 const switchCommandId = CommandId.make(`${name}:switch`);
859 yield* orchestrator.dispatch({
860 type: "thread.model-selection.set",
861 commandId: switchCommandId,
862 threadId,
863 modelSelection: replacementSelection,
864 });
865 yield* worker.drain();
866 const storedSwitchEvents = yield* eventSink
867 .readByCommandId({ commandId: switchCommandId })
868 .pipe(Stream.runCollect);
869 const detachedSessionIds = [...storedSwitchEvents].flatMap((stored) =>
870 stored.event.type === "provider-session.detached"
871 ? [stored.event.payload.providerSessionId]
872 : [],
873 );
874
875 const second = yield* dispatch("second", replacementSelection);
876 return {
877 projection: second,
878 captured: yield* Ref.get(state),
879 liveSessionId: liveSession.id,
880 detachedSessionIds,
881 };
882 }).pipe(Effect.provide(ProviderReplayHarness.layerWithRegistry({ name }, layerRegistry)));
883
884 const { projection, captured } = result;
885 assert.lengthOf(projection.runs, 2);
886 assert.equal(projection.runs[1]?.modelSelection.model, replacementSelection.model);
887 assert.deepEqual(modelSwitches(projection), [
888 { from: seedSelection, to: replacementSelection },
889 ]);
890 assert.deepEqual(result.detachedSessionIds, [result.liveSessionId]);
891 assert.equal(captured.closedSessionCount, 1);
892 assert.deepEqual(
893 captured.opened.map((open) => open.model),
894 [seedSelection.model, replacementSelection.model],
895 );
896 assert.deepEqual(
897 captured.started.map((turn) => turn.model),
898 [seedSelection.model, replacementSelection.model],
899 );
900 const servingSession = projection.providerSessions.find(
901 (session) =>
902 session.id ===
903 projection.providerThreads.find(
904 (providerThread) => providerThread.id === projection.thread.activeProviderThreadId,
905 )?.providerSessionId,
906 );
907 assert.equal(servingSession?.model, replacementSelection.model);
908 }),
909 );
910
911 const accountSwitchModes = [
912 "active",
913 "idle",
914 "selection-command",
915 "pooled",
916 "separate-home",
917 "active-separate-home",
918 ] as const;
919
920 it.live.each(accountSwitchModes)(
921 "preserves native history only for compatible account switches (%s)",
922 (mode) =>
923 Effect.gen(function* () {
924 const name = `shared-home-${mode}`;
925 const active = mode === "active" || mode === "active-separate-home";
926 const separateHome = mode === "separate-home" || mode === "active-separate-home";
927 const cwd = yield* checkpointWorkspace(name);
928 const threadId = ThreadId.make(`thread:${name}`);
929 const targetId = ProviderInstanceId.make("codex-shadow-account");
930 const state = yield* Ref.make<RestartAdapterState>({
931 activeTurn: null,
932 opened: [],
933 started: [],
934 closedSessionCount: 0,
935 failedReplacementOpen: true,
936 });
937 const resumes: Array<{
938 instanceId: ProviderInstanceId;
939 nativeId: string | null | undefined;
940 }> = [];
941 const messages: string[] = [];
942 const capabilities = mode === "pooled" ? pooledCapabilities : exclusiveCapabilities;
943 const adapters = [providerInstanceId, targetId].map((instanceId) => {
944 const base = makeRestartAdapter(state, capabilities, instanceId);
945 return {
946 ...base,
947 openSession: (input) =>
948 base.openSession(input).pipe(
949 Effect.map((session) => ({
950 ...session,
951 resumeThread: (input) =>
952 Effect.gen(function* () {
953 resumes.push({
954 instanceId,
955 nativeId: input.providerThread.nativeThreadRef?.nativeId,
956 });
957 return yield* session.resumeThread(input);
958 }),
959 startTurn: (input) =>
960 Effect.gen(function* () {
961 messages.push(input.message.text);
962 yield* session.startTurn(input);
963 }),
964 })),
965 ),
966 } satisfies ProviderAdapter.ProviderAdapterV2["Service"];
967 });
968 const layerRegistry = Layer.succeed(ProviderAdapterRegistry.ProviderAdapterRegistryV2, {
969 get: (instanceId) =>
970 Effect.succeed(adapters.find((adapter) => adapter.instanceId === instanceId)!),
971 list: () => Effect.succeed([providerInstanceId, targetId]),
972 getMetadata: (instanceId) =>
973 Effect.succeed({
974 driver,
975 continuationKey: separateHome ? `codex:home:/${instanceId}` : "codex:home:/shared",
976 enabled: true,
977 capabilities,
978 }),
979 });
980 yield* Effect.gen(function* () {
981 const orchestrator = yield* Orchestrator.OrchestratorV2;
982 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
983 const selection = {
984 ...initialSelection,
985 model: active ? initialSelection.model : "complete",
986 };
987 yield* orchestrator.dispatch({
988 type: "thread.create",
989 commandId: CommandId.make(`${name}:create`),
990 threadId,
991 projectId: ProjectId.make(`project:${name}`),
992 title: name,
993 modelSelection: selection,
994 runtimeMode: "full-access",
995 interactionMode: "default",
996 branch: null,
997 worktreePath: cwd,
998 createdBy: "user",
999 creationSource: "web",
1000 });
1001 const dispatch = (step: string, modelSelection: ModelSelection, targetRunId?: RunId) =>
1002 Effect.gen(function* () {
1003 const terminal = yield* orchestrator.streamDomainEvents.pipe(
1004 Stream.filter((event) =>
1005 active && step === "first"
1006 ? event.type === "provider-turn.updated" && event.payload.status === "running"
1007 : event.type === "run.updated" && event.payload.status === "completed",
1008 ),
1009 Stream.take(1),
1010 Stream.runDrain,
1011 Effect.forkScoped,
1012 );
1013 yield* orchestrator.dispatch({
1014 type: "message.dispatch",
1015 commandId: CommandId.make(`${name}:${step}`),
1016 threadId,
1017 messageId: MessageId.make(`${name}:${step}`),
1018 text: step,
1019 attachments: [],
1020 modelSelection,
1021 dispatchMode:
1022 targetRunId === undefined
1023 ? { type: "start_immediately" }
1024 : { type: "restart_active", targetRunId },
1025 createdBy: "user",
1026 creationSource: "web",
1027 });
1028 yield* worker.drain();
1029 yield* Fiber.join(terminal);
1030 yield* worker.drain();
1031 return yield* orchestrator.getThreadProjection(threadId);
1032 });
1033 const first = yield* dispatch("first", selection);
1034 const originalThread = first.providerThreads.find(
1035 (thread) => thread.id === first.thread.activeProviderThreadId,
1036 )!;
1037 const targetSelection = { instanceId: targetId, model: "complete" };
1038 if (mode === "selection-command") {
1039 yield* orchestrator.dispatch({
1040 type: "provider.switch",
1041 commandId: CommandId.make(`${name}:switch`),
1042 threadId,
1043 modelSelection: targetSelection,
1044 });
1045 yield* worker.drain();
1046 }
1047 const second = yield* dispatch(
1048 "second",
1049 targetSelection,
1050 active ? first.runs[0]!.id : undefined,
1051 );
1052 const targetThread = second.providerThreads.find(
1053 (thread) => thread.id === second.thread.activeProviderThreadId,
1054 )!;
1055 if (separateHome) {
1056 assert.notEqual(targetThread.id, originalThread.id);
1057 assert.lengthOf(second.contextHandoffs, 1);
1058 assert.deepEqual(
1059 second.turnItems.flatMap((item) =>
1060 item.type === "handoff" ? [item.contextHandoffId] : [],
1061 ),
1062 [second.contextHandoffs[0]!.id],
1063 );
1064 assert.lengthOf(second.contextTransfers, 1);
1065 assert.isEmpty(resumes);
1066 assert.notEqual(messages[1], "second");
1067 return;
1068 }
1069 assert.equal(targetThread.id, originalThread.id);
1070 assert.deepEqual(targetThread.nativeThreadRef, originalThread.nativeThreadRef);
1071 assert.equal(targetThread.providerInstanceId, targetId);
1072 assert.notEqual(targetThread.providerSessionId, originalThread.providerSessionId);
1073 assert.isEmpty(second.contextHandoffs);
1074 assert.isEmpty(second.contextTransfers);
1075 assert.deepEqual(resumes, [
1076 { instanceId: targetId, nativeId: originalThread.nativeThreadRef?.nativeId },
1077 ]);
1078 assert.deepEqual(messages, ["first", "second"]);
1079 assert.equal((yield* Ref.get(state)).closedSessionCount, mode === "pooled" ? 0 : 1);
1080 if (mode === "pooled") {
1081 assert.isFalse(
1082 second.providerSessions.some(
1083 (session) => session.id === originalThread.providerSessionId,
1084 ),
1085 );
1086 }
1087 const third = yield* dispatch("third", { ...selection, model: "complete" });
1088 const returnedThread = third.providerThreads.find(
1089 (thread) => thread.id === third.thread.activeProviderThreadId,
1090 )!;
1091 assert.equal(returnedThread.id, originalThread.id);
1092 assert.deepEqual(returnedThread.nativeThreadRef, originalThread.nativeThreadRef);
1093 assert.equal(returnedThread.providerInstanceId, providerInstanceId);
1094 assert.isEmpty(third.contextHandoffs);
1095 assert.deepEqual(messages, ["first", "second", "third"]);
1096 }).pipe(Effect.provide(ProviderReplayHarness.layerWithRegistry({ name }, layerRegistry)));
1097 }),
1098 );