Files
t3code/packages/client-runtime/src/operations/commands.test.ts
T
Julius Marminge 799e032fe9 Integrate orchestration V2 controls and process recovery
- Add V2 item inspection, thread relationships, queued-run controls, and composer dispatch policies
- Reconcile interrupted provider state safely across shutdown and startup
- Preserve checkpoint baselines and shared provider-session projections
2026-08-07 13:21:01 +02:00

461 lines
16 KiB
TypeScript

import {
CheckpointId,
CheckpointRef,
CheckpointScopeId,
CommandId,
EnvironmentId,
MessageId,
NodeId,
ORCHESTRATION_V2_WS_METHODS,
PlanId,
ProjectId,
ProviderInstanceId,
RunId,
ThreadId,
WS_METHODS,
type OrchestrationV2Command,
type OrchestrationV2ThreadLaunchInput,
type OrchestrationV2ThreadProjection,
type ProjectMutation,
} from "@t3tools/contracts";
import { describe, expect, it } from "@effect/vitest";
import * as Crypto from "effect/Crypto";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as SubscriptionRef from "effect/SubscriptionRef";
import {
AVAILABLE_CONNECTION_STATE,
PrimaryConnectionTarget,
type PreparedConnection,
} from "../connection/model.ts";
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
import * as RpcSession from "../rpc/session.ts";
import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
import { v2Now, v2Projection, v2ThreadId } from "../state/orchestrationV2TestFixtures.ts";
import {
archiveThread,
createProject,
forkThreadFromRun,
mergeThreadBack,
promoteQueuedRun,
reorderQueuedRun,
revertThreadCheckpoint,
startThreadTurn,
updateThreadMetadata,
} from "./commands.ts";
const TEST_CRYPTO_LAYER = Layer.succeed(
Crypto.Crypto,
Crypto.make({
randomBytes: (size) => new Uint8Array(size),
digest: (_algorithm, data) => Effect.succeed(data),
}),
);
const TARGET = new PrimaryConnectionTarget({
environmentId: EnvironmentId.make("environment-1"),
label: "Test environment",
httpBaseUrl: "https://environment.example.test",
wsBaseUrl: "wss://environment.example.test",
});
const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(function* (input: {
readonly commands: OrchestrationV2Command[];
readonly projects: ProjectMutation[];
readonly launches?: OrchestrationV2ThreadLaunchInput[];
readonly projection?: OrchestrationV2ThreadProjection;
}) {
const client = {
[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) =>
Effect.sync(() => {
input.commands.push(command);
return { sequence: input.commands.length };
}),
[ORCHESTRATION_V2_WS_METHODS.getThreadProjection]: () =>
Effect.succeed(input.projection ?? v2Projection),
[ORCHESTRATION_V2_WS_METHODS.launchThread]: (launchInput: OrchestrationV2ThreadLaunchInput) =>
Effect.sync(() => {
input.launches?.push(launchInput);
return {
threadId: launchInput.threadId ?? v2ThreadId,
projection: input.projection ?? v2Projection,
resumed: false,
};
}),
[WS_METHODS.projectsMutate]: (mutation: ProjectMutation) =>
Effect.sync(() => {
input.projects.push(mutation);
return {
id: mutation.projectId,
title: mutation.type === "project.create" ? mutation.title : "Project",
workspaceRoot:
mutation.type === "project.create" ? mutation.workspaceRoot : "/workspace/project",
repositoryIdentity: null,
faviconPath: null,
defaultModelSelection: null,
scripts: [],
createdAt: "2026-06-06T00:00:00.000Z",
updatedAt: "2026-06-06T00:00:00.000Z",
deletedAt: null,
};
}),
} as unknown as WsRpcProtocolClient;
const session: RpcSession.RpcSession = {
client,
initialConfig: Effect.never,
ready: Effect.void,
probe: Effect.void,
closed: Effect.never,
};
return EnvironmentSupervisor.EnvironmentSupervisor.of({
target: TARGET,
state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
session: yield* SubscriptionRef.make(Option.some(session)),
prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
connect: Effect.void,
disconnect: Effect.void,
retryNow: Effect.void,
} satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
});
describe("V2 environment commands", () => {
it.effect("routes projects through the event-sourced project transport", () =>
Effect.gen(function* () {
const projects: ProjectMutation[] = [];
const supervisor = yield* makeSupervisor({ commands: [], projects });
yield* createProject({
projectId: ProjectId.make("project-1"),
title: "Project",
workspaceRoot: "/workspace/project",
createWorkspaceRootIfMissing: true,
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(projects).toEqual([
{
type: "project.create",
commandId: "00000000-0000-4000-8000-000000000000",
projectId: "project-1",
title: "Project",
workspaceRoot: "/workspace/project",
createWorkspaceRootIfMissing: true,
},
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("preserves caller command ids for idempotent V2 commands", () =>
Effect.gen(function* () {
const commands: OrchestrationV2Command[] = [];
const supervisor = yield* makeSupervisor({ commands, projects: [] });
yield* archiveThread({
commandId: CommandId.make("queued-command"),
threadId: ThreadId.make("thread-1"),
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(commands).toEqual([
{ type: "thread.archive", commandId: "queued-command", threadId: "thread-1" },
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("resolves run ordinal zero to the persisted thread-start checkpoint", () =>
Effect.gen(function* () {
const scopeId = CheckpointScopeId.make("checkpoint-scope-root");
const checkpointId = CheckpointId.make("checkpoint-thread-start");
const projection: OrchestrationV2ThreadProjection = {
...v2Projection,
checkpoints: [
{
id: checkpointId,
threadId: v2ThreadId,
scopeId,
runId: null,
nodeId: NodeId.make("node-run-1"),
parentCheckpointId: null,
ordinalWithinScope: 0,
appRunOrdinal: null,
ref: CheckpointRef.make("refs/t3/thread-start"),
status: "ready",
files: [],
capturedAt: v2Now,
},
],
};
const commands: OrchestrationV2Command[] = [];
const supervisor = yield* makeSupervisor({ commands, projects: [], projection });
yield* revertThreadCheckpoint({
commandId: CommandId.make("rollback-thread-start"),
threadId: v2ThreadId,
turnCount: 0,
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(commands).toEqual([
{
type: "checkpoint.rollback",
commandId: "rollback-thread-start",
threadId: v2ThreadId,
scopeId,
checkpointId,
},
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("preserves plan implementation provenance on V2 runs", () =>
Effect.gen(function* () {
const commands: OrchestrationV2Command[] = [];
const supervisor = yield* makeSupervisor({ commands, projects: [] });
yield* startThreadTurn({
commandId: CommandId.make("implement-plan"),
threadId: v2ThreadId,
message: {
messageId: MessageId.make("message-implementation"),
role: "user",
text: "Implement the plan",
attachments: [],
},
modelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5.4",
},
runtimeMode: "full-access",
interactionMode: "default",
sourceProposedPlan: {
threadId: ThreadId.make("thread-plan"),
planId: PlanId.make("plan-1"),
},
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(commands).toHaveLength(1);
expect(commands[0]).toMatchObject({
type: "message.dispatch",
commandId: "implement-plan",
threadId: v2ThreadId,
sourcePlanRef: { threadId: "thread-plan", planId: "plan-1" },
dispatchMode: { type: "start_immediately" },
});
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("preserves an existing worktree and branch during first-message launch", () =>
Effect.gen(function* () {
const launches: OrchestrationV2ThreadLaunchInput[] = [];
const supervisor = yield* makeSupervisor({ commands: [], projects: [], launches });
yield* startThreadTurn({
commandId: CommandId.make("launch-existing-worktree"),
threadId: v2ThreadId,
message: {
messageId: MessageId.make("message-existing-worktree"),
role: "user",
text: "Continue here",
attachments: [],
},
runtimeMode: "full-access",
interactionMode: "default",
bootstrap: {
createThread: {
projectId: ProjectId.make("project-1"),
title: "Thread",
modelSelection: v2Projection.thread.modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: "feature",
worktreePath: "/workspace/project-worktrees/feature",
createdAt: "2026-06-20T00:00:00.000Z",
},
},
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(launches[0]).toMatchObject({
threadId: v2ThreadId,
workspaceStrategy: {
type: "existing_worktree",
worktreePath: "/workspace/project-worktrees/feature",
branch: "feature",
},
});
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("provisions an origin-based worktree for an existing empty thread", () =>
Effect.gen(function* () {
const launches: OrchestrationV2ThreadLaunchInput[] = [];
const supervisor = yield* makeSupervisor({ commands: [], projects: [], launches });
yield* startThreadTurn({
commandId: CommandId.make("launch-existing-thread-worktree"),
threadId: v2ThreadId,
message: {
messageId: MessageId.make("message-existing-thread-worktree"),
role: "user",
text: "Move to a worktree",
attachments: [],
},
runtimeMode: "full-access",
interactionMode: "default",
bootstrap: {
prepareWorktree: {
projectCwd: "/workspace/project",
baseBranch: "main",
branch: "feature",
startFromOrigin: true,
},
},
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(launches[0]).toMatchObject({
threadId: v2ThreadId,
reuseExistingThread: true,
projectId: v2Projection.thread.projectId,
workspaceStrategy: {
type: "worktree",
baseRef: "main",
branch: "feature",
startFromOrigin: true,
},
});
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("maps explicit active-run delivery modes to V2 dispatch semantics", () =>
Effect.gen(function* () {
const activeRunId = RunId.make("run-active");
const now = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
const projection: OrchestrationV2ThreadProjection = {
...v2Projection,
runs: [
{
id: activeRunId,
threadId: v2ThreadId,
ordinal: 1,
providerInstanceId: v2Projection.thread.providerInstanceId,
modelSelection: v2Projection.thread.modelSelection,
providerThreadId: null,
userMessageId: MessageId.make("message-active"),
rootNodeId: null,
activeAttemptId: null,
status: "running",
requestedAt: now,
startedAt: now,
completedAt: null,
checkpointId: null,
contextHandoffId: null,
},
],
};
const commands: OrchestrationV2Command[] = [];
const supervisor = yield* makeSupervisor({ commands, projects: [], projection });
for (const [mode, expectedType] of [
["queue", "queue_after_active"],
["steer", "steer_active"],
["restart", "restart_active"],
] as const) {
yield* startThreadTurn({
commandId: CommandId.make(`command-${mode}`),
threadId: v2ThreadId,
message: {
messageId: MessageId.make(`message-${mode}`),
role: "user",
text: mode,
attachments: [],
},
runtimeMode: "full-access",
interactionMode: "default",
dispatchMode: mode,
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(commands.at(-1)).toMatchObject({
type: "message.dispatch",
dispatchMode: {
type: expectedType,
...(mode === "queue" ? {} : { targetRunId: activeRunId }),
},
});
}
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect(
"dispatches V2-native relationship and queue commands without compatibility shaping",
() =>
Effect.gen(function* () {
const commands: OrchestrationV2Command[] = [];
const supervisor = yield* makeSupervisor({ commands, projects: [] });
const provide = Effect.provideService(
EnvironmentSupervisor.EnvironmentSupervisor,
supervisor,
);
yield* forkThreadFromRun({
commandId: CommandId.make("fork"),
sourceThreadId: v2ThreadId,
targetThreadId: ThreadId.make("thread-fork"),
runId: RunId.make("run-1"),
}).pipe(provide);
yield* mergeThreadBack({
commandId: CommandId.make("merge"),
sourceThreadId: ThreadId.make("thread-fork"),
targetThreadId: v2ThreadId,
runId: RunId.make("run-2"),
}).pipe(provide);
yield* reorderQueuedRun({
commandId: CommandId.make("reorder"),
threadId: v2ThreadId,
runId: RunId.make("run-3"),
beforeRunId: RunId.make("run-4"),
}).pipe(provide);
yield* promoteQueuedRun({
commandId: CommandId.make("promote"),
threadId: v2ThreadId,
queuedRunId: RunId.make("run-3"),
targetRunId: RunId.make("run-active"),
}).pipe(provide);
expect(commands).toMatchObject([
{ type: "thread.fork", sourcePoint: { type: "run", runId: "run-1" } },
{ type: "thread.merge_back", sourcePoint: { type: "run", runId: "run-2" } },
{ type: "queued-run.reorder", runId: "run-3", beforeRunId: "run-4" },
{
type: "queued-message.promote-to-steer",
queuedRunId: "run-3",
targetRunId: "run-active",
},
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
it.effect("uses provider.switch when model selection changes provider instance", () =>
Effect.gen(function* () {
const commands: OrchestrationV2Command[] = [];
const supervisor = yield* makeSupervisor({ commands, projects: [] });
yield* updateThreadMetadata({
commandId: CommandId.make("switch-provider"),
threadId: v2ThreadId,
modelSelection: {
instanceId: ProviderInstanceId.make("claude"),
model: "claude-sonnet-4-6",
},
}).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
expect(commands).toEqual([
{
type: "provider.switch",
commandId: "switch-provider",
threadId: v2ThreadId,
modelSelection: { instanceId: "claude", model: "claude-sonnet-4-6" },
},
]);
}).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
);
});