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
This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-08-07 13:21:01 +02:00
1 parent 8f521e516e
commit 799e032fe9
46 files changed
+3661 -1038

No files matched your search

@@ -1,7 +1,11 @@
import {
CheckpointId,
CheckpointRef,
CheckpointScopeId,
CommandId,
EnvironmentId,
MessageId,
NodeId,
ORCHESTRATION_V2_WS_METHODS,
PlanId,
ProjectId,
@@ -30,8 +34,18 @@ import {
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
import * as RpcSession from "../rpc/session.ts";
import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
import { v2Projection, v2ThreadId } from "../state/orchestrationV2TestFixtures.ts";
import { archiveThread, createProject, startThreadTurn } from "./commands.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,
@@ -149,6 +163,50 @@ describe("V2 environment commands", () => {
}).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[] = [];
@@ -325,4 +383,78 @@ describe("V2 environment commands", () => {
}
}).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)),
);
});
@@ -1,6 +1,7 @@
import {
CommandId,
ORCHESTRATION_V2_WS_METHODS,
OrchestrationV2CheckpointUnavailableError,
WS_METHODS,
type ChatAttachment,
type MessageId,
@@ -146,6 +147,29 @@ export interface RevertThreadCheckpointInput extends ThreadCommandInput {
export type StopThreadSessionInput = ThreadCommandInput;
export interface ForkThreadFromRunInput extends CommandMetadata {
readonly sourceThreadId: ThreadId;
readonly targetThreadId: ThreadId;
readonly runId: RunId;
readonly title?: string;
}
export interface MergeThreadBackInput extends CommandMetadata {
readonly sourceThreadId: ThreadId;
readonly targetThreadId: ThreadId;
readonly runId: RunId;
}
export interface ReorderQueuedRunInput extends ThreadCommandInput {
readonly runId: RunId;
readonly beforeRunId: RunId | null;
}
export interface PromoteQueuedRunInput extends ThreadCommandInput {
readonly queuedRunId: RunId;
readonly targetRunId: RunId;
}
const allocateCommandId = Effect.fn("EnvironmentCommands.allocateCommandId")(function* (
input: CommandMetadata,
) {
@@ -330,8 +354,13 @@ export const updateThreadMetadata = Effect.fn("EnvironmentCommands.updateThreadM
});
}
if (input.modelSelection !== undefined) {
const projection = yield* getProjection(input.threadId);
const type =
projection.thread.providerInstanceId === input.modelSelection.instanceId
? ("thread.model-selection.set" as const)
: ("provider.switch" as const);
result = yield* dispatch({
type: "thread.model-selection.set",
type,
commandId: result === null ? commandId : CommandId.make(`${commandId}:model-selection`),
threadId: input.threadId,
modelSelection: input.modelSelection,
@@ -517,8 +546,21 @@ export const revertThreadCheckpoint = Effect.fn("EnvironmentCommands.revertThrea
projection.checkpoints.find(
(candidate) => candidate.id === input.checkpointId && candidate.scopeId === input.scopeId,
) ??
projection.checkpoints.findLast((candidate) => candidate.appRunOrdinal === input.turnCount);
if (checkpoint === undefined) return { sequence: 0 };
projection.checkpoints.findLast((candidate) =>
input.turnCount === 0
? candidate.ordinalWithinScope === 0 && candidate.appRunOrdinal === null
: candidate.appRunOrdinal === input.turnCount,
);
if (checkpoint === undefined || checkpoint.status !== "ready") {
const target =
input.checkpointId === undefined
? `run ordinal ${input.turnCount ?? "unknown"}`
: `checkpoint ${input.checkpointId}`;
return yield* new OrchestrationV2CheckpointUnavailableError({
threadId: input.threadId,
target,
});
}
return yield* dispatch({
type: "checkpoint.rollback",
commandId: yield* allocateCommandId(input),
@@ -546,3 +588,56 @@ export const stopThreadSession = Effect.fn("EnvironmentCommands.stopThreadSessio
}
return result;
});
export const forkThreadFromRun = Effect.fn("EnvironmentCommands.forkThreadFromRun")(function* (
input: ForkThreadFromRunInput,
) {
return yield* dispatch({
type: "thread.fork",
commandId: yield* allocateCommandId(input),
createdBy: "user",
creationSource: input.creationSource ?? "web",
sourceThreadId: input.sourceThreadId,
targetThreadId: input.targetThreadId,
sourcePoint: { type: "run", runId: input.runId },
...(input.title === undefined ? {} : { title: input.title }),
});
});
export const mergeThreadBack = Effect.fn("EnvironmentCommands.mergeThreadBack")(function* (
input: MergeThreadBackInput,
) {
return yield* dispatch({
type: "thread.merge_back",
commandId: yield* allocateCommandId(input),
createdBy: "user",
creationSource: input.creationSource ?? "web",
sourceThreadId: input.sourceThreadId,
targetThreadId: input.targetThreadId,
sourcePoint: { type: "run", runId: input.runId },
});
});
export const reorderQueuedRun = Effect.fn("EnvironmentCommands.reorderQueuedRun")(function* (
input: ReorderQueuedRunInput,
) {
return yield* dispatch({
type: "queued-run.reorder",
commandId: yield* allocateCommandId(input),
threadId: input.threadId,
runId: input.runId,
beforeRunId: input.beforeRunId,
});
});
export const promoteQueuedRun = Effect.fn("EnvironmentCommands.promoteQueuedRun")(function* (
input: PromoteQueuedRunInput,
) {
return yield* dispatch({
type: "queued-message.promote-to-steer",
commandId: yield* allocateCommandId(input),
threadId: input.threadId,
queuedRunId: input.queuedRunId,
targetRunId: input.targetRunId,
});
});
@@ -7,6 +7,10 @@ import {
type CreateThreadInput,
type DeleteThreadInput,
type InterruptThreadTurnInput,
type ForkThreadFromRunInput,
type MergeThreadBackInput,
type PromoteQueuedRunInput,
type ReorderQueuedRunInput,
type RespondToThreadApprovalInput,
type RespondToThreadUserInputInput,
type RevertThreadCheckpointInput,
@@ -26,6 +30,10 @@ import {
createThread,
deleteThread,
interruptThreadTurn,
forkThreadFromRun,
mergeThreadBack,
promoteQueuedRun,
reorderQueuedRun,
respondToThreadApproval,
respondToThreadUserInput,
revertThreadCheckpoint,
@@ -49,6 +57,10 @@ export type {
CreateThreadInput,
DeleteThreadInput,
InterruptThreadTurnInput,
ForkThreadFromRunInput,
MergeThreadBackInput,
PromoteQueuedRunInput,
ReorderQueuedRunInput,
RespondToThreadApprovalInput,
RespondToThreadUserInputInput,
RevertThreadCheckpointInput,
@@ -191,5 +203,36 @@ export function createThreadEnvironmentAtoms<R, E>(
scheduler,
concurrency,
}),
forkFromRun: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:fork-from-run",
execute: (input: ForkThreadFromRunInput) => forkThreadFromRun(input),
scheduler,
concurrency: {
mode: "serial",
key: ({ environmentId, input }) => JSON.stringify([environmentId, input.sourceThreadId]),
},
}),
mergeBack: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:merge-back",
execute: (input: MergeThreadBackInput) => mergeThreadBack(input),
scheduler,
concurrency: {
mode: "serial",
key: ({ environmentId, input }) =>
JSON.stringify([environmentId, input.sourceThreadId, input.targetThreadId]),
},
}),
reorderQueuedRun: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:reorder-queued-run",
execute: (input: ReorderQueuedRunInput) => reorderQueuedRun(input),
scheduler,
concurrency,
}),
promoteQueuedRun: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:promote-queued-run",
execute: (input: PromoteQueuedRunInput) => promoteQueuedRun(input),
scheduler,
concurrency,
}),
};
}