@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 6,174 lines 256 KB
Raw
1 import { vi } from "vite-plus/test";
2 import { historyResponseItems } from "@t3tools/provider-core/server/handoffBudget";
3 import { assert, describe, it } from "@effect/vitest";
4 import {
5 CommandId,
6 type ChatAttachment,
7 EventId,
8 MessageId,
9 type ModelSelection,
10 type OrchestrationV2Command,
11 type OrchestrationV2DomainEvent,
12 type OrchestrationV2ProviderCapabilities,
13 type OrchestrationV2ProviderSession,
14 type OrchestrationV2ProviderThread,
15 type OrchestrationV2ProviderTurnTokenUsage,
16 type OrchestrationV2ThreadProjection,
17 ProjectId,
18 ProviderInstanceId,
19 ProviderSessionId,
20 ProviderThreadId,
21 ProviderTurnId,
22 ScheduledTaskId,
23 ThreadId,
24 TurnItemId,
25 ProviderDriverKind,
26 } from "@t3tools/contracts";
27 import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
28 import * as DateTime from "effect/DateTime";
29 import * as Deferred from "effect/Deferred";
30 import * as Effect from "effect/Effect";
31 import * as Fiber from "effect/Fiber";
32 import * as Layer from "effect/Layer";
33 import * as PubSub from "effect/PubSub";
34 import * as Ref from "effect/Ref";
35 import * as Stream from "effect/Stream";
36 import * as Schema from "effect/Schema";
37 import * as SqlClient from "effect/sql/SqlClient";
38
39 import * as SqlitePersistence from "../../persistence/Sqlite.ts";
40 import { CommandPolicyCapabilityUnsupportedError } from "../CommandPolicy.ts";
41 import {
42 ClaudeAgentSdkQueryRunnerError,
43 ClaudeAppendSystemPromptFileError,
44 ClaudeBackgroundWorkBlocksQueryReplacementError,
45 ClaudeProviderCapabilitiesV2,
46 } from "../Adapters/ClaudeAdapterV2.ts";
47 import {
48 CodexProviderCapabilitiesV2,
49 canReuseCodexContextUsage,
50 } from "../Adapters/CodexAdapterV2.ts";
51 import { AcpProviderCapabilitiesV2 } from "@t3tools/provider-acp/server/adapter";
52 import * as EventSink from "../EventSink.ts";
53 import * as EventStore from "../EventStore.ts";
54 import * as LegacyV1ThreadImporter from "../legacy/LegacyV1ThreadImporter.ts";
55 import * as Orchestrator from "../Orchestrator.ts";
56 import * as EffectWorker from "../EffectWorker.ts";
57 import * as EffectOutbox from "../EffectOutbox.ts";
58 import * as ProjectionReplay from "./ProjectionReplay.ts";
59 import * as ProjectionStore from "../ProjectionStore.ts";
60 import * as ProviderAdapterRegistry from "../ProviderAdapterRegistry.ts";
61 import * as ProviderSessionManager from "../ProviderSessionManager.ts";
62 import { makeProviderFailure } from "@t3tools/provider-core/server/failure";
63 import {
64 CLAUDE_MODEL_SELECTION,
65 CODEX_MODEL_SELECTION,
66 GROK_MODEL_SELECTION,
67 } from "./fixtures/shared.ts";
68 import * as ProviderReplayHarness from "./ProviderReplayHarness.ts";
69 import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
70 import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
71
72 const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
73
74 const threadId = ThreadId.make("thread:provider-switch");
75 const projectId = ProjectId.make("project:provider-switch");
76 const CODEX_DRIVER = ProviderDriverKind.make("codex");
77 const CLAUDE_DRIVER = ProviderDriverKind.make("claudeAgent");
78 const GROK_DRIVER = ProviderDriverKind.make("acp");
79
80 interface CapturedTurn {
81 readonly driver: ProviderDriverKind;
82 readonly threadId: ThreadId;
83 readonly providerThreadId: ProviderThreadId;
84 readonly text: string;
85 readonly modelSelection: ModelSelection;
86 readonly attachments: ReadonlyArray<ChatAttachment>;
87 readonly nativeThreadId?: string | null;
88 readonly providerTurnOrdinal?: number;
89 readonly nativeThreadHasTurns?: boolean;
90 }
91
92 function unimplemented(driver: ProviderDriverKind, detail: string) {
93 return Effect.fail(new ProviderAdapter.ProviderAdapterProtocolError({ driver, detail }));
94 }
95
96 function makeTestAdapter(input: {
97 readonly instanceId: ProviderInstanceId;
98 readonly driver: ProviderDriverKind;
99 readonly capabilities: OrchestrationV2ProviderCapabilities;
100 readonly modelSelection: ModelSelection;
101 readonly responseByRunOrdinal: Readonly<Record<number, string>>;
102 readonly responseByThreadId?: Readonly<Record<string, Readonly<Record<number, string>>>>;
103 readonly capturedTurns: Ref.Ref<ReadonlyArray<CapturedTurn>>;
104 readonly injectedHistory?: Ref.Ref<ReadonlyArray<unknown>>;
105 readonly failStartOnce?: Ref.Ref<boolean>;
106 readonly refuseStarts?: Ref.Ref<number>;
107 readonly refusalCause?: unknown;
108 readonly failInjectionOnce?: Ref.Ref<boolean>;
109 readonly nativeThreadGeneration?: Ref.Ref<number>;
110 readonly failResume?: boolean;
111 readonly failResumeOnce?: Ref.Ref<boolean>;
112 readonly initialContextUsage?: OrchestrationV2ProviderThread["contextUsage"];
113 readonly getModelContextWindow?: (selection: ModelSelection) => number | undefined;
114 readonly canReuseContextUsage?: ProviderAdapter.ProviderAdapterV2SessionRuntime["canReuseContextUsage"];
115 readonly tokenUsageByRunOrdinal?: Readonly<
116 Record<number, Omit<OrchestrationV2ProviderTurnTokenUsage, "updatedAt">>
117 >;
118 readonly failure?: ReturnType<typeof makeProviderFailure>;
119 readonly failedRunOrdinals?: ReadonlySet<number>;
120 readonly brokenRunOrdinals?: ReadonlySet<number>;
121 readonly interruptedRunOrdinals?: ReadonlySet<number>;
122 readonly cancelledRunOrdinals?: ReadonlySet<number>;
123 readonly holdRunOrdinal?: number;
124 readonly pendingBackgroundWork?: Ref.Ref<boolean>;
125 readonly subagentOnlyBackgroundWork?: boolean;
126 readonly holdFirstTurn?: Deferred.Deferred<void>;
127 readonly releaseFirstTurn?: Deferred.Deferred<void>;
128 readonly holdOpenSession?: Deferred.Deferred<void>;
129 /** A tool call the held turn reports running before it holds. */
130 readonly heldRunningToolCall?: {
131 readonly id: TurnItemId;
132 readonly toolName: string;
133 };
134 readonly backgroundTasksEndWithProcess?: true;
135 readonly backgroundWork?: Ref.Ref<boolean>;
136 readonly closedSessions?: Ref.Ref<ReadonlyArray<string>>;
137 /** Like ACP: opening resumes the initial native id, and forcing a new thread restarts the runtime. */
138 readonly runtimeStarts?: Ref.Ref<ReadonlyArray<string>>;
139 // ACP cannot truncate, so its rollback answers with a fresh, empty native session.
140 readonly rollback?: "truncates" | "replaces_native_thread";
141 readonly beforeForcedNativeThread?: Effect.Effect<void>;
142 }): ProviderAdapter.ProviderAdapterV2["Service"] {
143 return {
144 instanceId: input.instanceId,
145 driver: input.driver,
146 getCapabilities: () => Effect.succeed(input.capabilities),
147 planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }),
148 openSession: (sessionInput) =>
149 Effect.gen(function* () {
150 if (input.holdOpenSession !== undefined) yield* Deferred.await(input.holdOpenSession);
151 const events = yield* PubSub.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
152 if (input.runtimeStarts !== undefined) {
153 yield* Ref.update(input.runtimeStarts, (starts) => [
154 ...starts,
155 sessionInput.initialNativeThreadId ?? "new",
156 ]);
157 }
158 if (input.closedSessions !== undefined) {
159 const closedSessions = input.closedSessions;
160 yield* Effect.addFinalizer(() =>
161 Ref.update(closedSessions, (ids) => [...ids, sessionInput.providerSessionId]),
162 );
163 }
164 const now = yield* DateTime.now;
165 const providerSession: OrchestrationV2ProviderSession = {
166 id: sessionInput.providerSessionId,
167 driver: input.driver,
168 providerInstanceId: input.instanceId,
169 status: "ready",
170 cwd: sessionInput.runtimePolicy.cwd ?? process.cwd(),
171 model: input.modelSelection.model,
172 capabilities: input.capabilities,
173 createdAt: now,
174 updatedAt: now,
175 lastError: null,
176 };
177
178 const runtime: ProviderAdapter.ProviderAdapterV2SessionRuntime = {
179 instanceId: input.instanceId,
180 driver: input.driver,
181 providerSessionId: sessionInput.providerSessionId,
182 providerSession,
183 events: Stream.fromPubSub(events),
184 ...(input.backgroundTasksEndWithProcess === undefined
185 ? {}
186 : { backgroundTasksEndWithProcess: input.backgroundTasksEndWithProcess }),
187 ...(input.pendingBackgroundWork === undefined
188 ? {}
189 : {
190 hasPendingBackgroundWorkForThread: (_thread, options) =>
191 input.subagentOnlyBackgroundWork && options?.includeSubagents !== true
192 ? Effect.succeed(false)
193 : Ref.get(input.pendingBackgroundWork!),
194 }),
195 ...(input.getModelContextWindow === undefined
196 ? {}
197 : { getModelContextWindow: input.getModelContextWindow }),
198 ...(input.canReuseContextUsage === undefined
199 ? {}
200 : { canReuseContextUsage: input.canReuseContextUsage }),
201 ...(input.backgroundWork === undefined
202 ? {}
203 : {
204 // Like Claude: native subagents are visible only to the session-wide
205 // probe, never to the per-thread roster probe.
206 hasPendingBackgroundWork: Ref.get(input.backgroundWork),
207 hasPendingBackgroundWorkForThread: () => Effect.succeed(false),
208 }),
209 ensureThread: (threadInput) =>
210 Effect.gen(function* () {
211 if (threadInput.forceNewNativeThread && input.runtimeStarts !== undefined) {
212 yield* Ref.update(input.runtimeStarts, (starts) => [...starts, "replacement"]);
213 }
214 if (threadInput.forceNewNativeThread && input.beforeForcedNativeThread !== undefined)
215 yield* input.beforeForcedNativeThread;
216 const createdAt = yield* DateTime.now;
217 const generation =
218 input.nativeThreadGeneration === undefined
219 ? ""
220 : `:${yield* Ref.getAndUpdate(input.nativeThreadGeneration, (value) => value + 1)}`;
221 const nativeThreadId = `${input.driver}:${threadInput.threadId}${generation}`;
222 return {
223 id: ProviderThreadId.make(`provider-thread:${nativeThreadId}`),
224 driver: input.driver,
225 providerInstanceId: input.instanceId,
226 providerSessionId: sessionInput.providerSessionId,
227 appThreadId: threadInput.threadId,
228 ownerNodeId: null,
229 nativeThreadRef: {
230 driver: input.driver,
231 nativeId: nativeThreadId,
232 strength: "strong",
233 },
234 nativeConversationHeadRef: null,
235 status: "idle",
236 firstRunOrdinal: null,
237 lastRunOrdinal: null,
238 handoffIds: [],
239 contextUsage: input.initialContextUsage ?? null,
240 forkedFrom: null,
241 createdAt,
242 updatedAt: createdAt,
243 } satisfies OrchestrationV2ProviderThread;
244 }),
245 resumeThread: ({ providerThread }) =>
246 Effect.gen(function* () {
247 if (
248 (input.brokenRunOrdinals !== undefined && providerThread.status === "error") ||
249 input.failResume ||
250 (input.failResumeOnce !== undefined &&
251 (yield* Ref.getAndSet(input.failResumeOnce, false)))
252 )
253 return yield* unimplemented(input.driver, "simulated native resume failure");
254 return providerThread;
255 }),
256 ...(input.injectedHistory === undefined
257 ? {}
258 : {
259 injectHistory: (history: ProviderAdapter.ProviderAdapterV2HistoricalContext) =>
260 Ref.update(input.injectedHistory!, (current) => [
261 ...current,
262 ...historyResponseItems(history.messages, history.context),
263 ]).pipe(
264 Effect.andThen(
265 Effect.gen(function* () {
266 if (
267 input.failInjectionOnce !== undefined &&
268 (yield* Ref.getAndSet(input.failInjectionOnce, false))
269 )
270 return yield* unimplemented(
271 input.driver,
272 "lost injection acknowledgement",
273 );
274 return true;
275 }),
276 ),
277 ),
278 }),
279 compactThread: (turnInput) => runtime.startTurn(turnInput),
280 startTurn: (turnInput) =>
281 Effect.gen(function* () {
282 if (
283 input.failStartOnce !== undefined &&
284 (yield* Ref.getAndSet(input.failStartOnce, false))
285 )
286 return yield* unimplemented(input.driver, "turn start failed after injection");
287 if (
288 input.refuseStarts !== undefined &&
289 (yield* Ref.getAndUpdate(input.refuseStarts, (left) => Math.max(0, left - 1))) > 0
290 )
291 return yield* new ProviderAdapter.ProviderAdapterTurnStartError({
292 driver: input.driver,
293 threadId: turnInput.threadId,
294 providerThreadId: turnInput.providerThread.id,
295 runId: turnInput.runId,
296 cause:
297 input.refusalCause ?? new ClaudeBackgroundWorkBlocksQueryReplacementError(),
298 });
299 yield* Effect.yieldNow;
300 yield* Ref.update(input.capturedTurns, (turns) => [
301 ...turns,
302 {
303 driver: input.driver,
304 threadId: turnInput.threadId,
305 providerThreadId: turnInput.providerThread.id,
306 text: turnInput.message.text,
307 modelSelection: turnInput.modelSelection,
308 attachments: turnInput.message.attachments,
309 nativeThreadId: turnInput.providerThread.nativeThreadRef?.nativeId ?? null,
310 providerTurnOrdinal: turnInput.providerTurnOrdinal,
311 ...(turnInput.nativeThreadHasTurns === undefined
312 ? {}
313 : { nativeThreadHasTurns: turnInput.nativeThreadHasTurns }),
314 },
315 ]);
316 const providerTurnId = ProviderTurnId.make(
317 `provider-turn:${input.driver}:${turnInput.threadId}:${turnInput.runOrdinal}`,
318 );
319 if (
320 turnInput.runOrdinal === (input.holdRunOrdinal ?? 1) &&
321 input.holdFirstTurn !== undefined
322 ) {
323 if (input.heldRunningToolCall !== undefined) {
324 const startedAt = yield* DateTime.now;
325 yield* PubSub.publish(events, {
326 type: "provider_turn.updated",
327 driver: input.driver,
328 providerTurn: {
329 id: providerTurnId,
330 providerThreadId: turnInput.providerThread.id,
331 nodeId: turnInput.rootNodeId,
332 runAttemptId: turnInput.attemptId,
333 nativeTurnRef: null,
334 ordinal: turnInput.runOrdinal,
335 status: "running",
336 startedAt,
337 completedAt: null,
338 },
339 });
340 yield* PubSub.publish(events, {
341 type: "turn_item.updated",
342 driver: input.driver,
343 turnItem: {
344 id: input.heldRunningToolCall.id,
345 threadId: turnInput.threadId,
346 runId: turnInput.runId,
347 nodeId: turnInput.rootNodeId,
348 providerThreadId: turnInput.providerThread.id,
349 providerTurnId,
350 nativeItemRef: null,
351 parentItemId: null,
352 ordinal: turnInput.runOrdinal * 100 + 1,
353 status: "running",
354 title: null,
355 startedAt,
356 completedAt: null,
357 updatedAt: startedAt,
358 type: "dynamic_tool",
359 toolName: input.heldRunningToolCall.toolName,
360 input: {},
361 },
362 });
363 }
364 yield* Deferred.succeed(input.holdFirstTurn, undefined);
365 if (input.releaseFirstTurn === undefined) return;
366 yield* Deferred.await(input.releaseFirstTurn);
367 }
368 const eventTime = yield* DateTime.now;
369 const terminalStatus = input.failedRunOrdinals?.has(turnInput.runOrdinal)
370 ? "failed"
371 : input.interruptedRunOrdinals?.has(turnInput.runOrdinal)
372 ? "interrupted"
373 : input.cancelledRunOrdinals?.has(turnInput.runOrdinal)
374 ? "cancelled"
375 : "completed";
376 const response =
377 input.responseByThreadId?.[turnInput.threadId]?.[turnInput.runOrdinal] ??
378 input.responseByRunOrdinal[turnInput.runOrdinal] ??
379 `${input.driver} response for run ${turnInput.runOrdinal}`;
380 const providerEvents: ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event> = [
381 {
382 type: "provider_turn.updated",
383 driver: input.driver,
384 providerTurn: {
385 id: providerTurnId,
386 providerThreadId: turnInput.providerThread.id,
387 nodeId: turnInput.rootNodeId,
388 runAttemptId: turnInput.attemptId,
389 nativeTurnRef: {
390 driver: input.driver,
391 nativeId: `native-turn:${turnInput.threadId}:${turnInput.runOrdinal}`,
392 strength: "strong",
393 },
394 ordinal: turnInput.runOrdinal,
395 status: terminalStatus,
396 startedAt: eventTime,
397 completedAt: eventTime,
398 },
399 },
400 {
401 type: "turn_item.updated",
402 driver: input.driver,
403 turnItem: {
404 id: TurnItemId.make(
405 `turn-item:${input.driver}:${turnInput.threadId}:${turnInput.runOrdinal}:assistant`,
406 ),
407 threadId: turnInput.threadId,
408 runId: turnInput.runId,
409 nodeId: turnInput.rootNodeId,
410 providerThreadId: turnInput.providerThread.id,
411 providerTurnId,
412 nativeItemRef: null,
413 parentItemId: null,
414 ordinal: turnInput.runOrdinal * 100 + 1,
415 status: terminalStatus,
416 title: null,
417 startedAt: eventTime,
418 completedAt: eventTime,
419 updatedAt: eventTime,
420 type: "assistant_message",
421 messageId: MessageId.make(
422 `message:${input.driver}:${turnInput.threadId}:${turnInput.runOrdinal}:assistant`,
423 ),
424 text: response,
425 streaming: false,
426 },
427 },
428 {
429 type: "turn.terminal",
430 driver: input.driver,
431 providerThreadId: turnInput.providerThread.id,
432 providerTurnId,
433 runOrdinal: turnInput.runOrdinal,
434 ...(terminalStatus === "failed"
435 ? {
436 status: terminalStatus,
437 failureItemOrdinal: turnInput.runOrdinal * 100 + 2,
438 failure:
439 input.failure ??
440 makeProviderFailure({
441 message: "Simulated provider failure.",
442 code: "simulated_failure",
443 class: "provider_error",
444 }),
445 }
446 : { status: terminalStatus, failure: null }),
447 threadDisposition: input.brokenRunOrdinals?.has(turnInput.runOrdinal)
448 ? "broken"
449 : "reusable",
450 },
451 ];
452 const reportedUsage = input.tokenUsageByRunOrdinal?.[turnInput.runOrdinal];
453 const turnEvent = providerEvents[0];
454 if (reportedUsage && turnEvent?.type === "provider_turn.updated") {
455 yield* PubSub.publish(events, {
456 ...turnEvent,
457 providerTurn: {
458 ...turnEvent.providerTurn,
459 status: "running",
460 completedAt: null,
461 tokenUsage: { ...reportedUsage, updatedAt: DateTime.formatIso(eventTime) },
462 },
463 });
464 }
465 if (input.brokenRunOrdinals?.has(turnInput.runOrdinal)) {
466 yield* PubSub.publish(events, {
467 type: "provider_thread.updated",
468 driver: input.driver,
469 providerThread: {
470 ...turnInput.providerThread,
471 status: "error",
472 updatedAt: eventTime,
473 },
474 });
475 }
476 for (const event of providerEvents) {
477 yield* PubSub.publish(events, event);
478 }
479 }),
480 steerTurn: () => Effect.void,
481 interruptTurn: () => Effect.void,
482 respondToRuntimeRequest: () => Effect.void,
483 readThreadSnapshot: () =>
484 unimplemented(input.driver, "readThreadSnapshot unused in provider switch test"),
485 rollbackThread: ({ providerThread }) =>
486 input.rollback === undefined
487 ? unimplemented(input.driver, "rollbackThread unused in provider switch test")
488 : Effect.succeed({
489 providerThread:
490 input.rollback === "truncates" || providerThread.nativeThreadRef === null
491 ? providerThread
492 : {
493 ...providerThread,
494 nativeThreadRef: {
495 ...providerThread.nativeThreadRef,
496 nativeId: `${providerThread.nativeThreadRef.nativeId}:after-rollback`,
497 },
498 nativeConversationHeadRef: null,
499 },
500 providerTurns: [],
501 messages: [],
502 runtimeRequests: [],
503 }),
504 forkThread: () =>
505 unimplemented(input.driver, "forkThread unused in provider switch test"),
506 };
507 return runtime;
508 }),
509 };
510 }
511
512 const waitForIdle = Effect.fn("ProviderSwitchTest.waitForIdle")(function* (
513 targetThreadId: ThreadId,
514 ) {
515 const orchestrator = yield* Orchestrator.OrchestratorV2;
516 const eventSink = yield* EventSink.EventSinkV2;
517 const isIdle = (projection: OrchestrationV2ThreadProjection) =>
518 projection.runs.every(
519 (run) => !["queued", "starting", "running", "waiting"].includes(run.status),
520 );
521 const afterSequence = yield* eventSink.latestSequence();
522 const current = yield* orchestrator.getThreadProjection(targetThreadId);
523 if (isIdle(current)) {
524 return current;
525 }
526 // Projections commit with their events, so each later event is the only time to re-read.
527 const [idle] = yield* eventSink.stream({ afterSequence }).pipe(
528 Stream.mapEffect(() => orchestrator.getThreadProjection(targetThreadId)),
529 Stream.filter(isIdle),
530 Stream.take(1),
531 Stream.runCollect,
532 );
533 if (idle === undefined) {
534 return yield* Effect.die(
535 new Error("Provider switch test event stream ended before the thread went idle"),
536 );
537 }
538 return idle;
539 });
540
541 // Same tag as the adapter's unexported error raised when Claude never answers setMcpServers.
542 class ClaudeMcpRegistrationTimeoutError extends Schema.TaggedError<ClaudeMcpRegistrationTimeoutError>()(
543 "ClaudeMcpRegistrationTimeoutError",
544 {},
545 ) {}
546
547 describe("orchestration v2 provider switching", () => {
548 it.live.each([
549 "compact-native",
550 "compact-legacy",
551 "large-current-input",
552 "screenshot-fallback",
553 "screenshot-reported-capacity-native",
554 "screenshot-option-change-retry-native",
555 "screenshot-exhausted-target-native",
556 "screenshot-exhausted-target-fallback",
557 "screenshot-exhausted-target-estimate-native",
558 "screenshot-exhausted-target-oversized-native",
559 "screenshot-exhausted-target-tight-native",
560 "screenshot-exhausted-target-background-native",
561 "screenshot-exhausted-target-subagent-native",
562 "prior-images-reasoning-change-replacement-small-native",
563 "imported-prior-images-fallback",
564 "prior-images-telemetry-native",
565 "prior-images-unsent-native",
566 "prior-images-legacy-replacement-native",
567 "delivery-write-failure",
568 ] as const)("preserves handoffs through %s", (scenario) =>
569 Effect.gen(function* () {
570 const cwd = yield* checkpointWorkspace(`handoff-${scenario}`);
571 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
572 const injectedHistory = yield* Ref.make<ReadonlyArray<unknown>>([]);
573 const reasoningScenario = scenario.includes("reasoning-change");
574 const turnUsageScenario = reasoningScenario || scenario.includes("turn-usage");
575 const modelScenario =
576 scenario.includes("model") || scenario.includes("option-change") || reasoningScenario;
577 const failStartOnce = yield* Ref.make(false);
578 const failResumeOnce = yield* Ref.make(false);
579 const generation = yield* Ref.make(0);
580 const pendingBackgroundWork = yield* Ref.make(false);
581 const priorImages = scenario.includes("prior-images");
582 const exhaustedTarget = scenario.includes("exhausted-target");
583 const replaceNative = scenario.includes("replacement");
584 const capacityScenario = modelScenario || scenario.includes("reported-capacity");
585 const returning =
586 scenario === "large-current-input" ||
587 scenario.includes("-change-") ||
588 priorImages ||
589 exhaustedTarget;
590 const targetSelection: ModelSelection = !modelScenario
591 ? CLAUDE_MODEL_SELECTION
592 : {
593 ...CLAUDE_MODEL_SELECTION,
594 ...(reasoningScenario
595 ? { options: [{ id: "reasoningEffort", value: "low" }] }
596 : scenario.includes("option-change")
597 ? { options: [{ id: "contextWindow", value: "1m" }] }
598 : { model: `${CLAUDE_MODEL_SELECTION.model}-large` }),
599 };
600 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
601 makeTestAdapter({
602 instanceId: CODEX_MODEL_SELECTION.instanceId,
603 driver: CODEX_DRIVER,
604 capabilities: CodexProviderCapabilitiesV2,
605 modelSelection: CODEX_MODEL_SELECTION,
606 responseByRunOrdinal: { 1: "Original partial work" },
607 capturedTurns,
608 }),
609 makeTestAdapter({
610 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
611 driver: CLAUDE_DRIVER,
612 capabilities: ClaudeProviderCapabilitiesV2,
613 modelSelection: CLAUDE_MODEL_SELECTION,
614 responseByRunOrdinal: scenario.includes("target-estimate")
615 ? { 2: "x".repeat(64_000) }
616 : {},
617 capturedTurns,
618 ...(scenario.includes("retry") || scenario.includes("unsent") ? { failStartOnce } : {}),
619 ...(replaceNative ? { failResumeOnce, nativeThreadGeneration: generation } : {}),
620 ...(exhaustedTarget ? { nativeThreadGeneration: generation } : {}),
621 ...(scenario.includes("target-background") || scenario.includes("target-subagent")
622 ? {
623 pendingBackgroundWork,
624 subagentOnlyBackgroundWork: scenario.includes("target-subagent"),
625 }
626 : {}),
627 ...(scenario.includes("reported-capacity")
628 ? { initialContextUsage: { usedTokens: 999_999, maxTokens: 1_000_000 } }
629 : {}),
630 ...(turnUsageScenario
631 ? {
632 canReuseContextUsage: canReuseCodexContextUsage,
633 tokenUsageByRunOrdinal: {
634 2: {
635 usedTokens: replaceNative ? 30_000 : 37_321,
636 maxTokens: replaceNative
637 ? scenario.includes("small")
638 ? 32_000
639 : 64_000
640 : 258_400,
641 },
642 },
643 }
644 : modelScenario
645 ? {
646 getModelContextWindow: (selection: ModelSelection) =>
647 selection.model.endsWith("-large") ||
648 selection.options?.some(
649 (option) => option.id === "contextWindow" && option.value === "1m",
650 )
651 ? 1_000_000
652 : 32_000,
653 }
654 : scenario === "large-current-input" || priorImages || exhaustedTarget
655 ? {
656 getModelContextWindow: () =>
657 scenario.includes("target-tight") ? 18_241 : 32_000,
658 }
659 : {}),
660 ...(scenario.endsWith("-native") || scenario === "large-current-input"
661 ? { injectedHistory }
662 : {}),
663 }),
664 ]);
665 yield* Effect.gen(function* () {
666 const orchestrator = yield* Orchestrator.OrchestratorV2;
667 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
668 const eventSink = yield* EventSink.EventSinkV2;
669 const screenshot: ChatAttachment = {
670 type: "image",
671 id: "screenshot",
672 name: "screenshot.png",
673 mimeType: "image/png",
674 sizeBytes: 100_000,
675 };
676 const screenshots = Array.from(
677 {
678 length:
679 scenario.includes("eight") || capacityScenario
680 ? 8
681 : scenario.includes("pair")
682 ? 2
683 : 1,
684 },
685 (_, index) => ({ ...screenshot, id: `screenshot-${index}` }),
686 );
687 const targetOrdinal = returning ? 4 : 2;
688 const dispatch = (ordinal: number, text: string, selection: ModelSelection) =>
689 orchestrator.dispatch({
690 type: "message.dispatch",
691 commandId: CommandId.make(`regression:${ordinal}`),
692 threadId,
693 messageId: MessageId.make(`regression:${ordinal}`),
694 createdBy: "user",
695 creationSource: "web",
696 text,
697 attachments:
698 turnUsageScenario && ordinal === 2
699 ? Array.from({ length: 8 }, (_, index) => ({
700 ...screenshot,
701 id: `prior-${index}`,
702 }))
703 : turnUsageScenario && ordinal >= targetOrdinal
704 ? [screenshot, { ...screenshot, id: "current-2" }]
705 : scenario.startsWith("screenshot") &&
706 ordinal >= targetOrdinal &&
707 (!exhaustedTarget || ordinal === targetOrdinal)
708 ? screenshots
709 : priorImages && ordinal === (scenario.startsWith("imported") ? 1 : 2)
710 ? [screenshot]
711 : [],
712 modelSelection: selection,
713 dispatchMode: { type: "start_immediately" },
714 });
715 const wait = (ordinal: number) =>
716 orchestrator.streamStoredEvents.pipe(
717 Stream.filter(
718 ({ event }) =>
719 event.type === "run.updated" &&
720 event.payload.ordinal === ordinal &&
721 (event.payload.status === "completed" || event.payload.status === "failed"),
722 ),
723 Stream.runHead,
724 Effect.andThen(worker.drain()),
725 );
726 yield* orchestrator.dispatch({
727 type: "thread.create",
728 commandId: CommandId.make("regression:create"),
729 threadId,
730 projectId,
731 createdBy: "user",
732 creationSource: "web",
733 title: "Handoff regression",
734 modelSelection: CODEX_MODEL_SELECTION,
735 runtimeMode: "full-access",
736 interactionMode: "default",
737 branch: null,
738 worktreePath: null,
739 });
740 yield* dispatch(1, "Original request with constraints", CODEX_MODEL_SELECTION);
741 yield* wait(1);
742 const write = eventSink.write;
743 const spy =
744 scenario === "delivery-write-failure"
745 ? vi.spyOn(eventSink, "write").mockImplementation((input) =>
746 input.events.some(
747 (event) =>
748 event.type === "context-handoff.updated" &&
749 event.payload.delivery?.status === "inline",
750 )
751 ? Effect.fail(
752 new EventSink.EventSinkWriteError({
753 eventCount: input.events.length,
754 cause: "bookkeeping unavailable",
755 }),
756 )
757 : write(input),
758 )
759 : undefined;
760 yield* Effect.addFinalizer(() => Effect.sync(() => spy?.mockRestore()));
761 const current = scenario.startsWith("compact")
762 ? "/compact"
763 : scenario.includes("target-oversized")
764 ? "x".repeat(40_000)
765 : capacityScenario && !turnUsageScenario
766 ? "x".repeat(70_000)
767 : scenario === "large-current-input"
768 ? "x".repeat(9_000)
769 : "Continue work";
770 if (returning) {
771 if (scenario.includes("unsent")) yield* Ref.set(failStartOnce, true);
772 yield* dispatch(
773 2,
774 turnUsageScenario
775 ? "x".repeat(27_460)
776 : scenario.includes("target-estimate")
777 ? "x".repeat(12_000)
778 : "Establish target",
779 CLAUDE_MODEL_SELECTION,
780 );
781 yield* wait(2);
782 if (exhaustedTarget) {
783 assert.equal(
784 (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
785 "completed",
786 );
787 }
788 if (exhaustedTarget && !scenario.includes("target-estimate")) {
789 const target = (yield* orchestrator.getThreadProjection(threadId)).providerThreads.find(
790 (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
791 )!;
792 yield* eventSink.write({
793 events: [
794 {
795 id: EventId.make("exhausted-target-usage"),
796 type: "provider-thread.updated",
797 threadId,
798 occurredAt: yield* DateTime.now,
799 payload: { ...target, contextUsage: { usedTokens: 30_000, maxTokens: 32_000 } },
800 },
801 ],
802 });
803 }
804 if (modelScenario && !turnUsageScenario) {
805 const target = (yield* orchestrator.getThreadProjection(threadId)).providerThreads.find(
806 (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
807 )!;
808 yield* eventSink.write({
809 events: [
810 {
811 id: EventId.make("old-model-usage"),
812 type: "provider-thread.updated",
813 threadId,
814 occurredAt: yield* DateTime.now,
815 payload: {
816 ...target,
817 contextUsage: {
818 usedTokens: 30_000,
819 maxTokens: 32_000,
820 autoCompactThreshold: 31_000,
821 },
822 },
823 },
824 ],
825 });
826 }
827 if (scenario.includes("legacy-replacement")) {
828 const existing = yield* orchestrator.getThreadProjection(threadId);
829 yield* eventSink.write({
830 events: existing.attempts.map(
831 ({ nativeThreadId: _nativeThreadId, ...legacy }, index) => ({
832 id: EventId.make(`legacy-attempt:${index}`),
833 type: "run-attempt.updated" as const,
834 threadId,
835 occurredAt: existing.thread.createdAt,
836 payload: legacy,
837 }),
838 ),
839 });
840 }
841 if (scenario.includes("telemetry")) {
842 const target = (yield* orchestrator.getThreadProjection(threadId)).providerThreads.find(
843 (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
844 )!;
845 yield* eventSink.write({
846 events: [
847 {
848 id: EventId.make("compacted-context-usage"),
849 type: "provider-thread.updated",
850 threadId,
851 occurredAt: yield* DateTime.now,
852 payload: { ...target, contextUsage: { usedTokens: 100, maxTokens: 32_000 } },
853 },
854 ],
855 });
856 }
857 yield* dispatch(
858 3,
859 priorImages && !replaceNative
860 ? "New source constraint " + "q".repeat(9_000)
861 : "New source constraint",
862 CODEX_MODEL_SELECTION,
863 );
864 yield* wait(3);
865 }
866 if (replaceNative) {
867 // Switching back to Codex already released Claude's session.
868 yield* Ref.set(failResumeOnce, true);
869 }
870 if (scenario.includes("retry")) yield* Ref.set(failStartOnce, true);
871 if (scenario.includes("target-background") || scenario.includes("target-subagent"))
872 yield* Ref.set(pendingBackgroundWork, true);
873 yield* dispatch(targetOrdinal, current, targetSelection);
874 yield* wait(targetOrdinal);
875 if (scenario.includes("retry")) {
876 const failed = yield* orchestrator.getThreadProjection(threadId);
877 assert.equal(failed.runs.at(-1)?.status, "failed");
878 const failedUsage = failed.providerThreads.find(
879 (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
880 )!.contextUsage;
881 if (reasoningScenario || scenario.includes("turn-usage")) {
882 assert.deepEqual(failedUsage, { usedTokens: 37_321, maxTokens: 258_400 });
883 } else {
884 assert.deepEqual(failedUsage, { usedTokens: 30_000, maxTokens: 1_000_000 });
885 }
886 if (reasoningScenario) {
887 const target = failed.providerThreads.find(
888 (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
889 )!;
890 yield* orchestrator.dispatch({
891 type: "provider-session.detach",
892 commandId: CommandId.make("detach-before-reasoning-retry"),
893 threadId,
894 providerSessionId: target.providerSessionId!,
895 });
896 yield* worker.drain();
897 }
898 yield* dispatch(targetOrdinal + 1, current, targetSelection);
899 yield* wait(targetOrdinal + 1);
900 }
901 if (reasoningScenario && replaceNative) {
902 const replaced = yield* orchestrator.getThreadProjection(threadId);
903 assert.equal(
904 replaced.runs.at(-1)?.status,
905 scenario.includes("small") ? "failed" : "completed",
906 );
907 const target = replaced.providerThreads.find(
908 (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
909 )!;
910 assert.isNull(target.contextUsage);
911 assert.equal(yield* Ref.get(generation), 2);
912 assert.equal(
913 (yield* Ref.get(capturedTurns)).at(-1)!.driver,
914 scenario.includes("small") ? CODEX_DRIVER : CLAUDE_DRIVER,
915 );
916 return;
917 }
918 if (replaceNative) {
919 if (scenario.includes("legacy-replacement")) {
920 const recovered = yield* orchestrator.getThreadProjection(threadId);
921 const handoff = recovered.contextHandoffs.at(-1)!;
922 const { history: _history, ...legacyHandoff } = handoff;
923 yield* eventSink.write({
924 events: [
925 {
926 id: EventId.make("legacy-replacement-handoff"),
927 type: "context-handoff.updated",
928 threadId,
929 occurredAt: yield* DateTime.now,
930 payload: {
931 ...legacyHandoff,
932 delivery: {
933 nativeThreadId: handoff.delivery!.nativeThreadId,
934 status: handoff.delivery!.status,
935 itemIds: [],
936 },
937 },
938 },
939 ],
940 });
941 }
942 yield* dispatch(
943 targetOrdinal + 1,
944 "Continue after native replacement",
945 CLAUDE_MODEL_SELECTION,
946 );
947 yield* wait(targetOrdinal + 1);
948 yield* dispatch(
949 targetOrdinal + 2,
950 "Another source constraint " + "q".repeat(9_000),
951 CODEX_MODEL_SELECTION,
952 );
953 yield* wait(targetOrdinal + 2);
954 yield* dispatch(targetOrdinal + 3, current, CLAUDE_MODEL_SELECTION);
955 yield* wait(targetOrdinal + 3);
956 }
957 const projection = yield* orchestrator.getThreadProjection(threadId);
958 if (
959 scenario.includes("target-oversized") ||
960 scenario.includes("target-tight") ||
961 scenario.includes("target-background") ||
962 scenario.includes("target-subagent")
963 ) {
964 assert.equal(projection.runs.at(-1)?.status, "failed");
965 assert.equal(yield* Ref.get(generation), 1);
966 assert.equal(projection.messages.at(-1)!.text, current);
967 assert.deepEqual(projection.messages.at(-1)!.attachments, screenshots);
968 if (scenario.includes("target-background") || scenario.includes("target-subagent")) {
969 assert.isTrue(yield* Ref.get(pendingBackgroundWork));
970 const failure = projection.turnItems.findLast((item) => item.type === "error");
971 assert.equal(
972 failure?.type === "error" ? failure.failure.message : null,
973 "The target provider still has background work running. Wait for it to finish or stop it before retrying the handoff.",
974 );
975 }
976 return;
977 }
978 assert.equal(projection.runs.at(-1)?.status, "completed");
979 if (exhaustedTarget) {
980 assert.equal(yield* Ref.get(generation), 2);
981 const turns = (yield* Ref.get(capturedTurns)).filter(
982 (turn) => turn.driver === CLAUDE_DRIVER,
983 );
984 assert.notEqual(turns[0]!.nativeThreadId, turns[1]!.nativeThreadId);
985 const latestRun = projection.runs.at(-1)!;
986 assert.equal(
987 projection.attempts.find((attempt) => attempt.id === latestRun.activeAttemptId)!
988 .nativeThreadId,
989 turns[1]!.nativeThreadId,
990 );
991 const context = scenario.endsWith("-native")
992 ? yield* encodeJson(yield* Ref.get(injectedHistory))
993 : turns[1]!.text;
994 assert.include(context, "Original request with constraints");
995 assert.include(context, "Original partial work");
996 assert.include(context, "New source constraint");
997 assert.include(turns[1]!.text, current);
998 assert.deepEqual(turns[1]!.attachments, screenshots);
999 const historyBeforeFollowup = yield* Ref.get(injectedHistory);
1000 yield* dispatch(5, "Continue after automatic recovery", CLAUDE_MODEL_SELECTION);
1001 yield* wait(5);
1002 assert.equal(
1003 (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
1004 "completed",
1005 );
1006 assert.equal(yield* Ref.get(generation), 2);
1007 assert.equal(
1008 (yield* Ref.get(capturedTurns)).at(-1)!.text,
1009 "Continue after automatic recovery",
1010 );
1011 assert.deepEqual(yield* Ref.get(injectedHistory), historyBeforeFollowup);
1012 if (scenario.includes("target-estimate")) {
1013 yield* dispatch(6, "Another source turn", CODEX_MODEL_SELECTION);
1014 yield* wait(6);
1015 yield* dispatch(7, "Return after recovery", CLAUDE_MODEL_SELECTION);
1016 yield* wait(7);
1017 assert.equal(
1018 (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
1019 "completed",
1020 );
1021 assert.equal(yield* Ref.get(generation), 2);
1022 }
1023 return;
1024 }
1025 if (priorImages) {
1026 const handoff = projection.contextHandoffs.at(-1)!;
1027 const context = scenario.endsWith("-native")
1028 ? yield* encodeJson(yield* Ref.get(injectedHistory))
1029 : (yield* Ref.get(capturedTurns)).at(-1)!.text;
1030 const shouldFit =
1031 turnUsageScenario ||
1032 scenario.startsWith("imported") ||
1033 scenario.includes("telemetry") ||
1034 scenario.includes("unsent") ||
1035 replaceNative;
1036 const sourceText =
1037 `${replaceNative ? "Another" : "New"} source constraint ` + "q".repeat(9_000);
1038 const sourceItem = projection.turnItems.find(
1039 (item) => item.type === "user_message" && item.text === sourceText,
1040 )!;
1041 assert.isDefined(sourceItem);
1042 if (shouldFit) {
1043 assert.include(context, sourceText);
1044 assert.include(
1045 projection.contextHandoffs
1046 .filter(
1047 (record) =>
1048 record.targetRunId ===
1049 (scenario.includes("retry")
1050 ? projection.runs.find((run) => run.ordinal === targetOrdinal)!.id
1051 : projection.runs.at(-1)!.id),
1052 )
1053 .flatMap((record) => record.delivery?.itemIds ?? []),
1054 sourceItem.id,
1055 );
1056 } else {
1057 assert.notInclude(context, "q".repeat(9_000));
1058 assert.isAbove(handoff.delivery!.omittedItemIds!.length, 0);
1059 }
1060 const latestRun = projection.runs.at(-1)!;
1061 const latestAttempt = projection.attempts.find(
1062 (attempt) => attempt.id === latestRun.activeAttemptId,
1063 )!;
1064 const target = projection.providerThreads.find(
1065 (thread) => thread.id === latestAttempt.providerThreadId,
1066 )!;
1067 assert.equal(latestAttempt.nativeThreadId, target.nativeThreadRef!.nativeId);
1068 if (turnUsageScenario) {
1069 if (replaceNative) assert.isNull(target.contextUsage);
1070 else assert.deepEqual(target.contextUsage, { usedTokens: 37_321, maxTokens: 258_400 });
1071 assert.lengthOf((yield* Ref.get(capturedTurns)).at(-1)!.attachments, 2);
1072 }
1073 if (replaceNative) assert.equal(yield* Ref.get(generation), 2);
1074 return;
1075 }
1076 const handoff = projection.contextHandoffs.at(-1)!;
1077 if (scenario.startsWith("screenshot")) {
1078 assert.deepEqual((yield* Ref.get(capturedTurns)).at(-1)!.attachments, screenshots);
1079 }
1080 if (scenario === "compact-legacy") {
1081 const { history: _history, ...legacyHandoff } = handoff;
1082 yield* eventSink.write({
1083 events: [
1084 {
1085 id: EventId.make("legacy-handoff-shape"),
1086 type: "context-handoff.updated",
1087 threadId,
1088 runId: handoff.targetRunId,
1089 occurredAt: yield* DateTime.now,
1090 payload: legacyHandoff,
1091 },
1092 ],
1093 });
1094 assert.isUndefined(handoff.delivery);
1095 yield* dispatch(3, "Continue after compact", CLAUDE_MODEL_SELECTION);
1096 yield* wait(3);
1097 assert.include(
1098 (yield* Ref.get(capturedTurns)).at(-1)!.text,
1099 "Original request with constraints",
1100 );
1101 assert.equal(
1102 (yield* orchestrator.getThreadProjection(threadId)).contextHandoffs.at(-1)?.delivery
1103 ?.status,
1104 "inline",
1105 );
1106 } else if (
1107 scenario === "delivery-write-failure" ||
1108 (scenario.startsWith("screenshot") && scenario.endsWith("fallback"))
1109 ) {
1110 assert.equal(
1111 handoff.delivery?.status,
1112 scenario.startsWith("screenshot") ? "inline" : "pending",
1113 );
1114 assert.include(
1115 (yield* Ref.get(capturedTurns)).at(-1)!.text,
1116 "Original request with constraints",
1117 );
1118 } else {
1119 assert.equal(handoff.delivery?.status, "injected");
1120 assert.equal((yield* Ref.get(capturedTurns)).at(-1)!.text, current);
1121 assert.include(
1122 yield* encodeJson(yield* Ref.get(injectedHistory)),
1123 "Original request with constraints",
1124 );
1125 }
1126 }).pipe(
1127 Effect.provide(
1128 ProviderReplayHarness.layerWithRegistry(
1129 {
1130 name: `handoff-${scenario}`,
1131 runtimePolicyOverride: {
1132 cwd,
1133 approvalPolicy: "never",
1134 sandboxPolicy: { type: "readOnly" },
1135 },
1136 },
1137 layerRegistry,
1138 ),
1139 ),
1140 );
1141 }),
1142 );
1143 it.live.each([
1144 "turn-start",
1145 "injection",
1146 "large-missed-request",
1147 "exhausted-target-retry",
1148 ] as const)(
1149 "recovers %s failure without duplicating history in the same native thread",
1150 (failure) =>
1151 Effect.gen(function* () {
1152 const cwd = yield* checkpointWorkspace(`handoff-retry-${failure}`);
1153 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1154 const injectedHistory = yield* Ref.make<ReadonlyArray<unknown>>([]);
1155 const failOnce = yield* Ref.make(true);
1156 const generation = yield* Ref.make(0);
1157 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
1158 makeTestAdapter({
1159 instanceId: CODEX_MODEL_SELECTION.instanceId,
1160 driver: CODEX_DRIVER,
1161 capabilities: CodexProviderCapabilitiesV2,
1162 modelSelection: CODEX_MODEL_SELECTION,
1163 responseByRunOrdinal: { 1: "Original partial work" },
1164 capturedTurns,
1165 }),
1166 makeTestAdapter({
1167 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
1168 driver: CLAUDE_DRIVER,
1169 capabilities: ClaudeProviderCapabilitiesV2,
1170 modelSelection: CLAUDE_MODEL_SELECTION,
1171 responseByRunOrdinal: {},
1172 capturedTurns,
1173 injectedHistory,
1174 nativeThreadGeneration: generation,
1175 getModelContextWindow: () => 32_000,
1176 ...(failure !== "injection"
1177 ? { failStartOnce: failOnce }
1178 : { failInjectionOnce: failOnce }),
1179 }),
1180 ]);
1181 yield* Effect.gen(function* () {
1182 const orchestrator = yield* Orchestrator.OrchestratorV2;
1183 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1184 const dispatch = (ordinal: number, text: string, selection: ModelSelection) =>
1185 orchestrator.dispatch({
1186 type: "message.dispatch",
1187 commandId: CommandId.make(`retry:${ordinal}`),
1188 threadId,
1189 messageId: MessageId.make(`retry:${ordinal}`),
1190 createdBy: "user",
1191 creationSource: "web",
1192 text,
1193 attachments: [],
1194 modelSelection: selection,
1195 dispatchMode: { type: "start_immediately" },
1196 });
1197 const wait = (ordinal: number, status: "failed" | "completed") =>
1198 orchestrator.streamStoredEvents.pipe(
1199 Stream.filter(
1200 ({ event }) =>
1201 event.type === "run.updated" &&
1202 event.payload.ordinal === ordinal &&
1203 (event.payload.status === "completed" || event.payload.status === "failed"),
1204 ),
1205 Stream.runHead,
1206 Effect.andThen(worker.drain()),
1207 Effect.andThen(
1208 Effect.gen(function* () {
1209 assert.equal(
1210 (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
1211 status,
1212 );
1213 }),
1214 ),
1215 );
1216 yield* orchestrator.dispatch({
1217 type: "thread.create",
1218 commandId: CommandId.make("retry:create"),
1219 threadId,
1220 projectId,
1221 createdBy: "user",
1222 creationSource: "web",
1223 title: "Handoff retry",
1224 modelSelection: CODEX_MODEL_SELECTION,
1225 runtimeMode: "full-access",
1226 interactionMode: "default",
1227 branch: null,
1228 worktreePath: null,
1229 });
1230 yield* dispatch(1, "Original request with constraints", CODEX_MODEL_SELECTION);
1231 yield* wait(1, "completed");
1232 yield* dispatch(
1233 2,
1234 failure === "large-missed-request" ? "x".repeat(7_000) : "First target request",
1235 CLAUDE_MODEL_SELECTION,
1236 );
1237 yield* wait(2, "failed");
1238 const failed = yield* orchestrator.getThreadProjection(threadId);
1239 const failedHandoff = failed.contextHandoffs.at(-1)!;
1240 assert.equal(
1241 failedHandoff.delivery?.status,
1242 failure !== "injection" ? "injected" : "pending",
1243 );
1244 const historyBeforeRetry = (yield* Ref.get(injectedHistory)).length;
1245 if (failure === "exhausted-target-retry") {
1246 const target = failed.providerThreads.find(
1247 (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
1248 )!;
1249 yield* (yield* EventSink.EventSinkV2).write({
1250 events: [
1251 {
1252 id: EventId.make("exhausted-retry-usage"),
1253 type: "provider-thread.updated",
1254 threadId,
1255 occurredAt: yield* DateTime.now,
1256 payload: { ...target, contextUsage: { usedTokens: 30_000, maxTokens: 32_000 } },
1257 },
1258 ],
1259 });
1260 }
1261 const retryText =
1262 failure === "large-missed-request" ? "y".repeat(10_000) : "Retry target request";
1263 yield* dispatch(3, retryText, CLAUDE_MODEL_SELECTION);
1264 yield* wait(3, "completed");
1265 const retried = yield* orchestrator.getThreadProjection(threadId);
1266 const lastTurn = (yield* Ref.get(capturedTurns)).at(-1)!;
1267 assert.equal(lastTurn.text, retryText);
1268 if (failure !== "injection" && failure !== "exhausted-target-retry") {
1269 const delta = yield* encodeJson(
1270 (yield* Ref.get(injectedHistory)).slice(historyBeforeRetry),
1271 );
1272 if (failure === "large-missed-request") {
1273 assert.include(delta, "omitted 1 items");
1274 const missedRequest = retried.turnItems.find(
1275 (item) => item.type === "user_message" && item.text === "x".repeat(7_000),
1276 )!;
1277 const delivery = retried.contextHandoffs.at(-1)!.delivery!;
1278 assert.include(delivery.omittedItemIds ?? [], missedRequest.id);
1279 assert.notInclude(delivery.itemIds, missedRequest.id);
1280 } else assert.include(delta, "First target request");
1281 assert.notInclude(delta, "Original request with constraints");
1282 assert.notInclude(delta, "Original partial work");
1283 assert.equal(yield* Ref.get(generation), 1);
1284 } else {
1285 assert.equal(yield* Ref.get(generation), 2);
1286 assert.notEqual(
1287 retried.contextHandoffs.at(-1)?.delivery?.nativeThreadId,
1288 failedHandoff.delivery?.nativeThreadId,
1289 );
1290 const newHistory = yield* encodeJson(
1291 (yield* Ref.get(injectedHistory)).slice(historyBeforeRetry),
1292 );
1293 assert.include(newHistory, "Original request with constraints");
1294 assert.include(newHistory, "Original partial work");
1295 assert.notInclude(newHistory, "Retry target request");
1296 }
1297 const beforeFollowup = yield* Ref.get(injectedHistory);
1298 const followup = "z".repeat(6_000);
1299 yield* dispatch(4, followup, CLAUDE_MODEL_SELECTION);
1300 yield* wait(4, "completed");
1301 assert.equal((yield* Ref.get(capturedTurns)).at(-1)!.text, followup);
1302 assert.deepEqual(yield* Ref.get(injectedHistory), beforeFollowup);
1303 assert.equal(
1304 (yield* orchestrator.getThreadProjection(threadId)).contextHandoffs.length,
1305 retried.contextHandoffs.length,
1306 );
1307 }).pipe(
1308 Effect.provide(
1309 ProviderReplayHarness.layerWithRegistry(
1310 {
1311 name: `handoff-retry-${failure}`,
1312 runtimePolicyOverride: {
1313 cwd,
1314 approvalPolicy: "never",
1315 sandboxPolicy: { type: "readOnly" },
1316 },
1317 },
1318 layerRegistry,
1319 ),
1320 ),
1321 );
1322 }),
1323 );
1324
1325 it.live("keeps a native session whose stored prose is far below an unreported window", () =>
1326 Effect.gen(function* () {
1327 const cwd = yield* checkpointWorkspace("handoff-estimate-headroom");
1328 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1329 const generation = yield* Ref.make(0);
1330 // About 112 KB of ordinary English is about 25k tokens: a quarter of a 128k window.
1331 const prose = "The quick brown fox jumps over the lazy dog. ".repeat(2_500);
1332 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
1333 makeTestAdapter({
1334 instanceId: CODEX_MODEL_SELECTION.instanceId,
1335 driver: CODEX_DRIVER,
1336 capabilities: CodexProviderCapabilitiesV2,
1337 modelSelection: CODEX_MODEL_SELECTION,
1338 responseByRunOrdinal: { 2: "Source work" },
1339 capturedTurns,
1340 }),
1341 // No window and no usage, like an ACP registry agent that sends no usage_update.
1342 makeTestAdapter({
1343 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
1344 driver: CLAUDE_DRIVER,
1345 capabilities: ClaudeProviderCapabilitiesV2,
1346 modelSelection: CLAUDE_MODEL_SELECTION,
1347 responseByRunOrdinal: { 1: prose },
1348 capturedTurns,
1349 nativeThreadGeneration: generation,
1350 }),
1351 ]);
1352 yield* Effect.gen(function* () {
1353 const orchestrator = yield* Orchestrator.OrchestratorV2;
1354 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1355 const dispatch = (ordinal: number, text: string, selection: ModelSelection) =>
1356 orchestrator.dispatch({
1357 type: "message.dispatch",
1358 commandId: CommandId.make(`headroom:${ordinal}`),
1359 threadId,
1360 messageId: MessageId.make(`headroom:${ordinal}`),
1361 createdBy: "user",
1362 creationSource: "web",
1363 text,
1364 attachments: [],
1365 modelSelection: selection,
1366 dispatchMode: { type: "start_immediately" },
1367 });
1368 const wait = (ordinal: number) =>
1369 orchestrator.streamStoredEvents.pipe(
1370 Stream.filter(
1371 ({ event }) =>
1372 event.type === "run.updated" &&
1373 event.payload.ordinal === ordinal &&
1374 (event.payload.status === "completed" || event.payload.status === "failed"),
1375 ),
1376 Stream.runHead,
1377 Effect.andThen(worker.drain()),
1378 );
1379 yield* orchestrator.dispatch({
1380 type: "thread.create",
1381 commandId: CommandId.make("headroom:create"),
1382 threadId,
1383 projectId,
1384 createdBy: "user",
1385 creationSource: "web",
1386 title: "Handoff estimate headroom",
1387 modelSelection: CLAUDE_MODEL_SELECTION,
1388 runtimeMode: "full-access",
1389 interactionMode: "default",
1390 branch: null,
1391 worktreePath: null,
1392 });
1393 yield* dispatch(1, "Write the long report", CLAUDE_MODEL_SELECTION);
1394 yield* wait(1);
1395 yield* dispatch(2, "Continue elsewhere", CODEX_MODEL_SELECTION);
1396 yield* wait(2);
1397 yield* dispatch(3, "Back to the first provider", CLAUDE_MODEL_SELECTION);
1398 yield* wait(3);
1399 assert.equal(
1400 (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
1401 "completed",
1402 );
1403 // A second generation means T3 discarded the native session as full.
1404 assert.equal(yield* Ref.get(generation), 1);
1405 }).pipe(
1406 Effect.provide(
1407 ProviderReplayHarness.layerWithRegistry(
1408 {
1409 name: "handoff-estimate-headroom",
1410 runtimePolicyOverride: {
1411 cwd,
1412 approvalPolicy: "never",
1413 sandboxPolicy: { type: "readOnly" },
1414 },
1415 },
1416 layerRegistry,
1417 ),
1418 ),
1419 );
1420 }),
1421 );
1422
1423 const importedNativeSessionScenario = (
1424 scenario: "first-send" | "after-failed-switch" | "resume-fails",
1425 ) =>
1426 Effect.gen(function* () {
1427 const cwd = yield* checkpointWorkspace(`imported-native-session-${scenario}`);
1428 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1429 const claudeOrdinal = scenario === "after-failed-switch" ? 2 : 1;
1430 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
1431 makeTestAdapter({
1432 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
1433 driver: CLAUDE_DRIVER,
1434 capabilities: ClaudeProviderCapabilitiesV2,
1435 modelSelection: CLAUDE_MODEL_SELECTION,
1436 responseByRunOrdinal: { [claudeOrdinal]: "PELICAN" },
1437 capturedTurns,
1438 failResume: scenario === "resume-fails",
1439 }),
1440 makeTestAdapter({
1441 instanceId: CODEX_MODEL_SELECTION.instanceId,
1442 driver: CODEX_DRIVER,
1443 capabilities: CodexProviderCapabilitiesV2,
1444 modelSelection: CODEX_MODEL_SELECTION,
1445 responseByRunOrdinal: {},
1446 capturedTurns,
1447 failedRunOrdinals: new Set([1]),
1448 }),
1449 ]);
1450 yield* Effect.gen(function* () {
1451 const orchestrator = yield* Orchestrator.OrchestratorV2;
1452 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1453 yield* orchestrator.dispatch({
1454 type: "thread.create",
1455 commandId: CommandId.make("imported-native:create"),
1456 threadId,
1457 projectId,
1458 createdBy: "system",
1459 creationSource: "server",
1460 title: "Imported Claude Code session",
1461 modelSelection: CLAUDE_MODEL_SELECTION,
1462 runtimeMode: "full-access",
1463 interactionMode: "default",
1464 branch: null,
1465 worktreePath: null,
1466 });
1467 // The rows AgentSessionImporter writes: imported messages without a run,
1468 // bound to the strong native session they came from.
1469 const created = yield* orchestrator.getThreadProjection(threadId);
1470 const at = created.thread.createdAt;
1471 const providerThread: OrchestrationV2ProviderThread = {
1472 id: ProviderThreadId.make("provider-thread:imported-native"),
1473 driver: CLAUDE_DRIVER,
1474 providerInstanceId: CLAUDE_MODEL_SELECTION.instanceId,
1475 providerSessionId: null,
1476 appThreadId: threadId,
1477 ownerNodeId: null,
1478 nativeThreadRef: {
1479 driver: CLAUDE_DRIVER,
1480 nativeId: "native-session",
1481 strength: "strong",
1482 },
1483 nativeConversationHeadRef: null,
1484 status: "idle",
1485 firstRunOrdinal: null,
1486 lastRunOrdinal: null,
1487 handoffIds: [],
1488 forkedFrom: null,
1489 createdAt: at,
1490 updatedAt: at,
1491 };
1492 const common = {
1493 threadId,
1494 runId: null,
1495 nodeId: null,
1496 providerThreadId: null,
1497 providerTurnId: null,
1498 nativeItemRef: null,
1499 parentItemId: null,
1500 status: "completed" as const,
1501 title: null,
1502 startedAt: at,
1503 completedAt: at,
1504 updatedAt: at,
1505 };
1506 yield* (yield* EventSink.EventSinkV2).write({
1507 events: [
1508 {
1509 id: EventId.make("imported-native:thread"),
1510 type: "thread.metadata-updated",
1511 threadId,
1512 providerInstanceId: CLAUDE_MODEL_SELECTION.instanceId,
1513 occurredAt: at,
1514 payload: {
1515 ...created.thread,
1516 historyOrigin: "v1_import",
1517 activeProviderThreadId: providerThread.id,
1518 },
1519 },
1520 {
1521 id: EventId.make("imported-native:user"),
1522 type: "turn-item.updated",
1523 threadId,
1524 occurredAt: at,
1525 payload: {
1526 ...common,
1527 id: TurnItemId.make("imported-native:user"),
1528 ordinal: 1,
1529 createdBy: "user",
1530 creationSource: "server",
1531 type: "user_message",
1532 messageId: MessageId.make("imported-native:user"),
1533 inputIntent: "turn_start",
1534 text: "Remember the code word PELICAN.",
1535 attachments: [],
1536 },
1537 },
1538 {
1539 id: EventId.make("imported-native:assistant"),
1540 type: "turn-item.updated",
1541 threadId,
1542 occurredAt: at,
1543 payload: {
1544 ...common,
1545 id: TurnItemId.make("imported-native:assistant"),
1546 ordinal: 2,
1547 type: "assistant_message",
1548 messageId: MessageId.make("imported-native:assistant"),
1549 text: "OK",
1550 streaming: false,
1551 },
1552 },
1553 {
1554 id: EventId.make("imported-native:provider-thread"),
1555 type: "provider-thread.updated",
1556 threadId,
1557 driver: CLAUDE_DRIVER,
1558 providerInstanceId: CLAUDE_MODEL_SELECTION.instanceId,
1559 occurredAt: at,
1560 payload: providerThread,
1561 },
1562 ],
1563 });
1564 const send = (ordinal: number, modelSelection: ModelSelection) =>
1565 Effect.gen(function* () {
1566 yield* orchestrator.dispatch({
1567 type: "message.dispatch",
1568 commandId: CommandId.make(`imported-native:${ordinal}`),
1569 threadId,
1570 messageId: MessageId.make(`imported-native:${ordinal}`),
1571 createdBy: "user",
1572 creationSource: "web",
1573 text: "What was the code word?",
1574 attachments: [],
1575 modelSelection,
1576 dispatchMode: { type: "start_immediately" },
1577 });
1578 yield* orchestrator.streamStoredEvents.pipe(
1579 Stream.filter(
1580 ({ event }) =>
1581 event.type === "run.updated" &&
1582 event.payload.ordinal === ordinal &&
1583 (event.payload.status === "completed" || event.payload.status === "failed"),
1584 ),
1585 Stream.runHead,
1586 );
1587 yield* worker.drain();
1588 });
1589 if (scenario === "after-failed-switch") yield* send(1, CODEX_MODEL_SELECTION);
1590 yield* send(claudeOrdinal, CLAUDE_MODEL_SELECTION);
1591
1592 const projection = yield* orchestrator.getThreadProjection(threadId);
1593 assert.equal(projection.runs.at(-1)?.status, "completed");
1594 const claudeTurn = (yield* Ref.get(capturedTurns)).findLast(
1595 (turn) => turn.driver === CLAUDE_DRIVER,
1596 );
1597 const importHandoffTargets = projection.contextHandoffs
1598 .filter((handoff) => handoff.strategy === "manual_context")
1599 .map((handoff) => handoff.toProviderThreadId);
1600 if (scenario === "resume-fails") {
1601 // The replacement session is empty, so the recovery summary carries the import.
1602 assert.notEqual(claudeTurn?.nativeThreadId, "native-session");
1603 assert.deepEqual(
1604 projection.contextHandoffs.map((handoff) => handoff.strategy),
1605 ["full_thread_summary"],
1606 );
1607 assert.include(claudeTurn?.text ?? "", "Remember the code word PELICAN.");
1608 return;
1609 }
1610 assert.equal(claudeTurn?.nativeThreadId, "native-session");
1611 assert.notInclude(importHandoffTargets, providerThread.id);
1612 assert.lengthOf(importHandoffTargets, scenario === "after-failed-switch" ? 1 : 0);
1613 if (scenario === "first-send") assert.deepEqual(projection.contextHandoffs, []);
1614 assert.notInclude(claudeTurn?.text ?? "", "Remember the code word PELICAN.");
1615 }).pipe(
1616 Effect.provide(
1617 ProviderReplayHarness.layerWithRegistry(
1618 {
1619 name: `imported-native-session-${scenario}`,
1620 runtimePolicyOverride: {
1621 cwd,
1622 approvalPolicy: "never",
1623 sandboxPolicy: { type: "readOnly" },
1624 },
1625 },
1626 layerRegistry,
1627 ),
1628 ),
1629 );
1630 });
1631 it.live("resumes an imported native session without resending its history", () =>
1632 importedNativeSessionScenario("first-send"),
1633 );
1634 it.live("returns to an imported native session without resending its history", () =>
1635 importedNativeSessionScenario("after-failed-switch"),
1636 );
1637 it.live("recovers an imported native session's history when its resume fails", () =>
1638 importedNativeSessionScenario("resume-fails"),
1639 );
1640
1641 it.live.each(["native", "legacy"] as const)(
1642 "starts a replacement native thread as new after the resume fallback with %s attempts",
1643 (attempts) =>
1644 Effect.gen(function* () {
1645 const cwd = yield* checkpointWorkspace("resume-fallback-native-turns");
1646 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1647 const failResumeOnce = yield* Ref.make(false);
1648 const generation = yield* Ref.make(0);
1649 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
1650 makeTestAdapter({
1651 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
1652 driver: CLAUDE_DRIVER,
1653 capabilities: ClaudeProviderCapabilitiesV2,
1654 modelSelection: CLAUDE_MODEL_SELECTION,
1655 responseByRunOrdinal: {},
1656 capturedTurns,
1657 failResumeOnce,
1658 nativeThreadGeneration: generation,
1659 }),
1660 ]);
1661 yield* Effect.gen(function* () {
1662 const orchestrator = yield* Orchestrator.OrchestratorV2;
1663 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1664 const runToCompletion = Effect.fn("runToCompletion")(function* (ordinal: number) {
1665 yield* orchestrator.dispatch({
1666 type: "message.dispatch",
1667 commandId: CommandId.make(`resume-fallback:${ordinal}`),
1668 threadId,
1669 messageId: MessageId.make(`resume-fallback:${ordinal}`),
1670 createdBy: "user",
1671 creationSource: "web",
1672 text: `Request ${ordinal}`,
1673 attachments: [],
1674 modelSelection: CLAUDE_MODEL_SELECTION,
1675 dispatchMode: { type: "start_immediately" },
1676 });
1677 yield* orchestrator.streamStoredEvents.pipe(
1678 Stream.filter(
1679 ({ event }) =>
1680 event.type === "run.updated" &&
1681 event.payload.ordinal === ordinal &&
1682 (event.payload.status === "completed" || event.payload.status === "failed"),
1683 ),
1684 Stream.runHead,
1685 );
1686 yield* worker.drain();
1687 assert.equal(
1688 (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
1689 "completed",
1690 );
1691 });
1692 yield* orchestrator.dispatch({
1693 type: "thread.create",
1694 commandId: CommandId.make("resume-fallback:create"),
1695 threadId,
1696 projectId,
1697 createdBy: "user",
1698 creationSource: "web",
1699 title: "Resume fallback",
1700 modelSelection: CLAUDE_MODEL_SELECTION,
1701 runtimeMode: "full-access",
1702 interactionMode: "default",
1703 branch: null,
1704 worktreePath: null,
1705 });
1706 yield* runToCompletion(1);
1707 const providerThread = (yield* orchestrator.getThreadProjection(
1708 threadId,
1709 )).providerThreads.at(0);
1710 if (providerThread === undefined || providerThread.providerSessionId === null) {
1711 return yield* Effect.die("Expected a provider session after the first turn.");
1712 }
1713 yield* orchestrator.dispatch({
1714 type: "provider-session.detach",
1715 commandId: CommandId.make("resume-fallback:detach"),
1716 threadId,
1717 providerSessionId: providerThread.providerSessionId,
1718 });
1719 yield* worker.drain();
1720 if (attempts === "legacy") {
1721 const existing = yield* orchestrator.getThreadProjection(threadId);
1722 yield* (yield* EventSink.EventSinkV2).write({
1723 events: existing.attempts.map(
1724 ({ nativeThreadId: _nativeThreadId, ...legacy }, index) => ({
1725 id: EventId.make(`resume-fallback-legacy-attempt:${index}`),
1726 type: "run-attempt.updated" as const,
1727 threadId,
1728 occurredAt: existing.thread.createdAt,
1729 payload: legacy,
1730 }),
1731 ),
1732 });
1733 }
1734 yield* Ref.set(failResumeOnce, true);
1735 yield* runToCompletion(2);
1736 yield* runToCompletion(3);
1737
1738 const turns = yield* Ref.get(capturedTurns);
1739 assert.equal(turns.length, 3);
1740 assert.equal(yield* Ref.get(generation), 2);
1741 assert.notEqual(turns[1]?.nativeThreadId, turns[0]?.nativeThreadId);
1742 assert.isAbove(turns[1]?.providerTurnOrdinal ?? 0, 1);
1743 assert.isFalse(turns[1]?.nativeThreadHasTurns);
1744 assert.equal(turns[2]?.nativeThreadId, turns[1]?.nativeThreadId);
1745 assert.isTrue(turns[2]?.nativeThreadHasTurns);
1746
1747 const projection = yield* orchestrator.getThreadProjection(threadId);
1748 const run2 = projection.runs[1]!;
1749 const handoff = projection.contextHandoffs.find(
1750 (candidate) => candidate.targetRunId === run2.id,
1751 );
1752 assert.deepEqual(
1753 projection.turnItems
1754 .filter((item) => item.runId === run2.id)
1755 .toSorted((left, right) => left.ordinal - right.ordinal)
1756 .slice(0, 2)
1757 .map((item) => (item.type === "handoff" ? item.contextHandoffId : item.type)),
1758 [handoff?.id, "user_message"],
1759 );
1760 }).pipe(
1761 Effect.provide(
1762 ProviderReplayHarness.layerWithRegistry(
1763 {
1764 name: "resume-fallback-native-turns",
1765 runtimePolicyOverride: {
1766 cwd,
1767 approvalPolicy: "never",
1768 sandboxPolicy: { type: "readOnly" },
1769 },
1770 },
1771 layerRegistry,
1772 ),
1773 ),
1774 );
1775 }),
1776 );
1777
1778 const unverifiedReplacementHarness = <A, E, R>(
1779 name: string,
1780 adapters: ReadonlyArray<ProviderAdapter.ProviderAdapterV2["Service"]>,
1781 body: (helpers: {
1782 readonly orchestrator: Orchestrator.OrchestratorV2["Service"];
1783 readonly worker: EffectWorker.OrchestrationEffectWorkerV2["Service"];
1784 readonly dispatchRequest: (
1785 ordinal: number,
1786 selection: ModelSelection,
1787 ) => ReturnType<Orchestrator.OrchestratorV2["Service"]["dispatch"]>;
1788 readonly awaitSettled: (
1789 ordinal: number,
1790 ) => Effect.Effect<
1791 number,
1792 Orchestrator.OrchestratorV2Error | EffectWorker.OrchestrationEffectWorkerError
1793 >;
1794 }) => Effect.Effect<A, E, R>,
1795 ) =>
1796 Effect.scoped(
1797 Effect.gen(function* () {
1798 const cwd = yield* checkpointWorkspace(name);
1799 return yield* Effect.gen(function* () {
1800 const orchestrator = yield* Orchestrator.OrchestratorV2;
1801 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1802 yield* orchestrator.dispatch({
1803 type: "thread.create",
1804 commandId: CommandId.make("unverified-replacement:create"),
1805 threadId,
1806 projectId,
1807 createdBy: "user",
1808 creationSource: "web",
1809 title: "Unverified replacement",
1810 modelSelection: CLAUDE_MODEL_SELECTION,
1811 runtimeMode: "full-access",
1812 interactionMode: "default",
1813 branch: null,
1814 worktreePath: null,
1815 });
1816 return yield* body({
1817 orchestrator,
1818 worker,
1819 dispatchRequest: (ordinal, selection) =>
1820 orchestrator.dispatch({
1821 type: "message.dispatch",
1822 commandId: CommandId.make(`unverified-replacement:${ordinal}`),
1823 threadId,
1824 messageId: MessageId.make(`unverified-replacement:${ordinal}`),
1825 createdBy: "user",
1826 creationSource: "web",
1827 text: `Request ${ordinal}`,
1828 attachments: [],
1829 modelSelection: selection,
1830 dispatchMode: { type: "start_immediately" },
1831 }),
1832 awaitSettled: (ordinal) =>
1833 orchestrator.streamStoredEvents.pipe(
1834 Stream.filter(
1835 ({ event }) =>
1836 event.type === "run.updated" &&
1837 event.payload.ordinal === ordinal &&
1838 (event.payload.status === "completed" ||
1839 event.payload.status === "failed" ||
1840 event.payload.status === "interrupted"),
1841 ),
1842 Stream.runHead,
1843 Effect.andThen(worker.drain()),
1844 ),
1845 });
1846 }).pipe(
1847 Effect.provide(
1848 ProviderReplayHarness.layerWithRegistry(
1849 {
1850 name,
1851 runtimePolicyOverride: {
1852 cwd,
1853 approvalPolicy: "never",
1854 sandboxPolicy: { type: "readOnly" },
1855 },
1856 },
1857 ProviderAdapterRegistry.layerFromAdapters(adapters),
1858 ),
1859 ),
1860 );
1861 }),
1862 );
1863 // Replacement bookkeeping is a full-thread summary handoff plus a resume-fallback transfer.
1864 const replacementRecordsFor = (projection: OrchestrationV2ThreadProjection, runId: string) => [
1865 ...projection.contextHandoffs.filter(
1866 (handoff) => handoff.targetRunId === runId && handoff.strategy === "full_thread_summary",
1867 ),
1868 ...projection.contextTransfers.filter(
1869 (transfer) =>
1870 transfer.targetRunId === runId && transfer.id.includes("provider_resume_fallback"),
1871 ),
1872 ];
1873
1874 it.live("fails honestly when the provider reuses its session for a context handoff", () =>
1875 Effect.gen(function* () {
1876 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1877 yield* unverifiedReplacementHarness(
1878 "unverified-replacement-reused",
1879 [
1880 makeTestAdapter({
1881 instanceId: CODEX_MODEL_SELECTION.instanceId,
1882 driver: CODEX_DRIVER,
1883 capabilities: CodexProviderCapabilitiesV2,
1884 modelSelection: CODEX_MODEL_SELECTION,
1885 responseByRunOrdinal: { 2: `Source work ${"s".repeat(12_000)}` },
1886 capturedTurns,
1887 }),
1888 // Without a generation every ensureThread returns the same native id, like Muse.
1889 makeTestAdapter({
1890 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
1891 driver: CLAUDE_DRIVER,
1892 capabilities: ClaudeProviderCapabilitiesV2,
1893 modelSelection: CLAUDE_MODEL_SELECTION,
1894 responseByRunOrdinal: {},
1895 capturedTurns,
1896 getModelContextWindow: () => 32_000,
1897 }),
1898 ],
1899 ({ orchestrator, dispatchRequest, awaitSettled }) =>
1900 Effect.gen(function* () {
1901 yield* dispatchRequest(1, CLAUDE_MODEL_SELECTION);
1902 yield* awaitSettled(1);
1903 const target = (yield* orchestrator.getThreadProjection(threadId)).providerThreads[0]!;
1904 // The native session has no room left for the returning handoff.
1905 yield* (yield* EventSink.EventSinkV2).write({
1906 events: [
1907 {
1908 id: EventId.make("unverified-replacement-usage"),
1909 type: "provider-thread.updated",
1910 threadId,
1911 occurredAt: yield* DateTime.now,
1912 payload: { ...target, contextUsage: { usedTokens: 31_000, maxTokens: 32_000 } },
1913 },
1914 ],
1915 });
1916 yield* dispatchRequest(2, CODEX_MODEL_SELECTION);
1917 yield* awaitSettled(2);
1918 yield* dispatchRequest(3, CLAUDE_MODEL_SELECTION);
1919 yield* awaitSettled(3);
1920
1921 const projection = yield* orchestrator.getThreadProjection(threadId);
1922 const run3 = projection.runs.at(-1)!;
1923 assert.equal(run3.status, "failed");
1924 assert.deepEqual(replacementRecordsFor(projection, run3.id), []);
1925 const failure = projection.turnItems.findLast((item) => item.type === "error");
1926 assert.equal(
1927 failure?.type === "error" ? failure.failure.message : null,
1928 "claudeAgent provider protocol error: the provider returned its existing session when asked for a fresh one, so the conversation context could not be moved to a new session.",
1929 );
1930 assert.deepEqual(
1931 (yield* Ref.get(capturedTurns)).map((turn) => turn.driver),
1932 [CLAUDE_DRIVER, CODEX_DRIVER],
1933 );
1934 }),
1935 );
1936 }),
1937 );
1938
1939 it.live("fails without retrying when a failed resume's replacement reuses the session", () =>
1940 Effect.gen(function* () {
1941 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1942 const failResumeOnce = yield* Ref.make(false);
1943 yield* unverifiedReplacementHarness(
1944 "unverified-replacement-reused-resume",
1945 [
1946 // Without a generation every ensureThread returns the same native id, like Muse.
1947 makeTestAdapter({
1948 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
1949 driver: CLAUDE_DRIVER,
1950 capabilities: ClaudeProviderCapabilitiesV2,
1951 modelSelection: CLAUDE_MODEL_SELECTION,
1952 responseByRunOrdinal: {},
1953 capturedTurns,
1954 failResumeOnce,
1955 }),
1956 ],
1957 ({ orchestrator, worker, dispatchRequest, awaitSettled }) =>
1958 Effect.gen(function* () {
1959 yield* dispatchRequest(1, CLAUDE_MODEL_SELECTION);
1960 yield* awaitSettled(1);
1961 const original = (yield* orchestrator.getThreadProjection(threadId))
1962 .providerThreads[0]!;
1963 yield* orchestrator.dispatch({
1964 type: "provider-session.detach",
1965 commandId: CommandId.make("unverified-replacement:detach"),
1966 threadId,
1967 providerSessionId: original.providerSessionId!,
1968 });
1969 yield* worker.drain();
1970 // Only the first attempt fails to resume; a retry would find the session loaded.
1971 yield* Ref.set(failResumeOnce, true);
1972 yield* dispatchRequest(2, CLAUDE_MODEL_SELECTION);
1973 yield* awaitSettled(2);
1974
1975 const projection = yield* orchestrator.getThreadProjection(threadId);
1976 const run2 = projection.runs.at(-1)!;
1977 assert.equal(run2.status, "failed");
1978 assert.deepEqual(replacementRecordsFor(projection, run2.id), []);
1979 const failure = projection.turnItems.findLast((item) => item.type === "error");
1980 assert.equal(
1981 failure?.type === "error" ? failure.failure.message : null,
1982 "claudeAgent provider protocol error: the provider returned its existing session when asked for a fresh one, so the conversation context could not be moved to a new session.",
1983 );
1984 assert.equal((yield* Ref.get(capturedTurns)).length, 1);
1985 }),
1986 );
1987 }),
1988 );
1989
1990 it.live("keeps failing honestly on the next turn after rejecting a reused session", () =>
1991 Effect.gen(function* () {
1992 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1993 yield* unverifiedReplacementHarness(
1994 "unverified-replacement-reused-next-turn",
1995 [
1996 // Resume always fails and every ensureThread returns the same native id.
1997 makeTestAdapter({
1998 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
1999 driver: CLAUDE_DRIVER,
2000 capabilities: ClaudeProviderCapabilitiesV2,
2001 modelSelection: CLAUDE_MODEL_SELECTION,
2002 responseByRunOrdinal: {},
2003 capturedTurns,
2004 failResume: true,
2005 }),
2006 ],
2007 ({ orchestrator, worker, dispatchRequest, awaitSettled }) =>
2008 Effect.gen(function* () {
2009 yield* dispatchRequest(1, CLAUDE_MODEL_SELECTION);
2010 yield* awaitSettled(1);
2011 const original = (yield* orchestrator.getThreadProjection(threadId))
2012 .providerThreads[0]!;
2013 yield* orchestrator.dispatch({
2014 type: "provider-session.detach",
2015 commandId: CommandId.make("unverified-replacement:detach"),
2016 threadId,
2017 providerSessionId: original.providerSessionId!,
2018 });
2019 yield* worker.drain();
2020 yield* dispatchRequest(2, CLAUDE_MODEL_SELECTION);
2021 yield* awaitSettled(2);
2022 // The rejected session must not count as loaded, or this turn would skip resume and
2023 // run on it without the recovery history.
2024 yield* dispatchRequest(3, CLAUDE_MODEL_SELECTION);
2025 yield* awaitSettled(3);
2026
2027 const projection = yield* orchestrator.getThreadProjection(threadId);
2028 assert.deepEqual(
2029 projection.runs.map((run) => run.status),
2030 ["completed", "failed", "failed"],
2031 );
2032 assert.deepEqual(replacementRecordsFor(projection, projection.runs[2]!.id), []);
2033 assert.equal((yield* Ref.get(capturedTurns)).length, 1);
2034 }),
2035 );
2036 }),
2037 );
2038
2039 it.live("drops replacement bookkeeping when the run stops during replacement", () =>
2040 Effect.gen(function* () {
2041 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
2042 const failResumeOnce = yield* Ref.make(false);
2043 const replacementRequested = yield* Deferred.make<void>();
2044 const releaseReplacement = yield* Deferred.make<void>();
2045 yield* unverifiedReplacementHarness(
2046 "unverified-replacement-interrupted",
2047 [
2048 makeTestAdapter({
2049 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
2050 driver: CLAUDE_DRIVER,
2051 capabilities: ClaudeProviderCapabilitiesV2,
2052 modelSelection: CLAUDE_MODEL_SELECTION,
2053 responseByRunOrdinal: {},
2054 capturedTurns,
2055 failResumeOnce,
2056 nativeThreadGeneration: yield* Ref.make(0),
2057 beforeForcedNativeThread: Deferred.succeed(replacementRequested, undefined).pipe(
2058 Effect.andThen(Deferred.await(releaseReplacement)),
2059 ),
2060 }),
2061 ],
2062 ({ orchestrator, worker, dispatchRequest, awaitSettled }) =>
2063 Effect.gen(function* () {
2064 yield* dispatchRequest(1, CLAUDE_MODEL_SELECTION);
2065 yield* awaitSettled(1);
2066 const original = (yield* orchestrator.getThreadProjection(threadId))
2067 .providerThreads[0]!;
2068 yield* orchestrator.dispatch({
2069 type: "provider-session.detach",
2070 commandId: CommandId.make("unverified-replacement:detach"),
2071 threadId,
2072 providerSessionId: original.providerSessionId!,
2073 });
2074 yield* worker.drain();
2075 yield* Ref.set(failResumeOnce, true);
2076 yield* dispatchRequest(2, CLAUDE_MODEL_SELECTION);
2077 yield* Deferred.await(replacementRequested);
2078 const run2 = (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)!;
2079 assert.equal(run2.status, "starting");
2080 // run.interrupt would also cancel the start effect, so move the run on directly to
2081 // reach the window where the replacement session exists but is not yet recorded.
2082 const settledAt = yield* DateTime.now;
2083 yield* (yield* EventSink.EventSinkV2).write({
2084 events: [
2085 {
2086 id: EventId.make("unverified-replacement-interrupted"),
2087 type: "run.updated",
2088 threadId,
2089 runId: run2.id,
2090 occurredAt: settledAt,
2091 payload: { ...run2, status: "interrupted", completedAt: settledAt },
2092 },
2093 ],
2094 });
2095 yield* Deferred.succeed(releaseReplacement, undefined);
2096
2097 // Effects run one at a time per thread, so run 3 settles after run 2's start finishes.
2098 // The next turn resumes the original native session with no abandoned replacement.
2099 yield* dispatchRequest(3, CLAUDE_MODEL_SELECTION);
2100 yield* awaitSettled(3);
2101 const final = yield* orchestrator.getThreadProjection(threadId);
2102 const run3 = final.runs.at(-1)!;
2103 assert.equal(final.runs[1]?.status, "interrupted");
2104 assert.deepEqual(replacementRecordsFor(final, run2.id), []);
2105 assert.equal(run3.status, "completed");
2106 assert.deepEqual(replacementRecordsFor(final, run3.id), []);
2107 assert.equal(
2108 final.providerThreads[0]?.nativeThreadRef?.nativeId,
2109 original.nativeThreadRef?.nativeId,
2110 );
2111 const turns = yield* Ref.get(capturedTurns);
2112 assert.equal(turns.length, 2);
2113 assert.equal(turns[1]?.nativeThreadId, original.nativeThreadRef?.nativeId);
2114 assert.include(turns[1]!.text, "Request 3");
2115 }),
2116 );
2117 }),
2118 );
2119
2120 it.live.each(["an older handoff", "a missed request"] as const)(
2121 "keeps the latest turn when a resume fallback also delivers %s",
2122 (older) =>
2123 Effect.gen(function* () {
2124 // A run that fails before its provider turn starts is retried as a missed request.
2125 const missed = older === "a missed request";
2126 const name = `resume-fallback-handoff-order${missed ? "-missed" : ""}`;
2127 const cwd = yield* checkpointWorkspace(name);
2128 const failStartOnce = yield* Ref.make(false);
2129 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
2130 const injectedHistory = yield* Ref.make<ReadonlyArray<unknown>>([]);
2131 const oldResult = `old result ${"o".repeat(2_500)}`;
2132 const latestResult = `latest result ${"l".repeat(2_500)}`;
2133 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
2134 makeTestAdapter({
2135 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
2136 driver: CLAUDE_DRIVER,
2137 capabilities: ClaudeProviderCapabilitiesV2,
2138 modelSelection: CLAUDE_MODEL_SELECTION,
2139 responseByRunOrdinal: { 1: oldResult, 3: latestResult },
2140 capturedTurns,
2141 injectedHistory,
2142 failResume: true,
2143 ...(missed ? { failStartOnce } : { failedRunOrdinals: new Set([2]) }),
2144 nativeThreadGeneration: yield* Ref.make(0),
2145 }),
2146 ]);
2147 yield* Effect.gen(function* () {
2148 const orchestrator = yield* Orchestrator.OrchestratorV2;
2149 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
2150 const runAfterDetach = Effect.fn("runAfterDetach")(function* (
2151 ordinal: number,
2152 text: string,
2153 ) {
2154 const providerSessionId = (yield* orchestrator.getThreadProjection(threadId))
2155 .providerThreads[0]?.providerSessionId;
2156 if (providerSessionId != null) {
2157 yield* orchestrator.dispatch({
2158 type: "provider-session.detach",
2159 commandId: CommandId.make(`handoff-order:detach:${ordinal}`),
2160 threadId,
2161 providerSessionId,
2162 });
2163 yield* worker.drain();
2164 }
2165 yield* orchestrator.dispatch({
2166 type: "message.dispatch",
2167 commandId: CommandId.make(`handoff-order:${ordinal}`),
2168 threadId,
2169 messageId: MessageId.make(`handoff-order:${ordinal}`),
2170 createdBy: "user",
2171 creationSource: "web",
2172 text,
2173 attachments: [],
2174 modelSelection: CLAUDE_MODEL_SELECTION,
2175 dispatchMode: { type: "start_immediately" },
2176 });
2177 yield* orchestrator.streamStoredEvents.pipe(
2178 Stream.filter(
2179 ({ event }) =>
2180 event.type === "run.updated" &&
2181 event.payload.ordinal === ordinal &&
2182 (event.payload.status === "completed" || event.payload.status === "failed"),
2183 ),
2184 Stream.runHead,
2185 );
2186 yield* worker.drain();
2187 });
2188 yield* orchestrator.dispatch({
2189 type: "thread.create",
2190 commandId: CommandId.make("handoff-order:create"),
2191 threadId,
2192 projectId,
2193 createdBy: "user",
2194 creationSource: "web",
2195 title: "Resume fallback handoff order",
2196 modelSelection: CLAUDE_MODEL_SELECTION,
2197 runtimeMode: "full-access",
2198 interactionMode: "default",
2199 branch: null,
2200 worktreePath: null,
2201 });
2202 yield* runAfterDetach(1, `Original brief ${"b".repeat(9_000)}`);
2203 yield* Ref.set(failStartOnce, missed);
2204 yield* runAfterDetach(2, missed ? `Request 2 ${"r".repeat(6_000)}` : "Request 2");
2205 yield* runAfterDetach(3, "Request 3");
2206 const historyBeforeRun4 = (yield* Ref.get(injectedHistory)).length;
2207 yield* runAfterDetach(4, "Request 4");
2208
2209 const projection = yield* orchestrator.getThreadProjection(threadId);
2210 assert.deepEqual(
2211 projection.runs.map((run) => run.status),
2212 ["completed", "failed", "completed", "completed"],
2213 );
2214 const run4NativeThreadId = (yield* Ref.get(capturedTurns)).at(-1)?.nativeThreadId;
2215 // Run 3's summary already covers run 2's handoff, so run 4's fresh session must not get it again.
2216 assert.notEqual(
2217 projection.contextHandoffs.find(
2218 (handoff) => handoff.targetRunId === projection.runs[1]?.id,
2219 )?.delivery?.nativeThreadId,
2220 run4NativeThreadId,
2221 );
2222 const run4History = yield* encodeJson(
2223 (yield* Ref.get(injectedHistory)).slice(historyBeforeRun4),
2224 );
2225 assert.include(run4History, "Original brief");
2226 assert.include(run4History, latestResult);
2227 }).pipe(
2228 Effect.provide(
2229 ProviderReplayHarness.layerWithRegistry(
2230 {
2231 name,
2232 runtimePolicyOverride: {
2233 cwd,
2234 approvalPolicy: "never",
2235 sandboxPolicy: { type: "readOnly" },
2236 },
2237 },
2238 layerRegistry,
2239 ),
2240 ),
2241 );
2242 }),
2243 );
2244
2245 it.live.each([
2246 {
2247 name: "an ACP rollback that starts a fresh agent session",
2248 instanceId: GROK_MODEL_SELECTION.instanceId,
2249 driver: GROK_DRIVER,
2250 capabilities: AcpProviderCapabilitiesV2,
2251 modelSelection: GROK_MODEL_SELECTION,
2252 rollback: "replaces_native_thread",
2253 failResumeAfterRollback: false,
2254 history: "plain",
2255 delivers: ["favourite colour is teal"],
2256 withholds: ["favourite animal is an otter"],
2257 },
2258 {
2259 name: "a native rollback whose next resume fails",
2260 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
2261 driver: CLAUDE_DRIVER,
2262 capabilities: ClaudeProviderCapabilitiesV2,
2263 modelSelection: CLAUDE_MODEL_SELECTION,
2264 rollback: "truncates",
2265 failResumeAfterRollback: true,
2266 history: "plain",
2267 delivers: ["favourite colour is teal"],
2268 withholds: ["favourite animal is an otter"],
2269 },
2270 {
2271 // Pi forks into a new native thread that still holds the kept turns.
2272 name: "a native rollback that forks a new native thread",
2273 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
2274 driver: CLAUDE_DRIVER,
2275 capabilities: ClaudeProviderCapabilitiesV2,
2276 modelSelection: CLAUDE_MODEL_SELECTION,
2277 rollback: "replaces_native_thread",
2278 failResumeAfterRollback: false,
2279 history: "plain",
2280 delivers: [],
2281 withholds: ["favourite colour is teal", "favourite animal is an otter"],
2282 },
2283 {
2284 name: "an ACP rollback where no attempt recorded its native session",
2285 instanceId: GROK_MODEL_SELECTION.instanceId,
2286 driver: GROK_DRIVER,
2287 capabilities: AcpProviderCapabilitiesV2,
2288 modelSelection: GROK_MODEL_SELECTION,
2289 rollback: "replaces_native_thread",
2290 failResumeAfterRollback: false,
2291 history: "legacy_all_turns",
2292 delivers: ["favourite colour is teal"],
2293 withholds: ["favourite animal is an otter"],
2294 },
2295 {
2296 name: "an ACP rollback of a forked thread to its baseline",
2297 instanceId: GROK_MODEL_SELECTION.instanceId,
2298 driver: GROK_DRIVER,
2299 capabilities: AcpProviderCapabilitiesV2,
2300 modelSelection: GROK_MODEL_SELECTION,
2301 rollback: "replaces_native_thread",
2302 failResumeAfterRollback: false,
2303 history: "forked_to_baseline",
2304 delivers: ["home town is Oslo"],
2305 withholds: ["favourite colour is teal", "favourite animal is an otter"],
2306 },
2307 {
2308 name: "an ACP rollback in a thread restarted with fresh context",
2309 instanceId: GROK_MODEL_SELECTION.instanceId,
2310 driver: GROK_DRIVER,
2311 capabilities: AcpProviderCapabilitiesV2,
2312 modelSelection: GROK_MODEL_SELECTION,
2313 rollback: "replaces_native_thread",
2314 failResumeAfterRollback: false,
2315 history: "after_fresh_context",
2316 delivers: ["favourite colour is teal"],
2317 withholds: ["home town is Oslo", "favourite animal is an otter"],
2318 },
2319 ] as const)(
2320 "gives the agent the kept conversation and not the rewound turn after $name",
2321 (scenario) =>
2322 Effect.scoped(
2323 Effect.gen(function* () {
2324 const name = `rollback-kept-context-${scenario.driver}-${scenario.history}`;
2325 const cwd = yield* checkpointWorkspace(name);
2326 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
2327 const injectedHistory = yield* Ref.make<ReadonlyArray<unknown>>([]);
2328 const failResumeOnce = yield* Ref.make(false);
2329 const nativeThreadGeneration = yield* Ref.make(0);
2330 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
2331 makeTestAdapter({
2332 instanceId: scenario.instanceId,
2333 driver: scenario.driver,
2334 capabilities: scenario.capabilities,
2335 modelSelection: scenario.modelSelection,
2336 responseByRunOrdinal: { 1: "noted", 2: "noted", 3: "noted" },
2337 capturedTurns,
2338 injectedHistory,
2339 failResumeOnce,
2340 nativeThreadGeneration,
2341 rollback: scenario.rollback,
2342 }),
2343 ]);
2344 yield* Effect.gen(function* () {
2345 const orchestrator = yield* Orchestrator.OrchestratorV2;
2346 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
2347 const runToCompletion = Effect.fn("runToCompletion")(function* (
2348 ordinal: number,
2349 text: string,
2350 thread: ThreadId = threadId,
2351 freshContext = false,
2352 ) {
2353 yield* orchestrator.dispatch({
2354 type: "message.dispatch",
2355 commandId: CommandId.make(`${thread}:${ordinal}`),
2356 threadId: thread,
2357 messageId: MessageId.make(`${thread}:${ordinal}`),
2358 createdBy: "user",
2359 creationSource: "web",
2360 text,
2361 attachments: [],
2362 modelSelection: scenario.modelSelection,
2363 ...(freshContext ? { freshContext: true } : {}),
2364 dispatchMode: { type: "start_immediately" },
2365 });
2366 yield* orchestrator.streamStoredEvents.pipe(
2367 Stream.filter(
2368 ({ event }) =>
2369 event.type === "run.updated" &&
2370 event.threadId === thread &&
2371 event.payload.ordinal === ordinal &&
2372 (event.payload.status === "completed" || event.payload.status === "failed"),
2373 ),
2374 Stream.runHead,
2375 );
2376 yield* worker.drain();
2377 });
2378 const createThread = (thread: ThreadId) =>
2379 orchestrator.dispatch({
2380 type: "thread.create",
2381 commandId: CommandId.make(`${thread}:create`),
2382 threadId: thread,
2383 projectId,
2384 createdBy: "user",
2385 creationSource: "web",
2386 title: "Edit from here",
2387 modelSelection: scenario.modelSelection,
2388 runtimeMode: "full-access",
2389 interactionMode: "default",
2390 branch: null,
2391 worktreePath: null,
2392 });
2393 if (scenario.history === "forked_to_baseline") {
2394 const sourceThreadId = ThreadId.make(`${name}:source`);
2395 yield* createThread(sourceThreadId);
2396 yield* runToCompletion(
2397 1,
2398 "My home town is Oslo. Reply with only the word: noted",
2399 sourceThreadId,
2400 );
2401 yield* orchestrator.dispatch({
2402 type: "thread.fork",
2403 commandId: CommandId.make(`${name}:fork`),
2404 sourceThreadId,
2405 targetThreadId: threadId,
2406 sourcePoint: {
2407 type: "run",
2408 runId: (yield* orchestrator.getThreadProjection(sourceThreadId)).runs[0]!.id,
2409 },
2410 createdBy: "user",
2411 creationSource: "web",
2412 });
2413 } else {
2414 yield* createThread(threadId);
2415 }
2416 // Runs before a fresh-context run never reach a provider again.
2417 const first = scenario.history === "after_fresh_context" ? 2 : 1;
2418 if (first === 2) {
2419 yield* runToCompletion(1, "My home town is Oslo. Reply with only the word: noted");
2420 }
2421 yield* runToCompletion(
2422 first,
2423 "My favourite colour is teal. Reply with only the word: noted",
2424 threadId,
2425 first === 2,
2426 );
2427 yield* runToCompletion(
2428 first + 1,
2429 "My favourite animal is an otter. Reply with only the word: noted",
2430 );
2431 const beforeRollback = yield* orchestrator.getThreadProjection(threadId);
2432 if (scenario.history === "legacy_all_turns") {
2433 yield* (yield* EventSink.EventSinkV2).write({
2434 events: beforeRollback.attempts.map(
2435 ({ nativeThreadId: _nativeThreadId, ...legacy }, index) => ({
2436 id: EventId.make(`${name}:legacy-attempt:${index}`),
2437 type: "run-attempt.updated" as const,
2438 threadId,
2439 occurredAt: beforeRollback.thread.createdAt,
2440 payload: legacy,
2441 }),
2442 ),
2443 });
2444 }
2445 const keptOrdinal = scenario.history === "forked_to_baseline" ? 0 : first;
2446 const keptCheckpoint = beforeRollback.checkpoints.find(
2447 (checkpoint) =>
2448 (checkpoint.appRunOrdinal ?? 0) === keptOrdinal && checkpoint.status === "ready",
2449 );
2450 if (keptCheckpoint === undefined) {
2451 return yield* Effect.die("Expected a checkpoint after the first turn.");
2452 }
2453 yield* orchestrator.dispatch({
2454 type: "checkpoint.rollback",
2455 commandId: CommandId.make(`${name}:rollback`),
2456 threadId,
2457 scopeId: keptCheckpoint.scopeId,
2458 checkpointId: keptCheckpoint.id,
2459 restoreFiles: false,
2460 });
2461 yield* orchestrator.streamStoredEvents.pipe(
2462 Stream.filter(
2463 ({ event }) =>
2464 (event.type === "run.updated" &&
2465 event.payload.ordinal === first + 1 &&
2466 event.payload.status === "rolled_back") ||
2467 (event.type === "thread.metadata-updated" &&
2468 event.payload.rollbackFailure != null),
2469 ),
2470 Stream.runHead,
2471 );
2472 yield* worker.drain();
2473 assert.deepEqual(
2474 (yield* orchestrator.getThreadProjection(threadId)).runs.map((run) => run.status),
2475 scenario.history === "forked_to_baseline"
2476 ? ["rolled_back", "rolled_back"]
2477 : [...(first === 2 ? ["completed"] : []), "completed", "rolled_back"],
2478 );
2479 if (scenario.failResumeAfterRollback) {
2480 const providerSessionId = (yield* orchestrator.getThreadProjection(threadId))
2481 .providerThreads[0]?.providerSessionId;
2482 if (providerSessionId == null) {
2483 return yield* Effect.die("Expected a provider session after the rollback.");
2484 }
2485 yield* orchestrator.dispatch({
2486 type: "provider-session.detach",
2487 commandId: CommandId.make(`${name}:detach`),
2488 threadId,
2489 providerSessionId,
2490 });
2491 yield* worker.drain();
2492 yield* Ref.set(failResumeOnce, true);
2493 }
2494 const historyBeforeRun3 = (yield* Ref.get(injectedHistory)).length;
2495 yield* runToCompletion(first + 2, "What did I say first?");
2496
2497 const run3 = (yield* Ref.get(capturedTurns)).at(-1);
2498 const delivered =
2499 (run3?.text ?? "") +
2500 (yield* encodeJson((yield* Ref.get(injectedHistory)).slice(historyBeforeRun3)));
2501 for (const kept of scenario.delivers) {
2502 assert.include(delivered, kept, "the agent's session lacks this kept turn");
2503 }
2504 for (const withheld of scenario.withholds) {
2505 assert.notInclude(delivered, withheld, "the session holds it or it was rewound");
2506 }
2507 if (scenario.name === "an ACP rollback that starts a fresh agent session") {
2508 const recoveries = (projection: OrchestrationV2ThreadProjection) =>
2509 projection.contextHandoffs.filter(
2510 (handoff) => handoff.strategy === "full_thread_summary",
2511 ).length;
2512 const recoveriesBeforeRun4 = recoveries(
2513 yield* orchestrator.getThreadProjection(threadId),
2514 );
2515 const sessionsBeforeRun4 = yield* Ref.get(nativeThreadGeneration);
2516 yield* runToCompletion(4, "And what did I say second?");
2517 assert.strictEqual(
2518 recoveries(yield* orchestrator.getThreadProjection(threadId)),
2519 recoveriesBeforeRun4,
2520 "the recovered session holds the kept turns, so the next turn replays nothing",
2521 );
2522 assert.strictEqual(yield* Ref.get(nativeThreadGeneration), sessionsBeforeRun4);
2523 }
2524 }).pipe(
2525 Effect.provide(
2526 ProviderReplayHarness.layerWithRegistry(
2527 {
2528 name,
2529 runtimePolicyOverride: {
2530 cwd,
2531 approvalPolicy: "never",
2532 sandboxPolicy: { type: "readOnly" },
2533 },
2534 },
2535 layerRegistry,
2536 ),
2537 ),
2538 );
2539 }),
2540 ),
2541 );
2542
2543 it.live.each([
2544 ["background work", new ClaudeBackgroundWorkBlocksQueryReplacementError()],
2545 ["an unreadable append file", new ClaudeAppendSystemPromptFileError({ path: "missing.md" })],
2546 [
2547 "an MCP registration timeout",
2548 new ClaudeAgentSdkQueryRunnerError({
2549 method: "setMcpServers",
2550 cause: new ClaudeMcpRegistrationTimeoutError(),
2551 }),
2552 ],
2553 // Claude Code ends the query with "No message found with message.uuid" before the prompt.
2554 [
2555 "a resume point Claude rejects",
2556 new ProviderAdapter.ProviderAdapterProtocolError({
2557 driver: CLAUDE_DRIVER,
2558 detail: "Claude provider query ended before the turn could start.",
2559 }),
2560 ],
2561 // Injected history is text only, so a refusal after injection must not settle the image.
2562 [
2563 "background work after native history injection",
2564 new ClaudeBackgroundWorkBlocksQueryReplacementError(),
2565 true,
2566 ],
2567 [
2568 "background work followed by a lost native session",
2569 new ClaudeBackgroundWorkBlocksQueryReplacementError(),
2570 false,
2571 true,
2572 ],
2573 ] as const)("retains missed images after %s", ([, refusalCause, inject, loseSession]) =>
2574 Effect.gen(function* () {
2575 const cwd = yield* checkpointWorkspace("refused-start-keeps-native");
2576 const failResumeOnce = yield* Ref.make(false);
2577 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
2578 const injectedHistory = yield* Ref.make<ReadonlyArray<unknown>>([]);
2579 const refuseStarts = yield* Ref.make(0);
2580 const generation = yield* Ref.make(0);
2581 const registry = ProviderAdapterRegistry.layerFromAdapters([
2582 makeTestAdapter({
2583 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
2584 driver: CLAUDE_DRIVER,
2585 capabilities: ClaudeProviderCapabilitiesV2,
2586 modelSelection: CLAUDE_MODEL_SELECTION,
2587 responseByRunOrdinal: {},
2588 capturedTurns,
2589 refuseStarts,
2590 refusalCause,
2591 failResumeOnce,
2592 nativeThreadGeneration: generation,
2593 ...(inject === true ? { injectedHistory } : {}),
2594 }),
2595 ]);
2596 yield* Effect.gen(function* () {
2597 const orchestrator = yield* Orchestrator.OrchestratorV2;
2598 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
2599 const missedImage: ChatAttachment = {
2600 type: "image",
2601 id: "refused-start-image",
2602 name: "screenshot.png",
2603 mimeType: "image/png",
2604 sizeBytes: 100_000,
2605 };
2606 const run = Effect.fn("run")(function* (ordinal: number) {
2607 yield* orchestrator.dispatch({
2608 type: "message.dispatch",
2609 commandId: CommandId.make(`refused-start:${ordinal}`),
2610 threadId,
2611 messageId: MessageId.make(`refused-start:${ordinal}`),
2612 createdBy: "user",
2613 creationSource: "web",
2614 text: `Request ${ordinal}`,
2615 attachments: ordinal === 2 ? [missedImage] : [],
2616 modelSelection: CLAUDE_MODEL_SELECTION,
2617 dispatchMode: { type: "start_immediately" },
2618 });
2619 yield* orchestrator.streamStoredEvents.pipe(
2620 Stream.filter(
2621 ({ event }) =>
2622 event.type === "run.updated" &&
2623 event.payload.ordinal === ordinal &&
2624 (event.payload.status === "completed" || event.payload.status === "failed"),
2625 ),
2626 Stream.runHead,
2627 );
2628 yield* worker.drain();
2629 return (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status;
2630 });
2631 yield* orchestrator.dispatch({
2632 type: "thread.create",
2633 commandId: CommandId.make("refused-start:create"),
2634 threadId,
2635 projectId,
2636 createdBy: "user",
2637 creationSource: "web",
2638 title: "Refused start",
2639 modelSelection: CLAUDE_MODEL_SELECTION,
2640 runtimeMode: "full-access",
2641 interactionMode: "default",
2642 branch: null,
2643 worktreePath: null,
2644 });
2645 assert.equal(yield* run(1), "completed");
2646 yield* Ref.set(refuseStarts, 2);
2647 assert.equal(yield* run(2), "failed");
2648 assert.equal(yield* run(3), "failed");
2649 yield* Ref.set(failResumeOnce, loseSession === true);
2650 assert.equal(yield* run(4), "completed");
2651
2652 const turns = yield* Ref.get(capturedTurns);
2653 assert.equal(turns.length, 2);
2654 assert.equal(yield* Ref.get(generation), loseSession === true ? 2 : 1);
2655 if (loseSession !== true) {
2656 assert.equal(turns[1]?.nativeThreadId, turns[0]?.nativeThreadId);
2657 }
2658 const delivered = `${turns[1]?.text}${JSON.stringify(yield* Ref.get(injectedHistory))}`;
2659 assert.include(delivered, "Request 2");
2660 assert.include(delivered, "Request 3");
2661 assert.deepEqual(turns[1]?.attachments, [missedImage]);
2662 }).pipe(
2663 Effect.provide(
2664 ProviderReplayHarness.layerWithRegistry(
2665 {
2666 name: "refused-start-keeps-native",
2667 runtimePolicyOverride: {
2668 cwd,
2669 approvalPolicy: "never",
2670 sandboxPolicy: { type: "readOnly" },
2671 },
2672 },
2673 registry,
2674 ),
2675 ),
2676 );
2677 }),
2678 );
2679
2680 it.live.each(
2681 (
2682 [
2683 ["failed", false, false, false, false],
2684 ["failed", false, false, false, true],
2685 ["failed", true, true, true],
2686 ["interrupted", false, true, false],
2687 ["interrupted", true, false, true],
2688 ["cancelled", false, false, false],
2689 ["cancelled", true, true, false],
2690 ] as const
2691 ).map(
2692 ([status, queued, returning, native, broken]) =>
2693 [
2694 `${status}${broken ? " broken" : ""} context ${queued ? "through the queue" : "immediately"} to ${returning ? "a returning" : "a new"} provider via ${native ? "native history" : "text"}`,
2695 { status, queued, returning, native, broken },
2696 ] as const,
2697 ),
2698 )("hands off %s", ([, { status, queued, returning, native, broken }]) =>
2699 Effect.gen(function* () {
2700 const cwd = yield* checkpointWorkspace(`handoff-${status}-${queued}-${returning}`);
2701 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
2702 const injectedHistory = yield* Ref.make<ReadonlyArray<unknown>>([]);
2703 const started = yield* Deferred.make<void>();
2704 const release = yield* Deferred.make<void>();
2705 const sourceOrdinal = returning ? 2 : 1;
2706 const sourceSelection = returning ? CLAUDE_MODEL_SELECTION : CODEX_MODEL_SELECTION;
2707 const targetSelection = returning ? CODEX_MODEL_SELECTION : CLAUDE_MODEL_SELECTION;
2708 const generation = yield* Ref.make(0);
2709 const originalPrompt = "Keep the release marker violet and preserve the existing API.";
2710 const partialResponse = "I checked the API and found the release configuration.";
2711 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters(
2712 (
2713 [
2714 [CODEX_MODEL_SELECTION, CODEX_DRIVER, CodexProviderCapabilitiesV2],
2715 [CLAUDE_MODEL_SELECTION, CLAUDE_DRIVER, ClaudeProviderCapabilitiesV2],
2716 ] as const
2717 ).map(([modelSelection, driver, capabilities]) =>
2718 makeTestAdapter({
2719 instanceId: modelSelection.instanceId,
2720 driver,
2721 capabilities,
2722 modelSelection,
2723 responseByRunOrdinal: { [sourceOrdinal]: partialResponse },
2724 capturedTurns,
2725 ...(native && modelSelection.instanceId === targetSelection.instanceId
2726 ? { injectedHistory }
2727 : {}),
2728 ...(modelSelection.instanceId === sourceSelection.instanceId
2729 ? {
2730 failedRunOrdinals: new Set(status === "failed" ? [sourceOrdinal] : []),
2731 ...(broken
2732 ? {
2733 brokenRunOrdinals: new Set([sourceOrdinal]),
2734 nativeThreadGeneration: generation,
2735 }
2736 : {}),
2737 interruptedRunOrdinals: new Set(status === "interrupted" ? [sourceOrdinal] : []),
2738 cancelledRunOrdinals: new Set(status === "cancelled" ? [sourceOrdinal] : []),
2739 holdRunOrdinal: sourceOrdinal,
2740 holdFirstTurn: started,
2741 releaseFirstTurn: release,
2742 }
2743 : {}),
2744 }),
2745 ),
2746 );
2747 yield* Effect.gen(function* () {
2748 const orchestrator = yield* Orchestrator.OrchestratorV2;
2749 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
2750 const waitForRun = (
2751 ordinal: number,
2752 expectedStatus: "completed" | "failed" | "interrupted" | "cancelled",
2753 ) =>
2754 orchestrator.streamStoredEvents.pipe(
2755 Stream.filter(
2756 ({ event }) =>
2757 event.type === "run.updated" &&
2758 event.threadId === threadId &&
2759 event.payload.ordinal === ordinal &&
2760 event.payload.status === expectedStatus,
2761 ),
2762 Stream.runHead,
2763 Effect.andThen(worker.drain()),
2764 );
2765 const dispatch = (
2766 key: string,
2767 text: string,
2768 modelSelection: ModelSelection,
2769 queue = false,
2770 ) =>
2771 orchestrator.dispatch({
2772 type: "message.dispatch",
2773 commandId: CommandId.make(`command:handoff:${key}`),
2774 threadId,
2775 messageId: MessageId.make(`message:handoff:${key}`),
2776 createdBy: "user",
2777 creationSource: "web",
2778 text,
2779 attachments: [],
2780 modelSelection,
2781 dispatchMode: { type: queue ? "queue_after_active" : "start_immediately" },
2782 });
2783 yield* orchestrator.dispatch({
2784 type: "thread.create",
2785 commandId: CommandId.make("command:handoff:create"),
2786 threadId,
2787 projectId,
2788 createdBy: "user",
2789 creationSource: "web",
2790 title: "Interrupted provider handoff",
2791 modelSelection: CODEX_MODEL_SELECTION,
2792 runtimeMode: "full-access",
2793 interactionMode: "default",
2794 branch: null,
2795 worktreePath: null,
2796 });
2797 if (returning) {
2798 yield* dispatch("initial", "Earlier successful request.", CODEX_MODEL_SELECTION);
2799 yield* waitForRun(1, "completed");
2800 }
2801 yield* dispatch("source", originalPrompt, sourceSelection);
2802 yield* Deferred.await(started);
2803 if (queued) {
2804 yield* dispatch("target", "Continue", targetSelection, true);
2805 assert.equal(
2806 (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
2807 "queued",
2808 );
2809 }
2810 yield* Deferred.succeed(release, undefined);
2811 yield* waitForRun(sourceOrdinal, status);
2812 if (!queued) {
2813 yield* dispatch("target", "Continue", targetSelection);
2814 }
2815 yield* waitForRun(sourceOrdinal + 1, "completed");
2816 const projection = yield* orchestrator.getThreadProjection(threadId);
2817 const targetRun = projection.runs.at(-1)!;
2818 const handoff = projection.contextHandoffs.find(
2819 (candidate) => candidate.id === targetRun.contextHandoffId,
2820 );
2821 assert.isDefined(handoff);
2822 assert.equal(
2823 handoff?.strategy,
2824 returning ? "delta_since_target_last_seen" : "full_thread_summary",
2825 );
2826 assert.deepEqual(handoff?.coveredRunOrdinals, {
2827 from: sourceOrdinal,
2828 to: sourceOrdinal,
2829 });
2830 const delivered = (yield* Ref.get(capturedTurns)).at(-1)!;
2831 const history = yield* Ref.get(injectedHistory);
2832 const deliveredHistory = native ? yield* encodeJson(history) : delivered.text;
2833 assert.include(deliveredHistory, originalPrompt);
2834 assert.include(deliveredHistory, partialResponse);
2835 assert.include(deliveredHistory, `run-status=${status}`);
2836 if (native) {
2837 assert.equal(delivered.text, "Continue");
2838 assert.notInclude(deliveredHistory, '"text":"Continue"');
2839 assert.include(deliveredHistory, '"role":"assistant"');
2840 assert.include(deliveredHistory, '"role":"user"');
2841 assert.equal(handoff?.delivery?.status, "injected");
2842 } else {
2843 assert.include(delivered.text, "User message:\nContinue");
2844 assert.equal(handoff?.delivery?.status, "inline");
2845 }
2846 assert.notInclude(deliveredHistory, "Earlier successful request.");
2847 yield* dispatch("back", "Finish the remaining work", sourceSelection);
2848 yield* waitForRun(sourceOrdinal + 2, "completed");
2849 const back = (yield* Ref.get(capturedTurns)).at(-1)!;
2850 if (broken) {
2851 const source = (yield* Ref.get(capturedTurns)).find(
2852 (turn) => turn.modelSelection.instanceId === sourceSelection.instanceId,
2853 )!;
2854 assert.notEqual(back.nativeThreadId, source.nativeThreadId);
2855 assert.include(back.text, originalPrompt);
2856 assert.include(back.text, partialResponse);
2857 } else {
2858 assert.notInclude(back.text, originalPrompt);
2859 assert.notInclude(back.text, partialResponse);
2860 }
2861 }).pipe(
2862 Effect.provide(
2863 ProviderReplayHarness.layerWithRegistry(
2864 {
2865 name: `handoff-${status}-${queued}-${returning}`,
2866 runtimePolicyOverride: {
2867 cwd,
2868 approvalPolicy: "never",
2869 sandboxPolicy: {
2870 type: "readOnly",
2871 access: { type: "fullAccess" },
2872 networkAccess: false,
2873 },
2874 },
2875 },
2876 layerRegistry,
2877 ),
2878 ),
2879 );
2880 }),
2881 );
2882
2883 // regression: #15993 t3_worktree_handoff releases the Claude session that runs it. Nothing
2884 // was left to report how the call ended, so it stayed running and the thread stayed Waiting.
2885 it.live("interrupts the handoff tool call that releases its own session", () =>
2886 Effect.gen(function* () {
2887 const key = "handoff-cuts-off-tool-call";
2888 const worktreePath = yield* checkpointWorkspace(key);
2889 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
2890 const started = yield* Deferred.make<void>();
2891 const toolCallId = TurnItemId.make(`turn-item:${key}`);
2892 const scenarioThreadId = ThreadId.make(`thread:${key}`);
2893 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
2894 makeTestAdapter({
2895 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
2896 driver: CLAUDE_DRIVER,
2897 capabilities: ClaudeProviderCapabilitiesV2,
2898 modelSelection: CLAUDE_MODEL_SELECTION,
2899 responseByRunOrdinal: {},
2900 capturedTurns,
2901 holdFirstTurn: started,
2902 heldRunningToolCall: {
2903 id: toolCallId,
2904 toolName: "mcp__t3-code__t3_worktree_handoff",
2905 },
2906 backgroundTasksEndWithProcess: true,
2907 }),
2908 makeTestAdapter({
2909 instanceId: CODEX_MODEL_SELECTION.instanceId,
2910 driver: CODEX_DRIVER,
2911 capabilities: CodexProviderCapabilitiesV2,
2912 modelSelection: CODEX_MODEL_SELECTION,
2913 responseByRunOrdinal: {},
2914 capturedTurns,
2915 }),
2916 ]);
2917 yield* Effect.gen(function* () {
2918 const orchestrator = yield* Orchestrator.OrchestratorV2;
2919 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
2920 const awaitEvent = (predicate: (event: OrchestrationV2DomainEvent) => boolean) =>
2921 orchestrator.streamDomainEvents.pipe(
2922 Stream.filter(predicate),
2923 Stream.take(1),
2924 Stream.runDrain,
2925 Effect.forkScoped,
2926 );
2927 yield* orchestrator.dispatch({
2928 type: "thread.create",
2929 createdBy: "user",
2930 creationSource: "web",
2931 commandId: CommandId.make(`command:${key}:create`),
2932 threadId: scenarioThreadId,
2933 projectId: ProjectId.make(`project:${key}`),
2934 title: "Tool call releases its session",
2935 modelSelection: CLAUDE_MODEL_SELECTION,
2936 runtimeMode: "full-access",
2937 interactionMode: "default",
2938 branch: null,
2939 worktreePath: null,
2940 });
2941 const toolCallRunning = yield* awaitEvent(
2942 (event) =>
2943 event.type === "turn-item.updated" &&
2944 event.payload.id === toolCallId &&
2945 event.payload.status === "running",
2946 );
2947 yield* orchestrator.dispatch({
2948 type: "message.dispatch",
2949 createdBy: "user",
2950 creationSource: "web",
2951 commandId: CommandId.make(`command:${key}:message`),
2952 threadId: scenarioThreadId,
2953 messageId: MessageId.make(`message:${key}`),
2954 text: "Release this session",
2955 attachments: [],
2956 modelSelection: CLAUDE_MODEL_SELECTION,
2957 dispatchMode: { type: "start_immediately" },
2958 });
2959 yield* Deferred.await(started);
2960 yield* Fiber.join(toolCallRunning);
2961
2962 // The handoff itself is not a failure: the run completes once the session closes.
2963 const runSettled = yield* awaitEvent(
2964 (event) => event.type === "run.updated" && event.payload.status === "waiting",
2965 );
2966 yield* orchestrator.dispatch({
2967 type: "thread.metadata.update",
2968 commandId: CommandId.make(`command:${key}:release`),
2969 threadId: scenarioThreadId,
2970 branch: `t3/${key}`,
2971 worktreePath,
2972 expectedWorktreePath: null,
2973 });
2974 yield* worker.drain();
2975 yield* Fiber.join(runSettled);
2976 yield* worker.drain();
2977
2978 const projection = yield* orchestrator.getThreadProjection(scenarioThreadId);
2979 assert.equal(projection.runs.at(-1)?.status, "completed");
2980 const toolCall = projection.turnItems.find((item) => item.id === toolCallId);
2981 assert.equal(toolCall?.status, "interrupted");
2982 assert.isNotNull(toolCall?.completedAt);
2983 assert.deepEqual(
2984 derivePendingBackgroundWork({
2985 latestRun: projection.runs.at(-1),
2986 providerThreads: projection.providerThreads,
2987 turnItems: projection.turnItems,
2988 activeProviderThreadId: projection.thread.activeProviderThreadId,
2989 runs: projection.runs,
2990 }),
2991 [],
2992 );
2993 }).pipe(
2994 Effect.provide(ProviderReplayHarness.layerWithRegistry({ name: key }, layerRegistry)),
2995 );
2996 }),
2997 );
2998
2999 it.live("checks the queued provider's capability while the current provider stays running", () =>
3000 Effect.gen(function* () {
3001 for (const scenario of [
3002 { activeSupportsQueue: false, selectedSupportsQueue: true },
3003 { activeSupportsQueue: true, selectedSupportsQueue: false },
3004 ]) {
3005 const key = `active-${scenario.activeSupportsQueue}-selected-${scenario.selectedSupportsQueue}`;
3006 const cwd = yield* checkpointWorkspace(`queued-capability-${key}`);
3007 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
3008 const started = yield* Deferred.make<void>();
3009 const scenarioThreadId = ThreadId.make(`thread:queued-capability:${key}`);
3010 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
3011 makeTestAdapter({
3012 instanceId: CODEX_MODEL_SELECTION.instanceId,
3013 driver: CODEX_DRIVER,
3014 capabilities: {
3015 ...CodexProviderCapabilitiesV2,
3016 turns: {
3017 ...CodexProviderCapabilitiesV2.turns,
3018 supportsQueuedMessages: scenario.activeSupportsQueue,
3019 },
3020 },
3021 modelSelection: CODEX_MODEL_SELECTION,
3022 responseByRunOrdinal: {},
3023 capturedTurns,
3024 holdFirstTurn: started,
3025 }),
3026 makeTestAdapter({
3027 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
3028 driver: CLAUDE_DRIVER,
3029 capabilities: {
3030 ...ClaudeProviderCapabilitiesV2,
3031 turns: {
3032 ...ClaudeProviderCapabilitiesV2.turns,
3033 supportsQueuedMessages: scenario.selectedSupportsQueue,
3034 },
3035 },
3036 modelSelection: CLAUDE_MODEL_SELECTION,
3037 responseByRunOrdinal: {},
3038 capturedTurns,
3039 }),
3040 ]);
3041 yield* Effect.gen(function* () {
3042 const orchestrator = yield* Orchestrator.OrchestratorV2;
3043 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
3044 yield* orchestrator.dispatch({
3045 type: "thread.create",
3046 createdBy: "user",
3047 creationSource: "web",
3048 commandId: CommandId.make(`command:queued-capability:create:${key}`),
3049 threadId: scenarioThreadId,
3050 projectId: ProjectId.make(`project:queued-capability:${key}`),
3051 title: "Queued capability",
3052 modelSelection: CODEX_MODEL_SELECTION,
3053 runtimeMode: "full-access",
3054 interactionMode: "default",
3055 branch: null,
3056 worktreePath: cwd,
3057 });
3058 yield* orchestrator.dispatch({
3059 type: "message.dispatch",
3060 createdBy: "user",
3061 creationSource: "web",
3062 commandId: CommandId.make(`command:queued-capability:first:${key}`),
3063 threadId: scenarioThreadId,
3064 messageId: MessageId.make(`message:queued-capability:first:${key}`),
3065 text: "Current Codex turn",
3066 attachments: [],
3067 modelSelection: CODEX_MODEL_SELECTION,
3068 dispatchMode: { type: "start_immediately" },
3069 });
3070 yield* Deferred.await(started);
3071 yield* worker.drain();
3072 const queue = orchestrator.dispatch({
3073 type: "message.dispatch",
3074 createdBy: "user",
3075 creationSource: "web",
3076 commandId: CommandId.make(`command:queued-capability:claude:${key}`),
3077 threadId: scenarioThreadId,
3078 messageId: MessageId.make(`message:queued-capability:claude:${key}`),
3079 text: "Queued Claude turn",
3080 attachments: [],
3081 modelSelection: CLAUDE_MODEL_SELECTION,
3082 dispatchMode: { type: "queue_after_active" },
3083 });
3084 if (scenario.selectedSupportsQueue) {
3085 yield* queue;
3086 } else {
3087 const error = yield* queue.pipe(Effect.flip);
3088 assert.instanceOf(error, Orchestrator.OrchestratorDispatchError);
3089 assert.instanceOf(error.cause, CommandPolicyCapabilityUnsupportedError);
3090 assert.equal(error.cause.capability, "queued_messages");
3091 }
3092 const projection = yield* orchestrator.getThreadProjection(scenarioThreadId);
3093 assert.deepEqual(
3094 projection.runs.map((run) => run.status),
3095 scenario.selectedSupportsQueue ? ["running", "queued"] : ["running"],
3096 );
3097 assert.deepEqual(projection.thread.modelSelection, CODEX_MODEL_SELECTION);
3098 }).pipe(
3099 Effect.provide(
3100 ProviderReplayHarness.layerWithRegistry(
3101 {
3102 name: `queued-capability-${key}`,
3103 runtimePolicyOverride: {
3104 cwd,
3105 approvalPolicy: "never",
3106 sandboxPolicy: {
3107 type: "readOnly",
3108 access: { type: "fullAccess" },
3109 networkAccess: false,
3110 },
3111 },
3112 },
3113 layerRegistry,
3114 ),
3115 ),
3116 );
3117 }
3118 }),
3119 );
3120
3121 const steerProviderSwitchHarness = Effect.fn("steerProviderSwitchHarness")(function* (
3122 key: string,
3123 holdOpenSession?: Deferred.Deferred<void>,
3124 ) {
3125 const cwd = yield* checkpointWorkspace(`steer-provider-switch-${key}`);
3126 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
3127 const started = yield* Deferred.make<void>();
3128 const release = yield* Deferred.make<void>();
3129 const threadId = ThreadId.make(`thread:steer-provider-switch:${key}`);
3130 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
3131 makeTestAdapter({
3132 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
3133 driver: CLAUDE_DRIVER,
3134 capabilities: ClaudeProviderCapabilitiesV2,
3135 modelSelection: CLAUDE_MODEL_SELECTION,
3136 responseByRunOrdinal: { 1: "Claude finished the current turn" },
3137 capturedTurns,
3138 holdFirstTurn: started,
3139 releaseFirstTurn: release,
3140 ...(holdOpenSession === undefined ? {} : { holdOpenSession }),
3141 }),
3142 makeTestAdapter({
3143 instanceId: CODEX_MODEL_SELECTION.instanceId,
3144 driver: CODEX_DRIVER,
3145 capabilities: CodexProviderCapabilitiesV2,
3146 modelSelection: CODEX_MODEL_SELECTION,
3147 responseByRunOrdinal: { 2: "Codex continued" },
3148 capturedTurns,
3149 }),
3150 ]);
3151 const layer = ProviderReplayHarness.layerWithRegistry(
3152 {
3153 name: `steer-provider-switch-${key}`,
3154 runtimePolicyOverride: {
3155 cwd,
3156 approvalPolicy: "never",
3157 sandboxPolicy: {
3158 type: "readOnly",
3159 access: { type: "fullAccess" },
3160 networkAccess: false,
3161 },
3162 },
3163 },
3164 layerRegistry,
3165 );
3166 const startClaudeTurn = Effect.gen(function* () {
3167 const orchestrator = yield* Orchestrator.OrchestratorV2;
3168 yield* orchestrator.dispatch({
3169 type: "thread.create",
3170 createdBy: "user",
3171 creationSource: "web",
3172 commandId: CommandId.make(`command:steer-provider-switch:${key}:create`),
3173 threadId,
3174 projectId: ProjectId.make(`project:steer-provider-switch:${key}`),
3175 title: "Steer provider switch",
3176 modelSelection: CLAUDE_MODEL_SELECTION,
3177 runtimeMode: "full-access",
3178 interactionMode: "default",
3179 branch: null,
3180 worktreePath: cwd,
3181 });
3182 yield* orchestrator.dispatch({
3183 type: "message.dispatch",
3184 createdBy: "user",
3185 creationSource: "web",
3186 commandId: CommandId.make(`command:steer-provider-switch:${key}:first`),
3187 threadId,
3188 messageId: MessageId.make(`message:steer-provider-switch:${key}:first`),
3189 text: "Current Claude turn",
3190 attachments: [],
3191 modelSelection: CLAUDE_MODEL_SELECTION,
3192 dispatchMode: { type: "start_immediately" },
3193 });
3194 });
3195 const markClaudeTurnRunning = Effect.gen(function* () {
3196 const orchestrator = yield* Orchestrator.OrchestratorV2;
3197 const eventSink = yield* EventSink.EventSinkV2;
3198 const [claudeRun] = (yield* orchestrator.getThreadProjection(threadId)).runs;
3199 const now = yield* DateTime.now;
3200 yield* eventSink.write({
3201 events: [
3202 {
3203 id: EventId.make(`event:steer-provider-switch:${key}:first-turn-running`),
3204 type: "provider-turn.updated",
3205 threadId,
3206 runId: claudeRun!.id,
3207 nodeId: claudeRun!.rootNodeId!,
3208 driver: CLAUDE_DRIVER,
3209 providerInstanceId: CLAUDE_MODEL_SELECTION.instanceId,
3210 occurredAt: now,
3211 payload: {
3212 // The adapter's own turn id, so its completion ends this turn.
3213 id: ProviderTurnId.make(`provider-turn:${CLAUDE_DRIVER}:${threadId}:1`),
3214 providerThreadId: claudeRun!.providerThreadId!,
3215 nodeId: claudeRun!.rootNodeId!,
3216 runAttemptId: claudeRun!.activeAttemptId!,
3217 nativeTurnRef: null,
3218 ordinal: 1,
3219 status: "running",
3220 startedAt: now,
3221 completedAt: null,
3222 },
3223 },
3224 ],
3225 });
3226 });
3227 const steerToCodex = Effect.gen(function* () {
3228 const orchestrator = yield* Orchestrator.OrchestratorV2;
3229 return yield* orchestrator.dispatch({
3230 type: "message.dispatch",
3231 createdBy: "user",
3232 creationSource: "web",
3233 commandId: CommandId.make(`command:steer-provider-switch:${key}:codex`),
3234 threadId,
3235 messageId: MessageId.make(`message:steer-provider-switch:${key}:codex`),
3236 text: "Continue with Codex",
3237 attachments: [],
3238 modelSelection: CODEX_MODEL_SELECTION,
3239 deliveryIntent: "steer",
3240 dispatchMode: { type: "start_immediately" },
3241 });
3242 });
3243 return {
3244 threadId,
3245 started,
3246 release,
3247 capturedTurns,
3248 layer,
3249 startClaudeTurn,
3250 markClaudeTurnRunning,
3251 steerToCodex,
3252 };
3253 });
3254
3255 it.live.each(["is running", "has no provider session yet"] as const)(
3256 "runs a provider switch steered into a Claude turn that %s after that turn",
3257 (phase) =>
3258 Effect.gen(function* () {
3259 const sessionGate = yield* Deferred.make<void>();
3260 if (phase === "is running") yield* Deferred.succeed(sessionGate, undefined);
3261 const harness = yield* steerProviderSwitchHarness(
3262 phase === "is running" ? "running" : "before-session",
3263 sessionGate,
3264 );
3265 yield* Effect.gen(function* () {
3266 const orchestrator = yield* Orchestrator.OrchestratorV2;
3267 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
3268 const waitForRun = (ordinal: number) =>
3269 orchestrator.streamStoredEvents.pipe(
3270 Stream.filter(
3271 ({ event }) =>
3272 event.type === "run.updated" &&
3273 event.threadId === harness.threadId &&
3274 event.payload.ordinal === ordinal &&
3275 event.payload.status === "completed",
3276 ),
3277 Stream.runHead,
3278 Effect.andThen(worker.drain()),
3279 );
3280 yield* harness.startClaudeTurn;
3281 if (phase === "is running") {
3282 yield* Deferred.await(harness.started);
3283 yield* worker.drain();
3284 yield* harness.markClaudeTurnRunning;
3285 } else {
3286 const beforeSession = yield* orchestrator.getThreadProjection(harness.threadId);
3287 assert.include(["preparing", "starting"], beforeSession.runs[0]?.status);
3288 assert.lengthOf(beforeSession.providerSessions, 0);
3289 }
3290 // Claude cannot restart its turn for new input, so the switch waits for the turn.
3291 yield* harness.steerToCodex;
3292 const queued = (yield* orchestrator.getThreadProjection(harness.threadId)).runs.at(-1);
3293 assert.equal(queued?.status, "queued");
3294 assert.equal(queued?.providerInstanceId, CODEX_MODEL_SELECTION.instanceId);
3295 assert.isUndefined(queued?.steerWhenReadyRunId);
3296
3297 yield* Deferred.succeed(sessionGate, undefined);
3298 yield* Deferred.await(harness.started);
3299 yield* Deferred.succeed(harness.release, undefined);
3300 yield* waitForRun(1);
3301 yield* waitForRun(2);
3302 const projection = yield* orchestrator.getThreadProjection(harness.threadId);
3303 assert.deepEqual(
3304 projection.runs.map((run) => [run.status, run.providerInstanceId]),
3305 [
3306 ["completed", CLAUDE_MODEL_SELECTION.instanceId],
3307 ["completed", CODEX_MODEL_SELECTION.instanceId],
3308 ],
3309 );
3310 assert.deepEqual(projection.thread.modelSelection, CODEX_MODEL_SELECTION);
3311 const handoff = projection.contextHandoffs.find(
3312 (candidate) => candidate.id === projection.runs[1]?.contextHandoffId,
3313 );
3314 assert.isDefined(handoff);
3315 const codexTurn = (yield* Ref.get(harness.capturedTurns)).at(-1)!;
3316 assert.include(codexTurn.text, "Continue with Codex");
3317 assert.include(codexTurn.text, "Current Claude turn");
3318 assert.include(codexTurn.text, "Claude finished the current turn");
3319 }).pipe(Effect.provide(harness.layer));
3320 }),
3321 );
3322
3323 it.live("stops a Claude turn for a /goal steered into it with another provider", () =>
3324 Effect.gen(function* () {
3325 const harness = yield* steerProviderSwitchHarness("goal");
3326 yield* Effect.gen(function* () {
3327 const orchestrator = yield* Orchestrator.OrchestratorV2;
3328 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
3329 yield* harness.startClaudeTurn;
3330 yield* Deferred.await(harness.started);
3331 yield* worker.drain();
3332 yield* harness.markClaudeTurnRunning;
3333 const [claudeRun] = (yield* orchestrator.getThreadProjection(harness.threadId)).runs;
3334 // An explicit steer without a delivery intent, as thread tools send it.
3335 yield* orchestrator.dispatch({
3336 type: "message.dispatch",
3337 createdBy: "user",
3338 creationSource: "web",
3339 commandId: CommandId.make("command:steer-provider-switch:goal:codex"),
3340 threadId: harness.threadId,
3341 messageId: MessageId.make("message:steer-provider-switch:goal:codex"),
3342 text: "/goal all tests pass",
3343 attachments: [],
3344 modelSelection: CODEX_MODEL_SELECTION,
3345 dispatchMode: { type: "steer_active", targetRunId: claudeRun!.id },
3346 });
3347 const projection = yield* orchestrator.getThreadProjection(harness.threadId);
3348 assert.isTrue(
3349 projection.turnItems.some(
3350 (item) => item.type === "run_interrupt_request" && item.runId === claudeRun!.id,
3351 ),
3352 );
3353 assert.deepEqual(
3354 projection.runs.map((run) => [run.status, run.providerInstanceId]),
3355 [
3356 ["running", CLAUDE_MODEL_SELECTION.instanceId],
3357 ["queued", CODEX_MODEL_SELECTION.instanceId],
3358 ],
3359 );
3360 yield* Deferred.succeed(harness.release, undefined);
3361 }).pipe(Effect.provide(harness.layer));
3362 }),
3363 );
3364
3365 // regression: #32891 a steer that Stop already reached is refused, not queued
3366 it.live("refuses a provider switch steered into a Claude turn that Stop reached", () =>
3367 Effect.gen(function* () {
3368 const harness = yield* steerProviderSwitchHarness("after-stop");
3369 yield* Effect.gen(function* () {
3370 const orchestrator = yield* Orchestrator.OrchestratorV2;
3371 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
3372 yield* harness.startClaudeTurn;
3373 yield* Deferred.await(harness.started);
3374 yield* worker.drain();
3375 yield* harness.markClaudeTurnRunning;
3376 const [claudeRun] = (yield* orchestrator.getThreadProjection(harness.threadId)).runs;
3377 // The worker has not delivered the interrupt yet, so the run still reads as running.
3378 yield* orchestrator.dispatch({
3379 type: "run.interrupt",
3380 commandId: CommandId.make("command:steer-provider-switch:after-stop:stop"),
3381 threadId: harness.threadId,
3382 runId: claudeRun!.id,
3383 });
3384 const error = yield* harness.steerToCodex.pipe(Effect.flip);
3385 assert.instanceOf(error, Orchestrator.OrchestratorDispatchError);
3386 assert.deepEqual(
3387 (yield* orchestrator.getThreadProjection(harness.threadId)).runs.map(
3388 (run) => run.providerInstanceId,
3389 ),
3390 [CLAUDE_MODEL_SELECTION.instanceId],
3391 );
3392 yield* Deferred.succeed(harness.release, undefined);
3393 }).pipe(Effect.provide(harness.layer));
3394 }),
3395 );
3396
3397 it.live("hands completed Grok steering context to earlier queued Codex and later Claude", () =>
3398 Effect.gen(function* () {
3399 const cwd = yield* checkpointWorkspace("queued-steer-provider-switch");
3400 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
3401 const started = yield* Deferred.make<void>();
3402 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
3403 makeTestAdapter({
3404 instanceId: CODEX_MODEL_SELECTION.instanceId,
3405 driver: CODEX_DRIVER,
3406 capabilities: CodexProviderCapabilitiesV2,
3407 modelSelection: CODEX_MODEL_SELECTION,
3408 responseByRunOrdinal: { 2: "Codex queued response" },
3409 capturedTurns,
3410 holdFirstTurn: started,
3411 }),
3412 makeTestAdapter({
3413 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
3414 driver: CLAUDE_DRIVER,
3415 capabilities: ClaudeProviderCapabilitiesV2,
3416 modelSelection: CLAUDE_MODEL_SELECTION,
3417 responseByRunOrdinal: { 3: "Claude queued response" },
3418 capturedTurns,
3419 }),
3420 makeTestAdapter({
3421 instanceId: GROK_MODEL_SELECTION.instanceId,
3422 driver: GROK_DRIVER,
3423 capabilities: AcpProviderCapabilitiesV2,
3424 modelSelection: GROK_MODEL_SELECTION,
3425 responseByRunOrdinal: { 1: "Grok steered response" },
3426 capturedTurns,
3427 }),
3428 ]);
3429 const queuedThreadId = ThreadId.make("thread:queued-steer-provider-switch");
3430 const projection = yield* Effect.gen(function* () {
3431 const orchestrator = yield* Orchestrator.OrchestratorV2;
3432 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
3433 const eventSink = yield* EventSink.EventSinkV2;
3434 const dispatch = (
3435 key: string,
3436 modelSelection: ModelSelection,
3437 dispatchMode: Extract<
3438 OrchestrationV2Command,
3439 { readonly type: "message.dispatch" }
3440 >["dispatchMode"],
3441 ) =>
3442 orchestrator.dispatch({
3443 type: "message.dispatch",
3444 createdBy: "user",
3445 creationSource: "web",
3446 commandId: CommandId.make(`command:queued-steer-provider-switch:${key}`),
3447 threadId: queuedThreadId,
3448 messageId: MessageId.make(`message:queued-steer-provider-switch:${key}`),
3449 text: `Prompt ${key}`,
3450 attachments: [],
3451 modelSelection,
3452 dispatchMode,
3453 });
3454 yield* orchestrator.dispatch({
3455 type: "thread.create",
3456 createdBy: "user",
3457 creationSource: "web",
3458 commandId: CommandId.make("command:queued-steer-provider-switch:create"),
3459 threadId: queuedThreadId,
3460 projectId: ProjectId.make("project:queued-steer-provider-switch"),
3461 title: "Queued steer provider switch",
3462 modelSelection: CODEX_MODEL_SELECTION,
3463 runtimeMode: "full-access",
3464 interactionMode: "default",
3465 branch: null,
3466 worktreePath: cwd,
3467 });
3468 yield* dispatch("first", CODEX_MODEL_SELECTION, { type: "start_immediately" });
3469 yield* Deferred.await(started);
3470 yield* worker.drain();
3471 yield* dispatch("codex-queued", CODEX_MODEL_SELECTION, { type: "queue_after_active" });
3472 yield* dispatch("claude-queued", CLAUDE_MODEL_SELECTION, { type: "queue_after_active" });
3473 const beforeSteer = yield* orchestrator.getThreadProjection(queuedThreadId);
3474 assert.deepEqual(
3475 beforeSteer.runs.map((run) => run.status),
3476 ["running", "queued", "queued"],
3477 );
3478 assert.equal(beforeSteer.thread.providerInstanceId, CODEX_MODEL_SELECTION.instanceId);
3479 assert.lengthOf(beforeSteer.contextHandoffs, 0);
3480 const now = yield* DateTime.now;
3481 yield* eventSink.write({
3482 events: [
3483 {
3484 id: EventId.make("event:queued-steer-provider-switch:first-turn-running"),
3485 type: "provider-turn.updated",
3486 threadId: queuedThreadId,
3487 runId: beforeSteer.runs[0]!.id,
3488 nodeId: beforeSteer.runs[0]!.rootNodeId!,
3489 driver: CODEX_DRIVER,
3490 providerInstanceId: CODEX_MODEL_SELECTION.instanceId,
3491 occurredAt: now,
3492 payload: {
3493 id: ProviderTurnId.make("provider-turn:queued-steer-provider-switch:first"),
3494 providerThreadId: beforeSteer.runs[0]!.providerThreadId!,
3495 nodeId: beforeSteer.runs[0]!.rootNodeId!,
3496 runAttemptId: beforeSteer.runs[0]!.activeAttemptId!,
3497 nativeTurnRef: null,
3498 ordinal: 1,
3499 status: "running",
3500 startedAt: now,
3501 completedAt: null,
3502 },
3503 },
3504 ],
3505 });
3506 const queuedClaudeCompleted = yield* orchestrator.streamStoredEvents.pipe(
3507 Stream.filter(
3508 (event) =>
3509 event.event.type === "run.updated" &&
3510 event.event.runId === beforeSteer.runs[2]?.id &&
3511 event.event.payload.status === "completed",
3512 ),
3513 Stream.runHead,
3514 Effect.forkScoped,
3515 );
3516 yield* dispatch("grok-steer", GROK_MODEL_SELECTION, {
3517 type: "steer_active",
3518 targetRunId: beforeSteer.runs[0]!.id,
3519 });
3520 const afterSteer = yield* orchestrator.getThreadProjection(queuedThreadId);
3521 assert.deepEqual(afterSteer.thread.modelSelection, GROK_MODEL_SELECTION);
3522 const interruptedTurn = afterSteer.providerTurns.find(
3523 (turn) => turn.runAttemptId === beforeSteer.runs[0]?.activeAttemptId,
3524 )!;
3525 const interruptedAttempt = afterSteer.attempts.find(
3526 (attempt) => attempt.id === beforeSteer.runs[0]?.activeAttemptId,
3527 )!;
3528 const interruptedAt = yield* DateTime.now;
3529 yield* eventSink.write({
3530 events: [
3531 {
3532 id: EventId.make("event:queued-steer-provider-switch:first-turn-interrupted"),
3533 type: "provider-turn.updated",
3534 threadId: queuedThreadId,
3535 runId: beforeSteer.runs[0]!.id,
3536 nodeId: beforeSteer.runs[0]!.rootNodeId!,
3537 driver: CODEX_DRIVER,
3538 providerInstanceId: CODEX_MODEL_SELECTION.instanceId,
3539 occurredAt: interruptedAt,
3540 payload: {
3541 ...interruptedTurn,
3542 status: "interrupted",
3543 completedAt: interruptedAt,
3544 },
3545 },
3546 {
3547 id: EventId.make("event:queued-steer-provider-switch:first-attempt-interrupted"),
3548 type: "run-attempt.updated",
3549 threadId: queuedThreadId,
3550 runId: beforeSteer.runs[0]!.id,
3551 nodeId: beforeSteer.runs[0]!.rootNodeId!,
3552 providerInstanceId: CODEX_MODEL_SELECTION.instanceId,
3553 occurredAt: interruptedAt,
3554 payload: {
3555 ...interruptedAttempt,
3556 status: "interrupted",
3557 completedAt: interruptedAt,
3558 },
3559 },
3560 ],
3561 });
3562 yield* worker.drain();
3563 yield* Fiber.join(queuedClaudeCompleted);
3564 return yield* orchestrator.getThreadProjection(queuedThreadId);
3565 }).pipe(
3566 Effect.provide(
3567 ProviderReplayHarness.layerWithRegistry(
3568 {
3569 name: "queued-steer-provider-switch",
3570 runtimePolicyOverride: {
3571 cwd,
3572 approvalPolicy: "never",
3573 sandboxPolicy: {
3574 type: "readOnly",
3575 access: { type: "fullAccess" },
3576 networkAccess: false,
3577 },
3578 },
3579 },
3580 layerRegistry,
3581 ),
3582 ),
3583 );
3584 assert.deepEqual(
3585 projection.runs.map((run) => [run.providerInstanceId, run.status]),
3586 [
3587 [GROK_MODEL_SELECTION.instanceId, "completed"],
3588 [CODEX_MODEL_SELECTION.instanceId, "completed"],
3589 [CLAUDE_MODEL_SELECTION.instanceId, "completed"],
3590 ],
3591 );
3592 const turns = yield* Ref.get(capturedTurns);
3593 assert.deepEqual(
3594 turns.map((turn) => turn.driver),
3595 [CODEX_DRIVER, GROK_DRIVER, CODEX_DRIVER, CLAUDE_DRIVER],
3596 );
3597 assert.include(turns[2]?.text ?? "", "Grok steered response");
3598 assert.include(turns[3]?.text ?? "", "Grok steered response");
3599 assert.include(turns[3]?.text ?? "", "Codex queued response");
3600 assert.deepEqual(
3601 projection.contextHandoffs.map((handoff) => handoff.targetRunId),
3602 [projection.runs[0]?.id, projection.runs[1]?.id, projection.runs[2]?.id],
3603 );
3604 }),
3605 );
3606
3607 it.live("resumes a queued account switch without requiring a portable handoff", () =>
3608 Effect.gen(function* () {
3609 const cwd = yield* checkpointWorkspace("queued-account-switch");
3610 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
3611 const started = yield* Deferred.make<void>();
3612 const alternateSelection: ModelSelection = {
3613 ...CODEX_MODEL_SELECTION,
3614 instanceId: ProviderInstanceId.make("codex-alternate"),
3615 };
3616 const alternateCapabilities = {
3617 ...CodexProviderCapabilitiesV2,
3618 canConsumeHandoffSummaries: false,
3619 };
3620 const adapters = [
3621 makeTestAdapter({
3622 instanceId: CODEX_MODEL_SELECTION.instanceId,
3623 driver: CODEX_DRIVER,
3624 capabilities: CodexProviderCapabilitiesV2,
3625 modelSelection: CODEX_MODEL_SELECTION,
3626 responseByRunOrdinal: {},
3627 capturedTurns,
3628 holdFirstTurn: started,
3629 }),
3630 makeTestAdapter({
3631 instanceId: alternateSelection.instanceId,
3632 driver: CODEX_DRIVER,
3633 capabilities: alternateCapabilities,
3634 modelSelection: alternateSelection,
3635 responseByRunOrdinal: { 2: "Alternate account complete" },
3636 capturedTurns,
3637 }),
3638 ];
3639 const layerRegistry = Layer.succeed(
3640 ProviderAdapterRegistry.ProviderAdapterRegistryV2,
3641 ProviderAdapterRegistry.ProviderAdapterRegistryV2.of({
3642 get: (instanceId) => {
3643 const adapter = adapters.find((candidate) => candidate.instanceId === instanceId);
3644 return adapter === undefined
3645 ? Effect.fail(
3646 new ProviderAdapterRegistry.ProviderAdapterRegistryLookupError({ instanceId }),
3647 )
3648 : Effect.succeed(adapter);
3649 },
3650 list: () => Effect.succeed(adapters.map((adapter) => adapter.instanceId)),
3651 getMetadata: (instanceId) => {
3652 const adapter = adapters.find((candidate) => candidate.instanceId === instanceId);
3653 return adapter === undefined
3654 ? Effect.fail(
3655 new ProviderAdapterRegistry.ProviderAdapterRegistryLookupError({ instanceId }),
3656 )
3657 : Effect.succeed({
3658 driver: CODEX_DRIVER,
3659 continuationKey: "codex:shared-native-account-history",
3660 enabled: true,
3661 capabilities:
3662 instanceId === alternateSelection.instanceId
3663 ? alternateCapabilities
3664 : CodexProviderCapabilitiesV2,
3665 });
3666 },
3667 }),
3668 );
3669 const queuedThreadId = ThreadId.make("thread:queued-account-switch");
3670 const projection = yield* Effect.gen(function* () {
3671 const orchestrator = yield* Orchestrator.OrchestratorV2;
3672 const eventSink = yield* EventSink.EventSinkV2;
3673 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
3674 yield* orchestrator.dispatch({
3675 type: "thread.create",
3676 createdBy: "user",
3677 creationSource: "web",
3678 commandId: CommandId.make("command:queued-account-switch:create"),
3679 threadId: queuedThreadId,
3680 projectId: ProjectId.make("project:queued-account-switch"),
3681 title: "Queued account switch",
3682 modelSelection: CODEX_MODEL_SELECTION,
3683 runtimeMode: "full-access",
3684 interactionMode: "default",
3685 branch: null,
3686 worktreePath: cwd,
3687 });
3688 yield* orchestrator.dispatch({
3689 type: "message.dispatch",
3690 createdBy: "user",
3691 creationSource: "web",
3692 commandId: CommandId.make("command:queued-account-switch:first"),
3693 threadId: queuedThreadId,
3694 messageId: MessageId.make("message:queued-account-switch:first"),
3695 text: "First account turn",
3696 attachments: [],
3697 modelSelection: CODEX_MODEL_SELECTION,
3698 dispatchMode: { type: "start_immediately" },
3699 });
3700 yield* Deferred.await(started);
3701 yield* orchestrator.dispatch({
3702 type: "message.dispatch",
3703 createdBy: "user",
3704 creationSource: "web",
3705 commandId: CommandId.make("command:queued-account-switch:second"),
3706 threadId: queuedThreadId,
3707 messageId: MessageId.make("message:queued-account-switch:second"),
3708 text: "Alternate account turn",
3709 attachments: [],
3710 modelSelection: alternateSelection,
3711 dispatchMode: { type: "queue_after_active" },
3712 });
3713 const queued = yield* orchestrator.getThreadProjection(queuedThreadId);
3714 assert.deepEqual(
3715 queued.runs.map((run) => run.status),
3716 ["running", "queued"],
3717 );
3718 assert.equal(queued.thread.providerInstanceId, CODEX_MODEL_SELECTION.instanceId);
3719 const sourceNativeRef = queued.providerThreads.find(
3720 (providerThread) => providerThread.id === queued.runs[0]?.providerThreadId,
3721 )?.nativeThreadRef;
3722 assert.isNotNull(sourceNativeRef);
3723 const now = yield* DateTime.now;
3724 yield* eventSink.write({
3725 events: [
3726 {
3727 id: EventId.make("event:queued-account-switch:first-complete"),
3728 type: "run.updated",
3729 threadId: queuedThreadId,
3730 runId: queued.runs[0]!.id,
3731 providerInstanceId: CODEX_MODEL_SELECTION.instanceId,
3732 occurredAt: now,
3733 payload: { ...queued.runs[0]!, status: "completed", completedAt: now },
3734 },
3735 ],
3736 });
3737 yield* orchestrator.resumeQueuedRuns;
3738 yield* orchestrator.streamStoredEvents.pipe(
3739 Stream.filter(
3740 (event) =>
3741 event.event.type === "run.updated" &&
3742 event.event.runId === queued.runs[1]?.id &&
3743 event.event.payload.status === "completed",
3744 ),
3745 Stream.runHead,
3746 );
3747 yield* worker.drain();
3748 const delivered = yield* orchestrator.getThreadProjection(queuedThreadId);
3749 const targetNativeRef = delivered.providerThreads.find(
3750 (providerThread) => providerThread.id === delivered.runs[1]?.providerThreadId,
3751 )?.nativeThreadRef;
3752 assert.deepEqual(targetNativeRef, sourceNativeRef);
3753 return delivered;
3754 }).pipe(
3755 Effect.provide(
3756 ProviderReplayHarness.layerWithRegistry(
3757 {
3758 name: "queued-account-switch",
3759 runtimePolicyOverride: {
3760 cwd,
3761 approvalPolicy: "never",
3762 sandboxPolicy: {
3763 type: "readOnly",
3764 access: { type: "fullAccess" },
3765 networkAccess: false,
3766 },
3767 },
3768 },
3769 layerRegistry,
3770 ),
3771 ),
3772 );
3773 assert.deepEqual(
3774 projection.runs.map((run) => [run.providerInstanceId, run.status]),
3775 [
3776 [CODEX_MODEL_SELECTION.instanceId, "completed"],
3777 [alternateSelection.instanceId, "completed"],
3778 ],
3779 );
3780 assert.lengthOf(projection.contextHandoffs, 0);
3781 assert.deepEqual(
3782 (yield* Ref.get(capturedTurns)).map((turn) => turn.text),
3783 ["First account turn", "Alternate account turn"],
3784 );
3785 }),
3786 );
3787
3788 it.live("finishes earlier queued Codex turns before handing context to queued Claude", () =>
3789 Effect.gen(function* () {
3790 const cwd = yield* checkpointWorkspace("queued-provider-switch");
3791 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
3792 const started = yield* Deferred.make<void>();
3793 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
3794 makeTestAdapter({
3795 instanceId: ProviderInstanceId.make("codex"),
3796 driver: CODEX_DRIVER,
3797 capabilities: CodexProviderCapabilitiesV2,
3798 modelSelection: CODEX_MODEL_SELECTION,
3799 responseByRunOrdinal: {
3800 1: "Codex current turn complete",
3801 2: "Codex first queued turn complete",
3802 3: "Codex second queued turn complete",
3803 },
3804 capturedTurns,
3805 holdFirstTurn: started,
3806 }),
3807 makeTestAdapter({
3808 instanceId: ProviderInstanceId.make("claudeAgent"),
3809 driver: CLAUDE_DRIVER,
3810 capabilities: ClaudeProviderCapabilitiesV2,
3811 modelSelection: CLAUDE_MODEL_SELECTION,
3812 responseByRunOrdinal: { 4: "Claude turn complete" },
3813 capturedTurns,
3814 }),
3815 ]);
3816 const queuedThreadId = ThreadId.make("thread:queued-provider-switch");
3817 const layerDatabase = SqlitePersistence.layerMemory;
3818 const layerOutboxProvided = EffectOutbox.layer.pipe(Layer.provide(layerDatabase));
3819 const projection = yield* Effect.gen(function* () {
3820 const orchestrator = yield* Orchestrator.OrchestratorV2;
3821 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
3822 const eventSink = yield* EventSink.EventSinkV2;
3823 const effectOutbox = yield* EffectOutbox.EffectOutboxV2;
3824 const dispatch = (ordinal: number, modelSelection: ModelSelection) =>
3825 orchestrator.dispatch({
3826 type: "message.dispatch",
3827 createdBy: "user",
3828 creationSource: "web",
3829 commandId: CommandId.make(`command:queued-provider-switch:${ordinal}`),
3830 threadId: queuedThreadId,
3831 messageId: MessageId.make(`message:queued-provider-switch:${ordinal}`),
3832 text: `Prompt ${ordinal}`,
3833 attachments: [],
3834 modelSelection,
3835 dispatchMode: {
3836 type: ordinal === 1 ? "start_immediately" : "queue_after_active",
3837 },
3838 });
3839 yield* orchestrator.dispatch({
3840 type: "thread.create",
3841 createdBy: "user",
3842 creationSource: "web",
3843 commandId: CommandId.make("command:queued-provider-switch:create"),
3844 threadId: queuedThreadId,
3845 projectId: ProjectId.make("project:queued-provider-switch"),
3846 title: "Queued provider switch",
3847 modelSelection: CODEX_MODEL_SELECTION,
3848 runtimeMode: "full-access",
3849 interactionMode: "default",
3850 branch: null,
3851 worktreePath: cwd,
3852 });
3853 yield* dispatch(1, CODEX_MODEL_SELECTION);
3854 yield* Deferred.await(started);
3855 yield* dispatch(2, CODEX_MODEL_SELECTION);
3856 yield* dispatch(3, CODEX_MODEL_SELECTION);
3857 yield* dispatch(4, CLAUDE_MODEL_SELECTION);
3858 const queued = yield* orchestrator.getThreadProjection(queuedThreadId);
3859 assert.deepEqual(
3860 queued.runs.map((run) => run.status),
3861 ["running", "queued", "queued", "queued"],
3862 );
3863 assert.equal(queued.thread.activeProviderThreadId, queued.runs[0]?.providerThreadId);
3864 assert.equal(queued.thread.providerInstanceId, CODEX_MODEL_SELECTION.instanceId);
3865 assert.lengthOf(queued.contextHandoffs, 0);
3866 const activeSession = queued.providerSessions.find(
3867 (session) => session.providerInstanceId === CODEX_MODEL_SELECTION.instanceId,
3868 );
3869 assert.isDefined(activeSession);
3870 const now = yield* DateTime.now;
3871 yield* eventSink.write({
3872 events: [
3873 ...(["stopped", "error"] as const).map((status) => ({
3874 id: EventId.make(`event:queued-provider-switch:dead-session:${status}`),
3875 type: "provider-session.updated" as const,
3876 threadId: queuedThreadId,
3877 providerInstanceId: CODEX_MODEL_SELECTION.instanceId,
3878 occurredAt: now,
3879 payload: {
3880 ...activeSession!,
3881 id: ProviderSessionId.make(`provider-session:queued-provider-switch:${status}`),
3882 status,
3883 },
3884 })),
3885 {
3886 id: EventId.make("event:queued-provider-switch:first-response"),
3887 type: "turn-item.updated",
3888 threadId: queuedThreadId,
3889 runId: queued.runs[0]!.id,
3890 nodeId: queued.runs[0]!.rootNodeId!,
3891 providerInstanceId: CODEX_MODEL_SELECTION.instanceId,
3892 occurredAt: now,
3893 payload: {
3894 id: TurnItemId.make("turn-item:queued-provider-switch:first-response"),
3895 threadId: queuedThreadId,
3896 runId: queued.runs[0]!.id,
3897 nodeId: queued.runs[0]!.rootNodeId!,
3898 providerThreadId: queued.runs[0]!.providerThreadId,
3899 providerTurnId: null,
3900 nativeItemRef: null,
3901 parentItemId: null,
3902 ordinal: 101,
3903 status: "completed",
3904 title: null,
3905 startedAt: now,
3906 completedAt: now,
3907 updatedAt: now,
3908 type: "assistant_message",
3909 messageId: MessageId.make("message:queued-provider-switch:first-response"),
3910 text: "Codex current turn complete",
3911 streaming: false,
3912 },
3913 },
3914 {
3915 id: EventId.make("event:queued-provider-switch:first-complete"),
3916 type: "run.updated",
3917 threadId: queuedThreadId,
3918 runId: queued.runs[0]!.id,
3919 providerInstanceId: CODEX_MODEL_SELECTION.instanceId,
3920 occurredAt: now,
3921 payload: { ...queued.runs[0]!, status: "completed", completedAt: now },
3922 },
3923 ],
3924 });
3925 yield* orchestrator.resumeQueuedRuns;
3926 yield* orchestrator.streamStoredEvents.pipe(
3927 Stream.filter(
3928 (event) =>
3929 event.event.type === "run.updated" &&
3930 event.event.runId === queued.runs[3]?.id &&
3931 event.event.payload.status === "completed",
3932 ),
3933 Stream.runHead,
3934 );
3935 yield* worker.drain();
3936 const detachEvents = yield* eventSink.stream({ threadId: queuedThreadId }).pipe(
3937 Stream.filter((stored) => stored.event.type === "provider-session.detached"),
3938 Stream.take(1),
3939 Stream.runCollect,
3940 );
3941 assert.equal(
3942 detachEvents[0]?.event.type === "provider-session.detached"
3943 ? detachEvents[0].event.payload.providerSessionId
3944 : null,
3945 activeSession?.id,
3946 );
3947 const startCommandId = CommandId.make(`command:system:start-queued:${queued.runs[3]!.id}`);
3948 const detachEffects = (yield* effectOutbox.listByCommandId(startCommandId)).filter(
3949 (effect) => effect.request.type === "provider-session.detach",
3950 );
3951 assert.deepEqual(
3952 detachEffects.map((effect) =>
3953 effect.request.type === "provider-session.detach"
3954 ? effect.request.providerSessionId
3955 : null,
3956 ),
3957 [activeSession?.id],
3958 );
3959 return yield* orchestrator.getThreadProjection(queuedThreadId);
3960 }).pipe(
3961 Effect.provide(
3962 Layer.merge(
3963 ProviderReplayHarness.layerWithRegistry(
3964 {
3965 name: "queued-provider-switch",
3966 runtimePolicyOverride: {
3967 cwd,
3968 approvalPolicy: "never",
3969 sandboxPolicy: {
3970 type: "readOnly",
3971 access: { type: "fullAccess" },
3972 networkAccess: false,
3973 },
3974 },
3975 },
3976 layerRegistry,
3977 { databaseLayer: layerDatabase },
3978 ),
3979 layerOutboxProvided,
3980 ),
3981 ),
3982 );
3983 const turns = yield* Ref.get(capturedTurns);
3984 assert.deepEqual(
3985 projection.runs.map((run) => [run.providerInstanceId, run.status]),
3986 [
3987 ["codex", "completed"],
3988 ["codex", "completed"],
3989 ["codex", "completed"],
3990 ["claudeAgent", "completed"],
3991 ],
3992 );
3993 assert.deepEqual(
3994 turns.map((turn) => [turn.driver, turn.text.includes("Prompt 4")]),
3995 [
3996 ["codex", false],
3997 ["codex", false],
3998 ["codex", false],
3999 ["claudeAgent", true],
4000 ],
4001 );
4002 assert.lengthOf(projection.contextHandoffs, 1);
4003 assert.equal(projection.contextHandoffs[0]?.targetRunId, projection.runs[3]?.id);
4004 const handoffItem = projection.turnItems.find(
4005 (item) => item.type === "handoff" && item.runId === projection.runs[3]?.id,
4006 );
4007 assert.equal(
4008 handoffItem?.type === "handoff" ? handoffItem.contextHandoffId : null,
4009 projection.contextHandoffs[0]?.id,
4010 );
4011 const queuedUserItem = projection.turnItems.find(
4012 (item) => item.type === "user_message" && item.runId === projection.runs[3]?.id,
4013 );
4014 assert.isBelow(handoffItem?.ordinal ?? Infinity, queuedUserItem?.ordinal ?? -Infinity);
4015 assert.include(
4016 handoffItem?.type === "handoff" ? handoffItem.summary : "",
4017 "Codex second queued turn complete",
4018 );
4019 assert.include(turns[3]?.text ?? "", "Codex current turn complete");
4020 assert.include(turns[3]?.text ?? "", "Codex first queued turn complete");
4021 assert.include(turns[3]?.text ?? "", "Codex second queued turn complete");
4022 }),
4023 );
4024
4025 // regression: #20343 sending to an instance with separate history must release the old session
4026 it.live.each([
4027 [
4028 "detaches the previous instance's session when a send hands off to another instance",
4029 { background: false, queued: false, sharedHistory: false },
4030 ],
4031 [
4032 "refuses the hand-off while the previous instance still runs background work",
4033 { background: true, queued: false, sharedHistory: false },
4034 ],
4035 [
4036 "keeps a busy previous session running when a queued hand-off starts",
4037 { background: true, queued: true, sharedHistory: false },
4038 ],
4039 [
4040 "holds a queued account switch while the replaced session still runs background work",
4041 { background: true, queued: true, sharedHistory: true },
4042 ],
4043 ] as const)("%s", ([, { background, queued, sharedHistory }]) =>
4044 Effect.gen(function* () {
4045 const cwd = yield* checkpointWorkspace("handoff-releases-session");
4046 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
4047 const backgroundWork = yield* Ref.make(background);
4048 const closedSessions = yield* Ref.make<ReadonlyArray<string>>([]);
4049 const firstStarted = yield* Deferred.make<void>();
4050 const releaseFirst = yield* Deferred.make<void>();
4051 const secondSelection: ModelSelection = {
4052 ...CLAUDE_MODEL_SELECTION,
4053 instanceId: ProviderInstanceId.make("claude-second-account"),
4054 };
4055 const adapters = [
4056 makeTestAdapter({
4057 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
4058 driver: CLAUDE_DRIVER,
4059 capabilities: ClaudeProviderCapabilitiesV2,
4060 modelSelection: CLAUDE_MODEL_SELECTION,
4061 responseByRunOrdinal: { 1: "First account complete" },
4062 capturedTurns,
4063 backgroundWork,
4064 closedSessions,
4065 ...(queued ? { holdFirstTurn: firstStarted, releaseFirstTurn: releaseFirst } : {}),
4066 }),
4067 makeTestAdapter({
4068 instanceId: secondSelection.instanceId,
4069 driver: CLAUDE_DRIVER,
4070 capabilities: ClaudeProviderCapabilitiesV2,
4071 modelSelection: secondSelection,
4072 responseByRunOrdinal: { 2: "Second account complete" },
4073 capturedTurns,
4074 }),
4075 ];
4076 // Without a shared continuation key, each Claude instance owns its own native history.
4077 const layerRegistry = sharedHistory
4078 ? Layer.succeed(
4079 ProviderAdapterRegistry.ProviderAdapterRegistryV2,
4080 ProviderAdapterRegistry.ProviderAdapterRegistryV2.of({
4081 get: (instanceId) => {
4082 const adapter = adapters.find((candidate) => candidate.instanceId === instanceId);
4083 return adapter === undefined
4084 ? Effect.fail(
4085 new ProviderAdapterRegistry.ProviderAdapterRegistryLookupError({
4086 instanceId,
4087 }),
4088 )
4089 : Effect.succeed(adapter);
4090 },
4091 list: () => Effect.succeed(adapters.map((adapter) => adapter.instanceId)),
4092 getMetadata: () =>
4093 Effect.succeed({
4094 driver: CLAUDE_DRIVER,
4095 continuationKey: "claude:shared-native-account-history",
4096 enabled: true,
4097 capabilities: ClaudeProviderCapabilitiesV2,
4098 }),
4099 }),
4100 )
4101 : ProviderAdapterRegistry.layerFromAdapters(adapters);
4102 const handoffThreadId = ThreadId.make("thread:handoff-releases-session");
4103 yield* Effect.gen(function* () {
4104 const orchestrator = yield* Orchestrator.OrchestratorV2;
4105 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
4106 const send = (ordinal: number, modelSelection: ModelSelection, attempt = "") =>
4107 orchestrator.dispatch({
4108 type: "message.dispatch",
4109 createdBy: "user",
4110 creationSource: "web",
4111 commandId: CommandId.make(`command:handoff-releases-session:${ordinal}${attempt}`),
4112 threadId: handoffThreadId,
4113 messageId: MessageId.make(`message:handoff-releases-session:${ordinal}${attempt}`),
4114 text: `Prompt ${ordinal}`,
4115 attachments: [],
4116 modelSelection,
4117 dispatchMode: { type: queued ? "queue_after_active" : "start_immediately" },
4118 });
4119 yield* orchestrator.dispatch({
4120 type: "thread.create",
4121 createdBy: "user",
4122 creationSource: "web",
4123 commandId: CommandId.make("command:handoff-releases-session:create"),
4124 threadId: handoffThreadId,
4125 projectId: ProjectId.make("project:handoff-releases-session"),
4126 title: "Handoff releases session",
4127 modelSelection: CLAUDE_MODEL_SELECTION,
4128 runtimeMode: "full-access",
4129 interactionMode: "default",
4130 branch: null,
4131 worktreePath: cwd,
4132 });
4133 yield* send(1, CLAUDE_MODEL_SELECTION);
4134
4135 if (queued) {
4136 yield* Deferred.await(firstStarted);
4137 yield* send(2, secondSelection);
4138 const queuedRunId = (yield* orchestrator.getThreadProjection(handoffThreadId)).runs[1]
4139 ?.id;
4140 assert.isDefined(queuedRunId);
4141 yield* Deferred.succeed(releaseFirst, undefined);
4142 yield* orchestrator.streamStoredEvents.pipe(
4143 Stream.filter(
4144 ({ event }) =>
4145 event.type === "run.updated" &&
4146 event.runId === queuedRunId &&
4147 (event.payload.queueHeld === true || event.payload.status === "completed"),
4148 ),
4149 Stream.runHead,
4150 );
4151 yield* worker.drain();
4152 const afterQueue = yield* orchestrator.getThreadProjection(handoffThreadId);
4153 const firstSession = afterQueue.providerSessions.find(
4154 (session) => session.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
4155 );
4156 // Detaching would end the background agents, so the busy session must stay up.
4157 assert.deepEqual(yield* Ref.get(closedSessions), []);
4158 assert.equal(firstSession?.status, "ready");
4159 if (!sharedHistory) {
4160 assert.deepEqual(
4161 afterQueue.runs.map((run) => [run.providerInstanceId, run.status]),
4162 [
4163 [CLAUDE_MODEL_SELECTION.instanceId, "completed"],
4164 [secondSelection.instanceId, "completed"],
4165 ],
4166 );
4167 return;
4168 }
4169 // Resuming the native thread beside the busy session would fork its history.
4170 assert.deepEqual(
4171 afterQueue.runs.map((run) => [run.status, run.queueHeld === true]),
4172 [
4173 ["completed", false],
4174 ["queued", true],
4175 ],
4176 );
4177 assert.lengthOf(yield* Ref.get(capturedTurns), 1);
4178 yield* Ref.set(backgroundWork, false);
4179 yield* orchestrator.dispatch({
4180 type: "queue.resume",
4181 commandId: CommandId.make("command:handoff-releases-session:resume"),
4182 threadId: handoffThreadId,
4183 });
4184 yield* worker.drain();
4185 const resumed = yield* waitForIdle(handoffThreadId);
4186 assert.deepEqual(
4187 resumed.runs.map((run) => [run.providerInstanceId, run.status]),
4188 [
4189 [CLAUDE_MODEL_SELECTION.instanceId, "completed"],
4190 [secondSelection.instanceId, "completed"],
4191 ],
4192 );
4193 assert.deepEqual(yield* Ref.get(closedSessions), [firstSession!.id]);
4194 return;
4195 }
4196
4197 yield* worker.drain();
4198 const afterFirst = yield* waitForIdle(handoffThreadId);
4199 const firstSession = afterFirst.providerSessions.find(
4200 (session) => session.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
4201 );
4202 assert.equal(firstSession?.status, "ready");
4203
4204 if (background) {
4205 // Native subagents live only in the adapter's roster, so the projection's
4206 // pendingBackgroundTasks stays empty while the probe still reports work.
4207 assert.deepEqual(afterFirst.providerThreads[0]?.pendingBackgroundTasks ?? [], []);
4208 const refused = yield* send(2, secondSelection, ":refused").pipe(Effect.flip);
4209 assert.include(String(refused.cause), "still running background agents or commands");
4210 const unchanged = yield* orchestrator.getThreadProjection(handoffThreadId);
4211 assert.lengthOf(unchanged.runs, 1);
4212 assert.deepEqual(yield* Ref.get(closedSessions), []);
4213 assert.equal(unchanged.thread.providerInstanceId, CLAUDE_MODEL_SELECTION.instanceId);
4214 assert.deepEqual(
4215 unchanged.providerSessions.map((session) => [session.id, session.status]),
4216 [[firstSession!.id, "ready"]],
4217 );
4218 yield* Ref.set(backgroundWork, false);
4219 }
4220
4221 yield* send(2, secondSelection);
4222 yield* worker.drain();
4223 const afterSecond = yield* waitForIdle(handoffThreadId);
4224 yield* worker.drain();
4225 const projection = yield* orchestrator.getThreadProjection(handoffThreadId);
4226
4227 assert.deepEqual(
4228 projection.runs.map((run) => [run.providerInstanceId, run.status]),
4229 [
4230 [CLAUDE_MODEL_SELECTION.instanceId, "completed"],
4231 [secondSelection.instanceId, "completed"],
4232 ],
4233 );
4234 assert.lengthOf(afterSecond.contextHandoffs, 1);
4235 assert.deepEqual(
4236 projection.providerSessions.map((session) => session.providerInstanceId),
4237 [secondSelection.instanceId],
4238 );
4239 // The detached event alone hides the session; the runtime must also close.
4240 assert.deepEqual(yield* Ref.get(closedSessions), [firstSession!.id]);
4241 }).pipe(
4242 Effect.provide(
4243 ProviderReplayHarness.layerWithRegistry(
4244 {
4245 name: "handoff-releases-session",
4246 runtimePolicyOverride: {
4247 cwd,
4248 approvalPolicy: "never",
4249 sandboxPolicy: {
4250 type: "readOnly",
4251 access: { type: "fullAccess" },
4252 networkAccess: false,
4253 },
4254 },
4255 },
4256 layerRegistry,
4257 ),
4258 ),
4259 );
4260 }),
4261 );
4262
4263 // regression: #16000 thread setting changes must not end a Claude session's background work
4264 it.live.each([
4265 ["runtime mode", "runtime-mode"],
4266 ["worktree", "worktree"],
4267 ["provider instance", "instance"],
4268 ] as const)("keeps background work alive when the %s changes on an idle thread", ([, change]) =>
4269 Effect.gen(function* () {
4270 const cwd = yield* checkpointWorkspace(`settings-background-${change}`);
4271 const nextCwd = yield* checkpointWorkspace(`settings-background-${change}-next`);
4272 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
4273 const backgroundWork = yield* Ref.make(false);
4274 const closedSessions = yield* Ref.make<ReadonlyArray<string>>([]);
4275 const secondSelection: ModelSelection = {
4276 ...CLAUDE_MODEL_SELECTION,
4277 instanceId: ProviderInstanceId.make("claude-second-account"),
4278 };
4279 const threadId = ThreadId.make(`thread:settings-background-${change}`);
4280 yield* Effect.gen(function* () {
4281 const orchestrator = yield* Orchestrator.OrchestratorV2;
4282 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
4283 yield* orchestrator.dispatch({
4284 type: "thread.create",
4285 createdBy: "user",
4286 creationSource: "web",
4287 commandId: CommandId.make(`command:settings-background-${change}:create`),
4288 threadId,
4289 projectId: ProjectId.make(`project:settings-background-${change}`),
4290 title: "Settings change with background work",
4291 modelSelection: CLAUDE_MODEL_SELECTION,
4292 runtimeMode: "full-access",
4293 interactionMode: "default",
4294 branch: null,
4295 worktreePath: cwd,
4296 });
4297 yield* orchestrator.dispatch({
4298 type: "message.dispatch",
4299 createdBy: "user",
4300 creationSource: "web",
4301 commandId: CommandId.make(`command:settings-background-${change}:send`),
4302 threadId,
4303 messageId: MessageId.make(`message:settings-background-${change}:send`),
4304 text: "Start background work",
4305 attachments: [],
4306 modelSelection: CLAUDE_MODEL_SELECTION,
4307 dispatchMode: { type: "start_immediately" },
4308 });
4309 yield* worker.drain();
4310 const afterFirst = yield* waitForIdle(threadId);
4311 const session = afterFirst.providerSessions[0];
4312 assert.equal(session?.status, "ready");
4313 // The turn ended but a background subagent keeps running in the CLI process.
4314 yield* Ref.set(backgroundWork, true);
4315
4316 const commandId = CommandId.make(`command:settings-background-${change}:change`);
4317 yield* orchestrator
4318 .dispatch(
4319 change === "runtime-mode"
4320 ? {
4321 type: "thread.runtime-mode.set",
4322 commandId,
4323 threadId,
4324 runtimeMode: "auto-accept-edits",
4325 }
4326 : change === "worktree"
4327 ? { type: "thread.metadata.update", commandId, threadId, worktreePath: nextCwd }
4328 : {
4329 type: "thread.model-selection.set",
4330 commandId,
4331 threadId,
4332 modelSelection: secondSelection,
4333 },
4334 )
4335 .pipe(Effect.ignore);
4336 yield* worker.drain();
4337
4338 const projection = yield* orchestrator.getThreadProjection(threadId);
4339 // Refusing or deferring are both fine; closing the busy process is not.
4340 assert.deepEqual(yield* Ref.get(closedSessions), []);
4341 assert.deepEqual(
4342 projection.providerSessions.map((candidate) => [candidate.id, candidate.status]),
4343 [[session!.id, "ready"]],
4344 );
4345 }).pipe(
4346 Effect.provide(
4347 ProviderReplayHarness.layerWithRegistry(
4348 {
4349 name: `settings-background-${change}`,
4350 runtimePolicyOverride: {
4351 cwd,
4352 approvalPolicy: "never",
4353 sandboxPolicy: {
4354 type: "readOnly",
4355 access: { type: "fullAccess" },
4356 networkAccess: false,
4357 },
4358 },
4359 },
4360 ProviderAdapterRegistry.layerFromAdapters([
4361 makeTestAdapter({
4362 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
4363 driver: CLAUDE_DRIVER,
4364 capabilities: ClaudeProviderCapabilitiesV2,
4365 modelSelection: CLAUDE_MODEL_SELECTION,
4366 responseByRunOrdinal: { 1: "Background work started" },
4367 capturedTurns,
4368 backgroundWork,
4369 closedSessions,
4370 }),
4371 makeTestAdapter({
4372 instanceId: secondSelection.instanceId,
4373 driver: CLAUDE_DRIVER,
4374 capabilities: ClaudeProviderCapabilitiesV2,
4375 modelSelection: secondSelection,
4376 responseByRunOrdinal: {},
4377 capturedTurns,
4378 }),
4379 ]),
4380 ),
4381 ),
4382 );
4383 }),
4384 );
4385
4386 // regression: a session kept by a queued hand-off is probed when a later change detaches it
4387 it.live(
4388 "refuses a later instance change while a retained hand-off session runs background work",
4389 () =>
4390 Effect.gen(function* () {
4391 const cwd = yield* checkpointWorkspace("retained-session-background");
4392 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
4393 const codexBackgroundWork = yield* Ref.make(true);
4394 const firstStarted = yield* Deferred.make<void>();
4395 const releaseFirst = yield* Deferred.make<void>();
4396 const threadId = ThreadId.make("thread:retained-session-background");
4397 yield* Effect.gen(function* () {
4398 const orchestrator = yield* Orchestrator.OrchestratorV2;
4399 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
4400 const send = (ordinal: number, modelSelection: ModelSelection) =>
4401 orchestrator.dispatch({
4402 type: "message.dispatch",
4403 createdBy: "user",
4404 creationSource: "web",
4405 commandId: CommandId.make(`command:retained-session-background:${ordinal}`),
4406 threadId,
4407 messageId: MessageId.make(`message:retained-session-background:${ordinal}`),
4408 text: `Prompt ${ordinal}`,
4409 attachments: [],
4410 modelSelection,
4411 dispatchMode: { type: "queue_after_active" },
4412 });
4413 const selectGrok = (attempt: string) =>
4414 orchestrator.dispatch({
4415 type: "thread.model-selection.set",
4416 commandId: CommandId.make(`command:retained-session-background:select:${attempt}`),
4417 threadId,
4418 modelSelection: GROK_MODEL_SELECTION,
4419 });
4420 yield* orchestrator.dispatch({
4421 type: "thread.create",
4422 createdBy: "user",
4423 creationSource: "web",
4424 commandId: CommandId.make("command:retained-session-background:create"),
4425 threadId,
4426 projectId: ProjectId.make("project:retained-session-background"),
4427 title: "Retained session with background work",
4428 modelSelection: CODEX_MODEL_SELECTION,
4429 runtimeMode: "full-access",
4430 interactionMode: "default",
4431 branch: null,
4432 worktreePath: cwd,
4433 });
4434 yield* send(1, CODEX_MODEL_SELECTION);
4435 yield* Deferred.await(firstStarted);
4436 yield* send(2, CLAUDE_MODEL_SELECTION);
4437 yield* Deferred.succeed(releaseFirst, undefined);
4438 yield* worker.drain();
4439 const afterHandoff = yield* waitForIdle(threadId);
4440 assert.deepEqual(
4441 afterHandoff.runs.map((run) => [run.providerInstanceId, run.status]),
4442 [
4443 [CODEX_MODEL_SELECTION.instanceId, "completed"],
4444 [CLAUDE_MODEL_SELECTION.instanceId, "completed"],
4445 ],
4446 );
4447 // The queued hand-off kept the busy Codex session attached.
4448 const codexSession = afterHandoff.providerSessions.find(
4449 (session) => session.providerInstanceId === CODEX_MODEL_SELECTION.instanceId,
4450 );
4451 assert.equal(codexSession?.status, "ready");
4452
4453 const refused = yield* selectGrok("busy").pipe(Effect.flip);
4454 assert.include(String(refused.cause), "still running background agents or commands");
4455 yield* worker.drain();
4456 const unchanged = yield* orchestrator.getThreadProjection(threadId);
4457 assert.equal(unchanged.thread.providerInstanceId, CLAUDE_MODEL_SELECTION.instanceId);
4458 assert.include(
4459 unchanged.providerSessions.map((session) => session.id),
4460 codexSession!.id,
4461 );
4462
4463 yield* Ref.set(codexBackgroundWork, false);
4464 yield* selectGrok("idle");
4465 yield* worker.drain();
4466 const switched = yield* orchestrator.getThreadProjection(threadId);
4467 assert.equal(switched.thread.providerInstanceId, GROK_MODEL_SELECTION.instanceId);
4468 assert.notInclude(
4469 switched.providerSessions.map((session) => session.id),
4470 codexSession!.id,
4471 );
4472 }).pipe(
4473 Effect.provide(
4474 ProviderReplayHarness.layerWithRegistry(
4475 {
4476 name: "retained-session-background",
4477 runtimePolicyOverride: {
4478 cwd,
4479 approvalPolicy: "never",
4480 sandboxPolicy: {
4481 type: "readOnly",
4482 access: { type: "fullAccess" },
4483 networkAccess: false,
4484 },
4485 },
4486 },
4487 ProviderAdapterRegistry.layerFromAdapters([
4488 makeTestAdapter({
4489 instanceId: CODEX_MODEL_SELECTION.instanceId,
4490 driver: CODEX_DRIVER,
4491 capabilities: CodexProviderCapabilitiesV2,
4492 modelSelection: CODEX_MODEL_SELECTION,
4493 responseByRunOrdinal: { 1: "Codex started background work" },
4494 capturedTurns,
4495 pendingBackgroundWork: codexBackgroundWork,
4496 holdFirstTurn: firstStarted,
4497 releaseFirstTurn: releaseFirst,
4498 }),
4499 makeTestAdapter({
4500 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
4501 driver: CLAUDE_DRIVER,
4502 capabilities: ClaudeProviderCapabilitiesV2,
4503 modelSelection: CLAUDE_MODEL_SELECTION,
4504 responseByRunOrdinal: { 2: "Claude complete" },
4505 capturedTurns,
4506 }),
4507 makeTestAdapter({
4508 instanceId: GROK_MODEL_SELECTION.instanceId,
4509 driver: GROK_DRIVER,
4510 capabilities: AcpProviderCapabilitiesV2,
4511 modelSelection: GROK_MODEL_SELECTION,
4512 responseByRunOrdinal: {},
4513 capturedTurns,
4514 }),
4515 ]),
4516 ),
4517 ),
4518 );
4519 }),
4520 );
4521
4522 it.live.each([
4523 "immediately",
4524 "behind a running turn",
4525 "and recovers it after it breaks",
4526 "and recovers it after it breaks and the server restarts",
4527 ] as const)(
4528 "starts a fresh provider session %s and never hands earlier runs to a later provider",
4529 (variant) =>
4530 Effect.gen(function* () {
4531 const queued = variant === "behind a running turn";
4532 const restarted = variant === "and recovers it after it breaks and the server restarts";
4533 const broken = restarted || variant === "and recovers it after it breaks";
4534 const key = queued ? "queued" : restarted ? "restarted" : broken ? "broken" : "immediate";
4535 const cwd = yield* checkpointWorkspace(`fresh-context-${key}`);
4536 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
4537 const closedSessions = yield* Ref.make<ReadonlyArray<string>>([]);
4538 const runtimeStarts = yield* Ref.make<ReadonlyArray<string>>([]);
4539 // A queued start has nobody to refuse, so a busy session must stay up.
4540 const backgroundWork = yield* Ref.make(queued);
4541 const started = yield* Deferred.make<void>();
4542 const release = yield* Deferred.make<void>();
4543 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
4544 makeTestAdapter({
4545 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
4546 driver: CLAUDE_DRIVER,
4547 capabilities: ClaudeProviderCapabilitiesV2,
4548 modelSelection: CLAUDE_MODEL_SELECTION,
4549 responseByRunOrdinal: {},
4550 capturedTurns,
4551 closedSessions,
4552 runtimeStarts,
4553 backgroundWork,
4554 nativeThreadGeneration: yield* Ref.make(0),
4555 ...(queued
4556 ? { holdFirstTurn: started, releaseFirstTurn: release, holdRunOrdinal: 2 }
4557 : {}),
4558 ...(broken
4559 ? {
4560 failedRunOrdinals: new Set([3]),
4561 brokenRunOrdinals: new Set([3]),
4562 }
4563 : {}),
4564 }),
4565 makeTestAdapter({
4566 instanceId: CODEX_MODEL_SELECTION.instanceId,
4567 driver: CODEX_DRIVER,
4568 capabilities: CodexProviderCapabilitiesV2,
4569 modelSelection: CODEX_MODEL_SELECTION,
4570 responseByRunOrdinal: {},
4571 capturedTurns,
4572 }),
4573 ]);
4574 const freshThreadId = ThreadId.make(`thread:fresh-context:${key}`);
4575 yield* Effect.gen(function* () {
4576 const orchestrator = yield* Orchestrator.OrchestratorV2;
4577 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
4578 const send = (
4579 name: string,
4580 modelSelection: ModelSelection,
4581 options: { readonly freshContext?: boolean } = {},
4582 ) =>
4583 orchestrator.dispatch({
4584 type: "message.dispatch",
4585 createdBy: "user",
4586 creationSource: "web",
4587 commandId: CommandId.make(`command:fresh-context:${key}:${name}`),
4588 threadId: freshThreadId,
4589 messageId: MessageId.make(`message:fresh-context:${key}:${name}`),
4590 text: `Prompt ${name}`,
4591 attachments: [],
4592 modelSelection,
4593 ...options,
4594 // A fresh send must never steer the running turn.
4595 deliveryIntent: "auto",
4596 dispatchMode: { type: "start_immediately" },
4597 });
4598 yield* orchestrator.dispatch({
4599 type: "thread.create",
4600 createdBy: "user",
4601 creationSource: "web",
4602 commandId: CommandId.make(`command:fresh-context:${key}:create`),
4603 threadId: freshThreadId,
4604 projectId: ProjectId.make(`project:fresh-context:${key}`),
4605 title: "Fresh context",
4606 modelSelection: CLAUDE_MODEL_SELECTION,
4607 runtimeMode: "full-access",
4608 interactionMode: "default",
4609 branch: null,
4610 worktreePath: cwd,
4611 });
4612 // Codex keeps a native thread from before the fresh run that must not resume.
4613 yield* send("zero", CODEX_MODEL_SELECTION);
4614 yield* worker.drain();
4615 yield* waitForIdle(freshThreadId);
4616 yield* send("alpha", CLAUDE_MODEL_SELECTION);
4617 if (queued) {
4618 yield* Deferred.await(started);
4619 yield* worker.drain();
4620 } else {
4621 yield* worker.drain();
4622 yield* waitForIdle(freshThreadId);
4623 }
4624 const firstSession = (yield* orchestrator.getThreadProjection(
4625 freshThreadId,
4626 )).providerSessions.find(
4627 (session) => session.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
4628 )!;
4629 yield* send("beta", CLAUDE_MODEL_SELECTION, { freshContext: true });
4630 if (queued) {
4631 // Queued before the fresh run started, so it is bound to the old conversation.
4632 yield* send("gamma", CLAUDE_MODEL_SELECTION);
4633 const waiting = yield* orchestrator.getThreadProjection(freshThreadId);
4634 assert.deepEqual(
4635 waiting.runs.map((run) => run.status),
4636 ["completed", "running", "queued", "queued"],
4637 );
4638 // Steering would send it into the conversation it is meant to leave.
4639 const promoted = yield* orchestrator
4640 .dispatch({
4641 type: "queued-message.promote-to-steer",
4642 commandId: CommandId.make(`command:fresh-context:${key}:promote`),
4643 threadId: freshThreadId,
4644 queuedRunId: waiting.runs.find((run) => run.freshContext === true)!.id,
4645 targetRunId: waiting.runs.find((run) => run.status === "running")!.id,
4646 })
4647 .pipe(Effect.flip);
4648 assert.instanceOf(promoted, Orchestrator.OrchestratorDispatchError);
4649 assert.include(String(promoted.cause), "fresh session");
4650 yield* Deferred.succeed(release, undefined);
4651 }
4652 yield* worker.drain();
4653 yield* waitForIdle(freshThreadId);
4654 // Claude hosts one native thread per session, so an idle old one is closed.
4655 assert.deepEqual(yield* Ref.get(closedSessions), queued ? [] : [firstSession.id]);
4656 yield* Ref.set(backgroundWork, false);
4657 let startsBeforeRecovery = 0;
4658 if (broken) {
4659 if (restarted) {
4660 const brokenProjection = yield* orchestrator.getThreadProjection(freshThreadId);
4661 const brokenThread = brokenProjection.providerThreads.find(
4662 (thread) => thread.id === brokenProjection.thread.activeProviderThreadId,
4663 )!;
4664 assert.equal(brokenThread.status, "error");
4665 const sessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
4666 yield* sessions.close(brokenThread.providerSessionId!);
4667 }
4668 startsBeforeRecovery = (yield* Ref.get(runtimeStarts)).length;
4669 yield* send("epsilon", CLAUDE_MODEL_SELECTION);
4670 yield* worker.drain();
4671 yield* waitForIdle(freshThreadId);
4672 }
4673 yield* send("delta", CODEX_MODEL_SELECTION);
4674 yield* worker.drain();
4675 yield* waitForIdle(freshThreadId);
4676 yield* worker.drain();
4677 const projection = yield* orchestrator.getThreadProjection(freshThreadId);
4678 const turns = yield* Ref.get(capturedTurns);
4679 const turnFor = (name: string) =>
4680 turns.find((turn) => turn.text.endsWith(`Prompt ${name}`))!;
4681
4682 const freshRun = projection.runs.find(
4683 (run) => run.userMessageId === `message:fresh-context:${key}:beta`,
4684 )!;
4685 assert.isTrue(freshRun.freshContext);
4686 assert.equal(freshRun.contextHandoffId, null);
4687 assert.isTrue(
4688 projection.turnItems.some(
4689 (item) =>
4690 item.type === "user_message" &&
4691 item.messageId === freshRun.userMessageId &&
4692 item.freshContext === true,
4693 ),
4694 );
4695 assert.notEqual(turnFor("beta").providerThreadId, turnFor("alpha").providerThreadId);
4696 assert.notEqual(turnFor("beta").nativeThreadId, turnFor("alpha").nativeThreadId);
4697 assert.equal(turnFor("beta").text, "Prompt beta");
4698 if (queued) {
4699 assert.equal(turnFor("gamma").providerThreadId, turnFor("beta").providerThreadId);
4700 }
4701 if (broken) {
4702 const recovered = turnFor("epsilon");
4703 assert.equal(recovered.providerThreadId, turnFor("beta").providerThreadId);
4704 assert.notEqual(recovered.nativeThreadId, turnFor("beta").nativeThreadId);
4705 assert.include(recovered.text, "Prompt beta");
4706 assert.notInclude(recovered.text, "Prompt alpha");
4707 assert.notInclude(recovered.text, "Prompt zero");
4708 // The broken native id is never reopened; a reopened session starts fresh only once.
4709 assert.deepEqual(
4710 (yield* Ref.get(runtimeStarts)).slice(startsBeforeRecovery),
4711 restarted ? ["new"] : ["replacement"],
4712 );
4713 const recoveredRun = projection.runs.find(
4714 (run) => run.userMessageId === `message:fresh-context:${key}:epsilon`,
4715 )!;
4716 assert.equal(
4717 projection.contextHandoffs.find((handoff) => handoff.targetRunId === recoveredRun.id)!
4718 .coveredRunOrdinals.from,
4719 3,
4720 );
4721 }
4722 const codexTurn = turnFor("delta");
4723 assert.notEqual(codexTurn.providerThreadId, turnFor("zero").providerThreadId);
4724 assert.include(codexTurn.text, "Prompt beta");
4725 assert.notInclude(codexTurn.text, "Prompt alpha");
4726 assert.notInclude(codexTurn.text, "Prompt zero");
4727 assert.equal(projection.contextHandoffs.at(-1)!.coveredRunOrdinals.from, 3);
4728 }).pipe(
4729 Effect.provide(
4730 ProviderReplayHarness.layerWithRegistry(
4731 {
4732 name: `fresh-context-${key}`,
4733 runtimePolicyOverride: {
4734 cwd,
4735 approvalPolicy: "never",
4736 sandboxPolicy: {
4737 type: "readOnly",
4738 access: { type: "fullAccess" },
4739 networkAccess: false,
4740 },
4741 },
4742 },
4743 layerRegistry,
4744 ),
4745 ),
4746 );
4747 }),
4748 );
4749
4750 it.live(
4751 "starts a scheduled fresh run while the previous run's session still has background work",
4752 () =>
4753 Effect.gen(function* () {
4754 const cwd = yield* checkpointWorkspace("scheduled-fresh-background");
4755 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
4756 const closedSessions = yield* Ref.make<ReadonlyArray<string>>([]);
4757 const backgroundWork = yield* Ref.make(false);
4758 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
4759 makeTestAdapter({
4760 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
4761 driver: CLAUDE_DRIVER,
4762 capabilities: ClaudeProviderCapabilitiesV2,
4763 modelSelection: CLAUDE_MODEL_SELECTION,
4764 responseByRunOrdinal: {},
4765 capturedTurns,
4766 closedSessions,
4767 backgroundWork,
4768 nativeThreadGeneration: yield* Ref.make(0),
4769 }),
4770 ]);
4771 const threadId = ThreadId.make("thread:scheduled-fresh-background");
4772 yield* Effect.gen(function* () {
4773 const orchestrator = yield* Orchestrator.OrchestratorV2;
4774 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
4775 // The command ScheduledTaskService sends for a fresh-session schedule.
4776 const fire = (name: string) =>
4777 orchestrator.dispatch({
4778 type: "message.dispatch",
4779 createdBy: "user",
4780 creationSource: "web",
4781 commandId: CommandId.make(`scheduled-task:scheduled-fresh-background:${name}`),
4782 threadId,
4783 messageId: MessageId.make(`message:scheduled-fresh-background:${name}`),
4784 scheduledTaskId: ScheduledTaskId.make("scheduled-fresh-background"),
4785 text: `Scheduled ${name}`,
4786 attachments: [],
4787 modelSelection: CLAUDE_MODEL_SELECTION,
4788 freshContext: true,
4789 dispatchMode: { type: "queue_after_active" },
4790 });
4791 yield* orchestrator.dispatch({
4792 type: "thread.create",
4793 createdBy: "user",
4794 creationSource: "web",
4795 commandId: CommandId.make("command:scheduled-fresh-background:create"),
4796 threadId,
4797 projectId: ProjectId.make("project:scheduled-fresh-background"),
4798 title: "Scheduled fresh session",
4799 modelSelection: CLAUDE_MODEL_SELECTION,
4800 runtimeMode: "full-access",
4801 interactionMode: "default",
4802 branch: null,
4803 worktreePath: cwd,
4804 });
4805 yield* fire("first");
4806 yield* worker.drain();
4807 yield* waitForIdle(threadId);
4808 // The first run left a background command running after its turn ended.
4809 yield* Ref.set(backgroundWork, true);
4810 yield* fire("second");
4811 yield* worker.drain();
4812 yield* waitForIdle(threadId);
4813 const turns = yield* Ref.get(capturedTurns);
4814 const turnFor = (name: string) =>
4815 turns.find((turn) => turn.text.endsWith(`Scheduled ${name}`));
4816 assert.isDefined(turnFor("second"));
4817 assert.notEqual(turnFor("second")!.nativeThreadId, turnFor("first")!.nativeThreadId);
4818 // Closing the busy session would end its background work.
4819 assert.deepEqual(yield* Ref.get(closedSessions), []);
4820 }).pipe(
4821 Effect.provide(
4822 ProviderReplayHarness.layerWithRegistry(
4823 {
4824 name: "scheduled-fresh-background",
4825 runtimePolicyOverride: {
4826 cwd,
4827 approvalPolicy: "never",
4828 sandboxPolicy: {
4829 type: "readOnly",
4830 access: { type: "fullAccess" },
4831 networkAccess: false,
4832 },
4833 },
4834 },
4835 layerRegistry,
4836 ),
4837 ),
4838 );
4839 }),
4840 );
4841
4842 it.live("keeps the user's conversation after a scheduled fresh run in the same thread", () =>
4843 Effect.gen(function* () {
4844 const cwd = yield* checkpointWorkspace("scheduled-fresh-user-chat");
4845 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
4846 const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
4847 makeTestAdapter({
4848 instanceId: CLAUDE_MODEL_SELECTION.instanceId,
4849 driver: CLAUDE_DRIVER,
4850 capabilities: ClaudeProviderCapabilitiesV2,
4851 modelSelection: CLAUDE_MODEL_SELECTION,
4852 responseByRunOrdinal: {},
4853 capturedTurns,
4854 nativeThreadGeneration: yield* Ref.make(0),
4855 failedRunOrdinals: new Set([5]),
4856 brokenRunOrdinals: new Set([5]),
4857 }),
4858 ]);
4859 const threadId = ThreadId.make("thread:scheduled-fresh-user-chat");
4860 yield* Effect.gen(function* () {
4861 const orchestrator = yield* Orchestrator.OrchestratorV2;
4862 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
4863 const send = (name: string, scheduled: boolean) =>
4864 orchestrator.dispatch({
4865 type: "message.dispatch",
4866 createdBy: "user",
4867 creationSource: "web",
4868 commandId: CommandId.make(`command:scheduled-fresh-user-chat:${name}`),
4869 threadId,
4870 messageId: MessageId.make(`message:scheduled-fresh-user-chat:${name}`),
4871 text: name,
4872 attachments: [],
4873 modelSelection: CLAUDE_MODEL_SELECTION,
4874 dispatchMode: { type: "queue_after_active" },
4875 ...(scheduled
4876 ? {
4877 scheduledTaskId: ScheduledTaskId.make("scheduled-fresh-user-chat"),
4878 freshContext: true,
4879 }
4880 : {}),
4881 });
4882 yield* orchestrator.dispatch({
4883 type: "thread.create",
4884 createdBy: "user",
4885 creationSource: "web",
4886 commandId: CommandId.make("command:scheduled-fresh-user-chat:create"),
4887 threadId,
4888 projectId: ProjectId.make("project:scheduled-fresh-user-chat"),
4889 title: "User chat with a fresh-session schedule",
4890 modelSelection: CLAUDE_MODEL_SELECTION,
4891 runtimeMode: "full-access",
4892 interactionMode: "default",
4893 branch: null,
4894 worktreePath: cwd,
4895 });
4896 const eventSink = yield* EventSink.EventSinkV2;
4897 for (const [name, scheduled] of [
4898 ["user-first", false],
4899 ["scheduled", true],
4900 ["user-second", false],
4901 ["compaction", false],
4902 ["scheduled-after-compaction", true],
4903 ["user-third", false],
4904 ["user-fourth", false],
4905 ] as const) {
4906 if (name === "compaction") {
4907 // The user's run compacts, so later dispatches no longer read the first scheduled run's message.
4908 const run = (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)!;
4909 const compactedAt = yield* DateTime.now;
4910 yield* eventSink.write({
4911 events: [
4912 {
4913 id: EventId.make("event:scheduled-fresh-user-chat:compaction"),
4914 type: "turn-item.updated",
4915 threadId,
4916 runId: run.id,
4917 nodeId: run.rootNodeId!,
4918 occurredAt: compactedAt,
4919 payload: {
4920 id: TurnItemId.make("item:scheduled-fresh-user-chat:compaction"),
4921 threadId,
4922 runId: run.id,
4923 nodeId: run.rootNodeId,
4924 providerThreadId: null,
4925 providerTurnId: null,
4926 nativeItemRef: null,
4927 parentItemId: null,
4928 ordinal: 1_000,
4929 status: "completed",
4930 title: null,
4931 startedAt: compactedAt,
4932 completedAt: compactedAt,
4933 updatedAt: compactedAt,
4934 type: "compaction",
4935 driver: CLAUDE_DRIVER,
4936 },
4937 },
4938 ],
4939 });
4940 continue;
4941 }
4942 yield* send(name, scheduled);
4943 yield* worker.drain();
4944 yield* waitForIdle(threadId);
4945 }
4946 const turns = yield* Ref.get(capturedTurns);
4947 const turnFor = (name: string) => turns.find((turn) => turn.text.endsWith(name));
4948 assert.notEqual(
4949 turnFor("scheduled")!.nativeThreadId,
4950 turnFor("user-first")!.nativeThreadId,
4951 );
4952 // The scheduled run's fresh session must not replace the user's conversation.
4953 assert.equal(turnFor("user-second")!.nativeThreadId, turnFor("user-first")!.nativeThreadId);
4954 assert.equal(turnFor("user-third")!.nativeThreadId, turnFor("user-first")!.nativeThreadId);
4955 // The user's session broke; recovery reads the whole thread, so scheduled runs do not cut it.
4956 const recovered = turnFor("user-fourth")!;
4957 assert.equal(recovered.providerThreadId, turnFor("user-first")!.providerThreadId);
4958 assert.notEqual(recovered.nativeThreadId, turnFor("user-first")!.nativeThreadId);
4959 assert.include(recovered.text, "user-first");
4960 assert.include(recovered.text, "user-third");
4961 const projection = yield* orchestrator.getThreadProjection(threadId);
4962 const recoveredRun = projection.runs.find(
4963 (run) => run.userMessageId === "message:scheduled-fresh-user-chat:user-fourth",
4964 )!;
4965 assert.equal(
4966 projection.contextHandoffs.find((handoff) => handoff.targetRunId === recoveredRun.id)!
4967 .coveredRunOrdinals.from,
4968 1,
4969 );
4970 }).pipe(
4971 Effect.provide(
4972 ProviderReplayHarness.layerWithRegistry(
4973 {
4974 name: "scheduled-fresh-user-chat",
4975 runtimePolicyOverride: {
4976 cwd,
4977 approvalPolicy: "never",
4978 sandboxPolicy: {
4979 type: "readOnly",
4980 access: { type: "fullAccess" },
4981 networkAccess: false,
4982 },
4983 },
4984 },
4985 layerRegistry,
4986 ),
4987 ),
4988 );
4989 }),
4990 );
4991
4992 it.live(
4993 "keeps the post-clear conversation when the pre-clear session's background task settles",
4994 () =>
4995 Effect.gen(function* () {
4996 const cwd = yield* checkpointWorkspace("fresh-context-old-session-wake");
4997 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
4998 const backgroundWork = yield* Ref.make(false);
4999 const started = yield* Deferred.make<void>();
5000 const release = yield* Deferred.make<void>();
Showing first 5,000 of 6,174 lines. View raw