sailfork / t3code-github / apps/ server/ src/ orchestration-v2/ testkit/ ProviderSwitch.integration.test.ts

import { vi } from "vite-plus/test";
import { historyResponseItems } from "@t3tools/provider-core/server/handoffBudget";
import { assert, describe, it } from "@effect/vitest";
import {
  CommandId,
  type ChatAttachment,
  EventId,
  MessageId,
  type ModelSelection,
  type OrchestrationV2Command,
  type OrchestrationV2ProviderCapabilities,
  type OrchestrationV2ProviderSession,
  type OrchestrationV2ProviderThread,
  type OrchestrationV2ProviderTurnTokenUsage,
  type OrchestrationV2ThreadProjection,
  ProjectId,
  ProviderInstanceId,
  ProviderSessionId,
  ProviderThreadId,
  ProviderTurnId,
  ThreadId,
  TurnItemId,
  ProviderDriverKind,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as PubSub from "effect/PubSub";
import * as Ref from "effect/Ref";
import * as Stream from "effect/Stream";
import * as Schema from "effect/Schema";
import * as SqlClient from "effect/sql/SqlClient";

import * as SqlitePersistence from "../../persistence/Sqlite.ts";
import { CommandPolicyCapabilityUnsupportedError } from "../CommandPolicy.ts";
import {
  ClaudeAgentSdkQueryRunnerError,
  ClaudeAppendSystemPromptFileError,
  ClaudeBackgroundWorkBlocksQueryReplacementError,
  ClaudeProviderCapabilitiesV2,
} from "../Adapters/ClaudeAdapterV2.ts";
import {
  CodexProviderCapabilitiesV2,
  canReuseCodexContextUsage,
} from "../Adapters/CodexAdapterV2.ts";
import { AcpProviderCapabilitiesV2 } from "@t3tools/provider-acp/server/adapter";
import * as EventSink from "../EventSink.ts";
import * as EventStore from "../EventStore.ts";
import * as LegacyV1ThreadImporter from "../legacy/LegacyV1ThreadImporter.ts";
import * as Orchestrator from "../Orchestrator.ts";
import * as EffectWorker from "../EffectWorker.ts";
import * as EffectOutbox from "../EffectOutbox.ts";
import * as ProjectionReplay from "./ProjectionReplay.ts";
import * as ProjectionStore from "../ProjectionStore.ts";
import * as ProviderAdapterRegistry from "../ProviderAdapterRegistry.ts";
import { makeProviderFailure } from "@t3tools/provider-core/server/failure";
import {
  CLAUDE_MODEL_SELECTION,
  CODEX_MODEL_SELECTION,
  GROK_MODEL_SELECTION,
} from "./fixtures/shared.ts";
import * as ProviderReplayHarness from "./ProviderReplayHarness.ts";
import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";

const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));

const threadId = ThreadId.make("thread:provider-switch");
const projectId = ProjectId.make("project:provider-switch");
const CODEX_DRIVER = ProviderDriverKind.make("codex");
const CLAUDE_DRIVER = ProviderDriverKind.make("claudeAgent");
const GROK_DRIVER = ProviderDriverKind.make("acp");

interface CapturedTurn {
  readonly driver: ProviderDriverKind;
  readonly threadId: ThreadId;
  readonly providerThreadId: ProviderThreadId;
  readonly text: string;
  readonly modelSelection: ModelSelection;
  readonly attachments: ReadonlyArray<ChatAttachment>;
  readonly nativeThreadId?: string | null;
  readonly providerTurnOrdinal?: number;
  readonly nativeThreadHasTurns?: boolean;
}

function unimplemented(driver: ProviderDriverKind, detail: string) {
  return Effect.fail(new ProviderAdapter.ProviderAdapterProtocolError({ driver, detail }));
}

