Claude chat: a history read mid-reply carries the streaming block's beginning

Claude Code writes a content block to its session file only once the block
is complete. A client that read history while a block streamed (opening the
chat mid-reply, a reconnect, a resync) got only the deltas after its read,
and nothing re-sent the block's beginning, so the reply showed with its first
part cut off. Deltas are now numbered (seq) and their text retained for the
newest assistant messages; the newest history page adds every retained part
the transcript lacks and reports the last included seq (live_seq) and the
overlaid parts (live_parts).

History now numbers empty text/thinking blocks as the live stream does, so a
thinking block after an empty display-omitted one keeps one part id live and
in history. Text a content_block_start carries is published, not dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 15:41:57 +00:00
parent 1772100af2
commit ea25cf504f
6 changed files with 408 additions and 29 deletions
+44 -20
View File
@@ -244,7 +244,15 @@ func (s *session) handleStreamEvent(envelope lineEnvelope) {
}
ordinal := s.streamOrdinals[kind]
s.streamOrdinals[kind] = ordinal + 1
s.streamBlocks[event.Index] = blockState{kind: kind, ordinal: ordinal}
block := blockState{kind: kind, ordinal: ordinal}
s.streamBlocks[event.Index] = block
// The API starts a block empty, but text a block starts with is the
// beginning of the answer, never something to drop.
initial := event.ContentBlock.Text
if kind == protocol.ChatPartReasoning {
initial = event.ContentBlock.Thinking
}
s.streamDeltaLocked(event.Index, block, initial)
case "content_block_delta":
if event.Delta == nil {
return
@@ -275,23 +283,7 @@ func (s *session) handleStreamEvent(envelope lineEnvelope) {
default:
return
}
if delta == "" {
return
}
s.streamText[event.Index] += delta
messageID := s.streamID
if messageID == "" {
return
}
partID := protocol.ChatDeltaPartID(messageID, block.kind, block.ordinal)
s.broadcastLocked(protocol.ChatEventFrame{
SessionID: s.id,
NativeTurnID: s.turnID,
MessageID: messageID,
Kind: block.kind,
Delta: delta,
Part: &protocol.ChatPart{ID: partID, Type: block.kind},
})
s.streamDeltaLocked(event.Index, block, delta)
case "message_stop":
// Keep the assembled blocks: the CLI sends the completed assistant
// message after message_stop, and that message must not repeat text
@@ -299,6 +291,29 @@ func (s *session) handleStreamEvent(envelope lineEnvelope) {
}
}
// streamDeltaLocked assembles one streamed piece of a block and publishes it,
// numbered and retained for history reads (see live.go).
func (s *session) streamDeltaLocked(index int, block blockState, delta string) {
if delta == "" {
return
}
s.streamText[index] += delta
messageID := s.streamID
if messageID == "" {
return
}
frame := protocol.ChatEventFrame{
SessionID: s.id,
NativeTurnID: s.turnID,
MessageID: messageID,
Kind: block.kind,
Delta: delta,
Part: &protocol.ChatPart{ID: protocol.ChatDeltaPartID(messageID, block.kind, block.ordinal), Type: block.kind},
}
s.recordLiveLocked(&frame)
s.broadcastLocked(frame)
}
func streamKind(blockType string) string {
switch blockType {
case "text":
@@ -415,6 +430,7 @@ func (s *session) handleAssistant(envelope lineEnvelope) {
}
// Align the completed message with what was streamed, then emit only the
// remainder: the CLI sends deltas first and the full block afterwards.
streamedMessage := len(s.streamBlocks) > 0
var frames []protocol.ChatEventFrame
for index, block := range blocks {
switch block.Type {
@@ -426,6 +442,12 @@ func (s *session) handleAssistant(envelope lineEnvelope) {
full = block.Thinking
}
if strings.TrimSpace(full) == "" {
// A streamed block took its ordinal when it started. An empty
// block of a message that was not streamed takes one too, as
// history counts it, so the next block's id matches history's.
if !streamedMessage {
s.streamOrdinals[kind]++
}
continue
}
state, streamed, matched := s.claimStreamedLocked(kind, full, index)
@@ -444,14 +466,16 @@ func (s *session) handleAssistant(envelope lineEnvelope) {
if delta == "" {
continue
}
frames = append(frames, protocol.ChatEventFrame{
frame := protocol.ChatEventFrame{
SessionID: s.id,
NativeTurnID: s.turnID,
MessageID: messageID,
Kind: kind,
Delta: delta,
Part: &protocol.ChatPart{ID: protocol.ChatDeltaPartID(messageID, kind, state.ordinal), Type: kind},
})
}
s.recordLiveLocked(&frame)
frames = append(frames, frame)
case "tool_use":
if block.ID == "" {
continue