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

import { assert, it } from "@effect/vitest";
import { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts";

import { decideProviderSessionTransition } from "./ProviderSessionTransitionPolicy.ts";

const driver = ProviderDriverKind.make("codex");
const instanceId = ProviderInstanceId.make("codex");
const base = {
  driver,
  continuationIdentity: { driverKind: driver, continuationKey: "codex:account:one" },
  modelSelection: { instanceId, model: "gpt-5.1-codex" },
  runtimeMode: "full-access" as const,
  interactionMode: "default" as const,
  workspace: "/repo",
};

it("reuses compatible sessions and treats interaction mode as turn-scoped", () => {
  assert.deepEqual(
    decideProviderSessionTransition({
      current: base,
      target: { ...base, interactionMode: "plan", available: true },
    }),
    { type: "reuse" },
  );
});

it("uses the adapter's selection transition classification", () => {
  assert.deepEqual(
    decideProviderSessionTransition({
      current: base,
      target: {
        ...base,
        modelSelection: { ...base.modelSelection, model: "gpt-5.2-codex" },
        available: true,
      },
      selectionTransition: { type: "apply_on_next_turn" },
    }),
    { type: "switch_model_in_session" },
  );
  assert.deepEqual(
    decideProviderSessionTransition({
      current: {
        ...base,
      },
      target: {
        ...base,
        modelSelection: { ...base.modelSelection, model: "gpt-5.2-codex" },
        available: true,
      },
      selectionTransition: { type: "restart_session" },
    }),
    { type: "restart_and_resume" },
  );
});

it("restarts compatible instances for workspace or runtime changes", () => {
  assert.deepEqual(
    decideProviderSessionTransition({
      current: base,
      target: { ...base, workspace: "/other", available: true },
    }),
    { type: "restart_and_resume" },
  );
});

it("preserves handoff and missing-classification outcomes across workspace changes", () => {
  const target = {
    ...base,
    modelSelection: { ...base.modelSelection, model: "gpt-5.2-codex" },
    workspace: "/other",
    available: true,
  };
  assert.deepEqual(
    decideProviderSessionTransition({
      current: base,
      target,
      selectionTransition: { type: "create_with_handoff" },
    }),
    { type: "create_with_handoff" },
  );
  assert.deepEqual(decideProviderSessionTransition({ current: base, target }), {
    type: "reject",
    reason: "The provider adapter did not classify the selection change.",
  });
});

it("uses portable handoff for incompatible continuation identities", () => {
  assert.deepEqual(
    decideProviderSessionTransition({
      current: base,
      target: {
        ...base,
        modelSelection: {
          ...base.modelSelection,
          instanceId: ProviderInstanceId.make("codex_other"),
        },
        continuationIdentity: { driverKind: driver, continuationKey: "codex:account:other" },
        available: true,
      },
    }),
    { type: "create_with_handoff" },
  );
});