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:
@@ -14,63 +14,63 @@ func TestCompactionRetainedReplayIsolationAndTerminalClear(t *testing.T) {
|
||||
room.running, room.nativeTurnID = true, "turn_new"
|
||||
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
|
||||
emit := func(id, status, nativeID string) {
|
||||
h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, NativeTurnID: nativeID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{ID: id, Status: status}})
|
||||
}
|
||||
emit("old", "running", "turn_old")
|
||||
if len(sub.ch) != 0 {
|
||||
if sub.queued() != 0 {
|
||||
t.Fatal("old native turn surfaced")
|
||||
}
|
||||
emit("first", "running", "turn_new")
|
||||
<-sub.ch
|
||||
sub.take()
|
||||
if got := h.snapshot(room.sessionID); len(got) != 0 {
|
||||
t.Fatalf("fake message: %+v", got)
|
||||
}
|
||||
h.replayCompaction(sub)
|
||||
if f := (<-sub.ch).ChatEvent; f == nil || f.Compaction.ID != "first" || f.MessageID != "" {
|
||||
if f := (sub.take()).ChatEvent; f == nil || f.Compaction.ID != "first" || f.MessageID != "" {
|
||||
t.Fatalf("replay without assistant: %+v", f)
|
||||
}
|
||||
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.replayCompaction(otherSub)
|
||||
if len(otherSub.ch) != 0 {
|
||||
if otherSub.queued() != 0 {
|
||||
t.Fatal("foreign-session replay")
|
||||
}
|
||||
resetOld := h.compactionReset(room.sessionID)
|
||||
emit("second", "running", "turn_new")
|
||||
<-sub.ch
|
||||
sub.take()
|
||||
resetOld()
|
||||
emit("first", "completed", "turn_new")
|
||||
if len(sub.ch) != 0 || h.turns[room.sessionID].compaction.ID != "second" {
|
||||
if sub.queued() != 0 || h.turns[room.sessionID].compaction.ID != "second" {
|
||||
t.Fatal("old lifecycle cleared newer compaction")
|
||||
}
|
||||
h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, Intermediate: true}})
|
||||
<-sub.ch
|
||||
sub.take()
|
||||
if h.turns[room.sessionID].compaction == nil || !room.running {
|
||||
t.Fatal("provider step settled compaction or turn")
|
||||
}
|
||||
emit("second", "completed", "turn_new")
|
||||
<-sub.ch
|
||||
sub.take()
|
||||
if !room.running {
|
||||
t.Fatal("compaction completion released turn queue")
|
||||
}
|
||||
emit("third", "running", "turn_new")
|
||||
<-sub.ch
|
||||
sub.take()
|
||||
h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, TurnID: "turn_new"}})
|
||||
if f := (<-sub.ch).ChatEvent; f.Compaction == nil || f.Compaction.Status != "failed" {
|
||||
if f := (sub.take()).ChatEvent; f.Compaction == nil || f.Compaction.Status != "failed" {
|
||||
t.Fatalf("terminal cleanup: %+v", f)
|
||||
}
|
||||
<-sub.ch // authoritative step completion
|
||||
sub.take() // authoritative step completion
|
||||
if room.running || h.turns[room.sessionID].compaction != nil {
|
||||
t.Fatal("terminal state stuck")
|
||||
}
|
||||
emit("third", "running", "turn_new")
|
||||
if len(sub.ch) != 0 {
|
||||
if sub.queued() != 0 {
|
||||
t.Fatal("late old-turn activity revived label")
|
||||
}
|
||||
}
|
||||
@@ -83,10 +83,10 @@ func TestCompactionTransportLossAndInterruptClearDoNotReleaseTurn(t *testing.T)
|
||||
defer room.cancel()
|
||||
h.rooms[room.sessionID] = room
|
||||
room.running = true
|
||||
sub := &chatSub{hub: h, room: room, clientID: "watcher", ch: make(chan protocol.Message, 8), done: make(chan struct{})}
|
||||
sub := &chatSub{hub: h, room: room, clientID: "watcher", limit: 8, done: make(chan struct{})}
|
||||
room.subs[sub.clientID] = sub
|
||||
h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: "running", ID: "compact"}})
|
||||
<-sub.ch
|
||||
sub.take()
|
||||
switch mode {
|
||||
case "interrupt":
|
||||
h.compactionReset(room.sessionID)()
|
||||
|
||||
Reference in New Issue
Block a user