sailfork / t3code-github / apps/ server/ src/ orchestration-v2/ legacy/ LegacyV1Cutover.integration.test.ts

import { assert, describe, it } from "@effect/vitest";
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import {
  CommandId,
  MessageId,
  ProviderDriverKind,
  ProviderInstanceId,
  ProviderThreadId,
  ProviderTurnId,
  ThreadId,
  TurnItemId,
  type OrchestrationV2ProviderSession,
  type OrchestrationV2ProviderThread,
  type OrchestrationV2ThreadProjection,
} from "@t3tools/contracts";
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
import * as Digest from "effect/crypto/Digest";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Logger from "effect/Logger";
import * as Path from "effect/Path";
import * as PubSub from "effect/PubSub";
import * as References from "effect/References";
import * as Ref from "effect/Ref";
import * as Stream from "effect/Stream";
import * as SqlClient from "effect/sql/SqlClient";

import { runMigrations } from "../../persistence/Migrations.ts";
import * as SqlitePersistence from "../../persistence/Sqlite.ts";
import Migration0042 from "../../persistence/Migrations/042_ProjectionThreadLinkedPullRequest.ts";
import Migration0043 from "../../persistence/Migrations/043_ProjectionThreadsUnsettledAt.ts";
import Migration0044 from "../../persistence/Migrations/044_ClearAutomaticProjectModelDefaults.ts";
import Migration0045 from "../../persistence/Migrations/045_ProjectionProjectsAutoPull.ts";
import Migration0046 from "../../persistence/Migrations/046_RepairAutomaticSettlementTimestamps.ts";
import Migration0047 from "../../persistence/Migrations/047_ProjectionProjectIcon.ts";
import Migration0048 from "../../persistence/Migrations/048_ProjectionThreadBranchPullRequest.ts";
import Migration0049 from "../../persistence/Migrations/049_ProjectionThreadsActiveOrderKey.ts";
import { CodexProviderCapabilitiesV2 } from "../Adapters/CodexAdapterV2.ts";
import * as EffectWorker from "../EffectWorker.ts";
import * as EventSink from "../EventSink.ts";
import * as EventStore from "../EventStore.ts";
import * as LegacyV1ThreadImporter from "./LegacyV1ThreadImporter.ts";
import * as Orchestrator from "../Orchestrator.ts";
import * as ProjectionReplay from "../testkit/ProjectionReplay.ts";
import * as ProjectionStore from "../ProjectionStore.ts";
import * as ProviderAdapterRegistry from "../ProviderAdapterRegistry.ts";
import * as ProviderReplayHarness from "../testkit/ProviderReplayHarness.ts";
import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";

const PROJECT_ID = "project:cutover";
const ACTIVE_THREAD = "thread:cutover:active";
const ARCHIVED_THREAD = "thread:cutover:archived";
const PINNED_THREAD = "thread:cutover:pinned";
const SNOOZED_THREAD = "thread:cutover:snoozed";
const DELETED_THREAD = "thread:cutover:deleted";
const INTERRUPTED_THREAD = "thread:cutover:interrupted";
const LONG_THREAD = "thread:cutover:long";
const ALL_THREADS = [
  ACTIVE_THREAD,
  ARCHIVED_THREAD,
  PINNED_THREAD,
  SNOOZED_THREAD,
  DELETED_THREAD,
  INTERRUPTED_THREAD,
  LONG_THREAD,
] as const;

const EARLIEST_MARKER = "EARLIEST_IMPORT_MARKER";
const LATEST_MARKER = "LATEST_IMPORT_MARKER";
const CONTINUATION_PROMPT = "Continue the migrated thread.";
const CONTINUATION_RESPONSE = "codex continuation response";

const driver = ProviderDriverKind.make("codex");
const instanceId = ProviderInstanceId.make("codex");
const modelSelectionJson = '{"instanceId":"codex","model":"gpt-5.4"}';
const codexModelSelection = {
  instanceId,
  model: "gpt-5.4",
};

