Skip to content

Commit c5c86e1

Browse files
SparshKaushikclaude
andcommitted
fix(server): Claude threads revert to the message after a /compact
A Claude turn with no main-thread assistant message, such as `/compact`, records no resume cursor. Reverting to the message after one failed with "no SDK assistant message cursor was recorded for that turn", so idle compaction left one message per thread that could never be reverted to. Resume at the latest earlier turn's cursor instead. That point holds the same conversation; only the compaction is undone. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a773dc3 commit c5c86e1

2 files changed

Lines changed: 47 additions & 8 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5450,6 +5450,29 @@ describe("ClaudeAdapterV2 background wake turns", () => {
54505450
]).pipe(Effect.flip);
54515451
assert.instanceOf(error, ProviderAdapter.ProviderAdapterRollbackThreadError);
54525452
assert.include(String(error.cause), "no SDK assistant message cursor");
5453+
const earlierCursor = "00000000-0000-4000-8000-000000000648";
5454+
const pastSilentTurn = yield* rollbackTo([
5455+
{
5456+
...providerTurn,
5457+
id: ProviderTurnId.make("provider-turn-claude-cursor-earlier"),
5458+
ordinal: providerTurn.ordinal - 1,
5459+
nativeTurnRef: {
5460+
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
5461+
nativeId: earlierCursor,
5462+
strength: "weak",
5463+
},
5464+
},
5465+
providerTurn,
5466+
{
5467+
...providerTurn,
5468+
id: ProviderTurnId.make("provider-turn-claude-cursor-next"),
5469+
ordinal: providerTurn.ordinal + 1,
5470+
},
5471+
]);
5472+
assert.equal(
5473+
pastSilentTurn.providerThread.nativeConversationHeadRef?.nativeId,
5474+
earlierCursor,
5475+
);
54535476

54545477
yield* harness.runtime.startTurn(
54555478
makeClaudeTestTurnInput({

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

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

19231923
const isSyntheticClaudeTurnId = (nativeTurnId: string): boolean => nativeTurnId.startsWith("turn:");
19241924

1925+
const claudeTurnCursor = (turn: OrchestrationV2ProviderTurn): string | null => {
1926+
const ref = turn.nativeTurnRef;
1927+
return ref !== null &&
1928+
ref.driver === CLAUDE_PROVIDER &&
1929+
ref.nativeId !== null &&
1930+
!isSyntheticClaudeTurnId(ref.nativeId)
1931+
? ref.nativeId
1932+
: null;
1933+
};
1934+
19251935
const isTerminalProviderTurn = (turn: OrchestrationV2ProviderTurn): boolean =>
19261936
turn.status === "completed" ||
19271937
turn.status === "interrupted" ||
@@ -2008,14 +2018,9 @@ const resolveClaudeRollbackResumeSessionAt = Effect.fn(
20082018
});
20092019
}
20102020

2011-
const nativeTurnRef = target.providerTurn.nativeTurnRef;
2012-
if (
2013-
nativeTurnRef !== null &&
2014-
nativeTurnRef.driver === CLAUDE_PROVIDER &&
2015-
nativeTurnRef.nativeId !== null &&
2016-
!isSyntheticClaudeTurnId(nativeTurnRef.nativeId)
2017-
) {
2018-
return nativeTurnRef.nativeId;
2021+
const cursor = claudeTurnCursor(target.providerTurn);
2022+
if (cursor !== null) {
2023+
return cursor;
20192024
}
20202025

20212026
const providerTurnsAfterTarget = input.providerThreadTurns.filter(
@@ -2025,6 +2030,17 @@ const resolveClaudeRollbackResumeSessionAt = Effect.fn(
20252030
return null;
20262031
}
20272032

2033+
// Turns with no main-thread assistant message, such as `/compact`, record no cursor.
2034+
// The latest earlier cursor resumes the same conversation without that turn.
2035+
const earlierCursor = input.providerThreadTurns
2036+
.filter((turn) => turn.ordinal < target.providerTurn.ordinal)
2037+
.toSorted((left, right) => left.ordinal - right.ordinal)
2038+
.map(claudeTurnCursor)
2039+
.findLast((candidate) => candidate !== null);
2040+
if (earlierCursor != null) {
2041+
return earlierCursor;
2042+
}
2043+
20282044
return yield* new ProviderAdapter.ProviderAdapterRollbackThreadError({
20292045
driver: CLAUDE_PROVIDER,
20302046
providerThreadId: input.providerThread.id,

0 commit comments

Comments
 (0)