Skip to content

Commit 0c6c721

Browse files
SparshKaushikclaudemaria-rcks
authored
fix(server): Claude threads revert to the message after a /compact (#27381)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 41588d4 commit 0c6c721

4 files changed

Lines changed: 106 additions & 11 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6103,14 +6103,19 @@ describe("ClaudeAdapterV2 background wake turns", () => {
61036103

61046104
const providerTurn = lastProviderTurn(harness.events);
61056105
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+
) =>
61076111
harness.runtime.rollbackThread({
61086112
providerThread: harness.providerThread,
61096113
target: {
61106114
type: "provider_turn",
61116115
checkpointId: CheckpointId.make("checkpoint-claude-cursor-subagent-only"),
61126116
appRunOrdinal: 1,
6113-
providerTurn,
6117+
providerTurn: targetTurn,
6118+
message: { role: "user", text, attachments: [] },
61146119
},
61156120
providerThreadTurns,
61166121
});
@@ -6127,6 +6132,64 @@ describe("ClaudeAdapterV2 background wake turns", () => {
61276132
]).pipe(Effect.flip);
61286133
assert.instanceOf(error, ProviderAdapter.ProviderAdapterRollbackThreadError);
61296134
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+
);
61306193

61316194
yield* harness.runtime.startTurn(
61326195
makeClaudeTestTurnInput({

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,16 @@ const getNativeThreadId = Effect.fnUntraced(function* (
19331933

19341934
const isSyntheticClaudeTurnId = (nativeTurnId: string): boolean => nativeTurnId.startsWith("turn:");
19351935

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+
19361946
const isTerminalProviderTurn = (turn: OrchestrationV2ProviderTurn): boolean =>
19371947
turn.status === "completed" ||
19381948
turn.status === "interrupted" ||
@@ -2019,14 +2029,9 @@ const resolveClaudeRollbackResumeSessionAt = Effect.fn(
20192029
});
20202030
}
20212031

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;
20302035
}
20312036

20322037
const providerTurnsAfterTarget = input.providerThreadTurns.filter(
@@ -2036,6 +2041,18 @@ const resolveClaudeRollbackResumeSessionAt = Effect.fn(
20362041
return null;
20372042
}
20382043

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+
20392056
return yield* new ProviderAdapter.ProviderAdapterRollbackThreadError({
20402057
driver: CLAUDE_PROVIDER,
20412058
providerThreadId: input.providerThread.id,

‎apps/server/src/orchestration-v2/CheckpointRollbackService.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,33 @@ export const layer: Layer.Layer<
185185
const targetTurn =
186186
latestProviderTurnForAttempt(context.providerTurns, targetAttempt?.id) ??
187187
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+
) {
189193
return yield* new CheckpointRollbackExecutionError({
190194
reason: "provider-turn-unavailable",
191195
threadId: input.threadId,
192196
providerThreadId: input.providerThreadId,
193197
checkpointId: input.checkpointId,
194198
});
195199
}
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);
196209
return {
197210
type: "provider_turn" as const,
198211
checkpointId: checkpoint.id,
199212
appRunOrdinal: targetOrdinal,
200213
providerTurn: targetTurn,
214+
...(message === undefined ? {} : { message }),
201215
};
202216
});
203217

‎packages/provider-core/src/server/ProviderAdapter.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ export type ProviderAdapterV2RollbackTarget =
504504
readonly checkpointId: CheckpointId;
505505
readonly appRunOrdinal: number;
506506
readonly providerTurn: OrchestrationV2ProviderTurn;
507+
readonly message?: Pick<OrchestrationV2ConversationMessage, "role" | "text" | "attachments">;
507508
};
508509

509510
export interface ProviderAdapterV2RollbackThreadInput {

0 commit comments

Comments
 (0)