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:
@@ -0,0 +1,144 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// Claude Code writes an assistant content block to its session file only once
|
||||
// the block is complete. A client that reads history while a block streams
|
||||
// (it opened the chat mid-reply, or its socket reconnected) would otherwise
|
||||
// get only the deltas after its read, and nothing ever sends the block's
|
||||
// beginning again: the completed assistant event repeats nothing already
|
||||
// streamed. So every published delta is numbered and its text retained for
|
||||
// the newest assistant messages, and the newest history page carries the
|
||||
// retained text of every part the transcript lacks, with the number of the
|
||||
// last delta it includes (protocol.ChatHistoryPage.LiveSeq).
|
||||
|
||||
// liveMessagesRetained bounds the retained messages: the one streaming now and
|
||||
// the one before it, whose last block may not be written yet when the next
|
||||
// message starts.
|
||||
const liveMessagesRetained = 2
|
||||
|
||||
type liveMessage struct {
|
||||
id string
|
||||
parts []*livePart
|
||||
}
|
||||
|
||||
type livePart struct {
|
||||
id string
|
||||
kind string
|
||||
text strings.Builder
|
||||
}
|
||||
|
||||
// liveOverlay is a copy of the retained live text taken at one Seq.
|
||||
type liveOverlay struct {
|
||||
seq int64
|
||||
running bool
|
||||
messages []liveOverlayMessage
|
||||
}
|
||||
|
||||
type liveOverlayMessage struct {
|
||||
id string
|
||||
parts []protocol.ChatPart
|
||||
}
|
||||
|
||||
// recordLiveLocked numbers a text or reasoning delta frame about to be
|
||||
// published and retains its text. It runs for silent replays too, so the
|
||||
// retained text stays whole after a reattached host goes live mid-block.
|
||||
func (s *session) recordLiveLocked(frame *protocol.ChatEventFrame) {
|
||||
if frame.Part == nil || frame.Part.ID == "" || frame.Delta == "" || frame.MessageID == "" {
|
||||
return
|
||||
}
|
||||
if s.liveSeq == 0 {
|
||||
// Start from the clock so numbers keep increasing across a server
|
||||
// restart. Microseconds stay exact as a JavaScript number.
|
||||
s.liveSeq = time.Now().UnixMicro()
|
||||
}
|
||||
s.liveSeq++
|
||||
frame.Seq = s.liveSeq
|
||||
var message *liveMessage
|
||||
for _, held := range s.live {
|
||||
if held.id == frame.MessageID {
|
||||
message = held
|
||||
break
|
||||
}
|
||||
}
|
||||
if message == nil {
|
||||
message = &liveMessage{id: frame.MessageID}
|
||||
s.live = append(s.live, message)
|
||||
if len(s.live) > liveMessagesRetained {
|
||||
s.live = append([]*liveMessage(nil), s.live[len(s.live)-liveMessagesRetained:]...)
|
||||
}
|
||||
}
|
||||
var part *livePart
|
||||
for _, held := range message.parts {
|
||||
if held.id == frame.Part.ID {
|
||||
part = held
|
||||
break
|
||||
}
|
||||
}
|
||||
if part == nil {
|
||||
part = &livePart{id: frame.Part.ID, kind: frame.Kind}
|
||||
message.parts = append(message.parts, part)
|
||||
}
|
||||
part.text.WriteString(frame.Delta)
|
||||
}
|
||||
|
||||
// liveOverlaySnapshot copies the retained live text. A history read takes it
|
||||
// before reading the transcript, so a block that completes in between is
|
||||
// found whole in the transcript rather than lost from both.
|
||||
func (s *session) liveOverlaySnapshot() liveOverlay {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
overlay := liveOverlay{seq: s.liveSeq, running: s.running}
|
||||
for _, message := range s.live {
|
||||
copied := liveOverlayMessage{id: message.id}
|
||||
for _, part := range message.parts {
|
||||
copied.parts = append(copied.parts, protocol.ChatPart{ID: part.id, Type: part.kind, Text: part.text.String()})
|
||||
}
|
||||
overlay.messages = append(overlay.messages, copied)
|
||||
}
|
||||
return overlay
|
||||
}
|
||||
|
||||
// apply adds to the newest page every retained part its transcript lacks,
|
||||
// while a turn runs. Once the turn ended the transcript is complete, and an
|
||||
// interrupted block the CLI never wrote is not invented into history.
|
||||
func (overlay liveOverlay) apply(page *protocol.ChatHistoryPage, sessionID string) {
|
||||
page.LiveSeq = overlay.seq
|
||||
if !overlay.running {
|
||||
return
|
||||
}
|
||||
for _, live := range overlay.messages {
|
||||
index := -1
|
||||
for i := range page.Messages {
|
||||
if page.Messages[i].ID == live.id {
|
||||
index = i
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, part := range live.parts {
|
||||
if index >= 0 && hasPart(page.Messages[index].Parts, part.ID) {
|
||||
continue
|
||||
}
|
||||
if index < 0 {
|
||||
page.Messages = append(page.Messages, protocol.ChatMessage{ID: live.id, SessionID: sessionID, Role: protocol.ChatRoleAssistant, CreatedAt: time.Now().UnixMilli(), Parts: []protocol.ChatPart{}})
|
||||
index = len(page.Messages) - 1
|
||||
}
|
||||
page.Messages[index].Parts = append(page.Messages[index].Parts, part)
|
||||
page.LiveParts = append(page.LiveParts, part.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func hasPart(parts []protocol.ChatPart, id string) bool {
|
||||
for _, part := range parts {
|
||||
if part.ID == id {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user