fix(server): backfill assistant text from the snapshot when a stalled stream never closes its block #7142
main fromrefs/pull/7142/head·opened View on GitHubOne of two alternative fixes for #7137 — this is the small one. The deeper alternative is #7143: it keys blocks by message id instead of repairing the positional matching, and fixes three adjacent defects along the way. Pick one, they are not meant to be merged together.
What changed#
ClaudeAdapter assembles assistant text from stream deltas and uses the claude/assistant snapshot as a safety net when deltas go missing. Two defects made that net miss:
backfillAssistantTextBlocksFromSnapshotaligned snapshot text blocks against the turn-long block list by position from the start. A snapshot describes one message, but the list accumulates across the whole turn — so in any turn with an earlier text message, the snapshot text landed on the wrong, already-completed block.- The backfill only filled a block whose accumulated text was empty, and only completed one whose stream was already closed. A stream that dies mid-message satisfies neither.
This PR aligns snapshot blocks to the tail of the block list (last N open blocks, N = text blocks in the snapshot), and lets the backfill extend an open block when the snapshot text extends the streamed prefix. If the snapshot does not extend what was already shown to the user, nothing is overwritten and a warning is logged.
Why#
Hit organically (full event log in #7137): a stream emitted one text_delta — the letter P — then went silent for 3m16s and never sent content_block_stop. The finished 2.4 KB message arrived as a single snapshot. The snapshot text was written onto the wrong block, the dangling block was force-completed empty, and the message was persisted as P. Unrecoverable by restart: the event store never contained the text.
Tests#
- New regression test replays the exact incident event sequence; it fails without the fix (message persisted as
P) and passes with it. - Full server suite: 230 files passed, 2507 tests passed, 7 skipped, on top of current main.
🤖 Generated with Claude Code
Note
Fix stalled assistant stream repair to backfill only the unstreamed suffix from the snapshot#
AssistantTextBlockStatereplaces theemittedTextDeltaboolean with astreamedTextstring that accumulates all text already sent to the client, enabling suffix-only repairs.completeAssistantTextBlocknow emits only the missing suffix (snapshot text minus already-streamed prefix) instead of re-sending the full fallback text, preventing duplicate deltas.- The assistant snapshot handler aligns snapshot text blocks to open turn blocks by streamed prefix match rather than tail position, avoiding mis-assignment when stalls occur before later blocks arrive.
- Never-streamed paragraphs in a snapshot are synthesized as new blocks with distinct item IDs.
- Behavioral Change: blocks that stalled mid-stream now receive a partial delta repair rather than a full-text retransmission.
Note
Medium Risk Changes assistant text projection and persistence in the Claude adapter—a user-visible runtime path—but the behavior is narrowly scoped to stalled-stream repair with dedicated regression tests.
Overview
Fixes lost or truncated assistant messages when the Claude stream stalls mid-text and the finished content arrives only as a claude/assistant snapshot.
ClaudeAdapter now tracks streamedText per text block (replacing a boolean “had a delta” flag) and, on completion/backfill, emits content.delta only for the suffix the client has not already seen—so repairs do not duplicate partial stream output.
Snapshot blocks are aligned to the correct assistant message in a multi-message turn by choosing a shift into the turn-wide block list that best matches open blocks whose streamed prefixes extend the snapshot text, instead of matching from the head of the list. Undelivered snapshot paragraphs can be synthesized as new blocks; fallbackText is treated as authoritative for open blocks.
Tests add regressions for a stalled second message in a turn and for multi-block snapshots when streaming dies before later paragraphs start.
Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c0d7a59634d7c19b522daf3e7e18a15db1258e51. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot).
Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.
Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit de2a8e7659c0bf3beffde3dd756b37431b9dc091. Configure [here](https://www.cursor.com/dashboard/bugbot).apps/server/src/provider/Layers/ClaudeAdapter.ts line 2000Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1948,4 +1973,4 @@19481973 return created;19491974 }),19501975 ));1951- if (!entry) {1976+ if (!entry || entry.block.completionEmitted) {Tail match can bind earlier blocks#
Medium Severity
Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit de2a8e7659c0bf3beffde3dd756b37431b9dc091. Configure [here](https://www.cursor.com/dashboard/bugbot).tailOffsettreats the snapshot’s N texts as the last N entries in the turn-wideassistantTextBlockOrder. If the current message stalled before every snapshot text block was opened, that window includes already-completed earlier-message blocks. Those texts are then skipped viacompletionEmittedinstead of being synthesized, and later snapshot texts zip onto the wrong open block. A follow-upclaude/assistantsnapshot with multiple text parts after an earlier completed message can therefore leave the stalled item unrepaired or attach the wrong paragraph.Real find, fixed in c0d7a596. The failing case: stream dies before every snapshot block was opened AND the turn holds completed blocks of earlier messages — the last-N window then swallows those and each paragraph lands one block off.
Instead of arithmetic tail alignment, the backfill now picks the shift by content: every bound open block's streamed prefix must match its snapshot text, and the shift binding the most open blocks wins. Ties keep the first such shift — preferring a later one would slide past a fully-completed message whose snapshot arrives after the fact and re-synthesize it as a duplicate.
Added a regression test replaying exactly this scenario (earlier completed message + stall before the second text block of a two-block snapshot); it times out without the fix and passes with it.
Approvability#
Verdict: Needs human review
This bug fix modifies the core streaming path with a moderately complex alignment algorithm for matching snapshot blocks to partially-streamed content. Changes affect real-time text delivery to clients and warrant verification from someone familiar with the Claude adapter's streaming internals.
You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).Note
🤖 GPT-5.6 Sol responding on behalf of Theo
We're closing this PR as we clean up the T3 Code backlog. Thank you for taking the time to put this together.
We are keeping OPEN #7143 as the review path for incomplete Claude assistant text. This branch's snapshot-backfill approach remains useful reference.
If you believe we closed this in error, please reopen the PR and leave a comment explaining what we missed.
t3dotgg(GitHub profile, opens in a new tab) 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:
a225404a-50c1-4e6f-bd97-2196434e1edeYou 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