Files
pipelines/internal/claude/live_test.go
T
alan ea25cf504f 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>
2026-11-30 15:41:57 +00:00

170 lines
6.9 KiB
Go

package claude
import (
"context"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func liveStream(event map[string]any) map[string]any {
return map[string]any{"type": "stream_event", "event": event}
}
func liveTextDelta(index int, text string) map[string]any {
return liveStream(map[string]any{"type": "content_block_delta", "index": index, "delta": map[string]any{"type": "text_delta", "text": text}})
}
// A client that reads history while a reply's text block streams (it opened
// the chat mid-reply, or reconnected) used to get only the deltas after its
// read: Claude Code writes a block to its file once it is complete, and the
// completed assistant event repeats nothing already streamed, so the reply's
// beginning was never shown. The newest page now carries the streaming block's
// text so far under its live part id, with the number of the last delta.
func TestHistoryReadMidBlockCarriesTheStreamedBeginning(t *testing.T) {
const id = "ses_live_overlay"
const message = "msg_live"
assistant := func(uuid string, block map[string]any) map[string]any {
return map[string]any{"type": "assistant", "uuid": uuid, "timestamp": "2026-11-30T10:00:01Z", "message": map[string]any{
"id": message, "role": "assistant", "content": []any{block}}}
}
// The file holds what the CLI wrote so far: the prompt and the message's
// two completed thinking blocks (an empty, display-omitted one first).
writeTranscript(t, id, []map[string]any{
{"type": "user", "uuid": "u-1", "timestamp": "2026-11-30T10:00:00Z", "message": map[string]any{"role": "user", "content": []any{map[string]any{"type": "text", "text": "Why?"}}}},
assistant("a-1", map[string]any{"type": "thinking", "thinking": "", "signature": "sig"}),
assistant("a-2", map[string]any{"type": "thinking", "thinking": "Considering the cause.", "signature": "sig"}),
})
chat := New(context.Background())
t.Cleanup(chat.Close)
s := chat.sessionFor(id, t.TempDir())
subs := attachTestSubs(s, 1)
s.mu.Lock()
s.running = true
s.mu.Unlock()
feedLines(t, s, []map[string]any{
liveStream(map[string]any{"type": "message_start", "message": map[string]any{"id": message}}),
liveStream(map[string]any{"type": "content_block_start", "index": 0, "content_block": map[string]any{"type": "thinking", "thinking": ""}}),
liveStream(map[string]any{"type": "content_block_stop", "index": 0}),
liveStream(map[string]any{"type": "content_block_start", "index": 1, "content_block": map[string]any{"type": "thinking", "thinking": ""}}),
liveStream(map[string]any{"type": "content_block_delta", "index": 1, "delta": map[string]any{"type": "thinking_delta", "thinking": "Considering the cause."}}),
liveStream(map[string]any{"type": "content_block_stop", "index": 1}),
liveStream(map[string]any{"type": "content_block_start", "index": 2, "content_block": map[string]any{"type": "text", "text": "The "}}),
liveTextDelta(2, "first part. "),
})
page, err := s.transcriptMessages(200, "")
if err != nil {
t.Fatal(err)
}
feedLines(t, s, []map[string]any{liveTextDelta(2, "And the rest.")})
detachTestSubs(s, subs)
textID := protocol.ChatDeltaPartID(message, protocol.ChatPartText, 0)
reasoningID := protocol.ChatDeltaPartID(message, protocol.ChatPartReasoning, 1)
var frames []protocol.ChatEventFrame
for frame := range subs[0] {
frames = append(frames, frame)
}
// reasoning, the block's starting text, its first delta, and the late delta.
if len(frames) != 4 {
t.Fatalf("frames = %+v", frames)
}
if frames[0].Part.ID != reasoningID {
t.Fatalf("live reasoning id %q, want %q (history's id for the same block)", frames[0].Part.ID, reasoningID)
}
if frames[1].Delta != "The " || frames[1].Part.ID != textID {
t.Fatalf("a block's starting text was not published: %+v", frames[1])
}
for i := 1; i < len(frames); i++ {
if frames[i].Seq <= frames[i-1].Seq {
t.Fatalf("deltas are not numbered in order: %+v", frames)
}
}
last := page.Messages[len(page.Messages)-1]
if last.ID != message {
t.Fatalf("newest message %q", last.ID)
}
parts := map[string]string{}
for _, part := range last.Parts {
parts[part.ID] = part.Text
}
if parts[reasoningID] != "Considering the cause." {
t.Fatalf("history reasoning ids differ from live ones: %+v", last.Parts)
}
if parts[textID] != "The first part. " {
t.Fatalf("streaming block text = %q, want its beginning", parts[textID])
}
if len(page.LiveParts) != 1 || page.LiveParts[0] != textID {
t.Fatalf("live parts = %v", page.LiveParts)
}
if page.LiveSeq != frames[2].Seq {
t.Fatalf("live seq %d, want the last included delta %d", page.LiveSeq, frames[2].Seq)
}
// An older page is pure history, and so is any page once the turn ended.
s.mu.Lock()
s.running = false
s.mu.Unlock()
ended, err := s.transcriptMessages(200, "")
if err != nil {
t.Fatal(err)
}
if len(ended.LiveParts) != 0 {
t.Fatalf("an idle session overlaid live text: %v", ended.LiveParts)
}
}
// A brand-new session has no file yet while its first reply streams; the
// newest page still carries the reply so far.
func TestHistoryBeforeTheFileExistsCarriesTheStreamingReply(t *testing.T) {
writeTranscript(t, "ses_other", nil)
chat := New(context.Background())
t.Cleanup(chat.Close)
s := chat.sessionFor("ses_no_file", t.TempDir())
s.mu.Lock()
s.running = true
s.mu.Unlock()
feedLines(t, s, []map[string]any{
liveStream(map[string]any{"type": "message_start", "message": map[string]any{"id": "msg_first"}}),
liveStream(map[string]any{"type": "content_block_start", "index": 0, "content_block": map[string]any{"type": "text"}}),
liveTextDelta(0, "Hello"),
})
page, err := s.transcriptMessages(200, "")
if err != nil {
t.Fatal(err)
}
if len(page.Messages) != 1 || page.Messages[0].Parts[0].Text != "Hello" || page.Messages[0].Role != protocol.ChatRoleAssistant {
t.Fatalf("page = %+v", page)
}
}
// History numbers an unstreamed empty block like the stream does, so the text
// block after it keeps one id live and in history.
func TestUnstreamedEmptyBlocksKeepHistoryOrdinals(t *testing.T) {
chat := testChat(t)
s := chat.session(newCreatedChat(t, chat).ID)
subs := attachTestSubs(s, 1)
assistant := func(block map[string]any) map[string]any {
return map[string]any{"type": "assistant", "message": map[string]any{"id": "msg_plain", "role": "assistant", "content": []any{block}}}
}
feedLines(t, s, []map[string]any{
assistant(map[string]any{"type": "thinking", "thinking": "", "signature": "sig"}),
assistant(map[string]any{"type": "thinking", "thinking": "Hmm.", "signature": "sig"}),
assistant(map[string]any{"type": "text", "text": "Answer."}),
})
detachTestSubs(s, subs)
var ids []string
for frame := range subs[0] {
if frame.Part != nil {
ids = append(ids, frame.Part.ID)
}
}
want := []string{protocol.ChatDeltaPartID("msg_plain", protocol.ChatPartReasoning, 1), protocol.ChatDeltaPartID("msg_plain", protocol.ChatPartText, 0)}
if len(ids) != 2 || ids[0] != want[0] || ids[1] != want[1] {
t.Fatalf("ids = %v, want %v", ids, want)
}
}