diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 9b818c99a24ab..57ed889a62314 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -8973,6 +8973,20 @@ describe("ClaudeAdapterV2 background wake turns", () => { targetThreadId: ThreadId.make("thread-claude-fork-after-compact-rollback"), }); assert.equal(forkCalls[0]?.options.upToMessageId, earlierCursor); + // A running later turn writes past the head, so the head cannot bound a fork. + const busyForkError = yield* harness.runtime + .forkThread({ + sourceProviderThread: pastSilentTurn.providerThread, + sourceProviderTurns: [ + ...turnsAcrossSilent.slice(0, 2), + { ...turnsAcrossSilent[2]!, status: "running", completedAt: null }, + ], + providerTurnId: providerTurn.id, + targetThreadId: ThreadId.make("thread-claude-fork-while-source-runs"), + }) + .pipe(Effect.flip); + assert.instanceOf(busyForkError, ProviderAdapter.ProviderAdapterForkThreadError); + assert.equal(forkCalls.length, 1); const interruptedBeforeCompact = yield* rollbackTo( [ { diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 262f67ca93f80..a80ef58fdc5fa 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -524,6 +524,7 @@ export const ClaudeProviderCapabilitiesV2 = { canRollbackThread: true, canForkThread: true, canForkFromTurn: true, + canForkWhileSourceRunning: true, canForkFromSubagentThread: false, exposesNativeThreadId: true, }, @@ -2768,10 +2769,8 @@ const resolveClaudeForkUpToMessageId = Effect.fn("ClaudeAdapterV2.resolveForkUpT return boundaryNativeId; } - const terminalTurnsAfterBoundary = sourceTurns - .slice(boundaryIndex + 1) - .filter(isTerminalProviderTurn); - if (terminalTurnsAfterBoundary.length === 0) { + // The head only marks this boundary when no later turn, finished or still running, wrote after it. + if (sourceTurns.length === boundaryIndex + 1) { // Rollback pins the head but leaves the rolled-back turns in Claude's transcript. return yield* getNativeConversationHeadId(input.sourceProviderThread); } diff --git a/apps/server/src/orchestration-v2/CommandPolicy.test.ts b/apps/server/src/orchestration-v2/CommandPolicy.test.ts index c18f7136eb643..087b24646ef4e 100644 --- a/apps/server/src/orchestration-v2/CommandPolicy.test.ts +++ b/apps/server/src/orchestration-v2/CommandPolicy.test.ts @@ -13,6 +13,7 @@ import { } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; +import { ClaudeProviderCapabilitiesV2 } from "./Adapters/ClaudeAdapterV2.ts"; import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts"; import { CursorProviderCapabilitiesV2 } from "@t3tools/provider-cursor/testing"; import { makeGrokAcpAdapterFlavor } from "@t3tools/provider-grok/testing"; @@ -320,15 +321,19 @@ layer("CommandPolicyV2", (it) => { }), ); - it.effect("forks a busy source through portable context", () => + it.effect("forks a busy source natively only where the provider supports it", () => Effect.gen(function* () { const policy = yield* CommandPolicy.CommandPolicyV2; - const decide = (sourceBusy: boolean) => + const decide = ( + provider: "codex" | "claudeAgent", + capabilities: OrchestrationV2ProviderCapabilities, + sourceBusy: boolean, + ) => policy.decideForkExecution({ commandId, threadId, - providerInstanceId: ProviderInstanceId.make("codex"), - capabilities: CodexProviderCapabilitiesV2, + providerInstanceId: ProviderInstanceId.make(provider), + capabilities, sameProvider: true, hasStrongNativeSource: true, sourceRunStatus: "completed", @@ -336,8 +341,9 @@ layer("CommandPolicyV2", (it) => { sourceBusy, }); - assert.equal(yield* decide(false), "native_fork"); - assert.equal(yield* decide(true), "portable_context"); + 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"); }), ); }); diff --git a/apps/server/src/orchestration-v2/CommandPolicy.ts b/apps/server/src/orchestration-v2/CommandPolicy.ts index 67d61332ae7c8..69c5497d3b876 100644 --- a/apps/server/src/orchestration-v2/CommandPolicy.ts +++ b/apps/server/src/orchestration-v2/CommandPolicy.ts @@ -388,7 +388,7 @@ const decideForkExecution: CommandPolicyV2Shape["decideForkExecution"] = (input) (input.sourceRunStatus === "completed" || input.sourceRunStatus === "waiting") && input.sameProvider && input.hasStrongNativeSource && - !input.sourceBusy && + (!input.sourceBusy || input.capabilities.threads.canForkWhileSourceRunning === true) && input.capabilities.threads.canForkThread && (!input.fromSpecificTurn || input.capabilities.threads.canForkFromTurn) && input.capabilities.identity.nativeThreadIds === "strong"; diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts index 95595c3a0a2ae..edec4cad58790 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts @@ -394,6 +394,7 @@ function makeLocalCommandHarness(input: { readonly nativeResumeUnsupported?: boolean; readonly steeringRestart?: boolean; readonly nativeGoals?: boolean; + readonly canForkWhileSourceRunning?: boolean; readonly goalDriver?: "opencode" | "antigravity"; readonly worktreePath?: string; readonly worktreesDir?: string; @@ -1037,6 +1038,10 @@ function makeLocalCommandHarness(input: { model: null, capabilities: { ...CodexProviderCapabilitiesV2, + threads: { + ...CodexProviderCapabilitiesV2.threads, + canForkWhileSourceRunning: input.canForkWhileSourceRunning, + }, turns: { ...CodexProviderCapabilitiesV2.turns, ...(input.nativeGoals === undefined ? {} : { supportsNativeGoals: input.nativeGoals }), @@ -2221,13 +2226,21 @@ effectIt.effect.each([ transferStatus: "resolved_portable", handoffIds: ["side-chat-fork-context"], }, + { + nativeForkFrom: "busy", + canForkWhileSourceRunning: true, + forks: true, + transferStatus: "consumed", + handoffIds: [], + }, ] as const)( - "runs a planned native side chat fork when the parent is $nativeForkFrom", - ({ nativeForkFrom, forks, transferStatus, handoffIds }) => + "runs a planned native side chat fork when the parent is $nativeForkFrom (fork while running: $canForkWhileSourceRunning)", + ({ nativeForkFrom, forks, transferStatus, handoffIds, ...capability }) => Effect.gen(function* () { const harness = makeLocalCommandHarness({ text: "What is the main thread doing?", sideChat: { contextRun: "current", nativeForkFrom }, + ...capability, }); yield* harness.start; diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 7764bca5cca0c..7e6fe6a206c64 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -1211,11 +1211,13 @@ export const layer: Layer.Layer< cause: `Native fork transfer ${nativeForkTransfer.id} has no source provider execution.`, }); } - // Providers refuse to fork a busy thread, so a source that started a turn after + // Most providers refuse to fork a busy thread, so a source that started a turn after // dispatch planned this fork gets the portable context dispatch gives a busy source. - const sourceBusy = sourceProjection.runs.some( - (candidate) => candidate.ordinal > sourceRun.ordinal && isActiveRun(candidate), - ); + const sourceBusy = + session.providerSession.capabilities.threads.canForkWhileSourceRunning !== true && + sourceProjection.runs.some( + (candidate) => candidate.ordinal > sourceRun.ordinal && isActiveRun(candidate), + ); let nativeForkFailure: string | undefined; if (!sourceBusy) { // Rolled-back turns are no longer in the source conversation, so they cannot bound a fork. diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.ts index edc81c72fff5e..39543e9a5f261 100644 --- a/packages/contracts/src/orchestrationV2.ts +++ b/packages/contracts/src/orchestrationV2.ts @@ -286,6 +286,8 @@ export const OrchestrationV2ThreadCapabilities = Schema.Struct({ canRollbackThread: Schema.Boolean, canForkThread: Schema.Boolean, canForkFromTurn: Schema.Boolean, + // Fork from a completed turn while a later turn in the source is still running. + canForkWhileSourceRunning: Schema.optional(Schema.Boolean), canForkFromSubagentThread: Schema.Boolean, exposesNativeThreadId: Schema.Boolean, });