Files
pipelines/internal/server/chat_compaction_test.go
T
alan c316bc9bc8 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>
2026-12-03 00:39:38 +00:00

117 lines
4.3 KiB
Go

package server
import (
"context"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestCompactionRetainedReplayIsolationAndTerminalClear(t *testing.T) {
h := newChatHub()
room := newChatRoom(context.Background(), "ses_compact")
defer room.cancel()
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", 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 sub.queued() != 0 {
t.Fatal("old native turn surfaced")
}
emit("first", "running", "turn_new")
sub.take()
if got := h.snapshot(room.sessionID); len(got) != 0 {
t.Fatalf("fake message: %+v", got)
}
h.replayCompaction(sub)
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", limit: 4, done: make(chan struct{})}
other.subs[otherSub.clientID] = otherSub
h.replayCompaction(otherSub)
if otherSub.queued() != 0 {
t.Fatal("foreign-session replay")
}
resetOld := h.compactionReset(room.sessionID)
emit("second", "running", "turn_new")
sub.take()
resetOld()
emit("first", "completed", "turn_new")
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.take()
if h.turns[room.sessionID].compaction == nil || !room.running {
t.Fatal("provider step settled compaction or turn")
}
emit("second", "completed", "turn_new")
sub.take()
if !room.running {
t.Fatal("compaction completion released turn queue")
}
emit("third", "running", "turn_new")
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.take()).ChatEvent; f.Compaction == nil || f.Compaction.Status != "failed" {
t.Fatalf("terminal cleanup: %+v", f)
}
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 sub.queued() != 0 {
t.Fatal("late old-turn activity revived label")
}
}
func TestCompactionTransportLossAndInterruptClearDoNotReleaseTurn(t *testing.T) {
for _, mode := range []string{"transport", "interrupt", "detach"} {
t.Run(mode, func(t *testing.T) {
h := newChatHub()
room := newChatRoom(context.Background(), "ses_compact")
defer room.cancel()
h.rooms[room.sessionID] = room
room.running = true
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.take()
switch mode {
case "interrupt":
h.compactionReset(room.sessionID)()
case "detach":
h.detach(room.sessionID, sub)
case "transport":
result := make(chan error, 1)
result <- nil
// Cancellation prevents any reconnect I/O; drain still clears retained
// activity on the exact room whose stream ended.
room.cancel()
h.drainStream(room, result)
}
if mode == "detach" {
// The last client leaving a running turn keeps the room observing
// unattended: the turn, and its live compaction, are still current.
if h.rooms[room.sessionID] != room || !room.watching || !room.running {
t.Fatal("detach released the room of a live turn")
}
return
}
if h.turns[room.sessionID].compaction != nil || !room.running {
t.Fatal("cleanup stuck label or released live turn")
}
})
}
}