Expose native chat compaction activity and retained replay
This commit is contained in:
@@ -45,10 +45,12 @@ func handleChatClose(ctx context.Context, request protocol.Message, broker chatB
|
||||
// idle session is never interrupted.
|
||||
interrupted := hub.turnInFlight(sessionID)
|
||||
if interrupted {
|
||||
resetCompaction := hub.compactionReset(sessionID)
|
||||
if err := broker.Interrupt(ctx, sessionID); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " could not be stopped: " + err.Error()})
|
||||
return
|
||||
}
|
||||
resetCompaction()
|
||||
}
|
||||
if err := registry.CloseOperatorSession(ctx, sessionID); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
|
||||
@@ -42,10 +42,12 @@ func handleChatSimple(ctx context.Context, request protocol.Message, broker chat
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
|
||||
return
|
||||
}
|
||||
resetCompaction := hub.compactionReset(query.SessionID)
|
||||
if err := broker.Interrupt(ctx, query.SessionID); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
resetCompaction()
|
||||
_ = write(protocol.Message{Type: protocol.ChatInterrupt, ID: request.ID})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
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.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: ©, 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
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 h.turns[room.sessionID].compaction != nil || !room.running {
|
||||
t.Fatal("cleanup stuck label or released live turn")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -64,14 +64,15 @@ type chatMessage struct {
|
||||
}
|
||||
|
||||
type chatTurn struct {
|
||||
sessionID string
|
||||
order []string
|
||||
messages map[string]*chatMessage
|
||||
step *protocol.ChatStep
|
||||
usage *protocol.ChatUsage
|
||||
size int
|
||||
overflow bool
|
||||
updated time.Time
|
||||
compaction *protocol.ChatCompaction
|
||||
sessionID string
|
||||
order []string
|
||||
messages map[string]*chatMessage
|
||||
step *protocol.ChatStep
|
||||
usage *protocol.ChatUsage
|
||||
size int
|
||||
overflow bool
|
||||
updated time.Time
|
||||
// permissions holds the request ids already pushed to the client so a
|
||||
// repeated poll cannot push the same pending request twice; permissionOrder
|
||||
// bounds it to chatPermissionRetainedLimit.
|
||||
@@ -143,7 +144,21 @@ func (h *chatHub) applyLocked(frame protocol.ChatEventFrame) {
|
||||
}
|
||||
turn := h.turnLocked(frame.SessionID)
|
||||
switch frame.Kind {
|
||||
case protocol.ChatEventCompaction:
|
||||
if frame.Compaction == nil {
|
||||
return
|
||||
}
|
||||
if frame.Compaction.Status == "running" {
|
||||
copy := *frame.Compaction
|
||||
turn.compaction = ©
|
||||
} else if turn.compaction != nil && (frame.Compaction.ID == "" || turn.compaction.ID == frame.Compaction.ID) {
|
||||
turn.compaction = nil
|
||||
}
|
||||
return
|
||||
case protocol.ChatEventStep:
|
||||
if frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded && !frame.Step.Intermediate {
|
||||
turn.compaction = nil
|
||||
}
|
||||
turn.step = frame.Step
|
||||
return
|
||||
case protocol.ChatEventUsage:
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -243,4 +243,5 @@ func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatH
|
||||
// carry a shell — a shell outlives the turn and is not a turn — so this is the
|
||||
// shell half of the resume.
|
||||
hub.replayShells(sub)
|
||||
hub.replayCompaction(sub)
|
||||
}
|
||||
|
||||
@@ -883,6 +883,9 @@ func TestChatInterruptEndsRunningPrompt(t *testing.T) {
|
||||
interruptOnce.Do(func() { close(interrupted) })
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/session/ses_1/message":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":[]}`))
|
||||
default:
|
||||
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
|
||||
}
|
||||
@@ -1384,7 +1387,7 @@ func realAdapterChat(t *testing.T, frames []string) []protocol.ChatEventFrame {
|
||||
prompts.Add(1)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`))
|
||||
case r.Method == http.MethodGet && (r.URL.Path == "/api/session/ses_1/permission" || r.URL.Path == "/api/session/ses_1/form"):
|
||||
case r.Method == http.MethodGet && (r.URL.Path == "/api/session/ses_1/permission" || r.URL.Path == "/api/session/ses_1/form" || r.URL.Path == "/api/session/ses_1/message"):
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"location":{"directory":"/work"},"data":[]}`))
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/shell":
|
||||
|
||||
Reference in New Issue
Block a user