diff --git a/internal/claude/claude.go b/internal/claude/claude.go index c3ea0d8e..d0ff4cca 100644 --- a/internal/claude/claude.go +++ b/internal/claude/claude.go @@ -165,6 +165,7 @@ func (c *Chat) sessionFor(id, directory string) *session { streamBlocks: map[int]blockState{}, streamText: map[int]string{}, streamOrdinals: map[string]int{}, + streamClaimed: map[int]bool{}, lastUsed: time.Now(), } c.sessions[id] = s @@ -266,6 +267,7 @@ type session struct { streamBlocks map[int]blockState streamText map[int]string streamOrdinals map[string]int + streamClaimed map[int]bool toolMessages map[string]string } diff --git a/internal/claude/process.go b/internal/claude/process.go index 62d40359..4b1a4773 100644 --- a/internal/claude/process.go +++ b/internal/claude/process.go @@ -352,9 +352,7 @@ func (s *session) processExit(p *process, err error) { turn := s.turnID s.running = false s.turnID = "" - s.streamBlocks = map[int]blockState{} - s.streamText = map[int]string{} - s.streamOrdinals = map[string]int{} + s.resetStreamLocked() s.mu.Unlock() if !running { return diff --git a/internal/claude/split_blocks_test.go b/internal/claude/split_blocks_test.go new file mode 100644 index 00000000..c4d622ce --- /dev/null +++ b/internal/claude/split_blocks_test.go @@ -0,0 +1,117 @@ +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) + } +} diff --git a/internal/claude/translate.go b/internal/claude/translate.go index 125ed14f..ee750ea7 100644 --- a/internal/claude/translate.go +++ b/internal/claude/translate.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "regexp" + "sort" "strconv" "strings" "time" @@ -197,8 +198,7 @@ func (s *session) handleStreamEvent(envelope lineEnvelope) { if s.streamID == "" { s.streamID = envelope.UUID } - s.streamBlocks = map[int]blockState{} - s.streamText = map[int]string{} + s.resetStreamLocked() case "content_block_start": if event.ContentBlock == nil { return @@ -286,6 +286,55 @@ func deltaKind(deltaType string) string { } } +// resetStreamLocked forgets the assembled stream blocks of the previous message. +func (s *session) resetStreamLocked() { + s.streamBlocks = map[int]blockState{} + s.streamText = map[int]string{} + s.streamOrdinals = map[string]int{} + s.streamClaimed = map[int]bool{} +} + +// claimStreamedLocked finds the streamed block a completed text or thinking block +// repeats. The CLI sends one assistant event per content block, so a block's +// index within the event (usually 0) is not its stream index: a text block that +// follows a thinking block streams at index 1 but arrives alone in its event. +// The match is the first unclaimed streamed block of the same kind whose text +// is a prefix of the completed text, preferring the event-local index. An +// unclaimed streamed block of that kind that disagrees is returned too, so the +// caller suppresses the repeat instead of emitting the whole answer again. +func (s *session) claimStreamedLocked(kind, full string, local int) (blockState, string, bool) { + indices := make([]int, 0, len(s.streamBlocks)+1) + indices = append(indices, local) + others := make([]int, 0, len(s.streamBlocks)) + for index := range s.streamBlocks { + if index != local { + others = append(others, index) + } + } + sort.Ints(others) + indices = append(indices, others...) + disagree := -1 + for _, index := range indices { + block := s.streamBlocks[index] + streamed := s.streamText[index] + if block.kind != kind || s.streamClaimed[index] || streamed == "" { + continue + } + if strings.HasPrefix(full, streamed) { + s.streamClaimed[index] = true + return block, streamed, true + } + if disagree < 0 { + disagree = index + } + } + if disagree >= 0 { + s.streamClaimed[disagree] = true + return s.streamBlocks[disagree], s.streamText[disagree], true + } + return blockState{}, "", false +} + // broadcastLocked emits while the session lock is already held: the caller is a // stream-delta path that owns state it just updated. func (s *session) broadcastLocked(frame protocol.ChatEventFrame) { @@ -317,6 +366,12 @@ func (s *session) handleAssistant(envelope lineEnvelope) { } blocks := decodeBlocks(body.Content) s.mu.Lock() + if messageID != s.streamID { + // A message that was not streamed (or a new one): nothing streamed can + // be aligned with it. Later events of the same message keep this state. + s.resetStreamLocked() + s.streamID = messageID + } if body.Model != "" { s.modelActual = body.Model } @@ -335,12 +390,11 @@ func (s *session) handleAssistant(envelope lineEnvelope) { if strings.TrimSpace(full) == "" { continue } - state, ok := s.streamBlocks[index] - if !ok || state.kind != kind { + state, streamed, matched := s.claimStreamedLocked(kind, full, index) + if !matched { state = blockState{kind: kind, ordinal: s.streamOrdinals[kind]} s.streamOrdinals[kind]++ } - streamed := s.streamText[index] delta := full if strings.HasPrefix(full, streamed) { delta = full[len(streamed):] @@ -401,9 +455,6 @@ func (s *session) handleAssistant(envelope lineEnvelope) { } } } - s.streamBlocks = map[int]blockState{} - s.streamText = map[int]string{} - s.streamOrdinals = map[string]int{} framesCopy := frames s.mu.Unlock() for _, frame := range framesCopy { @@ -503,9 +554,7 @@ func (s *session) handleResult(envelope lineEnvelope) { s.turnID = "" s.approvals = map[string]*pendingApproval{} s.forms = map[string]*pendingForm{} - s.streamBlocks = map[int]blockState{} - s.streamText = map[int]string{} - s.streamOrdinals = map[string]int{} + s.resetStreamLocked() s.mu.Unlock() if envelope.Subtype != "success" && envelope.Subtype != "error_during_execution" && envelope.Subtype != "" { message := strings.TrimSpace(envelope.Result)