Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
{
Expand Down
7 changes: 3 additions & 4 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ export const ClaudeProviderCapabilitiesV2 = {
canRollbackThread: true,
canForkThread: true,
canForkFromTurn: true,
canForkWhileSourceRunning: true,
canForkFromSubagentThread: false,
exposesNativeThreadId: true,
},
Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 12 additions & 6 deletions apps/server/src/orchestration-v2/CommandPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -320,24 +321,29 @@ 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",
fromSpecificTurn: true,
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");
}),
);
});
2 changes: 1 addition & 1 deletion apps/server/src/orchestration-v2/CommandPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
17 changes: 15 additions & 2 deletions apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 }),
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions apps/server/src/orchestration-v2/ProviderTurnStartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions packages/contracts/src/orchestrationV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
Loading