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
+31 -9
View File
@@ -162,6 +162,18 @@ func transcriptTime(text string) (int64, bool) {
// merges the JSONL's one-entry-per-content-block shape by message id, attaches
// tool results to the call that produced them, and never invents a turn.
func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHistoryPage, error) {
newest := strings.TrimSpace(cursor) == "" || cursor == "0"
// Taken before the file is read: a block completing in between is then
// whole in the file, never missing from both (see live.go).
overlay := s.liveOverlaySnapshot()
page, err := s.transcriptPage(limit, cursor)
if err == nil && newest {
overlay.apply(&page, s.id)
}
return page, err
}
func (s *session) transcriptPage(limit int, cursor string) (protocol.ChatHistoryPage, error) {
path := findTranscriptFile(s.id)
if path == "" {
return protocol.ChatHistoryPage{Messages: []protocol.ChatMessage{}}, nil
@@ -206,6 +218,7 @@ func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHis
messages = append(messages, earlierHistoryNotice(s.id, skipped, created))
}
messageIndex := map[string]int{}
ordinals := blockOrdinals{}
toolIndex := map[string][2]int{} // tool id -> message/part index
addUser := func(entry transcriptEntry, text string, files []protocol.ChatPart) {
id := takePromptID(text)
@@ -259,17 +272,22 @@ func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHis
for _, block := range blocks {
switch block.Type {
case "text":
// Every block of a kind takes an ordinal, empty or not, as
// the live stream numbers them when they start: a part keeps
// one id live and in history.
ordinal := ordinals.next(messageID, protocol.ChatPartText)
if strings.TrimSpace(block.Text) == "" {
continue
}
ordinal := textOrdinal(messages[index].Parts, protocol.ChatPartText)
part := protocol.ChatPart{Type: protocol.ChatPartText, ID: protocol.ChatDeltaPartID(messageID, protocol.ChatPartText, ordinal), Text: block.Text}
messages[index].Parts = append(messages[index].Parts, part)
case "thinking":
// An empty (display-omitted) thinking block often precedes
// the one with text; it is counted, not shown.
ordinal := ordinals.next(messageID, protocol.ChatPartReasoning)
if strings.TrimSpace(block.Thinking) == "" {
continue
}
ordinal := textOrdinal(messages[index].Parts, protocol.ChatPartReasoning)
part := protocol.ChatPart{Type: protocol.ChatPartReasoning, ID: protocol.ChatDeltaPartID(messageID, protocol.ChatPartReasoning, ordinal), Text: block.Thinking}
messages[index].Parts = append(messages[index].Parts, part)
case "tool_use":
@@ -422,14 +440,18 @@ func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHis
return page, nil
}
func textOrdinal(parts []protocol.ChatPart, kind string) int {
count := 0
for _, part := range parts {
if part.Type == kind {
count++
}
// blockOrdinals counts each message's text and thinking blocks by kind.
type blockOrdinals map[string]map[string]int
func (o blockOrdinals) next(messageID, kind string) int {
counts := o[messageID]
if counts == nil {
counts = map[string]int{}
o[messageID] = counts
}
return count
ordinal := counts[kind]
counts[kind] = ordinal + 1
return ordinal
}
func applyTranscriptToolResult(messages *[]protocol.ChatMessage, toolIndex map[string][2]int, block contentBlock) {