d7ad468b78
Claude Code emits one assistant event per content block, so a text block that streamed at index 1 after a redacted (empty) thinking block arrives alone at index 0 of its event. The translator aligned completed blocks by that local index and cleared stream state after every event, found no streamed text, and re-emitted the whole paragraph as a new text part. Claim the streamed block of the same kind whose text prefixes the completed one, and keep stream state for the whole message (reset on message_start or a new message id).
118 lines
4.3 KiB
Go
118 lines
4.3 KiB
Go
package claude
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func feedLines(t *testing.T, s *session, lines []map[string]any) {
|
|
t.Helper()
|
|
for _, line := range lines {
|
|
raw, err := json.Marshal(line)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.handleLine(nil, raw)
|
|
}
|
|
}
|
|
|
|
func attachTestSubs(s *session, count int) []chan protocol.ChatEventFrame {
|
|
subs := make([]chan protocol.ChatEventFrame, count)
|
|
s.mu.Lock()
|
|
for i := range subs {
|
|
subs[i] = make(chan protocol.ChatEventFrame, 64)
|
|
s.subs[subs[i]] = struct{}{}
|
|
}
|
|
s.mu.Unlock()
|
|
return subs
|
|
}
|
|
|
|
func detachTestSubs(s *session, subs []chan protocol.ChatEventFrame) {
|
|
s.mu.Lock()
|
|
for _, ch := range subs {
|
|
delete(s.subs, ch)
|
|
close(ch)
|
|
}
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
// The CLI sends one assistant event per content block, so a text block that
|
|
// streamed at index 1 (after a redacted, empty thinking block at index 0)
|
|
// arrives alone in its event at index 0. Its completed text must not be emitted
|
|
// again, and every subscriber receives each delta exactly once.
|
|
func TestSplitAssistantBlocksDoNotRepeatStreamedText(t *testing.T) {
|
|
chat := testChat(t)
|
|
s := chat.session(newCreatedChat(t, chat).ID)
|
|
subs := attachTestSubs(s, 2)
|
|
|
|
const id = "msg_split"
|
|
paragraph := "Nothing obvious in the code, so I'll look at the pages."
|
|
stream := func(event map[string]any) map[string]any {
|
|
return map[string]any{"type": "stream_event", "event": event}
|
|
}
|
|
assistant := func(block map[string]any) map[string]any {
|
|
return map[string]any{"type": "assistant", "message": map[string]any{
|
|
"id": id, "role": "assistant", "model": "claude-test", "content": []any{block}}}
|
|
}
|
|
feedLines(t, s, []map[string]any{
|
|
stream(map[string]any{"type": "message_start", "message": map[string]any{"id": id}}),
|
|
stream(map[string]any{"type": "content_block_start", "index": 0, "content_block": map[string]any{"type": "thinking"}}),
|
|
stream(map[string]any{"type": "content_block_stop", "index": 0}),
|
|
assistant(map[string]any{"type": "thinking", "thinking": "", "signature": "sig"}),
|
|
stream(map[string]any{"type": "content_block_start", "index": 1, "content_block": map[string]any{"type": "text"}}),
|
|
stream(map[string]any{"type": "content_block_delta", "index": 1, "delta": map[string]any{"type": "text_delta", "text": paragraph[:20]}}),
|
|
stream(map[string]any{"type": "content_block_delta", "index": 1, "delta": map[string]any{"type": "text_delta", "text": paragraph[20:]}}),
|
|
stream(map[string]any{"type": "content_block_stop", "index": 1}),
|
|
assistant(map[string]any{"type": "text", "text": paragraph}),
|
|
stream(map[string]any{"type": "content_block_start", "index": 2, "content_block": map[string]any{"type": "tool_use"}}),
|
|
stream(map[string]any{"type": "content_block_stop", "index": 2}),
|
|
assistant(map[string]any{"type": "tool_use", "id": "toolu_split", "name": "Read", "input": map[string]any{"file_path": "x"}}),
|
|
stream(map[string]any{"type": "message_stop"}),
|
|
})
|
|
detachTestSubs(s, subs)
|
|
|
|
want := protocol.ChatDeltaPartID(id, protocol.ChatPartText, 0)
|
|
for n, frames := range subs {
|
|
texts := map[string]string{}
|
|
tools := 0
|
|
for frame := range frames {
|
|
switch frame.Kind {
|
|
case protocol.ChatPartText:
|
|
texts[frame.Part.ID] += frame.Delta
|
|
case protocol.ChatEventTool:
|
|
tools++
|
|
}
|
|
}
|
|
if len(texts) != 1 || texts[want] != paragraph {
|
|
t.Fatalf("subscriber %d text parts = %q, want only %s with one copy of the paragraph", n, texts, want)
|
|
}
|
|
if tools != 1 {
|
|
t.Fatalf("subscriber %d tool frames = %d, want 1", n, tools)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A completed message that was never streamed is still delivered in full, and
|
|
// its split text blocks keep distinct ordinals matching history.
|
|
func TestUnstreamedAssistantTextIsDelivered(t *testing.T) {
|
|
chat := testChat(t)
|
|
s := chat.session(newCreatedChat(t, chat).ID)
|
|
subs := attachTestSubs(s, 1)
|
|
for _, text := range []string{"First.", "Second."} {
|
|
feedLines(t, s, []map[string]any{{"type": "assistant", "message": map[string]any{
|
|
"id": "msg_plain", "role": "assistant", "content": []any{map[string]any{"type": "text", "text": text}}}}})
|
|
}
|
|
detachTestSubs(s, subs)
|
|
texts := map[string]string{}
|
|
for frame := range subs[0] {
|
|
if frame.Kind == protocol.ChatPartText {
|
|
texts[frame.Part.ID] += frame.Delta
|
|
}
|
|
}
|
|
if len(texts) != 2 || texts["msg_plain#text#0"] != "First." || texts["msg_plain#text#1"] != "Second." {
|
|
t.Fatalf("unstreamed text parts = %q", texts)
|
|
}
|
|
}
|