Files
pipelines/internal/server/chat_compaction_test.go
alan 80022ea41a Keep observing chats with an in-flight turn after the last client leaves
Chat attention notifications (questions, approvals, finished replies) come
from a session's event stream, which the hub only read while a room existed;
detach destroyed the room when the last client left, so a turn that finished
or asked with the app closed notified nobody.

A room whose last client leaves now stays observing, unattended, while its
turn is in flight or it holds an unanswered question/approval, and is torn
down when the turn ends (or after 6h for a stuck turn); at most 16 such rooms.
At server start, recently used open sessions whose harness reports a running
turn (OpenCode /api/session/active, Codex thread/turns/list) are observed
again so their reply still notifies. Settings, dedupe and app_notifications
recording are unchanged.
2026-11-02 16:07:42 +00:00

117 lines
4.4 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", ch: make(chan protocol.Message, 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 {
t.Fatal("old native turn surfaced")
}
emit("first", "running", "turn_new")
<-sub.ch
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 != "" {
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{})}
other.subs[otherSub.clientID] = otherSub
h.replayCompaction(otherSub)
if len(otherSub.ch) != 0 {
t.Fatal("foreign-session replay")
}
resetOld := h.compactionReset(room.sessionID)
emit("second", "running", "turn_new")
<-sub.ch
resetOld()
emit("first", "completed", "turn_new")
if len(sub.ch) != 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
if h.turns[room.sessionID].compaction == nil || !room.running {
t.Fatal("provider step settled compaction or turn")
}
emit("second", "completed", "turn_new")
<-sub.ch
if !room.running {
t.Fatal("compaction completion released turn queue")
}
emit("third", "running", "turn_new")
<-sub.ch
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" {
t.Fatalf("terminal cleanup: %+v", f)
}
<-sub.ch // 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 {
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", ch: make(chan protocol.Message, 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
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")
}
})
}
}