@devries-cameron20 / t3code-1 / commits / 0c6c721aea
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): Claude threads revert to the message after a /compact (#27381)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Sparsh Kaushik committed Nov 2, 2026 at 03:13 UTC 0c6c721aea4ab66062da54d340dad20201dc8df2
4 files changed +106 -11
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+65 -2
@@ -6103,14 +6103,19 @@ describe("ClaudeAdapterV2 background wake turns", () => {
6103
6104 const providerTurn = lastProviderTurn(harness.events);
6105 assert.notEqual(providerTurn.nativeTurnRef?.nativeId, subagentUuid);
6106 - const rollbackTo = (providerThreadTurns: ReadonlyArray<typeof providerTurn>) =>
6106 + const rollbackTo = (
6107 + providerThreadTurns: ReadonlyArray<typeof providerTurn>,
6108 + text = "Background task completed.",
6109 + targetTurn = providerTurn,
6110 + ) =>
6111 harness.runtime.rollbackThread({
6112 providerThread: harness.providerThread,
6113 target: {
6114 type: "provider_turn",
6115 checkpointId: CheckpointId.make("checkpoint-claude-cursor-subagent-only"),
6116 appRunOrdinal: 1,
6113 - providerTurn,
6117 + providerTurn: targetTurn,
6118 + message: { role: "user", text, attachments: [] },
6119 },
6120 providerThreadTurns,
6121 });
@@ -6127,6 +6132,64 @@ describe("ClaudeAdapterV2 background wake turns", () => {
6132 ]).pipe(Effect.flip);
6133 assert.instanceOf(error, ProviderAdapter.ProviderAdapterRollbackThreadError);
6134 assert.include(String(error.cause), "no SDK assistant message cursor");
6135 + const earlierCursor = "00000000-0000-4000-8000-000000000648";
6136 + const turnsAcrossSilent = [
6137 + {
6138 + ...providerTurn,
6139 + id: ProviderTurnId.make("provider-turn-claude-cursor-earlier"),
6140 + ordinal: providerTurn.ordinal - 1,
6141 + nativeTurnRef: {
6142 + driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
6143 + nativeId: earlierCursor,
6144 + strength: "weak",
6145 + },
6146 + },
6147 + providerTurn,
6148 + {
6149 + ...providerTurn,
6150 + id: ProviderTurnId.make("provider-turn-claude-cursor-next"),
6151 + ordinal: providerTurn.ordinal + 1,
6152 + },
6153 + ] satisfies ReadonlyArray<typeof providerTurn>;
6154 + const semanticError = yield* rollbackTo(turnsAcrossSilent).pipe(Effect.flip);
6155 + assert.instanceOf(semanticError, ProviderAdapter.ProviderAdapterRollbackThreadError);
6156 + const pastSilentTurn = yield* rollbackTo(turnsAcrossSilent, "/compact");
6157 + assert.equal(
6158 + pastSilentTurn.providerThread.nativeConversationHeadRef?.nativeId,
6159 + earlierCursor,
6160 + );
6161 + const interruptedBeforeCompact = yield* rollbackTo(
6162 + [
6163 + {
6164 + ...providerTurn,
6165 + id: ProviderTurnId.make("provider-turn-claude-cursor-earlier"),
6166 + ordinal: 1,
6167 + nativeTurnRef: {
6168 + driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
6169 + nativeId: earlierCursor,
6170 + strength: "weak",
6171 + },
6172 + },
6173 + {
6174 + ...providerTurn,
6175 + id: ProviderTurnId.make("provider-turn-claude-interrupted-before-compact"),
6176 + ordinal: 2,
6177 + status: "interrupted",
6178 + },
6179 + { ...providerTurn, ordinal: 3 },
6180 + {
6181 + ...providerTurn,
6182 + id: ProviderTurnId.make("provider-turn-claude-cursor-next"),
6183 + ordinal: 4,
6184 + },
6185 + ],
6186 + "/compact",
6187 + { ...providerTurn, ordinal: 3 },
6188 + ).pipe(Effect.flip);
6189 + assert.instanceOf(
6190 + interruptedBeforeCompact,
6191 + ProviderAdapter.ProviderAdapterRollbackThreadError,
6192 + );
6193
6194 yield* harness.runtime.startTurn(
6195 makeClaudeTestTurnInput({
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+25 -8
@@ -1933,6 +1933,16 @@ const getNativeThreadId = Effect.fnUntraced(function* (
1933
1934 const isSyntheticClaudeTurnId = (nativeTurnId: string): boolean => nativeTurnId.startsWith("turn:");
1935
1936 +const claudeTurnCursor = (turn: OrchestrationV2ProviderTurn): string | null => {
1937 + const ref = turn.nativeTurnRef;
1938 + return ref !== null &&
1939 + ref.driver === CLAUDE_PROVIDER &&
1940 + ref.nativeId !== null &&
1941 + !isSyntheticClaudeTurnId(ref.nativeId)
1942 + ? ref.nativeId
1943 + : null;
1944 +};
1945 +
1946 const isTerminalProviderTurn = (turn: OrchestrationV2ProviderTurn): boolean =>
1947 turn.status === "completed" ||
1948 turn.status === "interrupted" ||
@@ -2019,14 +2029,9 @@ const resolveClaudeRollbackResumeSessionAt = Effect.fn(
2029 });
2030 }
2031
2022 - const nativeTurnRef = target.providerTurn.nativeTurnRef;
2023 - if (
2024 - nativeTurnRef !== null &&
2025 - nativeTurnRef.driver === CLAUDE_PROVIDER &&
2026 - nativeTurnRef.nativeId !== null &&
2027 - !isSyntheticClaudeTurnId(nativeTurnRef.nativeId)
2028 - ) {
2029 - return nativeTurnRef.nativeId;
2032 + const cursor = claudeTurnCursor(target.providerTurn);
2033 + if (cursor !== null) {
2034 + return cursor;
2035 }
2036
2037 const providerTurnsAfterTarget = input.providerThreadTurns.filter(
@@ -2036,6 +2041,18 @@ const resolveClaudeRollbackResumeSessionAt = Effect.fn(
2041 return null;
2042 }
2043
2044 + // Only compaction can be omitted without dropping a retained user prompt.
2045 + if (target.message !== undefined && isCompactionCommandMessage(target.message)) {
2046 + const earlierTurn = input.providerThreadTurns
2047 + .filter((turn) => turn.ordinal < target.providerTurn.ordinal)
2048 + .toSorted((left, right) => left.ordinal - right.ordinal)
2049 + .at(-1);
2050 + const earlierCursor = earlierTurn === undefined ? null : claudeTurnCursor(earlierTurn);
2051 + if (earlierCursor != null) {
2052 + return earlierCursor;
2053 + }
2054 + }
2055 +
2056 return yield* new ProviderAdapter.ProviderAdapterRollbackThreadError({
2057 driver: CLAUDE_PROVIDER,
2058 providerThreadId: input.providerThread.id,
apps/server/src/orchestration-v2/CheckpointRollbackService.ts
+15 -1
@@ -185,7 +185,11 @@ export const layer: Layer.Layer<
185 const targetTurn =
186 latestProviderTurnForAttempt(context.providerTurns, targetAttempt?.id) ??
187 context.providerTurns.find((turn) => turn.id === targetAttempt?.providerTurnId);
188 - if (targetTurn === undefined || targetTurn.providerThreadId !== providerThread.id) {
188 + if (
189 + targetAttempt === undefined ||
190 + targetTurn === undefined ||
191 + targetTurn.providerThreadId !== providerThread.id
192 + ) {
193 return yield* new CheckpointRollbackExecutionError({
194 reason: "provider-turn-unavailable",
195 threadId: input.threadId,
@@ -193,11 +197,21 @@ export const layer: Layer.Layer<
197 checkpointId: input.checkpointId,
198 });
199 }
200 + const { attempt } = yield* projections.getProviderControlContext(input.threadId, {
201 + providerThreadId: providerThread.id,
202 + providerTurnId: targetTurn.id,
203 + attemptId: targetAttempt.id,
204 + });
205 + const message =
206 + attempt === undefined
207 + ? undefined
208 + : yield* projections.getRunMessage(input.threadId, attempt.runId);
209 return {
210 type: "provider_turn" as const,
211 checkpointId: checkpoint.id,
212 appRunOrdinal: targetOrdinal,
213 providerTurn: targetTurn,
214 + ...(message === undefined ? {} : { message }),
215 };
216 });
217
packages/provider-core/src/server/ProviderAdapter.ts
+1
@@ -504,6 +504,7 @@ export type ProviderAdapterV2RollbackTarget =
504 readonly checkpointId: CheckpointId;
505 readonly appRunOrdinal: number;
506 readonly providerTurn: OrchestrationV2ProviderTurn;
507 + readonly message?: Pick<OrchestrationV2ConversationMessage, "role" | "text" | "attachments">;
508 };
509
510 export interface ProviderAdapterV2RollbackThreadInput {