function makeTestAdapter(input: {
  readonly instanceId: ProviderInstanceId;
  readonly driver: ProviderDriverKind;
  readonly capabilities: OrchestrationV2ProviderCapabilities;
  readonly modelSelection: ModelSelection;
  readonly responseByRunOrdinal: Readonly<Record<number, string>>;
  readonly responseByThreadId?: Readonly<Record<string, Readonly<Record<number, string>>>>;
  readonly capturedTurns: Ref.Ref<ReadonlyArray<CapturedTurn>>;
  readonly injectedHistory?: Ref.Ref<ReadonlyArray<unknown>>;
  readonly failStartOnce?: Ref.Ref<boolean>;
  readonly refuseStarts?: Ref.Ref<number>;
  readonly refusalCause?: unknown;
  readonly failInjectionOnce?: Ref.Ref<boolean>;
  readonly nativeThreadGeneration?: Ref.Ref<number>;
  readonly failResume?: boolean;
  readonly failResumeOnce?: Ref.Ref<boolean>;
  readonly initialContextUsage?: OrchestrationV2ProviderThread["contextUsage"];
  readonly getModelContextWindow?: (selection: ModelSelection) => number | undefined;
  readonly canReuseContextUsage?: ProviderAdapter.ProviderAdapterV2SessionRuntime["canReuseContextUsage"];
  readonly tokenUsageByRunOrdinal?: Readonly<
    Record<number, Omit<OrchestrationV2ProviderTurnTokenUsage, "updatedAt">>
  >;
  readonly failure?: ReturnType<typeof makeProviderFailure>;
  readonly failedRunOrdinals?: ReadonlySet<number>;
  readonly interruptedRunOrdinals?: ReadonlySet<number>;
  readonly holdRunOrdinal?: number;
  readonly pendingBackgroundWork?: Ref.Ref<boolean>;
  readonly subagentOnlyBackgroundWork?: boolean;
  readonly holdFirstTurn?: Deferred.Deferred<void>;
  readonly releaseFirstTurn?: Deferred.Deferred<void>;
  readonly backgroundWork?: Ref.Ref<boolean>;
  readonly closedSessions?: Ref.Ref<ReadonlyArray<string>>;
}): ProviderAdapter.ProviderAdapterV2["Service"] {
  return {
    instanceId: input.instanceId,
    driver: input.driver,
    getCapabilities: () => Effect.succeed(input.capabilities),
    planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }),
    openSession: (sessionInput) =>
      Effect.gen(function* () {
        const events = yield* PubSub.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
        if (input.closedSessions !== undefined) {
          const closedSessions = input.closedSessions;
          yield* Effect.addFinalizer(() =>
            Ref.update(closedSessions, (ids) => [...ids, sessionInput.providerSessionId]),
          );
        }
        const now = yield* DateTime.now;
        const providerSession: OrchestrationV2ProviderSession = {
          id: sessionInput.providerSessionId,
          driver: input.driver,
          providerInstanceId: input.instanceId,
          status: "ready",
          cwd: sessionInput.runtimePolicy.cwd ?? process.cwd(),
          model: input.modelSelection.model,
          capabilities: input.capabilities,
          createdAt: now,
          updatedAt: now,
          lastError: null,
        };

        const runtime: ProviderAdapter.ProviderAdapterV2SessionRuntime = {
          instanceId: input.instanceId,
          driver: input.driver,
          providerSessionId: sessionInput.providerSessionId,
          providerSession,
          events: Stream.fromPubSub(events),
          ...(input.pendingBackgroundWork === undefined
            ? {}
            : {
                hasPendingBackgroundWorkForThread: (_thread, options) =>
                  input.subagentOnlyBackgroundWork && options?.includeSubagents !== true
                    ? Effect.succeed(false)
                    : Ref.get(input.pendingBackgroundWork!),
              }),
          ...(input.getModelContextWindow === undefined
            ? {}
            : { getModelContextWindow: input.getModelContextWindow }),
          ...(input.canReuseContextUsage === undefined
            ? {}
            : { canReuseContextUsage: input.canReuseContextUsage }),
          ...(input.backgroundWork === undefined
            ? {}
            : {
                // Like Claude: native subagents are visible only to the session-wide
                // probe, never to the per-thread roster probe.
                hasPendingBackgroundWork: Ref.get(input.backgroundWork),
                hasPendingBackgroundWorkForThread: () => Effect.succeed(false),
              }),
          ensureThread: (threadInput) =>
            Effect.gen(function* () {
              const createdAt = yield* DateTime.now;
              const generation =
                input.nativeThreadGeneration === undefined
                  ? ""
                  : `:${yield* Ref.getAndUpdate(input.nativeThreadGeneration, (value) => value + 1)}`;
              const nativeThreadId = `${input.driver}:${threadInput.threadId}${generation}`;
              return {
                id: ProviderThreadId.make(`provider-thread:${nativeThreadId}`),
                driver: input.driver,
                providerInstanceId: input.instanceId,
                providerSessionId: sessionInput.providerSessionId,
                appThreadId: threadInput.threadId,
                ownerNodeId: null,
                nativeThreadRef: {
                  driver: input.driver,
                  nativeId: nativeThreadId,
                  strength: "strong",
                },
                nativeConversationHeadRef: null,
                status: "idle",
                firstRunOrdinal: null,
                lastRunOrdinal: null,
                handoffIds: [],
                contextUsage: input.initialContextUsage ?? null,
                forkedFrom: null,
                createdAt,
                updatedAt: createdAt,
              } satisfies OrchestrationV2ProviderThread;
            }),
          resumeThread: ({ providerThread }) =>
            Effect.gen(function* () {
              if (
                input.failResume ||
                (input.failResumeOnce !== undefined &&
                  (yield* Ref.getAndSet(input.failResumeOnce, false)))
              )
                return yield* unimplemented(input.driver, "simulated native resume failure");
              return providerThread;
            }),
          ...(input.injectedHistory === undefined
            ? {}
            : {
                injectHistory: (history: ProviderAdapter.ProviderAdapterV2HistoricalContext) =>
                  Ref.update(input.injectedHistory!, (current) => [
                    ...current,
                    ...historyResponseItems(history.messages, history.context),
                  ]).pipe(
                    Effect.andThen(
                      Effect.gen(function* () {
                        if (
                          input.failInjectionOnce !== undefined &&
                          (yield* Ref.getAndSet(input.failInjectionOnce, false))
                        )
                          return yield* unimplemented(
                            input.driver,
                            "lost injection acknowledgement",
                          );
                        return true;
                      }),
                    ),
                  ),
              }),
          compactThread: (turnInput) => runtime.startTurn(turnInput),
          startTurn: (turnInput) =>
            Effect.gen(function* () {
              if (
                input.failStartOnce !== undefined &&
                (yield* Ref.getAndSet(input.failStartOnce, false))
              )
                return yield* unimplemented(input.driver, "turn start failed after injection");
              if (
                input.refuseStarts !== undefined &&
                (yield* Ref.getAndUpdate(input.refuseStarts, (left) => Math.max(0, left - 1))) > 0
              )
                return yield* new ProviderAdapter.ProviderAdapterTurnStartError({
                  driver: input.driver,
                  threadId: turnInput.threadId,
                  providerThreadId: turnInput.providerThread.id,
                  runId: turnInput.runId,
                  cause:
                    input.refusalCause ?? new ClaudeBackgroundWorkBlocksQueryReplacementError(),
                });
              yield* Effect.yieldNow;
              yield* Ref.update(input.capturedTurns, (turns) => [
                ...turns,
                {
                  driver: input.driver,
                  threadId: turnInput.threadId,
                  providerThreadId: turnInput.providerThread.id,
                  text: turnInput.message.text,
                  modelSelection: turnInput.modelSelection,
                  attachments: turnInput.message.attachments,
                  nativeThreadId: turnInput.providerThread.nativeThreadRef?.nativeId ?? null,
                  providerTurnOrdinal: turnInput.providerTurnOrdinal,
                  ...(turnInput.nativeThreadHasTurns === undefined
                    ? {}
                    : { nativeThreadHasTurns: turnInput.nativeThreadHasTurns }),
                },
              ]);
              if (
                turnInput.runOrdinal === (input.holdRunOrdinal ?? 1) &&
                input.holdFirstTurn !== undefined
              ) {
                yield* Deferred.succeed(input.holdFirstTurn, undefined);
                if (input.releaseFirstTurn === undefined) return;
                yield* Deferred.await(input.releaseFirstTurn);
              }
              const eventTime = yield* DateTime.now;
              const providerTurnId = ProviderTurnId.make(
                `provider-turn:${input.driver}:${turnInput.threadId}:${turnInput.runOrdinal}`,
              );
              const terminalStatus = input.failedRunOrdinals?.has(turnInput.runOrdinal)
                ? "failed"
                : input.interruptedRunOrdinals?.has(turnInput.runOrdinal)
                  ? "interrupted"
                  : "completed";
              const response =
                input.responseByThreadId?.[turnInput.threadId]?.[turnInput.runOrdinal] ??
                input.responseByRunOrdinal[turnInput.runOrdinal] ??
                `${input.driver} response for run ${turnInput.runOrdinal}`;
              const providerEvents: ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event> = [
                {
                  type: "provider_turn.updated",
                  driver: input.driver,
                  providerTurn: {
                    id: providerTurnId,
                    providerThreadId: turnInput.providerThread.id,
                    nodeId: turnInput.rootNodeId,
                    runAttemptId: turnInput.attemptId,
                    nativeTurnRef: {
                      driver: input.driver,
                      nativeId: `native-turn:${turnInput.threadId}:${turnInput.runOrdinal}`,
                      strength: "strong",
                    },
                    ordinal: turnInput.runOrdinal,
                    status: terminalStatus,
                    startedAt: eventTime,
                    completedAt: eventTime,
                  },
                },
                {
                  type: "turn_item.updated",
                  driver: input.driver,
                  turnItem: {
                    id: TurnItemId.make(
                      `turn-item:${input.driver}:${turnInput.threadId}:${turnInput.runOrdinal}:assistant`,
                    ),
                    threadId: turnInput.threadId,
                    runId: turnInput.runId,
                    nodeId: turnInput.rootNodeId,
                    providerThreadId: turnInput.providerThread.id,
                    providerTurnId,
                    nativeItemRef: null,
                    parentItemId: null,
                    ordinal: turnInput.runOrdinal * 100 + 1,
                    status: terminalStatus,
                    title: null,
                    startedAt: eventTime,
                    completedAt: eventTime,
                    updatedAt: eventTime,
                    type: "assistant_message",
                    messageId: MessageId.make(
                      `message:${input.driver}:${turnInput.threadId}:${turnInput.runOrdinal}:assistant`,
                    ),
                    text: response,
                    streaming: false,
                  },
                },
                {
                  type: "turn.terminal",
                  driver: input.driver,
                  providerThreadId: turnInput.providerThread.id,
                  providerTurnId,
                  runOrdinal: turnInput.runOrdinal,
                  ...(terminalStatus === "failed"
                    ? {
                        status: terminalStatus,
                        failureItemOrdinal: turnInput.runOrdinal * 100 + 2,
                        failure:
                          input.failure ??
                          makeProviderFailure({
                            message: "Simulated provider failure.",
                            code: "simulated_failure",
                            class: "provider_error",
                          }),
                      }
                    : { status: terminalStatus, failure: null }),
                  threadDisposition: "reusable",
                },
              ];
              const reportedUsage = input.tokenUsageByRunOrdinal?.[turnInput.runOrdinal];
              const turnEvent = providerEvents[0];
              if (reportedUsage && turnEvent?.type === "provider_turn.updated") {
                yield* PubSub.publish(events, {
                  ...turnEvent,
                  providerTurn: {
                    ...turnEvent.providerTurn,
                    status: "running",
                    completedAt: null,
                    tokenUsage: { ...reportedUsage, updatedAt: DateTime.formatIso(eventTime) },
                  },
                });
              }
              for (const event of providerEvents) {
                yield* PubSub.publish(events, event);
              }
            }),
          steerTurn: () => Effect.void,
          interruptTurn: () => Effect.void,
          respondToRuntimeRequest: () => Effect.void,
          readThreadSnapshot: () =>
            unimplemented(input.driver, "readThreadSnapshot unused in provider switch test"),
          rollbackThread: () =>
            unimplemented(input.driver, "rollbackThread unused in provider switch test"),
          forkThread: () =>
            unimplemented(input.driver, "forkThread unused in provider switch test"),
        };
        return runtime;
      }),
  };
}