/** Slot 41 models site-local migrations sharing production ids; the V2 runner applies only ids above the recorded maximum. */
const seedV1Database = (fixturePath: string, workspace: string) =>
  Effect.scoped(
    Effect.gen(function* () {
      const sql = yield* SqlClient.SqlClient;
      yield* sql`PRAGMA busy_timeout = 5000;`;
      yield* sql`PRAGMA foreign_keys = ON;`;
      yield* sql`PRAGMA journal_mode = WAL;`;
      yield* runMigrations({ toMigrationInclusive: 40 });
      yield* sql`
        INSERT INTO effect_sql_migrations (migration_id, name)
        VALUES (41, 'ThreadSummaryTimeline')
      `;
      yield* sql`
        CREATE TABLE thread_summary_timeline_entries (
          entry_id TEXT PRIMARY KEY,
          thread_id TEXT NOT NULL,
          payload_json TEXT NOT NULL
        )
      `;
      const tailMigrations = [
        [42, "ProjectionThreadLinkedPullRequest", Migration0042],
        [43, "ProjectionThreadsUnsettledAt", Migration0043],
        [44, "ClearAutomaticProjectModelDefaults", Migration0044],
        [45, "ProjectionProjectsAutoPull", Migration0045],
        [46, "RepairAutomaticSettlementTimestamps", Migration0046],
        [47, "ProjectionProjectIcon", Migration0047],
        [48, "ProjectionThreadBranchPullRequest", Migration0048],
        [49, "ProjectionThreadsActiveOrderKey", Migration0049],
      ] as const;
      for (const [id, name, migration] of tailMigrations) {
        yield* migration;
        yield* sql`
          INSERT INTO effect_sql_migrations (migration_id, name)
          VALUES (${id}, ${name})
        `;
      }

      yield* sql`
        INSERT INTO projection_projects (
          project_id, title, workspace_root, scripts_json, created_at, updated_at, deleted_at
        ) VALUES (
          ${PROJECT_ID},
          'Cutover project',
          ${workspace},
          '[]',
          '2026-01-01T00:00:00.000Z',
          '2026-01-10T00:00:00.000Z',
          NULL
        )
      `;

      const insertThread = (input: {
        readonly threadId: string;
        readonly title: string;
        readonly createdAt: string;
        readonly updatedAt: string;
        readonly archivedAt?: string;
        readonly snoozedAt?: string;
        readonly snoozedUntil?: string;
        readonly pinnedAt?: string;
        readonly pinOrderKey?: string;
        readonly deletedAt?: string;
        readonly worktreePath?: string;
        readonly linkedPullRequestJson?: string;
      }) =>
        sql`
          INSERT INTO projection_threads (
            thread_id, project_id, title, model_selection_json, runtime_mode,
            interaction_mode, branch, worktree_path, latest_turn_id,
            created_at, updated_at, archived_at, settled_override, settled_at,
            unsettled_at, snoozed_until, snoozed_at, pinned_at, pin_order_key,
            linked_pull_request_json, deleted_at
          ) VALUES (
            ${input.threadId},
            ${PROJECT_ID},
            ${input.title},
            ${modelSelectionJson},
            'full-access',
            'default',
            'main',
            ${input.worktreePath ?? null},
            NULL,
            ${input.createdAt},
            ${input.updatedAt},
            ${input.archivedAt ?? null},
            NULL,
            NULL,
            NULL,
            ${input.snoozedUntil ?? null},
            ${input.snoozedAt ?? null},
            ${input.pinnedAt ?? null},
            ${input.pinOrderKey ?? null},
            ${input.linkedPullRequestJson ?? null},
            ${input.deletedAt ?? null}
          )
        `;

      const insertMessage = (input: {
        readonly messageId: string;
        readonly threadId: string;
        readonly role: "user" | "assistant";
        readonly text: string;
        readonly createdAt: string;
        readonly isStreaming?: boolean;
        readonly attachmentsJson?: string;
      }) =>
        sql`
          INSERT INTO projection_thread_messages (
            message_id, thread_id, turn_id, role, text, attachments_json,
            is_streaming, created_at, updated_at
          ) VALUES (
            ${input.messageId},
            ${input.threadId},
            NULL,
            ${input.role},
            ${input.text},
            ${input.attachmentsJson ?? "[]"},
            ${input.isStreaming === true ? 1 : 0},
            ${input.createdAt},
            ${input.createdAt}
          )
        `;

      yield* insertThread({
        threadId: ACTIVE_THREAD,
        title: "Active conversation",
        createdAt: "2026-01-01T00:00:00.000Z",
        updatedAt: "2026-01-05T00:00:00.000Z",
        linkedPullRequestJson:
          '{"projectId":"project:cutover","repository":"pingdotgg/t3code","number":9100,"url":"https://github.com/pingdotgg/t3code/pull/9100"}',
      });
      yield* insertMessage({
        messageId: "message:cutover:active:1",
        threadId: ACTIVE_THREAD,
        role: "user",
        text: "First question",
        createdAt: "2026-01-01T01:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:active:2",
        threadId: ACTIVE_THREAD,
        role: "assistant",
        text: "First answer",
        createdAt: "2026-01-02T01:00:00.000Z",
        attachmentsJson:
          '[{"type":"image","id":"att-1","name":"shot.png","mimeType":"image/png","sizeBytes":1234}]',
      });
      yield* insertMessage({
        messageId: "message:cutover:active:3",
        threadId: ACTIVE_THREAD,
        role: "user",
        text: "Follow-up question",
        createdAt: "2026-01-03T01:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:active:4",
        threadId: ACTIVE_THREAD,
        role: "assistant",
        text: "Final answer",
        createdAt: "2026-01-05T01:00:00.000Z",
      });

      yield* insertThread({
        threadId: ARCHIVED_THREAD,
        title: "Archived conversation",
        createdAt: "2026-01-01T00:00:00.000Z",
        updatedAt: "2026-01-06T00:00:00.000Z",
        archivedAt: "2026-01-06T00:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:archived:1",
        threadId: ARCHIVED_THREAD,
        role: "user",
        text: "Archived question",
        createdAt: "2026-01-01T02:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:archived:2",
        threadId: ARCHIVED_THREAD,
        role: "assistant",
        text: "Archived answer",
        createdAt: "2026-01-02T02:00:00.000Z",
      });

      yield* insertThread({
        threadId: PINNED_THREAD,
        title: "Pinned conversation",
        createdAt: "2026-01-01T00:00:00.000Z",
        updatedAt: "2026-01-07T00:00:00.000Z",
        pinnedAt: "2026-01-03T00:00:00.000Z",
        pinOrderKey: "a0",
      });
      yield* insertMessage({
        messageId: "message:cutover:pinned:1",
        threadId: PINNED_THREAD,
        role: "user",
        text: "Pinned question",
        createdAt: "2026-01-01T03:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:pinned:2",
        threadId: PINNED_THREAD,
        role: "assistant",
        text: "Pinned answer",
        createdAt: "2026-01-02T03:00:00.000Z",
      });

      yield* insertThread({
        threadId: SNOOZED_THREAD,
        title: "Snoozed conversation",
        createdAt: "2026-01-01T00:00:00.000Z",
        updatedAt: "2026-01-08T00:00:00.000Z",
        snoozedAt: "2026-01-08T00:00:00.000Z",
        snoozedUntil: "2026-02-01T00:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:snoozed:1",
        threadId: SNOOZED_THREAD,
        role: "user",
        text: "Snoozed question",
        createdAt: "2026-01-01T04:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:snoozed:2",
        threadId: SNOOZED_THREAD,
        role: "assistant",
        text: "Snoozed answer part one",
        createdAt: "2026-01-02T04:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:snoozed:3",
        threadId: SNOOZED_THREAD,
        role: "assistant",
        text: "Snoozed answer part two",
        createdAt: "2026-01-03T04:00:00.000Z",
      });

      yield* insertThread({
        threadId: DELETED_THREAD,
        title: "Deleted conversation",
        createdAt: "2026-01-01T00:00:00.000Z",
        updatedAt: "2026-01-09T00:00:00.000Z",
        deletedAt: "2026-01-09T00:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:deleted:1",
        threadId: DELETED_THREAD,
        role: "user",
        text: "Deleted question",
        createdAt: "2026-01-01T05:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:deleted:2",
        threadId: DELETED_THREAD,
        role: "assistant",
        text: "Deleted answer",
        createdAt: "2026-01-02T05:00:00.000Z",
      });

      yield* insertThread({
        threadId: INTERRUPTED_THREAD,
        title: "Interrupted conversation",
        createdAt: "2026-01-01T00:00:00.000Z",
        updatedAt: "2026-01-09T01:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:interrupted:1",
        threadId: INTERRUPTED_THREAD,
        role: "user",
        text: "Interrupted question",
        createdAt: "2026-01-01T06:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:interrupted:2",
        threadId: INTERRUPTED_THREAD,
        role: "assistant",
        text: "Partial answer",
        createdAt: "2026-01-02T06:00:00.000Z",
        isStreaming: true,
      });

      yield* insertThread({
        threadId: LONG_THREAD,
        title: "Long conversation",
        createdAt: "2026-01-01T00:00:00.000Z",
        updatedAt: "2026-01-10T00:00:00.000Z",
        worktreePath: workspace,
      });
      const filler = "history detail ".repeat(800);
      yield* insertMessage({
        messageId: "message:cutover:long:1",
        threadId: LONG_THREAD,
        role: "user",
        text: `${EARLIEST_MARKER} ${filler}`,
        createdAt: "2026-01-01T07:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:long:2",
        threadId: LONG_THREAD,
        role: "assistant",
        text: `${filler} middle answer`,
        createdAt: "2026-01-02T07:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:long:3",
        threadId: LONG_THREAD,
        role: "user",
        text: `${filler} middle question`,
        createdAt: "2026-01-03T07:00:00.000Z",
      });
      yield* insertMessage({
        messageId: "message:cutover:long:4",
        threadId: LONG_THREAD,
        role: "assistant",
        text: `${filler} ${LATEST_MARKER}`,
        createdAt: "2026-01-04T07:00:00.000Z",
      });

      yield* sql`PRAGMA wal_checkpoint(TRUNCATE);`;

      // Deterministic pre-migration snapshot of the legacy recovery tables;
      // post-boot assertions compare preserved column contents, not counts.
      // Migrations may append columns, so the comparison is restricted to the
      // column set recorded here.
      const tableColumns = Effect.fn("seedV1Database.tableColumns")(function* (table: string) {
        const info = yield* sql<{
          readonly name: string;
        }>`SELECT name FROM pragma_table_info(${table})`;
        return info.map((row) => row.name);
      });
      return {
        projects: {
          columns: yield* tableColumns("projection_projects"),
          rows: yield* sql<Record<string, unknown>>`
            SELECT * FROM projection_projects ORDER BY project_id
          `,
        },
        threads: {
          columns: yield* tableColumns("projection_threads"),
          rows: yield* sql<Record<string, unknown>>`
            SELECT * FROM projection_threads ORDER BY thread_id
          `,
        },
        messages: {
          columns: yield* tableColumns("projection_thread_messages"),
          rows: yield* sql<Record<string, unknown>>`
            SELECT * FROM projection_thread_messages ORDER BY message_id
          `,
        },
      };
    }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: fixturePath }))),
  );

