fix(server): key assistant text blocks by message id instead of stream position #7143
ClosedKiri110K(GitHub profile, opens in a new tab)wants to merge 3 commits into
main fromrefs/pull/7143/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/provider/Layers/ClaudeAdapter.ts lines 2009 to 2010OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1946,6 +1996,15 @@1946- }1947- orderedBlocks.push(created);1948- return created;1949- }),1950- ));1951- if (!entry) {1996+ for (const { contentIndex, text } of snapshotTextBlocks) {1997+ // Match on the identity the data actually carries: the API message plus the position of1998+ // the block inside it. Matching by position in the turn-wide block list used to pour a1999+ // snapshot's text into blocks of *earlier* messages of the same turn, so the message2000+ // whose stream broke was never repaired. When either side has no message id (no2001+ // `message_start` was seen) fall back to the most recent block at that index.2002+ const candidates = turnState.assistantTextBlockOrder.filter(2003+ (block) => block.blockIndex === contentIndex,2004+ );2005+ const existing =2006+ candidates.findLast(2007+ (block) => block.messageId !== undefined && block.messageId === snapshotMessageId,2008+ ) ??2009+ candidates.findLast(2010+ (block) => block.messageId === undefined || snapshotMessageId === undefined,
- apps/server/src/provider/Layers/ClaudeAdapter.ts line 1964Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1895,12 +1940,17 @@1940+ * of the same snapshot finds the block again instead of emitting the text twice.1941+ */18951942 const createSyntheticAssistantTextBlock = Effect.fn("createSyntheticAssistantTextBlock")(1896- function* (context: ClaudeSessionContext, fallbackText: string) {1943+ function* (1944+ context: ClaudeSessionContext,1945+ fallbackText: string,1946+ location: { readonly contentIndex: number; readonly messageId: string | undefined },1947+ ) {18971948 const turnState = context.turnState;18981949 if (!turnState) {18991950 return undefined;19001951 }190119521902- const blockIndex = turnState.nextSyntheticAssistantBlockIndex;1903- turnState.nextSyntheticAssistantBlockIndex -= 1;1904- return yield* ensureAssistantTextBlock(context, blockIndex, {1953+ return yield* ensureAssistantTextBlock(context, location.contentIndex, {19051954 fallbackText,19061955 streamClosed: true,1956+ messageId: location.messageId,apps/server/src/provider/Layers/ClaudeAdapter.ts line 2128Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2021,5 +2096,19 @@2096+ }))?.block;2097+2098+ // Already delivered — the ordinary stream → `content_block_stop` → snapshot order. Its2099+ // text came from the deltas and re-emitting anything here would duplicate it.2100+ if (!block || block.completionEmitted) {20212101 continue;20222102 }202321032024- if (entry.block.fallbackText.length === 0) {2025- entry.block.fallbackText = text;2104+ if (!text.startsWith(block.streamedText)) {2105+ // The client has already seen text this snapshot does not continue. Appending would2106+ // corrupt the message, and there is no way to retract a delta, so leave it alone.2107+ yield* Effect.logWarning("claude.assistant.snapshot-diverged", {2108+ threadId: context.session.threadId,2109+ messageId: snapshotMessageId,2110+ contentIndex,2111+ streamedLength: block.streamedText.length,2112+ snapshotLength: text.length,2113+ });2114+ continue;
juliusmarminge added labelsmacroscope-review
juliusmarminge closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
7622e3ef-7768-483a-9103-5b59798ba62cYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share