Skip to content
Source
Sign inSign up
pingdotgg/t3code

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 intomain fromrefs/pull/7143/headopened View on GitHub

Fixes #7137. Fixes #11353. This was the deeper of two alternative fixes; the smaller positional-repair one (#7142) was closed on 2026-08-28 during the backlog cleanup, with this PR kept open as the review path for incomplete Claude assistant text. Rebased onto current main on 2026-09-03; the diff is the adapter and its tests only.

What changed

ClaudeAdapter tracked assistant text blocks in one turn-long list and matched snapshot text to blocks by list position. A claude/assistant snapshot describes one message, not the whole turn, so any turn with more than one assistant message could mismatch.

The adapter now reads message_start — previously ignored entirely — and keys every text block by its message id plus the block index within that message. Snapshot backfill resolves blocks of its own message only. A block whose stream was cut mid-message is completed from the snapshot when the snapshot text extends the streamed prefix; a snapshot that contradicts already-shown text is never written over it (warning logged instead).

Why this fixes four defects with one root cause

  • A stalled stream truncates the message to its first delta — the incident in #7137: one text_delta (P), 3m16s of silence, no content_block_stop, full 2.4 KB snapshot at the end. Positional matching wrote the snapshot onto an earlier completed block and the real block was force-completed empty.
  • Multi-text-block messages (text → tool call → text) could backfill the wrong block, since position-in-turn and position-in-message disagree.
  • Two consecutive messages merged into one when the second message restarted block indexes at zero while the first was still open.
  • A subagent's content_block_stop closed the parent's open block, truncating the parent message mid-stream.

Worth noting for review: #6429 (thinking blocks in timelines, now closed) copied the positional scheme into a new backfillReasoningBlocksFromSnapshot. Any future reasoning-block backfill should build on the message-scoped base from this PR instead of inheriting the same truncation bug.

Tests

  • Four new regression tests, one per defect above, each replaying the SDK event sequence; all four fail without the fix and pass with it.
  • Full server suite: 230 files passed, 2510 tests passed, 7 skipped, on top of current main.

Diff is +147/−40 source; the rest is tests.

🤖 Generated with Claude Code


Note

Medium Risk Changes live streaming and snapshot reconciliation for Claude assistant output—user-visible message text—with nuanced id-less and subagent paths, though behavior is heavily covered by new regression tests.

Overview Fixes incomplete or merged Claude assistant text by scoping text blocks to each API message (message_start → currentAssistantMessageId) plus content-block index, instead of matching snapshots to turn-wide block order.

Snapshot backfill now finds the right block for that message (with fallbacks when ids are missing), materializes snapshot-only messages without stealing completed blocks, and on completion emits only the unseen suffix of snapshot text relative to deltas already sent (streamedText). Subagent stream events no longer overwrite the parent message id or close the parent’s text block on content_block_stop.

Adds integration tests for stalled-stream repair, multi-block messages, reused block indexes across messages, subagent isolation, and id-less / snapshot-only edge cases.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 27530a12fc4140702f77e3e7b3f4356b0f985c2e. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Fix ClaudeAdapter to key assistant text blocks by message id instead of stream position

  • Replaces the turn-wide synthetic negative block-index counter with the assistant API message identifier captured from message-start events, so blocks from different messages with the same content index stay separate
  • Snapshot completion now compares snapshot fallback text against accumulated streamed text and emits only the missing suffix instead of replaying the already-delivered prefix
  • Subagent content_block_stop frames carrying a parent tool-use identifier can no longer close a parent assistant text block; only the parent's own stop frame completes it
  • Id-less streamed blocks fall back to compatible-block reuse, but reject a second snapshot for an already-snapshotted block and synthesize new runtime items for unmatched snapshot fragments
  • Risk: ensureAssistantTextBlock and completeAssistantTextBlock in ClaudeAdapter.ts change block lookup and completion semantics; existing consumers relying on synthetic negative indexes or full-prefix replay will break
