Files
pipelines/internal/server/chat_compaction.go
T

65 lines
2.3 KiB
Go

package server
import "git.a-wan.net/alan/pipelines/internal/protocol"
// Capture the exact activity before interrupt I/O. A late interrupt reply may
// clear that activity, but must not clear a newer compaction or replacement room.
func (h *chatHub) compactionReset(sessionID string) func() {
h.mu.Lock()
room := h.rooms[sessionID]
var current *protocol.ChatCompaction
if turn := h.turns[sessionID]; turn != nil {
current = turn.compaction
}
h.mu.Unlock()
return func() {
h.mu.Lock()
defer h.mu.Unlock()
if room != nil && h.rooms[sessionID] == room && h.turns[sessionID] != nil && h.turns[sessionID].compaction == current {
h.clearCompactionLocked(room)
}
}
}
// Replay and lifecycle updates enqueue under the fold lock so an older replay
// cannot overtake a terminal event. Compaction never releases the turn slot.
func (h *chatHub) replayCompaction(sub *chatSub) {
h.mu.Lock()
slow := false
if h.rooms[sub.room.sessionID] == sub.room && sub.room.subs[sub.clientID] == sub {
if current := h.turnLocked(sub.room.sessionID).compaction; current != nil {
copy := *current
frame := protocol.ChatEventFrame{SessionID: sub.room.sessionID, Kind: protocol.ChatEventCompaction, Compaction: &copy, NativeTurnID: sub.room.nativeTurnID}
slow = !enqueueChatLocked(sub, protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame})
}
}
h.mu.Unlock()
if slow {
sub.close()
}
}
// Loss of observability is not successful completion; clear only the activity
// label, preserving the harness turn and queue slot for authoritative readback.
func (h *chatHub) clearCompactionLocked(room *chatRoom) {
turn := h.turns[room.sessionID]
if turn == nil || turn.compaction == nil {
return
}
copy := *turn.compaction
copy.Status = "failed"
turn.compaction = nil
frame := protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventCompaction, Compaction: &copy, NativeTurnID: room.nativeTurnID}
if room.buffering {
room.buffered = append(room.buffered, frame)
return
}
for _, sub := range room.subs {
if !enqueueChatLocked(sub, protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}) {
// close detaches under h.mu; never block the folding lock. A full
// queue must reconnect rather than silently miss terminal cleanup.
go sub.close()
}
}
}