Chat and runs: tell a slow client to resync instead of dropping it silently; join queued deltas
A chat subscriber whose queue fills is detached and its writer ends the stream with a resync frame (the app re-attaches and re-reads history); a run subscriber gets a final runs_resync and the app subscribes again and re-reads the Runs list and open detail. Consecutive text/reasoning deltas of one part are joined in a chat subscriber's queue; joined numbered frames carry seq_from and delta_lens so the app trims exactly the prefix a history page already holds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// take pops the oldest queued frame (the zero Message when none is queued).
|
||||
func (s *chatSub) take() protocol.Message {
|
||||
msg, _, _ := s.next()
|
||||
return msg
|
||||
}
|
||||
|
||||
func deltaMessage(seq int64, delta string) protocol.Message {
|
||||
frame := protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventText, Delta: delta, Seq: seq,
|
||||
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_1-text-0"}}
|
||||
return protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}
|
||||
}
|
||||
|
||||
// A streaming burst must not overflow a client that is merely a little slow:
|
||||
// consecutive deltas of one part take one queue slot, and the joined frame
|
||||
// says which numbered deltas it holds and how long each one is.
|
||||
func TestChatSubJoinsConsecutiveDeltasOfOnePart(t *testing.T) {
|
||||
sub := &chatSub{limit: 2, done: make(chan struct{})}
|
||||
pieces := []string{"héllo ", "wörld ", "😀!"}
|
||||
for i, piece := range pieces {
|
||||
if !sub.enqueue(deltaMessage(int64(10+i), piece)) {
|
||||
t.Fatalf("delta %d overflowed a two-slot queue", i)
|
||||
}
|
||||
}
|
||||
if sub.queued() != 1 {
|
||||
t.Fatalf("queued = %d, want the burst joined into one frame", sub.queued())
|
||||
}
|
||||
// A frame of another kind is never joined.
|
||||
step := protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded}}
|
||||
if !sub.enqueue(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &step}) {
|
||||
t.Fatal("step frame refused")
|
||||
}
|
||||
if sub.enqueue(deltaMessage(13, "late")) {
|
||||
t.Fatal("a delta after the step joined across it")
|
||||
}
|
||||
joined := sub.take().ChatEvent
|
||||
if joined.Delta != strings.Join(pieces, "") || joined.SeqFrom != 10 || joined.Seq != 12 {
|
||||
t.Fatalf("joined = %+v", joined)
|
||||
}
|
||||
if want := []int{6, 6, 3}; len(joined.DeltaLens) != 3 || joined.DeltaLens[0] != want[0] || joined.DeltaLens[1] != want[1] || joined.DeltaLens[2] != want[2] {
|
||||
t.Fatalf("delta lengths = %v, want %v (UTF-16 units)", joined.DeltaLens, want)
|
||||
}
|
||||
// A gap in the numbering is never joined over.
|
||||
sub2 := &chatSub{limit: 4, done: make(chan struct{})}
|
||||
sub2.enqueue(deltaMessage(1, "a"))
|
||||
sub2.enqueue(deltaMessage(3, "c"))
|
||||
if sub2.queued() != 2 {
|
||||
t.Fatal("deltas with a numbering gap were joined")
|
||||
}
|
||||
}
|
||||
|
||||
// The frame a delta came from is shared by every subscriber, so joining for
|
||||
// one slow client must not change what another client is sent.
|
||||
func TestChatSubJoinDoesNotMutateSharedFrames(t *testing.T) {
|
||||
sub := &chatSub{limit: 4, done: make(chan struct{})}
|
||||
first := deltaMessage(1, "a")
|
||||
sub.enqueue(first)
|
||||
sub.enqueue(deltaMessage(2, "b"))
|
||||
if first.ChatEvent.Delta != "a" || first.ChatEvent.Seq != 1 || first.ChatEvent.SeqFrom != 0 {
|
||||
t.Fatalf("shared frame mutated: %+v", first.ChatEvent)
|
||||
}
|
||||
}
|
||||
|
||||
// The live incident: a slow client's 64-frame buffer filled and the server
|
||||
// silently dropped its subscription, so the app kept showing a frozen
|
||||
// transcript until the next reconnect. Now the client's last frame is a
|
||||
// resync, which makes the app re-attach and re-read the history.
|
||||
func TestChatSubOverflowEndsWithAResyncFrame(t *testing.T) {
|
||||
h := newChatHub()
|
||||
h.mu.Lock()
|
||||
room := h.roomLocked("ses_1")
|
||||
h.mu.Unlock()
|
||||
defer room.cancel()
|
||||
release := make(chan struct{})
|
||||
var mu sync.Mutex
|
||||
var written []protocol.Message
|
||||
write := func(msg protocol.Message) error {
|
||||
<-release // the client does not read until the burst is over
|
||||
mu.Lock()
|
||||
written = append(written, msg)
|
||||
mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
sub := h.attach("ses_1", "slow", "Phone", nil, write)
|
||||
for i := range chatRoomSubscriberBuffer + 8 {
|
||||
// Tool frames never join, so the queue fills.
|
||||
frame := protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventTool,
|
||||
Part: &protocol.ChatPart{Type: protocol.ChatPartTool, ID: "tool", Name: strings.Repeat("x", i%3+1)}}
|
||||
h.fanout("ses_1", protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}, "")
|
||||
}
|
||||
h.mu.Lock()
|
||||
attached := room.subs["slow"] == sub
|
||||
h.mu.Unlock()
|
||||
if attached {
|
||||
t.Fatal("an overflowed client stayed attached")
|
||||
}
|
||||
close(release)
|
||||
select {
|
||||
case <-sub.done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("the overflowed subscription never closed")
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
last := written[len(written)-1]
|
||||
if last.Type != protocol.ChatEvent || last.ChatEvent == nil || last.ChatEvent.Kind != protocol.ChatEventResync || last.ChatEvent.SessionID != "ses_1" {
|
||||
t.Fatalf("the overflowed client's last frame was %+v, want a resync", last)
|
||||
}
|
||||
// The client re-attaches with its own subscribe: a fresh subscription.
|
||||
again := h.attach("ses_1", "slow", "Phone", nil, func(protocol.Message) error { return nil })
|
||||
if again == nil || again == sub {
|
||||
t.Fatal("re-attaching after an overflow did not create a fresh subscription")
|
||||
}
|
||||
again.close()
|
||||
}
|
||||
Reference in New Issue
Block a user