Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export const CodexProviderCapabilitiesV2 = {
canRollbackThread: true,
canForkThread: true,
canForkFromTurn: true,
canForkWhileSourceRunning: true,
canForkFromSubagentThread: true,
exposesNativeThreadId: true,
},
Expand Down
78 changes: 55 additions & 23 deletions apps/server/src/orchestration-v2/CommandPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ layer("CommandPolicyV2", (it) => {
capabilities: CursorProviderCapabilitiesV2,
sameProvider: true,
hasStrongNativeSource: true,
hasNativeTurnBoundary: false,
sourceRunStatus: "completed",
fromSpecificTurn: true,
sourceBusy: false,
Expand All @@ -321,29 +322,60 @@ layer("CommandPolicyV2", (it) => {
}),
);

it.effect("forks a busy source natively only where the provider supports it", () =>
Effect.gen(function* () {
const policy = yield* CommandPolicy.CommandPolicyV2;
const decide = (
provider: "codex" | "claudeAgent",
capabilities: OrchestrationV2ProviderCapabilities,
sourceBusy: boolean,
) =>
policy.decideForkExecution({
commandId,
threadId,
providerInstanceId: ProviderInstanceId.make(provider),
capabilities,
sameProvider: true,
hasStrongNativeSource: true,
sourceRunStatus: "completed",
fromSpecificTurn: true,
sourceBusy,
});
it.effect.each([
{
provider: "codex",
capabilities: CodexProviderCapabilitiesV2,
hasNativeTurnBoundary: true,
busyStrategy: "native_fork",
},
{
provider: "codex without busy forks",
capabilities: {
...CodexProviderCapabilitiesV2,
threads: { ...CodexProviderCapabilitiesV2.threads, canForkWhileSourceRunning: undefined },
},
hasNativeTurnBoundary: true,
busyStrategy: "portable_context",
},
{
provider: "codex",
capabilities: CodexProviderCapabilitiesV2,
hasNativeTurnBoundary: false,
busyStrategy: "portable_context",
},
{
provider: "claudeAgent",
capabilities: ClaudeProviderCapabilitiesV2,
hasNativeTurnBoundary: false,
busyStrategy: "native_fork",
},
] satisfies ReadonlyArray<{
provider: string;
capabilities: OrchestrationV2ProviderCapabilities;
hasNativeTurnBoundary: boolean;
busyStrategy: CommandPolicy.ForkExecutionPolicyV2;
}>)(
"forks a busy $provider source with hasNativeTurnBoundary=$hasNativeTurnBoundary as $busyStrategy",
({ capabilities, hasNativeTurnBoundary, busyStrategy }) =>
Effect.gen(function* () {
const policy = yield* CommandPolicy.CommandPolicyV2;
const decide = (sourceBusy: boolean) =>
policy.decideForkExecution({
commandId,
threadId,
providerInstanceId: ProviderInstanceId.make("custom-instance"),
capabilities,
sameProvider: true,
hasStrongNativeSource: true,
hasNativeTurnBoundary,
sourceRunStatus: "completed",
fromSpecificTurn: true,
sourceBusy,
});

assert.equal(yield* decide("codex", CodexProviderCapabilitiesV2, false), "native_fork");
assert.equal(yield* decide("codex", CodexProviderCapabilitiesV2, true), "portable_context");
assert.equal(yield* decide("claudeAgent", ClaudeProviderCapabilitiesV2, true), "native_fork");
}),
assert.equal(yield* decide(false), "native_fork");
assert.equal(yield* decide(true), busyStrategy);
}),
);
});
20 changes: 18 additions & 2 deletions apps/server/src/orchestration-v2/CommandPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ export function isActiveRun(run: OrchestrationV2Run): boolean {
);
}

/**
* Providers that key forks on native turn ids can only fork a busy source from a turn with one;
* a synthetic or missing boundary would fork the live conversation, including the running turn.
*/
export function canForkBusySourceNatively(
capabilities: OrchestrationV2ProviderCapabilities,
hasNativeTurnBoundary: boolean,
): boolean {
return (
capabilities.threads.canForkWhileSourceRunning === true &&
(hasNativeTurnBoundary || !capabilities.turns.exposesNativeTurnId)
);
}

/** The run a follow-up to a finished child binds to: the latest active one, else the last that ran. */
export function followUpOwnerRun(
runs: ReadonlyArray<OrchestrationV2Run>,
Expand Down Expand Up @@ -219,9 +233,10 @@ export interface CommandPolicyV2Shape {
input: CapabilityCheckInput & {
readonly sameProvider: boolean;
readonly hasStrongNativeSource: boolean;
readonly hasNativeTurnBoundary: boolean;
readonly sourceRunStatus: OrchestrationV2Run["status"];
readonly fromSpecificTurn: boolean;
/** Later source runs are still active; providers cannot fork a thread mid-turn. */
/** Later source runs are still active. */
readonly sourceBusy: boolean;
},
) => Effect.Effect<ForkExecutionPolicyV2, CommandPolicyV2Error>;
Expand Down Expand Up @@ -388,7 +403,8 @@ const decideForkExecution: CommandPolicyV2Shape["decideForkExecution"] = (input)
(input.sourceRunStatus === "completed" || input.sourceRunStatus === "waiting") &&
input.sameProvider &&
input.hasStrongNativeSource &&
(!input.sourceBusy || input.capabilities.threads.canForkWhileSourceRunning === true) &&
(!input.sourceBusy ||
canForkBusySourceNatively(input.capabilities, input.hasNativeTurnBoundary)) &&
input.capabilities.threads.canForkThread &&
(!input.fromSpecificTurn || input.capabilities.threads.canForkFromTurn) &&
input.capabilities.identity.nativeThreadIds === "strong";
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8460,6 +8460,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
sameProvider:
pendingForkTransfer.sourceProviderInstanceId === modelSelection.instanceId,
hasStrongNativeSource: sourceProviderThread?.nativeThreadRef?.strength === "strong",
hasNativeTurnBoundary:
sourceProjection?.providerTurns.find((turn) => turn.id === sourceProviderTurnId)
?.nativeTurnRef != null,
sourceRunStatus: sourceRun.status,
fromSpecificTurn: sourceRun !== null,
sourceBusy: sourceRunsAfterFork.some(isActiveRun),
Expand Down
Loading
Loading