sailfork / t3code-github / apps/ server/ src/ orchestration-v2/ ThreadForkService.test.ts

import { assert, it } from "@effect/vitest";
import {
  CommandId,
  ContextTransferId,
  EnvironmentId,
  MessageId,
  type ModelSelection,
  type OrchestrationV2AppThread,
  type OrchestrationV2Run,
  type OrchestrationV2ThreadProjection,
  ProjectId,
  ProviderInstanceId,
  ProviderThreadId,
  RunId,
  ThreadId,
  type ThreadPullRequestLink,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";

import * as ThreadForkService from "./ThreadForkService.ts";

const sourceThreadId = ThreadId.make("thread:fork-snoozed-source");
const targetThreadId = ThreadId.make("thread:fork-awake-target");
const sourceRunId = RunId.make("run:fork-snoozed-source");
const providerInstanceId = ProviderInstanceId.make("codex");
const modelSelection = {
  instanceId: providerInstanceId,
  model: "gpt-5.4",
} satisfies ModelSelection;
const sourceCreatedAt = DateTime.makeUnsafe("2026-07-24T09:00:00.000Z");
const snoozedAt = DateTime.makeUnsafe("2026-07-24T09:05:00.000Z");
const snoozedUntil = DateTime.makeUnsafe("2026-07-25T09:00:00.000Z");
const forkCreatedAt = DateTime.makeUnsafe("2026-07-24T09:10:00.000Z");

function makeSourceThread(): OrchestrationV2AppThread {
  return {
    createdBy: "user",
    creationSource: "web",
    id: sourceThreadId,
    projectId: ProjectId.make("project:fork-snooze"),
    title: "Snoozed source",
    providerInstanceId,
    modelSelection,
    runtimeMode: "full-access",
    interactionMode: "plan",
    branch: "feature/source",
    worktreePath: "/tmp/source-worktree",
    activeProviderThreadId: ProviderThreadId.make("provider-thread:fork-snoozed-source"),
    lineage: {
      parentThreadId: null,
      relationshipToParent: null,
      rootThreadId: sourceThreadId,
    },
    forkedFrom: null,
    createdAt: sourceCreatedAt,
    updatedAt: snoozedAt,
    archivedAt: null,
    settledOverride: null,
    settledAt: null,
    lastVisitedAt: null,
    snoozedUntil,
    snoozedAt,
    deletedAt: null,
  };
}

function makeSourceRun(status: OrchestrationV2Run["status"]): OrchestrationV2Run {
  return {
    id: sourceRunId,
    threadId: sourceThreadId,
    ordinal: 1,
    providerInstanceId,
    modelSelection,
    providerThreadId: ProviderThreadId.make("provider-thread:fork-snoozed-source"),
    userMessageId: MessageId.make("message:fork-snoozed-source"),
    rootNodeId: null,
    activeAttemptId: null,
    status,
    queuePosition: null,
    requestedAt: sourceCreatedAt,
    startedAt: sourceCreatedAt,
    completedAt: snoozedAt,
    checkpointId: null,
    contextHandoffId: null,
  };
}

function makeSourceProjection(sourceRun: OrchestrationV2Run): OrchestrationV2ThreadProjection {
  return {
    thread: makeSourceThread(),
    runs: [sourceRun],
    attempts: [],
    nodes: [],
    subagents: [],
    providerSessions: [],
    providerThreads: [],
    providerTurns: [],
    runtimeRequests: [],
    messages: [],
    plans: [],
    turnItems: [],
    checkpointScopes: [],
    checkpoints: [],
    contextHandoffs: [],
    contextTransfers: [],
    visibleTurnItems: [],
    updatedAt: snoozedAt,
  };
}

const planFork = (
  sourceRun: OrchestrationV2Run,
  sourceProjection = makeSourceProjection(sourceRun),
  options: { readonly sideChat?: boolean } = {},
) =>
  Effect.gen(function* () {
    const service = yield* ThreadForkService.ThreadForkServiceV2;
    return yield* service.plan({
      sourceProjection,
      sourceRun,
      sourceProviderThread: undefined,
      canonicalSourcePoint: {
        threadId: sourceThreadId,
        runId: sourceRunId,
      },
      transferId: ContextTransferId.make("context-transfer:fork-snoozed-source"),
      targetThreadId,
      title: "Awake fork",
      ...options,
      createdBy: "user",
      creationSource: "mobile",
      createdAt: forkCreatedAt,
    });
  }).pipe(Effect.provide(ThreadForkService.layer));

it.effect("resets a fork's pin, snooze, settle and delegation state but keeps its origin", () =>
  Effect.gen(function* () {
    const linkOrigin = { sessionId: "mcp-session:fork-source", label: "Linked laptop" };
    const sourceThread: OrchestrationV2AppThread = {
      ...makeSourceThread(),
      lastSnoozeWakeAt: snoozedAt,
      pinnedAt: snoozedAt,
      pinOrderKey: "a0",
      activeOrderKey: "a1",
      autoSettleDisabledAt: snoozedAt,
      delegatedFrom: {
        environmentId: EnvironmentId.make("linked-environment"),
        threadId: ThreadId.make("linked-parent"),
        title: "Linked parent",
      },
      historyOrigin: "v1_import",
      linkOrigin,
      tags: ["private"],
    };
    const sourceRun = makeSourceRun("completed");
    const result = yield* planFork(sourceRun, {
      ...makeSourceProjection(sourceRun),
      thread: sourceThread,
    });

    assert.isNull(result.targetThread.snoozedUntil);
    assert.isNull(result.targetThread.snoozedAt);
    assert.isNull(result.targetThread.lastSnoozeWakeAt);
    assert.isNull(result.targetThread.pinnedAt);
    assert.isNull(result.targetThread.pinOrderKey);
    assert.isNull(result.targetThread.activeOrderKey);
    assert.isNull(result.targetThread.autoSettleDisabledAt);
    assert.isUndefined(result.targetThread.delegatedFrom);
    assert.equal(result.targetThread.historyOrigin, "v1_import");
    assert.deepEqual(result.targetThread.linkOrigin, linkOrigin);
    assert.deepEqual(result.targetThread.tags, ["private"]);
    assert.equal(result.targetThread.projectId, sourceThread.projectId);
    assert.equal(result.targetThread.providerInstanceId, sourceThread.providerInstanceId);
    assert.deepEqual(result.targetThread.modelSelection, sourceThread.modelSelection);
    assert.equal(result.targetThread.runtimeMode, sourceThread.runtimeMode);
    assert.equal(result.targetThread.interactionMode, sourceThread.interactionMode);
    assert.equal(result.targetThread.branch, sourceThread.branch);
    assert.equal(result.targetThread.worktreePath, sourceThread.worktreePath);
    assert.isNull(result.targetThread.activeProviderThreadId);
    assert.deepEqual(result.targetThread.lineage, {
      parentThreadId: sourceThreadId,
      relationshipToParent: "fork",
      rootThreadId: sourceThreadId,
    });
    assert.deepEqual(result.targetThread.forkedFrom, {
      type: "run",
      threadId: sourceThreadId,
      runId: sourceRunId,
    });
  }),
);

it.effect("starts a fork without the source thread's pending title regeneration", () =>
  Effect.gen(function* () {
    const sourceRun = makeSourceRun("completed");
    const titleRegeneration = {
      requestId: CommandId.make("command:fork-source-title"),
      startedAt: snoozedAt,
    };
    const sourceProjection = {
      ...makeSourceProjection(sourceRun),
      thread: { ...makeSourceThread(), titleRegeneration },
    };
    const result = yield* planFork(sourceRun, sourceProjection);

    assert.isNull(result.targetThread.titleRegeneration);
    assert.deepEqual(sourceProjection.thread.titleRegeneration, titleRegeneration);
  }),
);

it.effect("preserves pull request context without inheriting any watches", () =>
  Effect.gen(function* () {
    const sourceRun = makeSourceRun("completed");
    const watch = {
      startedAt: DateTime.formatIso(sourceCreatedAt),
      headSha: "source-head",
      failedChecks: ["lint"],
      passed: false,
      passedChecks: [],
      remarksThrough: DateTime.formatIso(snoozedAt),
      remarkIds: ["review-1"],
      conflicting: true,
      wakes: 2,
    };
    const links = (["agent", "stack", "manual", "stack-dismissed"] as const).map(
      (source, index) =>
        ({
          host: "github.com",
          repository: "example/repo",
          number: index + 1,
          url: `https://github.com/example/repo/pull/${index + 1}`,
          source,
          linkedAt: DateTime.formatIso(sourceCreatedAt),
          snapshot: {
            state: "open",
            title: `Pull request ${index + 1}`,
            headBranch: `feature-${index + 1}`,
            baseBranch: "main",
            isDraft: false,
            updatedAt: null,
            syncedAt: DateTime.formatIso(snoozedAt),
          },
          stack: {
            kind: "native",
            id: "stack-1",
            number: 1,
            url: "https://github.com/example/repo/stack/1",
            base: "main",
            layers: [{ number: 1, headBranch: "feature-1", state: "open" }],
          },
        }) satisfies ThreadPullRequestLink,
    );
    const watchedLinks: ReadonlyArray<ThreadPullRequestLink> = links.map((link, index) =>
      index < 2 ? { ...link, watch } : link,
    );
    const legacyLink = {
      projectId: makeSourceThread().projectId,
      repository: "example/repo",
      number: 1,
      url: links[0]!.url,
    };
    const sourceProjection = {
      ...makeSourceProjection(sourceRun),
      thread: {
        ...makeSourceThread(),
        pullRequests: watchedLinks,
        linkedPullRequest: legacyLink,
        branchPullRequest: legacyLink,
      },
    };
    const result = yield* planFork(sourceRun, sourceProjection);

    assert.deepEqual(result.targetThread.pullRequests, links);
    assert.deepEqual(result.targetThread.linkedPullRequest, legacyLink);
    assert.deepEqual(result.targetThread.branchPullRequest, legacyLink);
    assert.deepEqual(sourceProjection.thread.pullRequests, watchedLinks);
    assert.deepEqual(sourceProjection.thread.pullRequests[0]?.watch, watch);
    assert.deepEqual(sourceProjection.thread.pullRequests[1]?.watch, watch);
  }),
);

it.effect("rejects in-progress and rolled-back fork sources", () =>
  Effect.gen(function* () {
    for (const status of ["running", "rolled_back"] as const) {
      const sourceRun = makeSourceRun(status);
      const error = yield* planFork(sourceRun).pipe(Effect.flip);
      assert.equal(error._tag, "ThreadForkPlanError");
      assert.equal(error.sourceThreadId, sourceThreadId);
      assert.equal(error.targetThreadId, targetThreadId);
      assert.equal(error.cause, ThreadForkService.forkableSourceRunStatusError(sourceRun));
    }
  }),
);

it.effect("marks only requested forks as side chats and inherits parent permissions", () =>
  Effect.gen(function* () {
    const sourceRun = makeSourceRun("completed");
    const sideChat = yield* planFork(sourceRun, makeSourceProjection(sourceRun), {
      sideChat: true,
    });
    assert.isTrue(sideChat.targetThread.lineage.sideChat);
    assert.equal(sideChat.targetThread.runtimeMode, "full-access");
    for (const runtimeMode of [
      "approval-required",
      "auto-accept-edits",
      "auto",
      "full-access",
    ] as const) {
      const source = makeSourceProjection(sourceRun);
      const inherited = yield* planFork(
        sourceRun,
        {
          ...source,
          thread: { ...source.thread, runtimeMode },
        },
        { sideChat: true },
      );
      assert.equal(inherited.targetThread.runtimeMode, runtimeMode);
    }

    const sideChatSource = makeSourceProjection(sourceRun);
    const sideChatProjection = {
      ...sideChatSource,
      thread: {
        ...sideChatSource.thread,
        lineage: { ...sideChatSource.thread.lineage, sideChat: true },
      },
    };
    const forkOfSideChat = yield* planFork(sourceRun, sideChatProjection);
    assert.isUndefined(forkOfSideChat.targetThread.lineage.sideChat);
    assert.equal(forkOfSideChat.targetThread.runtimeMode, "full-access");

    const nested = yield* planFork(sourceRun, sideChatProjection, { sideChat: true }).pipe(
      Effect.flip,
    );
    assert.equal(nested.cause, "A side chat cannot start another side chat.");

    const subagentSource = makeSourceProjection(sourceRun);
    const fromSubagent = yield* planFork(
      sourceRun,
      {
        ...subagentSource,
        thread: {
          ...subagentSource.thread,
          lineage: { ...subagentSource.thread.lineage, relationshipToParent: "subagent" },
        },
      },
      { sideChat: true },
    ).pipe(Effect.flip);
    assert.equal(fromSubagent.cause, "A subagent cannot start a side chat.");

    const delegatedSource = makeSourceProjection(sourceRun);
    const fromDelegated = yield* planFork(
      sourceRun,
      {
        ...delegatedSource,
        thread: {
          ...delegatedSource.thread,
          delegatedFrom: {
            environmentId: EnvironmentId.make("linked-environment"),
            threadId: ThreadId.make("linked-parent"),
            title: "Linked parent",
          },
        },
      },
      { sideChat: true },
    ).pipe(Effect.flip);
    assert.equal(fromDelegated.cause, "A subagent cannot start a side chat.");
  }),
);