interface CapturedTurn {
  readonly threadId: ThreadId;
  readonly providerThreadId: ProviderThreadId;
  readonly text: string;
}

const unimplemented = (detail: string) =>
  Effect.fail(new ProviderAdapter.ProviderAdapterProtocolError({ driver, detail }));

const makeCodexAdapter = (capturedTurns: Ref.Ref<ReadonlyArray<CapturedTurn>>) =>
  ({
    instanceId,
    driver,
    getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2),
    planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }),
    openSession: (sessionInput) =>
      Effect.gen(function* () {
        const events = yield* PubSub.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
        const now = yield* DateTime.now;
        const providerSession: OrchestrationV2ProviderSession = {
          id: sessionInput.providerSessionId,
          driver,
          providerInstanceId: instanceId,
          status: "ready",
          cwd: sessionInput.runtimePolicy.cwd ?? process.cwd(),
          model: codexModelSelection.model,
          capabilities: CodexProviderCapabilitiesV2,
          createdAt: now,
          updatedAt: now,
          lastError: null,
        };
        return {
          instanceId,
          driver,
          providerSessionId: sessionInput.providerSessionId,
          providerSession,
          events: Stream.fromPubSub(events),
          ensureThread: (threadInput) =>
            Effect.gen(function* () {
              const createdAt = yield* DateTime.now;
              const nativeThreadId = `${driver}:${threadInput.threadId}`;
              return {
                id: ProviderThreadId.make(`provider-thread:${nativeThreadId}`),
                driver,
                providerInstanceId: instanceId,
                providerSessionId: sessionInput.providerSessionId,
                appThreadId: threadInput.threadId,
                ownerNodeId: null,
                nativeThreadRef: {
                  driver,
                  nativeId: nativeThreadId,
                  strength: "strong",
                },
                nativeConversationHeadRef: null,
                status: "idle",
                firstRunOrdinal: null,
                lastRunOrdinal: null,
                handoffIds: [],
                forkedFrom: null,
                createdAt,
                updatedAt: createdAt,
              } satisfies OrchestrationV2ProviderThread;
            }),
          resumeThread: ({ providerThread }) => Effect.succeed(providerThread),
          startTurn: (turnInput) =>
            Effect.gen(function* () {
              yield* Ref.update(capturedTurns, (turns) => [
                ...turns,
                {
                  threadId: turnInput.threadId,
                  providerThreadId: turnInput.providerThread.id,
                  text: turnInput.message.text,
                },
              ]);
              const eventTime = yield* DateTime.now;
              const providerTurnId = ProviderTurnId.make(
                `provider-turn:${turnInput.threadId}:${turnInput.runOrdinal}`,
              );
              yield* PubSub.publishAll(events, [
                {
                  type: "provider_turn.updated",
                  driver,
                  providerTurn: {
                    id: providerTurnId,
                    providerThreadId: turnInput.providerThread.id,
                    nodeId: turnInput.rootNodeId,
                    runAttemptId: turnInput.attemptId,
                    nativeTurnRef: {
                      driver,
                      nativeId: `native-turn:${turnInput.threadId}:${turnInput.runOrdinal}`,
                      strength: "strong",
                    },
                    ordinal: turnInput.runOrdinal,
                    status: "completed",
                    startedAt: eventTime,
                    completedAt: eventTime,
                  },
                },
                {
                  type: "turn_item.updated",
                  driver,
                  turnItem: {
                    id: TurnItemId.make(
                      `turn-item:${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: "completed",
                    title: null,
                    startedAt: eventTime,
                    completedAt: eventTime,
                    updatedAt: eventTime,
                    type: "assistant_message",
                    messageId: MessageId.make(
                      `message:${turnInput.threadId}:${turnInput.runOrdinal}:assistant`,
                    ),
                    text: CONTINUATION_RESPONSE,
                    streaming: false,
                  },
                },
                {
                  type: "turn.terminal",
                  driver,
                  providerThreadId: turnInput.providerThread.id,
                  providerTurnId,
                  runOrdinal: turnInput.runOrdinal,
                  status: "completed",
                  failure: null,
                  threadDisposition: "reusable",
                },
              ] satisfies ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>);
            }),
          steerTurn: () => Effect.void,
          interruptTurn: () => Effect.void,
          respondToRuntimeRequest: () => Effect.void,
          readThreadSnapshot: () => unimplemented("readThreadSnapshot unused in cutover test"),
          rollbackThread: () => unimplemented("rollbackThread unused in cutover test"),
          forkThread: () => unimplemented("forkThread unused in cutover test"),
        };
      }),
  }) satisfies ProviderAdapter.ProviderAdapterV2["Service"];

// Await the dispatched run's terminal event on the persisted domain-event stream
// instead of polling the projection, then drain remaining outbox effects.
const dispatchMessageAwaitingRun = Effect.fn("LegacyV1Cutover.dispatchMessageAwaitingRun")(
  function* (input: {
    readonly threadId: ThreadId;
    readonly messageId: string;
    readonly commandId: string;
    readonly text: string;
  }) {
    const orchestrator = yield* Orchestrator.OrchestratorV2;
    const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
    const eventSink = yield* EventSink.EventSinkV2;
    const messageId = MessageId.make(input.messageId);
    // Capture the cursor before dispatch so even a late subscriber replays
    // the terminal event instead of starting its live tail after completion.
    const afterSequence = yield* eventSink.latestSequence();
    const terminal = eventSink.stream({ afterSequence }).pipe(
      Stream.map((stored) => stored.event),
      Stream.filter((event) => event.type === "run.updated"),
      Stream.filter(
        (event) =>
          ["completed", "failed", "interrupted", "cancelled", "rolled_back"].includes(
            event.payload.status,
          ) &&
          event.payload.threadId === input.threadId &&
          event.payload.userMessageId === messageId,
      ),
      Stream.take(1),
      Stream.runForEach((event) =>
        Effect.sync(() =>
          assert.equal(event.payload.status, "completed", `Run for ${messageId} did not complete`),
        ),
      ),
    );
    yield* orchestrator.dispatch({
      type: "message.dispatch",
      createdBy: "user",
      creationSource: "web",
      commandId: CommandId.make(input.commandId),
      threadId: input.threadId,
      messageId,
      text: input.text,
      attachments: [],
      dispatchMode: { type: "start_immediately" },
    });
    yield* worker.drain();
    yield* terminal.pipe(
      Effect.timeoutOrElse({
        duration: "30 seconds",
        orElse: () => Effect.die(`No terminal run.updated for ${messageId}`),
      }),
    );
    yield* worker.drain();
    return yield* orchestrator.getThreadProjection(input.threadId);
  },
);

const layerBoot = (input: {
  readonly name: string;
  readonly dbPath: string;
  readonly workspace: string;
  readonly capturedTurns: Ref.Ref<ReadonlyArray<CapturedTurn>>;
}) => {
  const layerDatabase = SqlitePersistence.layerFromPath(input.dbPath).pipe(
    Layer.provide(NodeServices.layer),
  );
  const layerEventStoreProvided = EventStore.layer.pipe(Layer.provideMerge(layerDatabase));
  const layerProjectionStoreProvided = ProjectionStore.layer.pipe(
    Layer.provideMerge(layerDatabase),
  );
  const layerStoresProvided = Layer.mergeAll(
    layerDatabase,
    layerEventStoreProvided,
    layerProjectionStoreProvided,
  );
  const layerEventSinkProvided = EventSink.layer.pipe(Layer.provide(layerStoresProvided));
  const layerImporterProvided = LegacyV1ThreadImporter.layer.pipe(
    Layer.provide(Layer.mergeAll(layerStoresProvided, layerEventSinkProvided)),
  );
  const layerMaintenanceProvided = ProjectionReplay.layer.pipe(Layer.provide(layerStoresProvided));
  const layerOrchestratorProvided = ProviderReplayHarness.layerWithRegistry(
    {
      name: input.name,
      runtimePolicyOverride: {
        cwd: input.workspace,
        approvalPolicy: "never",
        sandboxPolicy: {
          type: "readOnly",
          access: { type: "fullAccess" },
          networkAccess: false,
        },
      },
    },
    ProviderAdapterRegistry.layerSingle(makeCodexAdapter(input.capturedTurns)),
    { databaseLayer: layerDatabase },
  );
  return Layer.mergeAll(
    layerStoresProvided,
    layerEventSinkProvided,
    layerImporterProvided,
    layerMaintenanceProvided,
    layerOrchestratorProvided,
  );
};

interface CapturedLog {
  readonly message: unknown;
  readonly annotations: Readonly<Record<string, unknown>>;
}

const makeCapturingLogger = (logs: CapturedLog[]) =>
  Logger.make(({ fiber, message }) => {
    logs.push({
      message,
      annotations: fiber.getRef(References.CurrentLogAnnotations),
    });
  });

const sha256File = Effect.fn("LegacyV1Cutover.sha256File")(function* (filePath: string) {
  const fs = yield* FileSystem.FileSystem;
  const digest = yield* Digest.Digest;
  return yield* digest.digestHex(Digest.sha256, yield* fs.readFile(filePath));
}, Effect.provide(NodeCrypto.layerDigest));

interface LegacyTableSnapshot {
  readonly columns: ReadonlyArray<string>;
  readonly rows: ReadonlyArray<Record<string, unknown>>;
}

// Migrations legitimately append columns; preservation means the columns the
// v1 schema already had keep their exact values.
const restrictToSnapshotColumns = (
  rows: ReadonlyArray<Record<string, unknown>>,
  snapshot: LegacyTableSnapshot,
) => rows.map((row) => Object.fromEntries(snapshot.columns.map((column) => [column, row[column]])));

const messageOrdinals = (projection: OrchestrationV2ThreadProjection) =>
  projection.turnItems
    .filter(
      (
        item,
      ): item is Extract<typeof item, { readonly type: "user_message" | "assistant_message" }> =>
        item.type === "user_message" || item.type === "assistant_message",
    )
    .map((item) => [String(item.messageId), item.ordinal, item.status] as const);

describe("orchestration v2 legacy v1 cutover", () => {
  it.live(
    "migrates an untouched v1 database copy through shell import, lazy transcripts, continuation and restart",
    () =>
      Effect.scoped(
        Effect.gen(function* () {
          const fs = yield* FileSystem.FileSystem;
          const path = yield* Path.Path;
          const workspace = yield* checkpointWorkspace("legacy-v1-cutover");
          const stateDir = yield* fs.makeTempDirectory({ prefix: "t3-v1-cutover-state-" });
          const fixturePath = path.join(stateDir, "v1-source.sqlite");
          const copyPath = path.join(stateDir, "userdata", "state.sqlite");
          yield* fs.makeDirectory(path.join(stateDir, "userdata"), { recursive: true });

          const v1Snapshot = yield* seedV1Database(fixturePath, workspace);
          const fixtureHashBefore = yield* sha256File(fixturePath);
          yield* fs.copyFile(fixturePath, copyPath);

          const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
          const longThreadId = ThreadId.make(LONG_THREAD);

          const boot1Logs: CapturedLog[] = [];
          const firstBoot = yield* Effect.scoped(
            Effect.gen(function* () {
              const sql = yield* SqlClient.SqlClient;
              const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
              const maintenance = yield* ProjectionReplay.ProjectionReplay;
              const projections = yield* ProjectionStore.ProjectionStoreV2;

              assert.equal(yield* importer.pendingThreadCount, ALL_THREADS.length);
              const shellImport = yield* importer.reconcileShells;
              assert.deepStrictEqual(shellImport, {
                importedThreadCount: ALL_THREADS.length,
                importedMessageCount: 14,
              });

              assert.deepStrictEqual(yield* importer.reconcileShells, {
                importedThreadCount: 0,
                importedMessageCount: 0,
              });
              assert.isTrue((yield* maintenance.rebuild).valid);

              const shellSnapshot = yield* projections.getShellSnapshot();
              const visibleShellIds = [
                ...shellSnapshot.threads.map((thread) => thread.id),
                ...shellSnapshot.archivedThreads.map((thread) => thread.id),
              ];
              assert.notInclude(visibleShellIds, ThreadId.make(DELETED_THREAD));
              assert.include(
                shellSnapshot.archivedThreads.map((thread) => thread.id),
                ThreadId.make(ARCHIVED_THREAD),
              );
              const pinnedShell = shellSnapshot.threads.find(
                (thread) => thread.id === PINNED_THREAD,
              );
              assert.equal(pinnedShell?.pinOrderKey, "a0");

              const activeShell = yield* projections.getThreadProjection(
                ThreadId.make(ACTIVE_THREAD),
              );
              assert.equal(activeShell.thread.historyOrigin, "v1_import");
              assert.isNull(activeShell.thread.activeProviderThreadId);
              assert.deepStrictEqual(
                activeShell.messages.map((message) => message.id),
                ["message:cutover:active:3", "message:cutover:active:4"],
              );
              assert.isEmpty(activeShell.providerThreads);

              assert.isEmpty(
                activeShell.turnItems.filter(
                  (item) => item.type !== "user_message" && item.type !== "assistant_message",
                ),
              );

              const deletedShell = yield* projections.getThreadProjection(
                ThreadId.make(DELETED_THREAD),
              );
              assert.isNotNull(deletedShell.thread.deletedAt);
              assert.equal(
                deletedShell.thread.deletedAt?.toString(),
                DateTime.makeUnsafe("2026-01-09T00:00:00.000Z").toString(),
              );

              for (const threadId of [
                ACTIVE_THREAD,
                ARCHIVED_THREAD,
                PINNED_THREAD,
                DELETED_THREAD,
                INTERRUPTED_THREAD,
                LONG_THREAD,
              ]) {
                yield* importer.ensureTranscript(ThreadId.make(threadId));
              }
              assert.deepStrictEqual(
                yield* importer.ensureTranscript(ThreadId.make(ACTIVE_THREAD)),
                { importedThreadCount: 0, importedMessageCount: 0 },
              );

              const active = yield* projections.getThreadProjection(ThreadId.make(ACTIVE_THREAD));
              assert.deepStrictEqual(
                active.messages.map((message) => message.id),
                [
                  "message:cutover:active:1",
                  "message:cutover:active:2",
                  "message:cutover:active:3",
                  "message:cutover:active:4",
                ],
              );
              assert.deepStrictEqual(messageOrdinals(active), [
                ["message:cutover:active:1", 1, "completed"],
                ["message:cutover:active:2", 2, "completed"],
                ["message:cutover:active:3", 3, "completed"],
                ["message:cutover:active:4", 4, "completed"],
              ]);
              assert.deepStrictEqual(
                active.messages.find((message) => message.id === "message:cutover:active:2")
                  ?.attachments,
                [
                  {
                    type: "image",
                    id: "att-1",
                    name: "shot.png",
                    mimeType: "image/png",
                    sizeBytes: 1234,
                  },
                ],
              );
              assert.equal(active.thread.linkedPullRequest?.number, 9100);
              assert.deepStrictEqual(
                active.thread.pullRequests?.map((pullRequest) => pullRequest.number),
                [9100],
              );

              const archived = yield* projections.getThreadProjection(
                ThreadId.make(ARCHIVED_THREAD),
              );
              assert.isNotNull(archived.thread.archivedAt);
              const pinned = yield* projections.getThreadProjection(ThreadId.make(PINNED_THREAD));
              assert.isNotNull(pinned.thread.pinnedAt);
              assert.equal(pinned.thread.pinOrderKey, "a0");
              const snoozed = yield* projections.getThreadProjection(ThreadId.make(SNOOZED_THREAD));
              assert.equal(
                snoozed.thread.snoozedUntil?.toString(),
                DateTime.makeUnsafe("2026-02-01T00:00:00.000Z").toString(),
              );

              assert.deepStrictEqual(
                snoozed.messages.map((message) => message.id),
                ["message:cutover:snoozed:1", "message:cutover:snoozed:3"],
              );
              const snoozedImportRow = yield* sql<{
                readonly transcript_imported_at: string | null;
              }>`
              SELECT transcript_imported_at
              FROM orchestration_v2_legacy_imports
              WHERE thread_id = ${SNOOZED_THREAD}
            `;
              assert.isNull(snoozedImportRow[0]?.transcript_imported_at);

              // A failed hydration leaves the legacy rows and pending marker
              // available for retry in a fresh runtime after restart.
              yield* sql`
                CREATE TRIGGER fail_snoozed_hydration
                BEFORE INSERT ON orchestration_events
                WHEN NEW.event_id = 'migration:v1:message:message:cutover:snoozed:2'
                BEGIN
                  SELECT RAISE(ABORT, 'injected cutover hydration failure');
                END
              `;
              const hydrationError = yield* importer
                .ensureTranscript(ThreadId.make(SNOOZED_THREAD))
                .pipe(Effect.flip);
              assert.equal(hydrationError._tag, "LegacyV1ThreadImportError");
              assert.equal(yield* importer.pendingThreadCount, 1);

              const interrupted = yield* projections.getThreadProjection(
                ThreadId.make(INTERRUPTED_THREAD),
              );
              assert.deepStrictEqual(messageOrdinals(interrupted), [
                ["message:cutover:interrupted:1", 1, "completed"],
                ["message:cutover:interrupted:2", 2, "interrupted"],
              ]);

              const continued = yield* dispatchMessageAwaitingRun({
                threadId: longThreadId,
                messageId: "message:cutover:long:continuation",
                commandId: "command:cutover:continue",
                text: CONTINUATION_PROMPT,
              });

              const runs = continued.runs.filter((run) => run.ordinal >= 1);
              assert.equal(runs.at(-1)?.status, "completed");
              assert.equal(continued.providerThreads.length, 1);
              assert.isNotNull(continued.providerThreads[0]?.nativeThreadRef);
              assert.equal(continued.contextHandoffs.length, 1);
              const handoff = continued.contextHandoffs[0]!;
              assert.equal(handoff.strategy, "manual_context");
              assert.isAtMost(handoff.summaryText.length, 32_000);
              assert.include(handoff.summaryText, LATEST_MARKER);
              assert.notInclude(handoff.summaryText, EARLIEST_MARKER);
              const turns = yield* Ref.get(capturedTurns);
              assert.equal(turns.length, 1);
              assert.include(turns[0]!.text, "Context handoff (manual_context):");
              assert.include(turns[0]!.text, `User message:\n${CONTINUATION_PROMPT}`);

              const continuedAgain = yield* dispatchMessageAwaitingRun({
                threadId: longThreadId,
                messageId: "message:cutover:long:continuation:2",
                commandId: "command:cutover:continue:again",
                text: "One more.",
              });
              assert.equal(continuedAgain.contextHandoffs.length, 1);
              assert.equal((yield* Ref.get(capturedTurns)).length, 2);

              const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
              yield* worker.drain();

              const migrationEventCount = yield* sql<{ readonly count: number }>`
              SELECT COUNT(*) AS count
              FROM orchestration_events
              WHERE application_event_version = 2
                AND event_id LIKE 'migration:v1:%'
            `;
              const importRows = yield* sql<{
                readonly thread_id: string;
                readonly transcript_imported_at: string | null;
              }>`
              SELECT thread_id, transcript_imported_at
              FROM orchestration_v2_legacy_imports
              ORDER BY thread_id
            `;
              const legacyProjects = yield* sql<Record<string, unknown>>`
              SELECT * FROM projection_projects ORDER BY project_id
            `;
              const legacyThreads = yield* sql<Record<string, unknown>>`
              SELECT * FROM projection_threads ORDER BY thread_id
            `;
              const legacyMessages = yield* sql<Record<string, unknown>>`
              SELECT * FROM projection_thread_messages ORDER BY message_id
            `;
              const recordedMigration41 = yield* sql<{ readonly name: string }>`
              SELECT name FROM effect_sql_migrations WHERE migration_id = 41
            `;
              const authSessionColumns = yield* sql<{ readonly name: string }>`
              PRAGMA table_info(auth_sessions)
            `;
              return {
                migrationEventCount: migrationEventCount[0]?.count ?? 0,
                importRows,
                legacyProjects,
                legacyThreads,
                legacyMessages,
                longProjection: continuedAgain,
                migration41Name: recordedMigration41[0]?.name ?? null,
                authSessionColumnNames: authSessionColumns.map((column) => column.name),
              };
            }).pipe(
              Effect.provide(
                layerBoot({
                  name: "legacy-v1-cutover-first",
                  dbPath: copyPath,
                  workspace,
                  capturedTurns,
                }),
              ),
              Effect.provideService(
                Logger.CurrentLoggers,
                new Set([makeCapturingLogger(boot1Logs)]),
              ),
            ),
          );

          const divergenceLog = boot1Logs.find((log) =>
            String(log.message).includes("migration history diverges"),
          );
          assert.deepStrictEqual(divergenceLog?.annotations.divergent, [
            "41:ThreadSummaryTimeline (this build: AuthSessionClientConnection)",
          ]);
          assert.equal(firstBoot.migration41Name, "ThreadSummaryTimeline");

          assert.notInclude(firstBoot.authSessionColumnNames, "client_surface");
          assert.notInclude(firstBoot.authSessionColumnNames, "client_app_version");

          // Full row contents of the legacy recovery tables survive migration
          // and continuation unchanged, not just their counts.
          assert.deepStrictEqual(
            restrictToSnapshotColumns(firstBoot.legacyProjects, v1Snapshot.projects),
            v1Snapshot.projects.rows,
          );
          assert.deepStrictEqual(
            restrictToSnapshotColumns(firstBoot.legacyThreads, v1Snapshot.threads),
            v1Snapshot.threads.rows,
          );
          assert.deepStrictEqual(
            restrictToSnapshotColumns(firstBoot.legacyMessages, v1Snapshot.messages),
            v1Snapshot.messages.rows,
          );

          assert.equal(firstBoot.importRows.length, ALL_THREADS.length);
          const unhydratedRows = firstBoot.importRows.filter(
            (row) => row.transcript_imported_at === null,
          );
          assert.deepStrictEqual(
            unhydratedRows.map((row) => row.thread_id),
            [SNOOZED_THREAD],
          );

          const boot2Logs: CapturedLog[] = [];
          yield* Effect.scoped(
            Effect.gen(function* () {
              const sql = yield* SqlClient.SqlClient;
              const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
              const projections = yield* ProjectionStore.ProjectionStoreV2;

              assert.equal(yield* importer.pendingThreadCount, 1);
              assert.deepStrictEqual(yield* importer.reconcileShells, {
                importedThreadCount: 0,
                importedMessageCount: 0,
              });
              yield* sql`DROP TRIGGER fail_snoozed_hydration`;
              assert.deepStrictEqual(
                yield* importer.ensureTranscript(ThreadId.make(SNOOZED_THREAD)),
                { importedThreadCount: 1, importedMessageCount: 1 },
              );
              const snoozed = yield* projections.getThreadProjection(ThreadId.make(SNOOZED_THREAD));
              assert.deepStrictEqual(
                snoozed.messages.map((message) => message.id),
                [
                  "message:cutover:snoozed:1",
                  "message:cutover:snoozed:2",
                  "message:cutover:snoozed:3",
                ],
              );
              assert.equal(yield* importer.pendingThreadCount, 0);
              for (const threadId of ALL_THREADS) {
                assert.deepStrictEqual(yield* importer.ensureTranscript(ThreadId.make(threadId)), {
                  importedThreadCount: 0,
                  importedMessageCount: 0,
                });
              }
              const migrationEventCount = yield* sql<{ readonly count: number }>`
              SELECT COUNT(*) AS count
              FROM orchestration_events
              WHERE application_event_version = 2
                AND event_id LIKE 'migration:v1:%'
            `;

              assert.equal(migrationEventCount[0]?.count, firstBoot.migrationEventCount + 2);

              const restarted = yield* projections.getThreadProjection(longThreadId);
              assert.equal(restarted.thread.historyOrigin, "v1_import");
              assert.equal(restarted.contextHandoffs.length, 1);
              assert.isNotNull(restarted.thread.activeProviderThreadId);
              assert.deepStrictEqual(
                restarted.runs.map((run) => run.status),
                firstBoot.longProjection.runs.map((run) => run.status),
              );

              assert.deepStrictEqual(
                restrictToSnapshotColumns(
                  yield* sql<Record<string, unknown>>`
                    SELECT * FROM projection_projects ORDER BY project_id
                  `,
                  v1Snapshot.projects,
                ),
                v1Snapshot.projects.rows,
              );
              assert.deepStrictEqual(
                restrictToSnapshotColumns(
                  yield* sql<Record<string, unknown>>`
                    SELECT * FROM projection_threads ORDER BY thread_id
                  `,
                  v1Snapshot.threads,
                ),
                v1Snapshot.threads.rows,
              );
              assert.deepStrictEqual(
                restrictToSnapshotColumns(
                  yield* sql<Record<string, unknown>>`
                    SELECT * FROM projection_thread_messages ORDER BY message_id
                  `,
                  v1Snapshot.messages,
                ),
                v1Snapshot.messages.rows,
              );
              const quickCheck = yield* sql<{ readonly quick_check: string }>`
              PRAGMA quick_check
            `;
              assert.deepStrictEqual(
                quickCheck.map((row) => row.quick_check),
                ["ok"],
              );
            }).pipe(
              Effect.provide(
                layerBoot({
                  name: "legacy-v1-cutover-restart",
                  dbPath: copyPath,
                  workspace,
                  capturedTurns,
                }),
              ),
              Effect.provideService(
                Logger.CurrentLoggers,
                new Set([makeCapturingLogger(boot2Logs)]),
              ),
            ),
          );

          assert.isTrue(
            boot2Logs.some((log) => String(log.message).includes("migration history diverges")),
          );

          // The seeded source file is the untouched recovery copy: migration
          // only ever ran against the file copied out of it.
          assert.equal(yield* sha256File(fixturePath), fixtureHashBefore);
        }).pipe(Effect.provide(NodeServices.layer)),
      ),
  );
});