c316bc9bc8
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>
65 lines
2.3 KiB
Go
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: ©, NativeTurnID: sub.room.nativeTurnID}
|
|
slow = !enqueueChatLocked(sub, protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame})
|
|
}
|
|
}
|
|
h.mu.Unlock()
|
|
if slow {
|
|
sub.overflow()
|
|
}
|
|
}
|
|
|
|
// 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: ©, 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}) {
|
|
// overflow detaches under h.mu; never block the folding lock. A
|
|
// full queue must resync rather than silently miss terminal cleanup.
|
|
go sub.overflow()
|
|
}
|
|
}
|
|
}
|