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:
@@ -15,20 +15,20 @@ func TestUsageRetainedWithoutMessageReplayResetAndAdmissionIsolation(t *testing.
|
||||
room.nativeTurnID = "turn_keep"
|
||||
room.turnDone = make(chan struct{})
|
||||
h.rooms[room.sessionID] = room
|
||||
sub := &chatSub{hub: h, room: room, clientID: "watcher", ch: make(chan protocol.Message, 32), done: make(chan struct{})}
|
||||
sub := &chatSub{hub: h, room: room, clientID: "watcher", limit: 32, done: make(chan struct{})}
|
||||
room.subs[sub.clientID] = sub
|
||||
h.apply(protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: "running"}})
|
||||
for _, n := range []int64{123, 123, 0, 5} {
|
||||
total := n
|
||||
u := &protocol.ChatUsage{Scope: "session", TotalTokens: &total}
|
||||
h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventUsage, Usage: u})
|
||||
live := (<-sub.ch).ChatEvent
|
||||
live := (sub.take()).ChatEvent
|
||||
if live.MessageID != "" || *live.Usage.TotalTokens != n {
|
||||
t.Fatalf("live=%+v", live)
|
||||
}
|
||||
total = 999 // producer-owned data must not mutate retained state
|
||||
h.replayUsage(sub)
|
||||
frame := (<-sub.ch).ChatEvent
|
||||
frame := (sub.take()).ChatEvent
|
||||
if frame == nil || frame.Kind != protocol.ChatEventUsage || frame.MessageID != "" || frame.Usage.TotalTokens == nil || *frame.Usage.TotalTokens != n {
|
||||
t.Fatalf("replay=%+v", frame)
|
||||
}
|
||||
@@ -47,18 +47,18 @@ func TestUsageRetainedWithoutMessageReplayResetAndAdmissionIsolation(t *testing.
|
||||
}
|
||||
// A full snapshot may legitimately reset every unknown field to nil.
|
||||
h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventUsage, Usage: &protocol.ChatUsage{Scope: "session"}})
|
||||
<-sub.ch
|
||||
sub.take()
|
||||
h.replayUsage(sub)
|
||||
if f := (<-sub.ch).ChatEvent; f.Usage.TotalTokens != nil {
|
||||
if f := (sub.take()).ChatEvent; f.Usage.TotalTokens != nil {
|
||||
t.Fatal("unknown snapshot accumulated old counter")
|
||||
}
|
||||
other := newChatRoom(context.Background(), "ses_other")
|
||||
defer other.cancel()
|
||||
h.rooms[other.sessionID] = other
|
||||
otherSub := &chatSub{hub: h, room: other, clientID: "other", ch: make(chan protocol.Message, 4), done: make(chan struct{})}
|
||||
otherSub := &chatSub{hub: h, room: other, clientID: "other", limit: 4, done: make(chan struct{})}
|
||||
other.subs[otherSub.clientID] = otherSub
|
||||
h.replayUsage(otherSub)
|
||||
if len(otherSub.ch) != 0 {
|
||||
if otherSub.queued() != 0 {
|
||||
t.Fatal("foreign usage replayed")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user