fix(orchestration-v2): compose provider switch merge context

Allow a pending merge-back to be consumed while changing providers and deliver both the provider-switch history and fork delta to the selected provider.

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-08-15 16:49:00 +02:00
1 parent 49a3ba50c1
commit d3449cdf1d
3 files changed
+187 -24

No files matched your search

@@ -157,17 +157,24 @@ export function providerMessageWithContextHandoff(input: {
readonly handoff: OrchestrationV2ContextHandoff;
readonly userText: string;
}): string {
const label =
input.handoff.strategy === "fork_delta_summary"
? "merge_back / fork_delta_summary"
: input.handoff.strategy;
return [
`Context handoff (${label}):`,
input.handoff.summaryText,
"",
"User message:",
input.userText,
].join("\n");
return providerMessageWithContextHandoffs({
handoffs: [input.handoff],
userText: input.userText,
});
}
export function providerMessageWithContextHandoffs(input: {
readonly handoffs: ReadonlyArray<OrchestrationV2ContextHandoff>;
readonly userText: string;
}): string {
const handoffSections = input.handoffs.flatMap((handoff) => {
const label =
handoff.strategy === "fork_delta_summary"
? "merge_back / fork_delta_summary"
: handoff.strategy;
return [`Context handoff (${label}):`, handoff.summaryText, ""];
});
return [...handoffSections, "User message:", input.userText].join("\n");
}
const makeContextHandoffService = Effect.fn("orchestrationV2.ContextHandoffService.layer")(
@@ -38,6 +38,7 @@ import { CommandReceiptStoreV2 } from "./CommandReceiptStore.ts";
import {
ContextHandoffServiceV2,
providerMessageWithContextHandoff,
providerMessageWithContextHandoffs,
} from "./ContextHandoffService.ts";
import { EventSinkV2 } from "./EventSink.ts";
import { IdAllocatorV2 } from "./IdAllocator.ts";
@@ -1667,13 +1668,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
const isProviderSwitch =
activeProviderThread !== undefined &&
activeProviderThread.provider !== modelSelection.instanceId;
if (isProviderSwitch && pendingMergeBackTransfer !== undefined) {
return yield* new OrchestratorDispatchError({
commandId: command.commandId,
commandType: command.type,
cause: `Thread ${command.threadId} cannot switch providers while merge-back transfer ${pendingMergeBackTransfer.id} is pending.`,
});
}
const sourceProjection =
pendingForkTransfer === undefined
? null
@@ -2199,7 +2193,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
text: command.text,
attachments: command.attachments,
};
const activeHandoff = portableForkHandoff ?? providerSwitchHandoff ?? mergeBackHandoff;
const activeHandoff = portableForkHandoff ?? mergeBackHandoff ?? providerSwitchHandoff;
const providerHandoffs = [
portableForkHandoff,
providerSwitchHandoff,
mergeBackHandoff,
].flatMap((handoff) => (handoff === null ? [] : [handoff]));
const handoffTurnItem: OrchestrationV2TurnItem | null =
activeHandoff === null
? null
@@ -2526,12 +2525,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
}
const providerMessageText =
activeHandoff === null
providerHandoffs.length === 0
? command.text
: providerMessageWithContextHandoff({
handoff: activeHandoff,
userText: command.text,
});
: providerHandoffs.length === 1
? providerMessageWithContextHandoff({
handoff: providerHandoffs[0]!,
userText: command.text,
})
: providerMessageWithContextHandoffs({
handoffs: providerHandoffs,
userText: command.text,
});
yield* runExecution
.startRootRun({
commandId: command.commandId,
@@ -526,4 +526,156 @@ describe("orchestration v2 provider switching", () => {
);
}
});
it("switches providers while consuming a pending cross-provider merge-back", async () => {
const sourceThreadId = ThreadId.make("thread:cross-provider-merge:source");
const forkThreadId = ThreadId.make("thread:cross-provider-merge:fork");
const sourcePrompt = "Remember that the source marker is amber.";
const forkPrompt = "Remember that the fork marker is cobalt.";
const mergePrompt = "Report both remembered markers.";
const cwd = await makeCheckpointWorkspace("cross-provider-merge");
const capturedTurns = await Effect.runPromise(Ref.make<ReadonlyArray<CapturedTurn>>([]));
const registryLayer = makeProviderAdapterRegistryLayer([
makeTestAdapter({
instanceId: ProviderInstanceId.make("codex"),
provider: "codex",
capabilities: CodexProviderCapabilitiesV2,
modelSelection: CODEX_MODEL_SELECTION,
responseByRunOrdinal: {
1: "I will remember cobalt.",
2: "The markers are amber and cobalt.",
},
capturedTurns,
}),
makeTestAdapter({
instanceId: ProviderInstanceId.make("claudeAgent"),
provider: "claudeAgent",
capabilities: ClaudeProviderCapabilitiesV2,
modelSelection: CLAUDE_MODEL_SELECTION,
responseByRunOrdinal: { 1: "I will remember amber." },
capturedTurns,
}),
]);
const commands = [
{
type: "thread.create",
commandId: CommandId.make("command:cross-provider-merge:create"),
threadId: sourceThreadId,
projectId,
title: "Cross-provider merge source",
modelSelection: CLAUDE_MODEL_SELECTION,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
},
{
type: "message.dispatch",
commandId: CommandId.make("command:cross-provider-merge:source"),
threadId: sourceThreadId,
messageId: MessageId.make("message:cross-provider-merge:source"),
text: sourcePrompt,
attachments: [],
modelSelection: CLAUDE_MODEL_SELECTION,
dispatchMode: { type: "start_immediately" },
},
{
type: "thread.fork",
commandId: CommandId.make("command:cross-provider-merge:fork"),
sourceThreadId,
targetThreadId: forkThreadId,
sourcePoint: { type: "latest_stable" },
title: "Cross-provider merge fork",
},
{
type: "message.dispatch",
commandId: CommandId.make("command:cross-provider-merge:fork-turn"),
threadId: forkThreadId,
messageId: MessageId.make("message:cross-provider-merge:fork-turn"),
text: forkPrompt,
attachments: [],
modelSelection: CODEX_MODEL_SELECTION,
dispatchMode: { type: "start_immediately" },
},
{
type: "thread.merge_back",
commandId: CommandId.make("command:cross-provider-merge:merge"),
sourceThreadId: forkThreadId,
targetThreadId: sourceThreadId,
sourcePoint: { type: "latest_stable" },
},
{
type: "message.dispatch",
commandId: CommandId.make("command:cross-provider-merge:consume"),
threadId: sourceThreadId,
messageId: MessageId.make("message:cross-provider-merge:consume"),
text: mergePrompt,
attachments: [],
modelSelection: CODEX_MODEL_SELECTION,
dispatchMode: { type: "start_immediately" },
},
] satisfies ReadonlyArray<OrchestrationV2Command>;
try {
const projection = await Effect.runPromise(
Effect.gen(function* () {
const orchestrator = yield* OrchestratorV2;
yield* orchestrator.dispatch(commands[0]!);
yield* orchestrator.dispatch(commands[1]!);
yield* waitForIdle(sourceThreadId);
yield* orchestrator.dispatch(commands[2]!);
yield* orchestrator.dispatch(commands[3]!);
yield* waitForIdle(forkThreadId);
yield* orchestrator.dispatch(commands[4]!);
yield* orchestrator.dispatch(commands[5]!);
return yield* waitForIdle(sourceThreadId);
}).pipe(
Effect.provide(
makeOrchestratorV2ReplayLayerWithRegistry(
{
name: "cross-provider-merge",
runtimePolicyOverride: {
cwd,
approvalPolicy: "never",
sandboxPolicy: {
type: "readOnly",
access: { type: "fullAccess" },
networkAccess: false,
},
},
},
registryLayer,
),
),
),
);
const turns = await Effect.runPromise(Ref.get(capturedTurns));
const mergedTurn = turns.find(
(turn) => turn.threadId === sourceThreadId && turn.provider === "codex",
);
const mergeTransfer = projection.contextTransfers.find(
(transfer) => transfer.type === "merge_back",
);
assert.isDefined(mergedTurn);
assert.include(mergedTurn.text, "Context handoff (full_thread_summary):");
assert.include(mergedTurn.text, sourcePrompt);
assert.include(mergedTurn.text, "I will remember amber.");
assert.include(mergedTurn.text, "Context handoff (merge_back / fork_delta_summary):");
assert.include(mergedTurn.text, forkPrompt);
assert.include(mergedTurn.text, "I will remember cobalt.");
assert.include(mergedTurn.text, mergePrompt);
assert.isDefined(mergeTransfer);
assert.equal(mergeTransfer.status, "consumed");
assert.equal(mergeTransfer.targetProvider, "codex");
assert.equal(mergeTransfer.resolution?.strategy, "fork_delta_context");
} finally {
await Effect.runPromise(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
yield* fs.remove(cwd, { recursive: true, force: true });
}).pipe(Effect.provide(NodeServices.layer)),
);
}
});
});