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 @@ -6350,9 +6350,15 @@ describe("ClaudeAdapterV2 background wake turns", () => {

it.effect("never records a subagent uuid for a turn with no main-thread assistant message", () =>
Effect.gen(function* () {
const forkCalls: Array<ClaudeAdapterV2.ClaudeAgentSdkSessionForkInput> = [];
const harness = yield* makeWakeHarnessWithOptions({
close: (sdkMessages) => Queue.shutdown(sdkMessages),
freshQueueOnReopen: true,
forkSession: (input) =>
Effect.sync(() => {
forkCalls.push(input);
return { sessionId: "forked-after-compact-rollback" };
}),
});
const now = yield* DateTime.now;
const subagentUuid = "00000000-0000-4000-8000-000000000644";
Expand Down Expand Up @@ -6433,6 +6439,14 @@ describe("ClaudeAdapterV2 background wake turns", () => {
pastSilentTurn.providerThread.nativeConversationHeadRef?.nativeId,
earlierCursor,
);
// Forks omit rolled-back turns, which Claude's transcript still holds past the pinned head.
yield* harness.runtime.forkThread({
sourceProviderThread: pastSilentTurn.providerThread,
sourceProviderTurns: turnsAcrossSilent.slice(0, 2),
providerTurnId: providerTurn.id,
targetThreadId: ThreadId.make("thread-claude-fork-after-compact-rollback"),
});
assert.equal(forkCalls[0]?.options.upToMessageId, earlierCursor);
const interruptedBeforeCompact = yield* rollbackTo(
[
{
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2070,7 +2070,8 @@ const resolveClaudeForkUpToMessageId = Effect.fn("ClaudeAdapterV2.resolveForkUpT
.slice(boundaryIndex + 1)
.filter(isTerminalProviderTurn);
if (terminalTurnsAfterBoundary.length === 0) {
return undefined;
// Rollback pins the head but leaves the rolled-back turns in Claude's transcript.
return yield* getNativeConversationHeadId(input.sourceProviderThread);
}

return yield* new ProviderAdapter.ProviderAdapterForkThreadError({
Expand Down
55 changes: 50 additions & 5 deletions apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,35 @@ function makeLocalCommandHarness(input: {
strength: "strong" as const,
},
};
const parentProviderTurn = (ordinal: number, attemptRunId: RunId) => ({
attempt: {
...projection.attempts[0]!,
id: RunAttemptId.make(`side-chat-parent-attempt-${ordinal}`),
runId: attemptRunId,
providerThreadId: parentProviderThread.id,
status: "completed" as const,
},
turn: {
id: ProviderTurnId.make(`side-chat-parent-turn-${ordinal}`),
providerThreadId: parentProviderThread.id,
nodeId: rootNodeId,
runAttemptId: RunAttemptId.make(`side-chat-parent-attempt-${ordinal}`),
nativeTurnRef: {
driver: providerThread.driver,
nativeId: `side-chat-parent-native-turn-${ordinal}`,
strength: "strong" as const,
},
ordinal,
status: "completed" as const,
startedAt: now,
completedAt: now,
},
});
const parentRolledBackRunId = RunId.make("side-chat-parent-rolled-back-run");
const parentTurns = [
parentProviderTurn(1, parentRunId),
parentProviderTurn(2, parentRolledBackRunId),
];
const parentRecords = {
...projection,
thread: { ...projection.thread, id: parentThreadId },
Expand All @@ -498,26 +527,36 @@ function makeLocalCommandHarness(input: {
threadId: parentThreadId,
ordinal: 1,
providerThreadId: parentProviderThread.id,
activeAttemptId: null,
activeAttemptId: parentTurns[0]!.attempt.id,
status: "completed" as const,
completedAt: now,
},
{
...run,
id: parentRolledBackRunId,
threadId: parentThreadId,
ordinal: 2,
providerThreadId: parentProviderThread.id,
activeAttemptId: parentTurns[1]!.attempt.id,
status: "rolled_back" as const,
completedAt: now,
},
...(input.sideChat?.nativeForkFrom === "busy"
? [
{
...run,
id: RunId.make("side-chat-parent-busy-run"),
threadId: parentThreadId,
ordinal: 2,
ordinal: 3,
providerThreadId: parentProviderThread.id,
status: "running" as const,
},
]
: []),
],
providerThreads: [parentProviderThread],
attempts: [],
providerTurns: [],
attempts: parentTurns.map(({ attempt }) => attempt),
providerTurns: parentTurns.map(({ turn }) => turn),
turnItems: [],
};
if (input.sideChat !== undefined) {
Expand Down Expand Up @@ -749,7 +788,9 @@ function makeLocalCommandHarness(input: {
strength: "strong" as const,
},
};
const forkThread = vi.fn(() => Effect.succeed(sideChatThread));
const forkThread = vi.fn((_fork: ProviderAdapter.ProviderAdapterV2ForkThreadInput) =>
Effect.succeed(sideChatThread),
);
const sideChatSession = {
driver: providerThread.driver,
providerSession,
Expand Down Expand Up @@ -1273,6 +1314,10 @@ effectIt.effect.each([
yield* harness.start;

expect(harness.forkThread).toHaveBeenCalledTimes(forks ? 1 : 0);
// The parent's rolled-back turn is gone from its native conversation.
expect(
harness.forkThread.mock.calls[0]?.[0].sourceProviderTurns?.map((turn) => turn.id),
).toEqual(forks ? ["side-chat-parent-turn-1"] : undefined);
expect(harness.projection().contextTransfers).toMatchObject([
{ id: "side-chat-fork", status: transferStatus },
]);
Expand Down
16 changes: 15 additions & 1 deletion apps/server/src/orchestration-v2/ProviderTurnStartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,10 +1150,24 @@ export const layer: Layer.Layer<
(candidate) => candidate.ordinal > sourceRun.ordinal && isActiveRun(candidate),
);
if (!sourceBusy) {
// Rolled-back turns are no longer in the source conversation, so they cannot bound a fork.
const rolledBackAttemptIds = new Set(
sourceProjection.attempts
.filter((attempt) =>
sourceProjection.runs.some(
(candidate) =>
candidate.id === attempt.runId && candidate.status === "rolled_back",
),
)
.map((attempt) => attempt.id),
);
return yield* loadFromProvider(
session.forkThread({
sourceProviderThread,
sourceProviderTurns: sourceProjection.providerTurns,
sourceProviderTurns: sourceProjection.providerTurns.filter(
(turn) =>
turn.runAttemptId === null || !rolledBackAttemptIds.has(turn.runAttemptId),
),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
targetThreadId: projection.thread.id,
modelSelection: run.modelSelection,
runtimePolicy: resolvedRuntimePolicy,
Expand Down
Loading