const waitForIdle = Effect.fn("ProviderSwitchTest.waitForIdle")(function* (
  targetThreadId: ThreadId,
) {
  const orchestrator = yield* Orchestrator.OrchestratorV2;
  const eventSink = yield* EventSink.EventSinkV2;
  const isIdle = (projection: OrchestrationV2ThreadProjection) =>
    projection.runs.every(
      (run) => !["queued", "starting", "running", "waiting"].includes(run.status),
    );
  const afterSequence = yield* eventSink.latestSequence();
  const current = yield* orchestrator.getThreadProjection(targetThreadId);
  if (isIdle(current)) {
    return current;
  }
  // Projections commit with their events, so each later event is the only time to re-read.
  const [idle] = yield* eventSink.stream({ afterSequence }).pipe(
    Stream.mapEffect(() => orchestrator.getThreadProjection(targetThreadId)),
    Stream.filter(isIdle),
    Stream.take(1),
    Stream.runCollect,
  );
  if (idle === undefined) {
    return yield* Effect.die(
      new Error("Provider switch test event stream ended before the thread went idle"),
    );
  }
  return idle;
});

// Same tag as the adapter's unexported error raised when Claude never answers setMcpServers.
class ClaudeMcpRegistrationTimeoutError extends Schema.TaggedError<ClaudeMcpRegistrationTimeoutError>()(
  "ClaudeMcpRegistrationTimeoutError",
  {},
) {}

