The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 05:45 UTC
fix(server): native forks after an undo no longer copy later turns (#29902)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Alex Southwell committed
Nov 3, 2026 at 19:18 UTC
3f48896580eb404ae72b5f02e0af61bf16b69adf
4 files changed
+81
-7
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+14
@@ -6350,9 +6350,15 @@ describe("ClaudeAdapterV2 background wake turns", () => {
6350
6351
it.effect("never records a subagent uuid for a turn with no main-thread assistant message", () =>
6352
Effect.gen(function* () {
6353
+ const forkCalls: Array<ClaudeAdapterV2.ClaudeAgentSdkSessionForkInput> = [];
6354
const harness = yield* makeWakeHarnessWithOptions({
6355
close: (sdkMessages) => Queue.shutdown(sdkMessages),
6356
freshQueueOnReopen: true,
6357
+ forkSession: (input) =>
6358
+ Effect.sync(() => {
6359
+ forkCalls.push(input);
6360
+ return { sessionId: "forked-after-compact-rollback" };
6361
+ }),
6362
});
6363
const now = yield* DateTime.now;
6364
const subagentUuid = "00000000-0000-4000-8000-000000000644";
@@ -6433,6 +6439,14 @@ describe("ClaudeAdapterV2 background wake turns", () => {
6439
pastSilentTurn.providerThread.nativeConversationHeadRef?.nativeId,
6440
earlierCursor,
6441
);
6442
+ // Forks omit rolled-back turns, which Claude's transcript still holds past the pinned head.
6443
+ yield* harness.runtime.forkThread({
6444
+ sourceProviderThread: pastSilentTurn.providerThread,
6445
+ sourceProviderTurns: turnsAcrossSilent.slice(0, 2),
6446
+ providerTurnId: providerTurn.id,
6447
+ targetThreadId: ThreadId.make("thread-claude-fork-after-compact-rollback"),
6448
+ });
6449
+ assert.equal(forkCalls[0]?.options.upToMessageId, earlierCursor);
6450
const interruptedBeforeCompact = yield* rollbackTo(
6451
[
6452
{
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+2
-1
@@ -2070,7 +2070,8 @@ const resolveClaudeForkUpToMessageId = Effect.fn("ClaudeAdapterV2.resolveForkUpT
2070
.slice(boundaryIndex + 1)
2071
.filter(isTerminalProviderTurn);
2072
if (terminalTurnsAfterBoundary.length === 0) {
2073
- return undefined;
2073
+ // Rollback pins the head but leaves the rolled-back turns in Claude's transcript.
2074
+ return yield* getNativeConversationHeadId(input.sourceProviderThread);
2075
}
2076
2077
return yield* new ProviderAdapter.ProviderAdapterForkThreadError({
apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts
+50
-5
@@ -488,6 +488,35 @@ function makeLocalCommandHarness(input: {
488
strength: "strong" as const,
489
},
490
};
491
+ const parentProviderTurn = (ordinal: number, attemptRunId: RunId) => ({
492
+ attempt: {
493
+ ...projection.attempts[0]!,
494
+ id: RunAttemptId.make(`side-chat-parent-attempt-${ordinal}`),
495
+ runId: attemptRunId,
496
+ providerThreadId: parentProviderThread.id,
497
+ status: "completed" as const,
498
+ },
499
+ turn: {
500
+ id: ProviderTurnId.make(`side-chat-parent-turn-${ordinal}`),
501
+ providerThreadId: parentProviderThread.id,
502
+ nodeId: rootNodeId,
503
+ runAttemptId: RunAttemptId.make(`side-chat-parent-attempt-${ordinal}`),
504
+ nativeTurnRef: {
505
+ driver: providerThread.driver,
506
+ nativeId: `side-chat-parent-native-turn-${ordinal}`,
507
+ strength: "strong" as const,
508
+ },
509
+ ordinal,
510
+ status: "completed" as const,
511
+ startedAt: now,
512
+ completedAt: now,
513
+ },
514
+ });
515
+ const parentRolledBackRunId = RunId.make("side-chat-parent-rolled-back-run");
516
+ const parentTurns = [
517
+ parentProviderTurn(1, parentRunId),
518
+ parentProviderTurn(2, parentRolledBackRunId),
519
+ ];
520
const parentRecords = {
521
...projection,
522
thread: { ...projection.thread, id: parentThreadId },
@@ -498,17 +527,27 @@ function makeLocalCommandHarness(input: {
527
threadId: parentThreadId,
528
ordinal: 1,
529
providerThreadId: parentProviderThread.id,
501
- activeAttemptId: null,
530
+ activeAttemptId: parentTurns[0]!.attempt.id,
531
status: "completed" as const,
532
completedAt: now,
533
},
534
+ {
535
+ ...run,
536
+ id: parentRolledBackRunId,
537
+ threadId: parentThreadId,
538
+ ordinal: 2,
539
+ providerThreadId: parentProviderThread.id,
540
+ activeAttemptId: parentTurns[1]!.attempt.id,
541
+ status: "rolled_back" as const,
542
+ completedAt: now,
543
+ },
544
...(input.sideChat?.nativeForkFrom === "busy"
545
? [
546
{
547
...run,
548
id: RunId.make("side-chat-parent-busy-run"),
549
threadId: parentThreadId,
511
- ordinal: 2,
550
+ ordinal: 3,
551
providerThreadId: parentProviderThread.id,
552
status: "running" as const,
553
},
@@ -516,8 +555,8 @@ function makeLocalCommandHarness(input: {
555
: []),
556
],
557
providerThreads: [parentProviderThread],
519
- attempts: [],
520
- providerTurns: [],
558
+ attempts: parentTurns.map(({ attempt }) => attempt),
559
+ providerTurns: parentTurns.map(({ turn }) => turn),
560
turnItems: [],
561
};
562
if (input.sideChat !== undefined) {
@@ -749,7 +788,9 @@ function makeLocalCommandHarness(input: {
788
strength: "strong" as const,
789
},
790
};
752
- const forkThread = vi.fn(() => Effect.succeed(sideChatThread));
791
+ const forkThread = vi.fn((_fork: ProviderAdapter.ProviderAdapterV2ForkThreadInput) =>
792
+ Effect.succeed(sideChatThread),
793
+ );
794
const sideChatSession = {
795
driver: providerThread.driver,
796
providerSession,
@@ -1273,6 +1314,10 @@ effectIt.effect.each([
1314
yield* harness.start;
1315
1316
expect(harness.forkThread).toHaveBeenCalledTimes(forks ? 1 : 0);
1317
+ // The parent's rolled-back turn is gone from its native conversation.
1318
+ expect(
1319
+ harness.forkThread.mock.calls[0]?.[0].sourceProviderTurns?.map((turn) => turn.id),
1320
+ ).toEqual(forks ? ["side-chat-parent-turn-1"] : undefined);
1321
expect(harness.projection().contextTransfers).toMatchObject([
1322
{ id: "side-chat-fork", status: transferStatus },
1323
]);
apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+15
-1
@@ -1150,10 +1150,24 @@ export const layer: Layer.Layer<
1150
(candidate) => candidate.ordinal > sourceRun.ordinal && isActiveRun(candidate),
1151
);
1152
if (!sourceBusy) {
1153
+ // Rolled-back turns are no longer in the source conversation, so they cannot bound a fork.
1154
+ const rolledBackAttemptIds = new Set(
1155
+ sourceProjection.attempts
1156
+ .filter((attempt) =>
1157
+ sourceProjection.runs.some(
1158
+ (candidate) =>
1159
+ candidate.id === attempt.runId && candidate.status === "rolled_back",
1160
+ ),
1161
+ )
1162
+ .map((attempt) => attempt.id),
1163
+ );
1164
return yield* loadFromProvider(
1165
session.forkThread({
1166
sourceProviderThread,
1156
- sourceProviderTurns: sourceProjection.providerTurns,
1167
+ sourceProviderTurns: sourceProjection.providerTurns.filter(
1168
+ (turn) =>
1169
+ turn.runAttemptId === null || !rolledBackAttemptIds.has(turn.runAttemptId),
1170
+ ),
1171
targetThreadId: projection.thread.id,
1172
modelSelection: run.modelSelection,
1173
runtimePolicy: resolvedRuntimePolicy,