Macroscope summarized 27530a1.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 7622e3ef-7768-483a-9103-5b59798ba62c

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      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
      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Not approved

      Macroscope's review found this PR not approvable — This changes production Claude streaming and snapshot reconciliation, including message grouping, suffix repair, and completion behavior across several stateful edge cases. The scope is focused and well tested, but the runtime semantics are substantial enough to warrant human review.

      Notes:

      • Diff unchanged. Approvability was decided on eligibility alone.
      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. I traced a production incident against T3 0.0.33 and it appears to be a direct real-world instance of the snapshot-only regression covered by this PR.

      The turn had many previously completed assistant messages. Its final message did:

      message_start
      content_block_start(index=0, type=text)
      [~27s, no text_delta/content_block_stop/message_stop]
      claude/assistant   # complete 1666-char snapshot
      claude/result/success  # same 1666 chars

      The complete text is present in T3's raw provider event log, but the completed turn has no assistant message linked in the local state.sqlite projection.

      Following the current 0.0.33 adapter logic, snapshot text position 0 can resolve against the turn-wide assistantTextBlockOrder[0], which in this turn belongs to an earlier already-completed assistant message. That is consistent with the final snapshot being swallowed instead of producing the final assistant item.

      The regression case described here as a snapshot-only second message — specifically avoiding claiming an already-completed id-less block — matches this incident very closely. The message-scoped matching in #7143 would prevent the completed earlier block from claiming this snapshot and should either find the actual pending block or synthesize the snapshot-only message.

      I found a second highly similar incident as well (79-char final snapshot after a long tool-heavy turn), although I did not re-verify every block-index/canonical-event detail of that one as exhaustively as the first.

      This may be useful as confirmation that the snapshot-only regression is occurring in real T3 0.0.33 sessions, not only in the synthetic regression test.

    5. Rebased onto current main (b3ead4b). Conflict was only in ClaudeTurnState: kept main's new latestAssistantUsage / compactedSinceLatestAssistantUsage fields alongside currentAssistantMessageId, and dropped nextSyntheticAssistantBlockIndex as this PR already did. The stray pnpm-lock.yaml hunk (two deprecated: annotations from a local install) is gone; the diff is now the adapter and its tests only. ClaudeAdapter.test.ts: 83/83, server typecheck clean.

    6. Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

      Fix All in Cursor ❌ 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 higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.

      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit b3ead4ba3aa50f039a373d11a0df77ef19d4abe2. Configure [here](https://www.cursor.com/dashboard/bugbot).
    7. Independent corroboration of the design direction: a minimal local backport for the snapshot-only stall converged on the same predicate shape — prefer a claimable positional block, else reverse-search for a claimable one, else synthesize — deliberately without the wider message-id identity fields.

      We also keep an offline regression harness that reproduces the swallow on legacy bytes (snapshot position resolving into an unrelated already-completed block) and passes on the canonical predicate. Happy to share those cases if they help review.

    8. Rechecked this against current main (d2c9281b). I would keep this PR as the canonical review path for #11353 rather than open a competing positional fix. The architecture still looks right: assistant block identity belongs to the provider message + block index, and subagent traffic must not mutate the parent message's open block.

      The immediate blocker is freshness: this head (27530a12) is now non-mergeable and its merge base is 5b8445b7; current main has advanced ~1,392 commits from that base. On rebase I would preserve current-main Claude task/usage/lifecycle work rather than resolving toward the old adapter wholesale.

      Post-rebase merge gate I would use:

      • exact #11353 text → tool_use → text: both text blocks remain visible/in order;
      • stalled/id-less stream snapshot repair still emits only the unseen suffix;
      • two messages reusing block index 0 stay distinct;
      • subagent content_block_stop cannot close the parent's block;
      • snapshot-only second message is synthesized rather than claimed by an earlier completed/open id-less block;
      • full ClaudeAdapter.test.ts + server typecheck on current main.

      The existing regressions already cover most of this, so this should be a rebase-and-reprove job, not a redesign.

    9. juliusmarminge added labelsmacroscope-review

    10. Thanks for working on this. We merged the orchestrator V2 rewrite in #2829, and we are closing this PR as part of that transition.

      This change touches ClaudeAdapter.ts, which the V2 merge removed. Claude execution now runs through ClaudeAdapterV2.

      Sorry for the extra work this creates. If the change is still needed on V2, please rebuild it on current main, verify it there, and open a new PR linking back here. We're closing the current implementation without assuming the underlying request is resolved.

    11. juliusmarminge closed this

Sign in to join this conversation.