describe("orchestration v2 provider switching", () => {
  it.live.each([
    "compact-native",
    "compact-legacy",
    "large-current-input",
    "screenshot-fallback",
    "screenshot-reported-capacity-native",
    "screenshot-option-change-retry-native",
    "screenshot-exhausted-target-native",
    "screenshot-exhausted-target-fallback",
    "screenshot-exhausted-target-estimate-native",
    "screenshot-exhausted-target-oversized-native",
    "screenshot-exhausted-target-tight-native",
    "screenshot-exhausted-target-background-native",
    "screenshot-exhausted-target-subagent-native",
    "prior-images-reasoning-change-replacement-small-native",
    "imported-prior-images-fallback",
    "prior-images-telemetry-native",
    "prior-images-unsent-native",
    "prior-images-legacy-replacement-native",
    "delivery-write-failure",
  ] as const)("preserves handoffs through %s", (scenario) =>
    Effect.scoped(
      Effect.gen(function* () {
        const cwd = yield* checkpointWorkspace(`handoff-${scenario}`);
        const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
        const injectedHistory = yield* Ref.make<ReadonlyArray<unknown>>([]);
        const reasoningScenario = scenario.includes("reasoning-change");
        const turnUsageScenario = reasoningScenario || scenario.includes("turn-usage");
        const modelScenario =
          scenario.includes("model") || scenario.includes("option-change") || reasoningScenario;
        const failStartOnce = yield* Ref.make(false);
        const failResumeOnce = yield* Ref.make(false);
        const generation = yield* Ref.make(0);
        const pendingBackgroundWork = yield* Ref.make(false);
        const priorImages = scenario.includes("prior-images");
        const exhaustedTarget = scenario.includes("exhausted-target");
        const replaceNative = scenario.includes("replacement");
        const capacityScenario = modelScenario || scenario.includes("reported-capacity");
        const returning =
          scenario === "large-current-input" ||
          scenario.includes("-change-") ||
          priorImages ||
          exhaustedTarget;
        const targetSelection: ModelSelection = !modelScenario
          ? CLAUDE_MODEL_SELECTION
          : {
              ...CLAUDE_MODEL_SELECTION,
              ...(reasoningScenario
                ? { options: [{ id: "reasoningEffort", value: "low" }] }
                : scenario.includes("option-change")
                  ? { options: [{ id: "contextWindow", value: "1m" }] }
                  : { model: `${CLAUDE_MODEL_SELECTION.model}-large` }),
            };
        const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
          makeTestAdapter({
            instanceId: CODEX_MODEL_SELECTION.instanceId,
            driver: CODEX_DRIVER,
            capabilities: CodexProviderCapabilitiesV2,
            modelSelection: CODEX_MODEL_SELECTION,
            responseByRunOrdinal: { 1: "Original partial work" },
            capturedTurns,
          }),
          makeTestAdapter({
            instanceId: CLAUDE_MODEL_SELECTION.instanceId,
            driver: CLAUDE_DRIVER,
            capabilities: ClaudeProviderCapabilitiesV2,
            modelSelection: CLAUDE_MODEL_SELECTION,
            responseByRunOrdinal: scenario.includes("target-estimate")
              ? { 2: "x".repeat(10_000) }
              : {},
            capturedTurns,
            ...(scenario.includes("retry") || scenario.includes("unsent") ? { failStartOnce } : {}),
            ...(replaceNative ? { failResumeOnce, nativeThreadGeneration: generation } : {}),
            ...(exhaustedTarget ? { nativeThreadGeneration: generation } : {}),
            ...(scenario.includes("target-background") || scenario.includes("target-subagent")
              ? {
                  pendingBackgroundWork,
                  subagentOnlyBackgroundWork: scenario.includes("target-subagent"),
                }
              : {}),
            ...(scenario.includes("reported-capacity")
              ? { initialContextUsage: { usedTokens: 999_999, maxTokens: 1_000_000 } }
              : {}),
            ...(turnUsageScenario
              ? {
                  canReuseContextUsage: canReuseCodexContextUsage,
                  tokenUsageByRunOrdinal: {
                    2: {
                      usedTokens: replaceNative ? 30_000 : 37_321,
                      maxTokens: replaceNative
                        ? scenario.includes("small")
                          ? 32_000
                          : 64_000
                        : 258_400,
                    },
                  },
                }
              : modelScenario
                ? {
                    getModelContextWindow: (selection: ModelSelection) =>
                      selection.model.endsWith("-large") ||
                      selection.options?.some(
                        (option) => option.id === "contextWindow" && option.value === "1m",
                      )
                        ? 1_000_000
                        : 32_000,
                  }
                : scenario === "large-current-input" || priorImages || exhaustedTarget
                  ? {
                      getModelContextWindow: () =>
                        scenario.includes("target-tight") ? 25_121 : 32_000,
                    }
                  : {}),
            ...(scenario.endsWith("-native") || scenario === "large-current-input"
              ? { injectedHistory }
              : {}),
          }),
        ]);
        yield* Effect.gen(function* () {
          const orchestrator = yield* Orchestrator.OrchestratorV2;
          const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
          const eventSink = yield* EventSink.EventSinkV2;
          const screenshot: ChatAttachment = {
            type: "image",
            id: "screenshot",
            name: "screenshot.png",
            mimeType: "image/png",
            sizeBytes: 100_000,
          };
          const screenshots = Array.from(
            {
              length:
                scenario.includes("eight") || capacityScenario
                  ? 8
                  : scenario.includes("pair")
                    ? 2
                    : 1,
            },
            (_, index) => ({ ...screenshot, id: `screenshot-${index}` }),
          );
          const targetOrdinal = returning ? 4 : 2;
          const dispatch = (ordinal: number, text: string, selection: ModelSelection) =>
            orchestrator.dispatch({
              type: "message.dispatch",
              commandId: CommandId.make(`regression:${ordinal}`),
              threadId,
              messageId: MessageId.make(`regression:${ordinal}`),
              createdBy: "user",
              creationSource: "web",
              text,
              attachments:
                turnUsageScenario && ordinal === 2
                  ? Array.from({ length: 8 }, (_, index) => ({
                      ...screenshot,
                      id: `prior-${index}`,
                    }))
                  : turnUsageScenario && ordinal >= targetOrdinal
                    ? [screenshot, { ...screenshot, id: "current-2" }]
                    : scenario.startsWith("screenshot") &&
                        ordinal >= targetOrdinal &&
                        (!exhaustedTarget || ordinal === targetOrdinal)
                      ? screenshots
                      : priorImages && ordinal === (scenario.startsWith("imported") ? 1 : 2)
                        ? [screenshot]
                        : [],
              modelSelection: selection,
              dispatchMode: { type: "start_immediately" },
            });
          const wait = (ordinal: number) =>
            orchestrator.streamStoredEvents.pipe(
              Stream.filter(
                ({ event }) =>
                  event.type === "run.updated" &&
                  event.payload.ordinal === ordinal &&
                  (event.payload.status === "completed" || event.payload.status === "failed"),
              ),
              Stream.runHead,
              Effect.andThen(worker.drain()),
            );
          yield* orchestrator.dispatch({
            type: "thread.create",
            commandId: CommandId.make("regression:create"),
            threadId,
            projectId,
            createdBy: "user",
            creationSource: "web",
            title: "Handoff regression",
            modelSelection: CODEX_MODEL_SELECTION,
            runtimeMode: "full-access",
            interactionMode: "default",
            branch: null,
            worktreePath: null,
          });
          yield* dispatch(1, "Original request with constraints", CODEX_MODEL_SELECTION);
          yield* wait(1);
          const write = eventSink.write;
          const spy =
            scenario === "delivery-write-failure"
              ? vi.spyOn(eventSink, "write").mockImplementation((input) =>
                  input.events.some(
                    (event) =>
                      event.type === "context-handoff.updated" &&
                      event.payload.delivery?.status === "inline",
                  )
                    ? Effect.fail(
                        new EventSink.EventSinkWriteError({
                          eventCount: input.events.length,
                          cause: "bookkeeping unavailable",
                        }),
                      )
                    : write(input),
                )
              : undefined;
          yield* Effect.addFinalizer(() => Effect.sync(() => spy?.mockRestore()));
          const current = scenario.startsWith("compact")
            ? "/compact"
            : scenario.includes("target-oversized")
              ? "x".repeat(40_000)
              : capacityScenario && !turnUsageScenario
                ? "x".repeat(70_000)
                : scenario === "large-current-input"
                  ? "x".repeat(9_000)
                  : "Continue work";
          if (returning) {
            if (scenario.includes("unsent")) yield* Ref.set(failStartOnce, true);
            yield* dispatch(
              2,
              turnUsageScenario
                ? "x".repeat(27_460)
                : scenario.includes("target-estimate")
                  ? "x".repeat(12_000)
                  : "Establish target",
              CLAUDE_MODEL_SELECTION,
            );
            yield* wait(2);
            if (exhaustedTarget) {
              assert.equal(
                (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
                "completed",
              );
            }
            if (exhaustedTarget && !scenario.includes("target-estimate")) {
              const target = (yield* orchestrator.getThreadProjection(
                threadId,
              )).providerThreads.find(
                (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
              )!;
              yield* eventSink.write({
                events: [
                  {
                    id: EventId.make("exhausted-target-usage"),
                    type: "provider-thread.updated",
                    threadId,
                    occurredAt: yield* DateTime.now,
                    payload: { ...target, contextUsage: { usedTokens: 30_000, maxTokens: 32_000 } },
                  },
                ],
              });
            }
            if (modelScenario && !turnUsageScenario) {
              const target = (yield* orchestrator.getThreadProjection(
                threadId,
              )).providerThreads.find(
                (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
              )!;
              yield* eventSink.write({
                events: [
                  {
                    id: EventId.make("old-model-usage"),
                    type: "provider-thread.updated",
                    threadId,
                    occurredAt: yield* DateTime.now,
                    payload: {
                      ...target,
                      contextUsage: {
                        usedTokens: 30_000,
                        maxTokens: 32_000,
                        autoCompactThreshold: 31_000,
                      },
                    },
                  },
                ],
              });
            }
            if (scenario.includes("legacy-replacement")) {
              const existing = yield* orchestrator.getThreadProjection(threadId);
              yield* eventSink.write({
                events: existing.attempts.map(
                  ({ nativeThreadId: _nativeThreadId, ...legacy }, index) => ({
                    id: EventId.make(`legacy-attempt:${index}`),
                    type: "run-attempt.updated" as const,
                    threadId,
                    occurredAt: existing.thread.createdAt,
                    payload: legacy,
                  }),
                ),
              });
            }
            if (scenario.includes("telemetry")) {
              const target = (yield* orchestrator.getThreadProjection(
                threadId,
              )).providerThreads.find(
                (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
              )!;
              yield* eventSink.write({
                events: [
                  {
                    id: EventId.make("compacted-context-usage"),
                    type: "provider-thread.updated",
                    threadId,
                    occurredAt: yield* DateTime.now,
                    payload: { ...target, contextUsage: { usedTokens: 100, maxTokens: 32_000 } },
                  },
                ],
              });
            }
            yield* dispatch(
              3,
              priorImages && !replaceNative
                ? "New source constraint " + "q".repeat(9_000)
                : "New source constraint",
              CODEX_MODEL_SELECTION,
            );
            yield* wait(3);
          }
          if (replaceNative) {
            // Switching back to Codex already released Claude's session.
            yield* Ref.set(failResumeOnce, true);
          }
          if (scenario.includes("retry")) yield* Ref.set(failStartOnce, true);
          if (scenario.includes("target-background") || scenario.includes("target-subagent"))
            yield* Ref.set(pendingBackgroundWork, true);
          yield* dispatch(targetOrdinal, current, targetSelection);
          yield* wait(targetOrdinal);
          if (scenario.includes("retry")) {
            const failed = yield* orchestrator.getThreadProjection(threadId);
            assert.equal(failed.runs.at(-1)?.status, "failed");
            const failedUsage = failed.providerThreads.find(
              (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
            )!.contextUsage;
            if (reasoningScenario || scenario.includes("turn-usage")) {
              assert.deepEqual(failedUsage, { usedTokens: 37_321, maxTokens: 258_400 });
            } else {
              assert.deepEqual(failedUsage, { usedTokens: 30_000, maxTokens: 1_000_000 });
            }
            if (reasoningScenario) {
              const target = failed.providerThreads.find(
                (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
              )!;
              yield* orchestrator.dispatch({
                type: "provider-session.detach",
                commandId: CommandId.make("detach-before-reasoning-retry"),
                threadId,
                providerSessionId: target.providerSessionId!,
              });
              yield* worker.drain();
            }
            yield* dispatch(targetOrdinal + 1, current, targetSelection);
            yield* wait(targetOrdinal + 1);
          }
          if (reasoningScenario && replaceNative) {
            const replaced = yield* orchestrator.getThreadProjection(threadId);
            assert.equal(
              replaced.runs.at(-1)?.status,
              scenario.includes("small") ? "failed" : "completed",
            );
            const target = replaced.providerThreads.find(
              (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
            )!;
            assert.isNull(target.contextUsage);
            assert.equal(yield* Ref.get(generation), 2);
            assert.equal(
              (yield* Ref.get(capturedTurns)).at(-1)!.driver,
              scenario.includes("small") ? CODEX_DRIVER : CLAUDE_DRIVER,
            );
            return;
          }
          if (replaceNative) {
            if (scenario.includes("legacy-replacement")) {
              const recovered = yield* orchestrator.getThreadProjection(threadId);
              const handoff = recovered.contextHandoffs.at(-1)!;
              const { history: _history, ...legacyHandoff } = handoff;
              yield* eventSink.write({
                events: [
                  {
                    id: EventId.make("legacy-replacement-handoff"),
                    type: "context-handoff.updated",
                    threadId,
                    occurredAt: yield* DateTime.now,
                    payload: {
                      ...legacyHandoff,
                      delivery: {
                        nativeThreadId: handoff.delivery!.nativeThreadId,
                        status: handoff.delivery!.status,
                        itemIds: [],
                      },
                    },
                  },
                ],
              });
            }
            yield* dispatch(
              targetOrdinal + 1,
              "Continue after native replacement",
              CLAUDE_MODEL_SELECTION,
            );
            yield* wait(targetOrdinal + 1);
            yield* dispatch(
              targetOrdinal + 2,
              "Another source constraint " + "q".repeat(9_000),
              CODEX_MODEL_SELECTION,
            );
            yield* wait(targetOrdinal + 2);
            yield* dispatch(targetOrdinal + 3, current, CLAUDE_MODEL_SELECTION);
            yield* wait(targetOrdinal + 3);
          }
          const projection = yield* orchestrator.getThreadProjection(threadId);
          if (
            scenario.includes("target-oversized") ||
            scenario.includes("target-tight") ||
            scenario.includes("target-background") ||
            scenario.includes("target-subagent")
          ) {
            assert.equal(projection.runs.at(-1)?.status, "failed");
            assert.equal(yield* Ref.get(generation), 1);
            assert.equal(projection.messages.at(-1)!.text, current);
            assert.deepEqual(projection.messages.at(-1)!.attachments, screenshots);
            if (scenario.includes("target-background") || scenario.includes("target-subagent")) {
              assert.isTrue(yield* Ref.get(pendingBackgroundWork));
              const failure = projection.turnItems.findLast((item) => item.type === "error");
              assert.equal(
                failure?.type === "error" ? failure.failure.message : null,
                "The target provider still has background work running. Wait for it to finish or stop it before retrying the handoff.",
              );
            }
            return;
          }
          assert.equal(projection.runs.at(-1)?.status, "completed");
          if (exhaustedTarget) {
            assert.equal(yield* Ref.get(generation), 2);
            const turns = (yield* Ref.get(capturedTurns)).filter(
              (turn) => turn.driver === CLAUDE_DRIVER,
            );
            assert.notEqual(turns[0]!.nativeThreadId, turns[1]!.nativeThreadId);
            const latestRun = projection.runs.at(-1)!;
            assert.equal(
              projection.attempts.find((attempt) => attempt.id === latestRun.activeAttemptId)!
                .nativeThreadId,
              turns[1]!.nativeThreadId,
            );
            const context = scenario.endsWith("-native")
              ? yield* encodeJson(yield* Ref.get(injectedHistory))
              : turns[1]!.text;
            assert.include(context, "Original request with constraints");
            assert.include(context, "Original partial work");
            assert.include(context, "New source constraint");
            assert.include(turns[1]!.text, current);
            assert.deepEqual(turns[1]!.attachments, screenshots);
            const historyBeforeFollowup = yield* Ref.get(injectedHistory);
            yield* dispatch(5, "Continue after automatic recovery", CLAUDE_MODEL_SELECTION);
            yield* wait(5);
            assert.equal(
              (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
              "completed",
            );
            assert.equal(yield* Ref.get(generation), 2);
            assert.equal(
              (yield* Ref.get(capturedTurns)).at(-1)!.text,
              "Continue after automatic recovery",
            );
            assert.deepEqual(yield* Ref.get(injectedHistory), historyBeforeFollowup);
            if (scenario.includes("target-estimate")) {
              yield* dispatch(6, "Another source turn", CODEX_MODEL_SELECTION);
              yield* wait(6);
              yield* dispatch(7, "Return after recovery", CLAUDE_MODEL_SELECTION);
              yield* wait(7);
              assert.equal(
                (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
                "completed",
              );
              assert.equal(yield* Ref.get(generation), 2);
            }
            return;
          }
          if (priorImages) {
            const handoff = projection.contextHandoffs.at(-1)!;
            const context = scenario.endsWith("-native")
              ? yield* encodeJson(yield* Ref.get(injectedHistory))
              : (yield* Ref.get(capturedTurns)).at(-1)!.text;
            const shouldFit =
              turnUsageScenario ||
              scenario.startsWith("imported") ||
              scenario.includes("telemetry") ||
              scenario.includes("unsent") ||
              replaceNative;
            const sourceText =
              `${replaceNative ? "Another" : "New"} source constraint ` + "q".repeat(9_000);
            const sourceItem = projection.turnItems.find(
              (item) => item.type === "user_message" && item.text === sourceText,
            )!;
            assert.isDefined(sourceItem);
            if (shouldFit) {
              assert.include(context, sourceText);
              assert.include(
                projection.contextHandoffs
                  .filter(
                    (record) =>
                      record.targetRunId ===
                      (scenario.includes("retry")
                        ? projection.runs.find((run) => run.ordinal === targetOrdinal)!.id
                        : projection.runs.at(-1)!.id),
                  )
                  .flatMap((record) => record.delivery?.itemIds ?? []),
                sourceItem.id,
              );
            } else {
              assert.notInclude(context, "q".repeat(9_000));
              assert.isAbove(handoff.delivery!.omittedItemIds!.length, 0);
            }
            const latestRun = projection.runs.at(-1)!;
            const latestAttempt = projection.attempts.find(
              (attempt) => attempt.id === latestRun.activeAttemptId,
            )!;
            const target = projection.providerThreads.find(
              (thread) => thread.id === latestAttempt.providerThreadId,
            )!;
            assert.equal(latestAttempt.nativeThreadId, target.nativeThreadRef!.nativeId);
            if (turnUsageScenario) {
              if (replaceNative) assert.isNull(target.contextUsage);
              else
                assert.deepEqual(target.contextUsage, { usedTokens: 37_321, maxTokens: 258_400 });
              assert.lengthOf((yield* Ref.get(capturedTurns)).at(-1)!.attachments, 2);
            }
            if (replaceNative) assert.equal(yield* Ref.get(generation), 2);
            return;
          }
          const handoff = projection.contextHandoffs.at(-1)!;
          if (scenario.startsWith("screenshot")) {
            assert.deepEqual((yield* Ref.get(capturedTurns)).at(-1)!.attachments, screenshots);
          }
          if (scenario === "compact-legacy") {
            const { history: _history, ...legacyHandoff } = handoff;
            yield* eventSink.write({
              events: [
                {
                  id: EventId.make("legacy-handoff-shape"),
                  type: "context-handoff.updated",
                  threadId,
                  runId: handoff.targetRunId,
                  occurredAt: yield* DateTime.now,
                  payload: legacyHandoff,
                },
              ],
            });
            assert.isUndefined(handoff.delivery);
            yield* dispatch(3, "Continue after compact", CLAUDE_MODEL_SELECTION);
            yield* wait(3);
            assert.include(
              (yield* Ref.get(capturedTurns)).at(-1)!.text,
              "Original request with constraints",
            );
            assert.equal(
              (yield* orchestrator.getThreadProjection(threadId)).contextHandoffs.at(-1)?.delivery
                ?.status,
              "inline",
            );
          } else if (
            scenario === "delivery-write-failure" ||
            (scenario.startsWith("screenshot") && scenario.endsWith("fallback"))
          ) {
            assert.equal(
              handoff.delivery?.status,
              scenario.startsWith("screenshot") ? "inline" : "pending",
            );
            assert.include(
              (yield* Ref.get(capturedTurns)).at(-1)!.text,
              "Original request with constraints",
            );
          } else {
            assert.equal(handoff.delivery?.status, "injected");
            assert.equal((yield* Ref.get(capturedTurns)).at(-1)!.text, current);
            assert.include(
              yield* encodeJson(yield* Ref.get(injectedHistory)),
              "Original request with constraints",
            );
          }
        }).pipe(
          Effect.provide(
            ProviderReplayHarness.layerWithRegistry(
              {
                name: `handoff-${scenario}`,
                runtimePolicyOverride: {
                  cwd,
                  approvalPolicy: "never",
                  sandboxPolicy: { type: "readOnly" },
                },
              },
              layerRegistry,
            ),
          ),
        );
      }),
    ),
  );
  it.live.each([
    "turn-start",
    "injection",
    "large-missed-request",
    "exhausted-target-retry",
  ] as const)(
    "recovers %s failure without duplicating history in the same native thread",
    (failure) =>
      Effect.scoped(
        Effect.gen(function* () {
          const cwd = yield* checkpointWorkspace(`handoff-retry-${failure}`);
          const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
          const injectedHistory = yield* Ref.make<ReadonlyArray<unknown>>([]);
          const failOnce = yield* Ref.make(true);
          const generation = yield* Ref.make(0);
          const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
            makeTestAdapter({
              instanceId: CODEX_MODEL_SELECTION.instanceId,
              driver: CODEX_DRIVER,
              capabilities: CodexProviderCapabilitiesV2,
              modelSelection: CODEX_MODEL_SELECTION,
              responseByRunOrdinal: { 1: "Original partial work" },
              capturedTurns,
            }),
            makeTestAdapter({
              instanceId: CLAUDE_MODEL_SELECTION.instanceId,
              driver: CLAUDE_DRIVER,
              capabilities: ClaudeProviderCapabilitiesV2,
              modelSelection: CLAUDE_MODEL_SELECTION,
              responseByRunOrdinal: {},
              capturedTurns,
              injectedHistory,
              nativeThreadGeneration: generation,
              getModelContextWindow: () => 32_000,
              ...(failure !== "injection"
                ? { failStartOnce: failOnce }
                : { failInjectionOnce: failOnce }),
            }),
          ]);
          yield* Effect.gen(function* () {
            const orchestrator = yield* Orchestrator.OrchestratorV2;
            const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
            const dispatch = (ordinal: number, text: string, selection: ModelSelection) =>
              orchestrator.dispatch({
                type: "message.dispatch",
                commandId: CommandId.make(`retry:${ordinal}`),
                threadId,
                messageId: MessageId.make(`retry:${ordinal}`),
                createdBy: "user",
                creationSource: "web",
                text,
                attachments: [],
                modelSelection: selection,
                dispatchMode: { type: "start_immediately" },
              });
            const wait = (ordinal: number, status: "failed" | "completed") =>
              orchestrator.streamStoredEvents.pipe(
                Stream.filter(
                  ({ event }) =>
                    event.type === "run.updated" &&
                    event.payload.ordinal === ordinal &&
                    (event.payload.status === "completed" || event.payload.status === "failed"),
                ),
                Stream.runHead,
                Effect.andThen(worker.drain()),
                Effect.andThen(
                  Effect.gen(function* () {
                    assert.equal(
                      (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
                      status,
                    );
                  }),
                ),
              );
            yield* orchestrator.dispatch({
              type: "thread.create",
              commandId: CommandId.make("retry:create"),
              threadId,
              projectId,
              createdBy: "user",
              creationSource: "web",
              title: "Handoff retry",
              modelSelection: CODEX_MODEL_SELECTION,
              runtimeMode: "full-access",
              interactionMode: "default",
              branch: null,
              worktreePath: null,
            });
            yield* dispatch(1, "Original request with constraints", CODEX_MODEL_SELECTION);
            yield* wait(1, "completed");
            yield* dispatch(
              2,
              failure === "large-missed-request" ? "x".repeat(7_000) : "First target request",
              CLAUDE_MODEL_SELECTION,
            );
            yield* wait(2, "failed");
            const failed = yield* orchestrator.getThreadProjection(threadId);
            const failedHandoff = failed.contextHandoffs.at(-1)!;
            assert.equal(
              failedHandoff.delivery?.status,
              failure !== "injection" ? "injected" : "pending",
            );
            const historyBeforeRetry = (yield* Ref.get(injectedHistory)).length;
            if (failure === "exhausted-target-retry") {
              const target = failed.providerThreads.find(
                (thread) => thread.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
              )!;
              yield* (yield* EventSink.EventSinkV2).write({
                events: [
                  {
                    id: EventId.make("exhausted-retry-usage"),
                    type: "provider-thread.updated",
                    threadId,
                    occurredAt: yield* DateTime.now,
                    payload: { ...target, contextUsage: { usedTokens: 30_000, maxTokens: 32_000 } },
                  },
                ],
              });
            }
            const retryText =
              failure === "large-missed-request" ? "y".repeat(10_000) : "Retry target request";
            yield* dispatch(3, retryText, CLAUDE_MODEL_SELECTION);
            yield* wait(3, "completed");
            const retried = yield* orchestrator.getThreadProjection(threadId);
            const lastTurn = (yield* Ref.get(capturedTurns)).at(-1)!;
            assert.equal(lastTurn.text, retryText);
            if (failure !== "injection" && failure !== "exhausted-target-retry") {
              const delta = yield* encodeJson(
                (yield* Ref.get(injectedHistory)).slice(historyBeforeRetry),
              );
              if (failure === "large-missed-request") {
                assert.include(delta, "omitted 1 items");
                const missedRequest = retried.turnItems.find(
                  (item) => item.type === "user_message" && item.text === "x".repeat(7_000),
                )!;
                const delivery = retried.contextHandoffs.at(-1)!.delivery!;
                assert.include(delivery.omittedItemIds ?? [], missedRequest.id);
                assert.notInclude(delivery.itemIds, missedRequest.id);
              } else assert.include(delta, "First target request");
              assert.notInclude(delta, "Original request with constraints");
              assert.notInclude(delta, "Original partial work");
              assert.equal(yield* Ref.get(generation), 1);
            } else {
              assert.equal(yield* Ref.get(generation), 2);
              assert.notEqual(
                retried.contextHandoffs.at(-1)?.delivery?.nativeThreadId,
                failedHandoff.delivery?.nativeThreadId,
              );
              const newHistory = yield* encodeJson(
                (yield* Ref.get(injectedHistory)).slice(historyBeforeRetry),
              );
              assert.include(newHistory, "Original request with constraints");
              assert.include(newHistory, "Original partial work");
              assert.notInclude(newHistory, "Retry target request");
            }
            const beforeFollowup = yield* Ref.get(injectedHistory);
            const followup = "z".repeat(6_000);
            yield* dispatch(4, followup, CLAUDE_MODEL_SELECTION);
            yield* wait(4, "completed");
            assert.equal((yield* Ref.get(capturedTurns)).at(-1)!.text, followup);
            assert.deepEqual(yield* Ref.get(injectedHistory), beforeFollowup);
            assert.equal(
              (yield* orchestrator.getThreadProjection(threadId)).contextHandoffs.length,
              retried.contextHandoffs.length,
            );
          }).pipe(
            Effect.provide(
              ProviderReplayHarness.layerWithRegistry(
                {
                  name: `handoff-retry-${failure}`,
                  runtimePolicyOverride: {
                    cwd,
                    approvalPolicy: "never",
                    sandboxPolicy: { type: "readOnly" },
                  },
                },
                layerRegistry,
              ),
            ),
          );
        }),
      ),
  );

  it.live.each(["native", "legacy"] as const)(
    "starts a replacement native thread as new after the resume fallback with %s attempts",
    (attempts) =>
      Effect.scoped(
        Effect.gen(function* () {
          const cwd = yield* checkpointWorkspace("resume-fallback-native-turns");
          const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
          const failResumeOnce = yield* Ref.make(false);
          const generation = yield* Ref.make(0);
          const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
            makeTestAdapter({
              instanceId: CLAUDE_MODEL_SELECTION.instanceId,
              driver: CLAUDE_DRIVER,
              capabilities: ClaudeProviderCapabilitiesV2,
              modelSelection: CLAUDE_MODEL_SELECTION,
              responseByRunOrdinal: {},
              capturedTurns,
              failResumeOnce,
              nativeThreadGeneration: generation,
            }),
          ]);
          yield* Effect.gen(function* () {
            const orchestrator = yield* Orchestrator.OrchestratorV2;
            const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
            const runToCompletion = Effect.fn("runToCompletion")(function* (ordinal: number) {
              yield* orchestrator.dispatch({
                type: "message.dispatch",
                commandId: CommandId.make(`resume-fallback:${ordinal}`),
                threadId,
                messageId: MessageId.make(`resume-fallback:${ordinal}`),
                createdBy: "user",
                creationSource: "web",
                text: `Request ${ordinal}`,
                attachments: [],
                modelSelection: CLAUDE_MODEL_SELECTION,
                dispatchMode: { type: "start_immediately" },
              });
              yield* orchestrator.streamStoredEvents.pipe(
                Stream.filter(
                  ({ event }) =>
                    event.type === "run.updated" &&
                    event.payload.ordinal === ordinal &&
                    (event.payload.status === "completed" || event.payload.status === "failed"),
                ),
                Stream.runHead,
              );
              yield* worker.drain();
              assert.equal(
                (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
                "completed",
              );
            });
            yield* orchestrator.dispatch({
              type: "thread.create",
              commandId: CommandId.make("resume-fallback:create"),
              threadId,
              projectId,
              createdBy: "user",
              creationSource: "web",
              title: "Resume fallback",
              modelSelection: CLAUDE_MODEL_SELECTION,
              runtimeMode: "full-access",
              interactionMode: "default",
              branch: null,
              worktreePath: null,
            });
            yield* runToCompletion(1);
            const providerThread = (yield* orchestrator.getThreadProjection(
              threadId,
            )).providerThreads.at(0);
            if (providerThread === undefined || providerThread.providerSessionId === null) {
              return yield* Effect.die("Expected a provider session after the first turn.");
            }
            yield* orchestrator.dispatch({
              type: "provider-session.detach",
              commandId: CommandId.make("resume-fallback:detach"),
              threadId,
              providerSessionId: providerThread.providerSessionId,
            });
            yield* worker.drain();
            if (attempts === "legacy") {
              const existing = yield* orchestrator.getThreadProjection(threadId);
              yield* (yield* EventSink.EventSinkV2).write({
                events: existing.attempts.map(
                  ({ nativeThreadId: _nativeThreadId, ...legacy }, index) => ({
                    id: EventId.make(`resume-fallback-legacy-attempt:${index}`),
                    type: "run-attempt.updated" as const,
                    threadId,
                    occurredAt: existing.thread.createdAt,
                    payload: legacy,
                  }),
                ),
              });
            }
            yield* Ref.set(failResumeOnce, true);
            yield* runToCompletion(2);
            yield* runToCompletion(3);

            const turns = yield* Ref.get(capturedTurns);
            assert.equal(turns.length, 3);
            assert.equal(yield* Ref.get(generation), 2);
            assert.notEqual(turns[1]?.nativeThreadId, turns[0]?.nativeThreadId);
            assert.isAbove(turns[1]?.providerTurnOrdinal ?? 0, 1);
            assert.isFalse(turns[1]?.nativeThreadHasTurns);
            assert.equal(turns[2]?.nativeThreadId, turns[1]?.nativeThreadId);
            assert.isTrue(turns[2]?.nativeThreadHasTurns);
          }).pipe(
            Effect.provide(
              ProviderReplayHarness.layerWithRegistry(
                {
                  name: "resume-fallback-native-turns",
                  runtimePolicyOverride: {
                    cwd,
                    approvalPolicy: "never",
                    sandboxPolicy: { type: "readOnly" },
                  },
                },
                layerRegistry,
              ),
            ),
          );
        }),
      ),
  );

  it.live.each(["an older handoff", "a missed request"] as const)(
    "keeps the latest turn when a resume fallback also delivers %s",
    (older) =>
      Effect.scoped(
        Effect.gen(function* () {
          // A run that fails before its provider turn starts is retried as a missed request.
          const missed = older === "a missed request";
          const name = `resume-fallback-handoff-order${missed ? "-missed" : ""}`;
          const cwd = yield* checkpointWorkspace(name);
          const failStartOnce = yield* Ref.make(false);
          const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
          const injectedHistory = yield* Ref.make<ReadonlyArray<unknown>>([]);
          const oldResult = `old result ${"o".repeat(2_500)}`;
          const latestResult = `latest result ${"l".repeat(2_500)}`;
          const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
            makeTestAdapter({
              instanceId: CLAUDE_MODEL_SELECTION.instanceId,
              driver: CLAUDE_DRIVER,
              capabilities: ClaudeProviderCapabilitiesV2,
              modelSelection: CLAUDE_MODEL_SELECTION,
              responseByRunOrdinal: { 1: oldResult, 3: latestResult },
              capturedTurns,
              injectedHistory,
              failResume: true,
              ...(missed ? { failStartOnce } : { failedRunOrdinals: new Set([2]) }),
              nativeThreadGeneration: yield* Ref.make(0),
            }),
          ]);
          yield* Effect.gen(function* () {
            const orchestrator = yield* Orchestrator.OrchestratorV2;
            const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
            const runAfterDetach = Effect.fn("runAfterDetach")(function* (
              ordinal: number,
              text: string,
            ) {
              const providerSessionId = (yield* orchestrator.getThreadProjection(threadId))
                .providerThreads[0]?.providerSessionId;
              if (providerSessionId != null) {
                yield* orchestrator.dispatch({
                  type: "provider-session.detach",
                  commandId: CommandId.make(`handoff-order:detach:${ordinal}`),
                  threadId,
                  providerSessionId,
                });
                yield* worker.drain();
              }
              yield* orchestrator.dispatch({
                type: "message.dispatch",
                commandId: CommandId.make(`handoff-order:${ordinal}`),
                threadId,
                messageId: MessageId.make(`handoff-order:${ordinal}`),
                createdBy: "user",
                creationSource: "web",
                text,
                attachments: [],
                modelSelection: CLAUDE_MODEL_SELECTION,
                dispatchMode: { type: "start_immediately" },
              });
              yield* orchestrator.streamStoredEvents.pipe(
                Stream.filter(
                  ({ event }) =>
                    event.type === "run.updated" &&
                    event.payload.ordinal === ordinal &&
                    (event.payload.status === "completed" || event.payload.status === "failed"),
                ),
                Stream.runHead,
              );
              yield* worker.drain();
            });
            yield* orchestrator.dispatch({
              type: "thread.create",
              commandId: CommandId.make("handoff-order:create"),
              threadId,
              projectId,
              createdBy: "user",
              creationSource: "web",
              title: "Resume fallback handoff order",
              modelSelection: CLAUDE_MODEL_SELECTION,
              runtimeMode: "full-access",
              interactionMode: "default",
              branch: null,
              worktreePath: null,
            });
            yield* runAfterDetach(1, `Original brief ${"b".repeat(9_000)}`);
            yield* Ref.set(failStartOnce, missed);
            yield* runAfterDetach(2, missed ? `Request 2 ${"r".repeat(6_000)}` : "Request 2");
            yield* runAfterDetach(3, "Request 3");
            const historyBeforeRun4 = (yield* Ref.get(injectedHistory)).length;
            yield* runAfterDetach(4, "Request 4");

            const projection = yield* orchestrator.getThreadProjection(threadId);
            assert.deepEqual(
              projection.runs.map((run) => run.status),
              ["completed", "failed", "completed", "completed"],
            );
            const run4NativeThreadId = (yield* Ref.get(capturedTurns)).at(-1)?.nativeThreadId;
            assert.equal(
              projection.contextHandoffs.find(
                (handoff) => handoff.targetRunId === projection.runs[1]?.id,
              )?.delivery?.nativeThreadId,
              run4NativeThreadId,
            );
            const run4History = yield* encodeJson(
              (yield* Ref.get(injectedHistory)).slice(historyBeforeRun4),
            );
            assert.include(run4History, "Original brief");
            assert.include(run4History, latestResult);
          }).pipe(
            Effect.provide(
              ProviderReplayHarness.layerWithRegistry(
                {
                  name,
                  runtimePolicyOverride: {
                    cwd,
                    approvalPolicy: "never",
                    sandboxPolicy: { type: "readOnly" },
                  },
                },
                layerRegistry,
              ),
            ),
          );
        }),
      ),
  );

  it.live.each([
    ["background work", new ClaudeBackgroundWorkBlocksQueryReplacementError()],
    ["an unreadable append file", new ClaudeAppendSystemPromptFileError({ path: "missing.md" })],
    [
      "an MCP registration timeout",
      new ClaudeAgentSdkQueryRunnerError({
        method: "setMcpServers",
        cause: new ClaudeMcpRegistrationTimeoutError(),
      }),
    ],