The best way to control Claude Code, Codex and more from any computer or phone
fix(server): fork a running Claude thread natively (#37955)
Julius Marminge committed
Nov 16, 2026 at 22:47 UTC
2904c397ad4e247eb5b77857ab629e5d4fd19426
7 files changed
+53
-17
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+14
@@ -8973,6 +8973,20 @@ describe("ClaudeAdapterV2 background wake turns", () => {
8973
targetThreadId: ThreadId.make("thread-claude-fork-after-compact-rollback"),
8974
});
8975
assert.equal(forkCalls[0]?.options.upToMessageId, earlierCursor);
8976
+ // A running later turn writes past the head, so the head cannot bound a fork.
8977
+ const busyForkError = yield* harness.runtime
8978
+ .forkThread({
8979
+ sourceProviderThread: pastSilentTurn.providerThread,
8980
+ sourceProviderTurns: [
8981
+ ...turnsAcrossSilent.slice(0, 2),
8982
+ { ...turnsAcrossSilent[2]!, status: "running", completedAt: null },
8983
+ ],
8984
+ providerTurnId: providerTurn.id,
8985
+ targetThreadId: ThreadId.make("thread-claude-fork-while-source-runs"),
8986
+ })
8987
+ .pipe(Effect.flip);
8988
+ assert.instanceOf(busyForkError, ProviderAdapter.ProviderAdapterForkThreadError);
8989
+ assert.equal(forkCalls.length, 1);
8990
const interruptedBeforeCompact = yield* rollbackTo(
8991
[
8992
{
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+3
-4
@@ -524,6 +524,7 @@ export const ClaudeProviderCapabilitiesV2 = {
524
canRollbackThread: true,
525
canForkThread: true,
526
canForkFromTurn: true,
527
+ canForkWhileSourceRunning: true,
528
canForkFromSubagentThread: false,
529
exposesNativeThreadId: true,
530
},
@@ -2764,10 +2765,8 @@ const resolveClaudeForkUpToMessageId = Effect.fn("ClaudeAdapterV2.resolveForkUpT
2765
return boundaryNativeId;
2766
}
2767
2767
- const terminalTurnsAfterBoundary = sourceTurns
2768
- .slice(boundaryIndex + 1)
2769
- .filter(isTerminalProviderTurn);
2770
- if (terminalTurnsAfterBoundary.length === 0) {
2768
+ // The head only marks this boundary when no later turn, finished or still running, wrote after it.
2769
+ if (sourceTurns.length === boundaryIndex + 1) {
2770
// Rollback pins the head but leaves the rolled-back turns in Claude's transcript.
2771
return yield* getNativeConversationHeadId(input.sourceProviderThread);
2772
}
apps/server/src/orchestration-v2/CommandPolicy.test.ts
+12
-6
@@ -13,6 +13,7 @@ import {
13
} from "@t3tools/contracts";
14
import * as Effect from "effect/Effect";
15
16
+import { ClaudeProviderCapabilitiesV2 } from "./Adapters/ClaudeAdapterV2.ts";
17
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
18
import { CursorProviderCapabilitiesV2 } from "@t3tools/provider-cursor/testing";
19
import { makeGrokAcpAdapterFlavor } from "@t3tools/provider-grok/testing";
@@ -320,15 +321,19 @@ layer("CommandPolicyV2", (it) => {
321
}),
322
);
323
323
- it.effect("forks a busy source through portable context", () =>
324
+ it.effect("forks a busy source natively only where the provider supports it", () =>
325
Effect.gen(function* () {
326
const policy = yield* CommandPolicy.CommandPolicyV2;
326
- const decide = (sourceBusy: boolean) =>
327
+ const decide = (
328
+ provider: "codex" | "claudeAgent",
329
+ capabilities: OrchestrationV2ProviderCapabilities,
330
+ sourceBusy: boolean,
331
+ ) =>
332
policy.decideForkExecution({
333
commandId,
334
threadId,
330
- providerInstanceId: ProviderInstanceId.make("codex"),
331
- capabilities: CodexProviderCapabilitiesV2,
335
+ providerInstanceId: ProviderInstanceId.make(provider),
336
+ capabilities,
337
sameProvider: true,
338
hasStrongNativeSource: true,
339
sourceRunStatus: "completed",
@@ -336,8 +341,9 @@ layer("CommandPolicyV2", (it) => {
341
sourceBusy,
342
});
343
339
- assert.equal(yield* decide(false), "native_fork");
340
- assert.equal(yield* decide(true), "portable_context");
344
+ assert.equal(yield* decide("codex", CodexProviderCapabilitiesV2, false), "native_fork");
345
+ assert.equal(yield* decide("codex", CodexProviderCapabilitiesV2, true), "portable_context");
346
+ assert.equal(yield* decide("claudeAgent", ClaudeProviderCapabilitiesV2, true), "native_fork");
347
}),
348
);
349
});
apps/server/src/orchestration-v2/CommandPolicy.ts
+1
-1
@@ -388,7 +388,7 @@ const decideForkExecution: CommandPolicyV2Shape["decideForkExecution"] = (input)
388
(input.sourceRunStatus === "completed" || input.sourceRunStatus === "waiting") &&
389
input.sameProvider &&
390
input.hasStrongNativeSource &&
391
- !input.sourceBusy &&
391
+ (!input.sourceBusy || input.capabilities.threads.canForkWhileSourceRunning === true) &&
392
input.capabilities.threads.canForkThread &&
393
(!input.fromSpecificTurn || input.capabilities.threads.canForkFromTurn) &&
394
input.capabilities.identity.nativeThreadIds === "strong";
apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts
+15
-2
@@ -394,6 +394,7 @@ function makeLocalCommandHarness(input: {
394
readonly nativeResumeUnsupported?: boolean;
395
readonly steeringRestart?: boolean;
396
readonly nativeGoals?: boolean;
397
+ readonly canForkWhileSourceRunning?: boolean;
398
readonly goalDriver?: "opencode" | "antigravity";
399
readonly worktreePath?: string;
400
readonly worktreesDir?: string;
@@ -1037,6 +1038,10 @@ function makeLocalCommandHarness(input: {
1038
model: null,
1039
capabilities: {
1040
...CodexProviderCapabilitiesV2,
1041
+ threads: {
1042
+ ...CodexProviderCapabilitiesV2.threads,
1043
+ canForkWhileSourceRunning: input.canForkWhileSourceRunning,
1044
+ },
1045
turns: {
1046
...CodexProviderCapabilitiesV2.turns,
1047
...(input.nativeGoals === undefined ? {} : { supportsNativeGoals: input.nativeGoals }),
@@ -2221,13 +2226,21 @@ effectIt.effect.each([
2226
transferStatus: "resolved_portable",
2227
handoffIds: ["side-chat-fork-context"],
2228
},
2229
+ {
2230
+ nativeForkFrom: "busy",
2231
+ canForkWhileSourceRunning: true,
2232
+ forks: true,
2233
+ transferStatus: "consumed",
2234
+ handoffIds: [],
2235
+ },
2236
] as const)(
2225
- "runs a planned native side chat fork when the parent is $nativeForkFrom",
2226
- ({ nativeForkFrom, forks, transferStatus, handoffIds }) =>
2237
+ "runs a planned native side chat fork when the parent is $nativeForkFrom (fork while running: $canForkWhileSourceRunning)",
2238
+ ({ nativeForkFrom, forks, transferStatus, handoffIds, ...capability }) =>
2239
Effect.gen(function* () {
2240
const harness = makeLocalCommandHarness({
2241
text: "What is the main thread doing?",
2242
sideChat: { contextRun: "current", nativeForkFrom },
2243
+ ...capability,
2244
});
2245
2246
yield* harness.start;
apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+6
-4
@@ -1211,11 +1211,13 @@ export const layer: Layer.Layer<
1211
cause: `Native fork transfer ${nativeForkTransfer.id} has no source provider execution.`,
1212
});
1213
}
1214
- // Providers refuse to fork a busy thread, so a source that started a turn after
1214
+ // Most providers refuse to fork a busy thread, so a source that started a turn after
1215
// dispatch planned this fork gets the portable context dispatch gives a busy source.
1216
- const sourceBusy = sourceProjection.runs.some(
1217
- (candidate) => candidate.ordinal > sourceRun.ordinal && isActiveRun(candidate),
1218
- );
1216
+ const sourceBusy =
1217
+ session.providerSession.capabilities.threads.canForkWhileSourceRunning !== true &&
1218
+ sourceProjection.runs.some(
1219
+ (candidate) => candidate.ordinal > sourceRun.ordinal && isActiveRun(candidate),
1220
+ );
1221
let nativeForkFailure: string | undefined;
1222
if (!sourceBusy) {
1223
// Rolled-back turns are no longer in the source conversation, so they cannot bound a fork.
packages/contracts/src/orchestrationV2.ts
+2
@@ -286,6 +286,8 @@ export const OrchestrationV2ThreadCapabilities = Schema.Struct({
286
canRollbackThread: Schema.Boolean,
287
canForkThread: Schema.Boolean,
288
canForkFromTurn: Schema.Boolean,
289
+ // Fork from a completed turn while a later turn in the source is still running.
290
+ canForkWhileSourceRunning: Schema.optional(Schema.Boolean),
291
canForkFromSubagentThread: Schema.Boolean,
292
exposesNativeThreadId: Schema.Boolean,
293
});