Expose native chat compaction activity and retained replay

This commit is contained in:
2026-10-20 22:17:13 +01:00
parent d8b111913e
commit 4969d667b3
17 changed files with 666 additions and 26 deletions
+29 -1
View File
@@ -301,6 +301,7 @@ func (h *chatHub) detach(sessionID string, sub *chatSub) {
}
delete(room.subs, sub.clientID)
if len(room.subs) == 0 {
h.clearCompactionLocked(room)
delete(h.rooms, sessionID)
room.cancel()
// ReleaseShells is a local identity-cache operation (no broker I/O).
@@ -374,6 +375,26 @@ func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) {
h.mu.Unlock()
return
}
if frame.Kind == protocol.ChatEventCompaction {
if frame.Compaction == nil {
h.mu.Unlock()
return
}
if room != nil && frame.NativeTurnID != "" && room.nativeTurnID != "" && frame.NativeTurnID != room.nativeTurnID {
h.mu.Unlock()
return
}
current := h.turnLocked(frame.SessionID).compaction
lastStep := h.turnLocked(frame.SessionID).step
if frame.NativeTurnID != "" && lastStep != nil && lastStep.Status == protocol.ChatStepEnded && !lastStep.Intermediate && lastStep.TurnID == frame.NativeTurnID {
h.mu.Unlock()
return
}
if frame.Compaction.Status != "running" && current != nil && frame.Compaction.ID != "" && frame.Compaction.ID != current.ID {
h.mu.Unlock()
return
}
}
if room != nil && frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.TurnID != "" && !frame.Step.Intermediate {
if frame.Step.Status == protocol.ChatStepStarted {
room.nativeTurnID = frame.Step.TurnID
@@ -383,6 +404,9 @@ func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) {
return
}
}
if room != nil && frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded && !frame.Step.Intermediate {
h.clearCompactionLocked(room)
}
h.applyLocked(frame)
buffered := false
var targets []*chatSub
@@ -401,7 +425,7 @@ func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) {
}
// Shell replay and live lifecycle delivery must enqueue in the same order
// as folding. Otherwise an exit can overtake an older running replay.
if frame.Kind == protocol.ChatEventShell && !buffered {
if (frame.Kind == protocol.ChatEventShell || frame.Kind == protocol.ChatEventCompaction) && !buffered {
msg := protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}
var slow []*chatSub
for _, sub := range targets {
@@ -484,6 +508,7 @@ func (h *chatHub) replaceShellsLocked(sessionID string, shells []protocol.ChatSh
// finishTurnLocked marks the room's turn finished and releases the turn runner's
// wait. Callers must hold h.mu.
func (h *chatHub) finishTurnLocked(room *chatRoom) {
h.clearCompactionLocked(room)
room.running = false
room.nativeTurnID = ""
if room.turnDone != nil {
@@ -807,6 +832,9 @@ func (h *chatHub) startStream(room *chatRoom, broker chatBroker) error {
func (h *chatHub) drainStream(room *chatRoom, result <-chan error) {
err := <-result
h.mu.Lock()
if h.rooms[room.sessionID] == room {
h.clearCompactionLocked(room)
}
room.streaming = false
h.mu.Unlock()
if err != nil && room.ctx.Err() == nil {