Preserve background shell visibility across readback races
Reconcile authoritative shell snapshots with bounded lifecycle journals so concurrent exits and creates cannot be overwritten, coalesce observation setup, and retain verified ownership across reconnects. Add deterministic reconciliation and cancellation regressions plus stable limited-visibility handling.\n\nChecked packaged templates and authoring skill references: none need updating. The task-owned docs note records the stale shared chat documentation for docs-fold; shared documentation was not edited.
This commit is contained in:
@@ -32,9 +32,21 @@ type chatBroker interface {
|
||||
// Permissions lists the session's pending OpenCode approval requests so the
|
||||
// handler can push chat_permission frames.
|
||||
Permissions(ctx context.Context, sessionID string) ([]protocol.ChatPermissionFrame, error)
|
||||
// Shells returns only shell operations whose ownership is authoritatively
|
||||
// verified for sessionID. It is the reconnect/readback source.
|
||||
Shells(ctx context.Context, sessionID string) ([]protocol.ChatShell, error)
|
||||
Reply(ctx context.Context, request protocol.ChatReplyRequest) error
|
||||
}
|
||||
|
||||
// chatShellReleaser is implemented by brokers that retain verified shell ids for
|
||||
// attributing exit events. The hub calls it only after the last observer leaves
|
||||
// a room; a later attach obtains a fresh authoritative snapshot.
|
||||
// This is a bounded local-cache operation: it must not perform I/O or call the
|
||||
// hub, which serializes it with room replacement under its mutex.
|
||||
type chatShellReleaser interface {
|
||||
ReleaseShells(sessionID string)
|
||||
}
|
||||
|
||||
// operatorRegistry is the server-side operator-session seam. *pipeline.Store
|
||||
// satisfies it. It is a table of its own, separate from the pipeline stage
|
||||
// ownership (SessionOwner/run_session_owners) that settles a run: chat_sessions
|
||||
|
||||
@@ -133,6 +133,13 @@ func (h *chatHub) apply(frame protocol.ChatEventFrame) {
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.applyLocked(frame)
|
||||
}
|
||||
|
||||
func (h *chatHub) applyLocked(frame protocol.ChatEventFrame) {
|
||||
if frame.SessionID == "" {
|
||||
return
|
||||
}
|
||||
turn := h.turnLocked(frame.SessionID)
|
||||
switch frame.Kind {
|
||||
case protocol.ChatEventStep:
|
||||
@@ -144,6 +151,9 @@ func (h *chatHub) apply(frame protocol.ChatEventFrame) {
|
||||
case protocol.ChatEventShell:
|
||||
// A shell frame carries no message id (it outlives the tool call that
|
||||
// started it), so it is folded before the message-id gate below.
|
||||
if room := h.rooms[frame.SessionID]; room != nil {
|
||||
room.shellReadback.Record(frame.Shell)
|
||||
}
|
||||
turn.applyShell(frame.Shell)
|
||||
return
|
||||
}
|
||||
@@ -240,7 +250,7 @@ func (t *chatTurn) applyShell(shell *protocol.ChatShell) {
|
||||
if shell == nil || shell.ID == "" {
|
||||
return
|
||||
}
|
||||
if shell.Status == protocol.ChatShellExited {
|
||||
if shell.Status != protocol.ChatShellRunning {
|
||||
old, ok := t.shells[shell.ID]
|
||||
if !ok {
|
||||
return
|
||||
|
||||
@@ -82,6 +82,8 @@ func (c *channelChat) Session(context.Context, string) (protocol.ChatSession, bo
|
||||
return protocol.ChatSession{}, false, nil
|
||||
}
|
||||
|
||||
func (c *channelChat) Shells(context.Context, string) ([]protocol.ChatShell, error) { return nil, nil }
|
||||
|
||||
func (c *channelChat) Permissions(context.Context, string) ([]protocol.ChatPermissionFrame, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
@@ -106,6 +106,11 @@ type chatQueuedPrompt struct {
|
||||
acked bool
|
||||
}
|
||||
|
||||
type chatObservation struct {
|
||||
done chan struct{}
|
||||
err error // published by closing done; belongs to this attempt, not the next
|
||||
}
|
||||
|
||||
// chatRoom is one session's shared live channel. Every field is guarded by the
|
||||
// owning chatHub's mutex.
|
||||
type chatRoom struct {
|
||||
@@ -117,6 +122,11 @@ type chatRoom struct {
|
||||
queue []*chatQueuedPrompt
|
||||
running bool
|
||||
streaming bool
|
||||
// observing is non-nil while one caller is attaching the shared broker
|
||||
// stream. It prevents concurrent subscribers from opening duplicate global
|
||||
// event streams before streaming can be published.
|
||||
observing *chatObservation
|
||||
shellReadback *opencode.ShellSnapshot
|
||||
// buffering holds the turn's frames until the prompting client's ack has been
|
||||
// written, so the ack always precedes the frames it acknowledges. It is set
|
||||
// only while the very first turn subscribes to the firehose.
|
||||
@@ -160,6 +170,81 @@ func (h *chatHub) roomLocked(sessionID string) *chatRoom {
|
||||
return room
|
||||
}
|
||||
|
||||
// ensureObserving starts the firehose even for a read-only subscriber, then
|
||||
// reconciles retained shells from the endpoint's authoritative list. A bounded
|
||||
// journal folds concurrent lifecycle events over that snapshot atomically. All
|
||||
// observers share this reconciliation, with no broker I/O under the hub mutex.
|
||||
func (h *chatHub) ensureObserving(room *chatRoom, broker chatBroker) error {
|
||||
if broker == nil {
|
||||
return errors.New("chat is not configured on this server")
|
||||
}
|
||||
h.mu.Lock()
|
||||
if err := room.ctx.Err(); err != nil {
|
||||
h.mu.Unlock()
|
||||
return err
|
||||
}
|
||||
if waiting := room.observing; waiting != nil {
|
||||
h.mu.Unlock()
|
||||
select {
|
||||
case <-waiting.done:
|
||||
return waiting.err
|
||||
case <-room.ctx.Done():
|
||||
return room.ctx.Err()
|
||||
}
|
||||
}
|
||||
if room.streaming {
|
||||
h.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
room.observing = &chatObservation{done: make(chan struct{})}
|
||||
waiting := room.observing
|
||||
room.shellReadback = &opencode.ShellSnapshot{}
|
||||
h.mu.Unlock()
|
||||
var observeErr error
|
||||
defer func() {
|
||||
h.mu.Lock()
|
||||
room.shellReadback = nil
|
||||
waiting.err = observeErr
|
||||
room.observing = nil
|
||||
close(waiting.done)
|
||||
h.mu.Unlock()
|
||||
}()
|
||||
if err := h.startStream(room, broker); err != nil {
|
||||
observeErr = err
|
||||
return err
|
||||
}
|
||||
shells, err := broker.Shells(room.ctx, room.sessionID)
|
||||
h.mu.Lock()
|
||||
if room.ctx.Err() != nil || h.rooms[room.sessionID] != room {
|
||||
h.mu.Unlock()
|
||||
observeErr = context.Canceled
|
||||
return observeErr
|
||||
}
|
||||
if err == nil {
|
||||
shells, err = room.shellReadback.Merge(shells)
|
||||
}
|
||||
if err != nil {
|
||||
// The firehose remains useful when its authoritative shell readback is
|
||||
// temporarily unavailable. Do not turn that read failure into a chat
|
||||
// failure (or claim the last retained snapshot is current): clear the
|
||||
// replay cache and tell attached clients that shell visibility is limited.
|
||||
// Preserve events already folded during this read, but clear stale replay.
|
||||
live, mergeErr := room.shellReadback.Merge(nil)
|
||||
if mergeErr == nil {
|
||||
h.replaceShellsLocked(room.sessionID, live)
|
||||
}
|
||||
h.mu.Unlock()
|
||||
// Never log broker bodies/URLs: they may contain credentials or private output.
|
||||
log.Printf("chat shell readback session=%s failed (%T)", room.sessionID, err)
|
||||
h.fanout(room.sessionID, protocol.Message{Type: protocol.Error, Error: "background shell visibility is limited"}, "")
|
||||
return nil
|
||||
}
|
||||
h.replaceShellsLocked(room.sessionID, shells)
|
||||
room.shellReadback = nil
|
||||
h.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// attach registers write as one client's subscription to sessionID's stream and
|
||||
// returns it, or the existing subscription when the same connection has already
|
||||
// attached. It is idempotent so a client may prompt and subscribe without
|
||||
@@ -209,6 +294,11 @@ func (h *chatHub) detach(sessionID string, sub *chatSub) {
|
||||
if len(room.subs) == 0 {
|
||||
delete(h.rooms, sessionID)
|
||||
room.cancel()
|
||||
// ReleaseShells is a local identity-cache operation (no broker I/O).
|
||||
// Complete it before a reconnect can acquire this session's new room.
|
||||
if releaser, ok := room.broker.(chatShellReleaser); ok {
|
||||
releaser.ReleaseShells(sessionID)
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
}
|
||||
@@ -265,9 +355,17 @@ func (h *chatHub) broadcastUser(frame protocol.ChatEventFrame, exceptClientID st
|
||||
// once it is written. A step-ended frame finishes the turn, letting the queue
|
||||
// advance.
|
||||
func (h *chatHub) emit(frame protocol.ChatEventFrame) {
|
||||
h.apply(frame)
|
||||
h.emitRoom(nil, frame)
|
||||
}
|
||||
|
||||
func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) {
|
||||
h.mu.Lock()
|
||||
room := h.rooms[frame.SessionID]
|
||||
if source != nil && (source != room || source.ctx.Err() != nil) {
|
||||
h.mu.Unlock()
|
||||
return
|
||||
}
|
||||
h.applyLocked(frame)
|
||||
buffered := false
|
||||
var targets []*chatSub
|
||||
if room != nil {
|
||||
@@ -283,6 +381,22 @@ func (h *chatHub) emit(frame protocol.ChatEventFrame) {
|
||||
if room != nil && frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded {
|
||||
h.finishTurnLocked(room)
|
||||
}
|
||||
// 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 {
|
||||
msg := protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}
|
||||
var slow []*chatSub
|
||||
for _, sub := range targets {
|
||||
if !enqueueChatLocked(sub, msg) {
|
||||
slow = append(slow, sub)
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
for _, sub := range slow {
|
||||
sub.close()
|
||||
}
|
||||
return
|
||||
}
|
||||
h.mu.Unlock()
|
||||
if buffered {
|
||||
return
|
||||
@@ -293,6 +407,62 @@ func (h *chatHub) emit(frame protocol.ChatEventFrame) {
|
||||
}
|
||||
}
|
||||
|
||||
// enqueueChatLocked never blocks or calls detach under the hub mutex.
|
||||
func enqueueChatLocked(sub *chatSub, msg protocol.Message) bool {
|
||||
select {
|
||||
case <-sub.done:
|
||||
return true
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case sub.ch <- msg:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (h *chatHub) replayShells(sub *chatSub) {
|
||||
h.mu.Lock()
|
||||
slow := false
|
||||
if h.rooms[sub.room.sessionID] == sub.room && sub.room.subs[sub.clientID] == sub {
|
||||
turn := h.turnLocked(sub.room.sessionID)
|
||||
for _, id := range turn.shellOrder {
|
||||
shell := turn.shells[id]
|
||||
frame := protocol.ChatEventFrame{SessionID: sub.room.sessionID, Kind: protocol.ChatEventShell, Shell: &shell}
|
||||
if !enqueueChatLocked(sub, protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}) {
|
||||
slow = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
if slow {
|
||||
sub.close()
|
||||
}
|
||||
}
|
||||
|
||||
// replaceShells reconciles a successful authoritative readback. It drops stale
|
||||
// retained entries (including exits missed while disconnected) and upserts the
|
||||
// verified running set without fabricating a lifecycle event for foreign shells.
|
||||
func (h *chatHub) replaceShells(sessionID string, shells []protocol.ChatShell) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.replaceShellsLocked(sessionID, shells)
|
||||
}
|
||||
|
||||
func (h *chatHub) replaceShellsLocked(sessionID string, shells []protocol.ChatShell) {
|
||||
turn := h.turnLocked(sessionID)
|
||||
for _, shell := range turn.shells {
|
||||
turn.shrink(len(shell.Command))
|
||||
}
|
||||
turn.shells = nil
|
||||
turn.shellOrder = nil
|
||||
for i := range shells {
|
||||
turn.applyShell(&shells[i])
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
@@ -331,14 +501,20 @@ func (h *chatHub) flushBuffered(sessionID string) {
|
||||
targets = append(targets, sub)
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
var slow []*chatSub
|
||||
for i := range pending {
|
||||
frame := pending[i]
|
||||
msg := protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}
|
||||
for _, sub := range targets {
|
||||
sub.send(msg)
|
||||
if !enqueueChatLocked(sub, msg) {
|
||||
slow = append(slow, sub)
|
||||
}
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
for _, sub := range slow {
|
||||
sub.close()
|
||||
}
|
||||
}
|
||||
|
||||
// submitPrompt accepts one prompt: it starts the turn runner immediately when no
|
||||
@@ -476,7 +652,7 @@ func (h *chatHub) runTurn(room *chatRoom, p *chatQueuedPrompt, done chan struct{
|
||||
return
|
||||
}
|
||||
if first {
|
||||
if err := h.startStream(room, broker); err != nil {
|
||||
if err := h.ensureObserving(room, broker); err != nil {
|
||||
h.failPrompt(room, p, err)
|
||||
h.endRoomTurn(room)
|
||||
return
|
||||
@@ -550,7 +726,7 @@ func (h *chatHub) startStream(room *chatRoom, broker chatBroker) error {
|
||||
result <- broker.Stream(ctx, room.sessionID, func() {
|
||||
once.Do(func() { close(attached) })
|
||||
}, func(frame protocol.ChatEventFrame) error {
|
||||
h.emit(frame)
|
||||
h.emitRoom(room, frame)
|
||||
return nil
|
||||
})
|
||||
}()
|
||||
|
||||
@@ -0,0 +1,261 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
)
|
||||
|
||||
type shellBarrierBroker struct {
|
||||
*fakeChat
|
||||
read chan struct{}
|
||||
apply chan struct{}
|
||||
events chan shellBarrierEvent
|
||||
stopped chan struct{}
|
||||
reads atomic.Int32
|
||||
snapshot []protocol.ChatShell
|
||||
}
|
||||
|
||||
// Exercise the real websocket handler on a loopback scratch server and a fresh
|
||||
// SQLite database. A marker on the shared stream bounds the replay assertion
|
||||
// without sleeps, polling or a negative-read timeout.
|
||||
func TestChatShellReconciliationScratchServer(t *testing.T) {
|
||||
b := newShellBarrierBroker()
|
||||
b.snapshot = []protocol.ChatShell{{ID: "sh_old", Status: protocol.ChatShellRunning}}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
srv := httptest.NewServer(HandlerWithOptions(ctx, Options{Chat: b}, newOperatorStore(t)))
|
||||
defer srv.Close()
|
||||
conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.CloseNow()
|
||||
subscribe := func(id string) {
|
||||
t.Helper()
|
||||
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSubscribe, ID: id, ChatSubscribe: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
read := func() protocol.Message {
|
||||
t.Helper()
|
||||
var m protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
if m := read(); m.Type != protocol.PipelineList {
|
||||
t.Fatalf("server greeting = %s", m.Type)
|
||||
}
|
||||
subscribe("first")
|
||||
if m := read(); m.ID != "first" || m.Type != protocol.ChatSubscribe {
|
||||
t.Fatalf("ack = %+v", m)
|
||||
}
|
||||
shellBarrierWait(t, b.read)
|
||||
done := make(chan struct{})
|
||||
b.events <- shellBarrierEvent{protocol.ChatShell{ID: "sh_old", Status: protocol.ChatShellExited}, done}
|
||||
shellBarrierWait(t, done)
|
||||
if m := read(); m.ChatEvent == nil || m.ChatEvent.Shell.Status != protocol.ChatShellExited {
|
||||
t.Fatalf("exit not visible: %+v", m)
|
||||
}
|
||||
close(b.apply)
|
||||
subscribe("second")
|
||||
for {
|
||||
m := read()
|
||||
if m.ChatEvent != nil && m.ChatEvent.Shell.Status == protocol.ChatShellRunning {
|
||||
t.Fatalf("stale replay: %+v", m)
|
||||
}
|
||||
if m.ID == "second" {
|
||||
break
|
||||
}
|
||||
}
|
||||
done = make(chan struct{})
|
||||
b.events <- shellBarrierEvent{protocol.ChatShell{ID: "sh_marker", Status: protocol.ChatShellExited}, done}
|
||||
shellBarrierWait(t, done)
|
||||
for {
|
||||
m := read()
|
||||
if m.ChatEvent == nil {
|
||||
continue
|
||||
}
|
||||
if m.ChatEvent.Shell.Status == protocol.ChatShellRunning {
|
||||
t.Fatalf("ghost visible after apply: %+v", m)
|
||||
}
|
||||
if m.ChatEvent.Shell.ID == "sh_marker" {
|
||||
break
|
||||
}
|
||||
}
|
||||
if b.reads.Load() != 1 {
|
||||
t.Fatalf("shared observer read count = %d", b.reads.Load())
|
||||
}
|
||||
conn.CloseNow()
|
||||
shellBarrierWait(t, b.stopped)
|
||||
}
|
||||
|
||||
type shellBarrierEvent struct {
|
||||
shell protocol.ChatShell
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func newShellBarrierBroker() *shellBarrierBroker {
|
||||
return &shellBarrierBroker{fakeChat: scriptedChat(), read: make(chan struct{}, 8), apply: make(chan struct{}), events: make(chan shellBarrierEvent), stopped: make(chan struct{})}
|
||||
}
|
||||
|
||||
func (b *shellBarrierBroker) Shells(ctx context.Context, _ string) ([]protocol.ChatShell, error) {
|
||||
b.reads.Add(1)
|
||||
snapshot := append([]protocol.ChatShell(nil), b.snapshot...)
|
||||
b.read <- struct{}{}
|
||||
select {
|
||||
case <-b.apply:
|
||||
return snapshot, nil
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *shellBarrierBroker) Stream(ctx context.Context, id string, ready func(), emit func(protocol.ChatEventFrame) error) error {
|
||||
defer close(b.stopped)
|
||||
ready()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case event := <-b.events:
|
||||
if err := emit(protocol.ChatEventFrame{SessionID: id, Kind: protocol.ChatEventShell, Shell: &event.shell}); err != nil {
|
||||
return err
|
||||
}
|
||||
close(event.done)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func shellBarrierWait(t *testing.T, ch <-chan struct{}) {
|
||||
t.Helper()
|
||||
select {
|
||||
case <-ch:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("barrier timed out")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatShellReconciliationBarrier(t *testing.T) {
|
||||
for _, status := range []string{protocol.ChatShellExited, protocol.ChatShellRunning, "timeout", "killed"} {
|
||||
t.Run(status, func(t *testing.T) {
|
||||
b := newShellBarrierBroker()
|
||||
b.snapshot = []protocol.ChatShell{{ID: "sh_old", Status: protocol.ChatShellRunning}}
|
||||
h := newChatHub()
|
||||
messages := make(chan protocol.Message, 32)
|
||||
sub := h.attach("ses_1", "one", "", b, func(m protocol.Message) error { messages <- m; return nil })
|
||||
t.Cleanup(sub.close)
|
||||
result := make(chan error, 2)
|
||||
go func() { result <- h.ensureObserving(sub.room, b) }()
|
||||
shellBarrierWait(t, b.read)
|
||||
// A second observer must share the in-flight read, not apply an
|
||||
// independently completing older/newer snapshot out of order.
|
||||
go func() { result <- h.ensureObserving(sub.room, b) }()
|
||||
shell := protocol.ChatShell{ID: "sh_old", Status: status}
|
||||
if status == protocol.ChatShellRunning {
|
||||
shell.ID = "sh_new"
|
||||
shell.Command = "sleep 10"
|
||||
}
|
||||
done := make(chan struct{})
|
||||
select {
|
||||
case b.events <- shellBarrierEvent{shell, done}:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("event blocked behind readback")
|
||||
}
|
||||
shellBarrierWait(t, done)
|
||||
select {
|
||||
case m := <-messages:
|
||||
if m.ChatEvent == nil || m.ChatEvent.Shell == nil || m.ChatEvent.Shell.ID != shell.ID || m.ChatEvent.Shell.Status != status {
|
||||
t.Fatalf("live shell = %+v", m)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("live event not visible during read")
|
||||
}
|
||||
close(b.apply)
|
||||
for range 2 {
|
||||
select {
|
||||
case err := <-result:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("observer stuck")
|
||||
}
|
||||
}
|
||||
if b.reads.Load() != 1 {
|
||||
t.Fatalf("redundant/out-of-order reads: %d", b.reads.Load())
|
||||
}
|
||||
snapshot := h.shellSnapshot("ses_1")
|
||||
if status == protocol.ChatShellRunning {
|
||||
if len(snapshot) != 2 {
|
||||
t.Fatalf("new create lost: %+v", snapshot)
|
||||
}
|
||||
} else if len(snapshot) != 0 {
|
||||
t.Fatalf("terminal shell resurrected: %+v", snapshot)
|
||||
}
|
||||
// Assert the same retained state is replayed to the client.
|
||||
h.replayShells(sub)
|
||||
for range len(snapshot) {
|
||||
select {
|
||||
case m := <-messages:
|
||||
if m.ChatEvent == nil || m.ChatEvent.Shell.Status != protocol.ChatShellRunning {
|
||||
t.Fatalf("replay = %+v", m)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("replay missing")
|
||||
}
|
||||
}
|
||||
sub.close()
|
||||
shellBarrierWait(t, b.stopped)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatShellReconciliationCancelReconnectBarrier(t *testing.T) {
|
||||
b := newShellBarrierBroker()
|
||||
h := newChatHub()
|
||||
sub := h.attach("ses_1", "one", "", b, func(protocol.Message) error { return nil })
|
||||
results := make(chan error, 2)
|
||||
go func() { results <- h.ensureObserving(sub.room, b) }()
|
||||
shellBarrierWait(t, b.read)
|
||||
go func() { results <- h.ensureObserving(sub.room, b) }()
|
||||
sub.close()
|
||||
for range 2 {
|
||||
select {
|
||||
case err := <-results:
|
||||
if err == nil {
|
||||
t.Fatal("canceled observer succeeded")
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("canceled waiter stuck")
|
||||
}
|
||||
}
|
||||
shellBarrierWait(t, b.stopped)
|
||||
next := newShellBarrierBroker()
|
||||
next.snapshot = []protocol.ChatShell{{ID: "sh_new", Status: protocol.ChatShellRunning}}
|
||||
close(next.apply)
|
||||
newSub := h.attach("ses_1", "two", "", next, func(protocol.Message) error { return nil })
|
||||
t.Cleanup(newSub.close)
|
||||
if err := h.ensureObserving(newSub.room, next); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// A late callback from the canceled stream cannot poison the new room,
|
||||
// nor can a foreign-session callback from the current stream.
|
||||
h.emitRoom(sub.room, protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventShell, Shell: &protocol.ChatShell{ID: "sh_ghost", Status: protocol.ChatShellRunning}})
|
||||
h.emitRoom(newSub.room, protocol.ChatEventFrame{SessionID: "ses_foreign", Kind: protocol.ChatEventShell, Shell: &protocol.ChatShell{ID: "sh_foreign", Status: protocol.ChatShellRunning}})
|
||||
got := h.shellSnapshot("ses_1")
|
||||
if len(got) != 1 || got[0].ID != "sh_new" || len(h.shellSnapshot("ses_foreign")) != 0 {
|
||||
t.Fatalf("reconnect state = %+v", got)
|
||||
}
|
||||
newSub.close()
|
||||
shellBarrierWait(t, next.stopped)
|
||||
}
|
||||
@@ -111,7 +111,14 @@ func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatH
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "could not attach to session " + query.SessionID})
|
||||
return
|
||||
}
|
||||
// Acknowledge before the stream is allowed to fan out: otherwise an immediately
|
||||
// available retained shell can overtake the subscribe reply on this connection.
|
||||
_ = write(protocol.Message{Type: protocol.ChatSubscribe, ID: request.ID})
|
||||
if err := hub.ensureObserving(sub.room, broker); err != nil {
|
||||
sub.close()
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "could not observe session " + query.SessionID})
|
||||
return
|
||||
}
|
||||
// Replay every still-unresolved approval request the room holds, not only the
|
||||
// most recent one, so a client that attaches after two requests were raised
|
||||
// still sees both cards.
|
||||
@@ -124,9 +131,5 @@ func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatH
|
||||
// shows them rather than nothing until they exit. The message snapshot cannot
|
||||
// carry a shell — a shell outlives the turn and is not a turn — so this is the
|
||||
// shell half of the resume.
|
||||
for _, shell := range hub.shellSnapshot(query.SessionID) {
|
||||
running := shell
|
||||
frame := protocol.ChatEventFrame{SessionID: query.SessionID, Kind: protocol.ChatEventShell, Shell: &running}
|
||||
sub.send(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame})
|
||||
}
|
||||
hub.replayShells(sub)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -52,6 +53,8 @@ type fakeChat struct {
|
||||
permissionSessionID string
|
||||
permissions []protocol.ChatPermissionFrame
|
||||
permissionErr error
|
||||
shells []protocol.ChatShell
|
||||
shellErr error
|
||||
sessionCalls int
|
||||
sessions []protocol.ChatSession
|
||||
sessionErr error
|
||||
@@ -62,6 +65,8 @@ type fakeChat struct {
|
||||
started chan struct{}
|
||||
done chan struct{}
|
||||
startOnce sync.Once
|
||||
streamCalls int
|
||||
readyGate <-chan struct{}
|
||||
block bool
|
||||
subscribed bool
|
||||
promptBeforeSubscribe bool
|
||||
@@ -91,13 +96,18 @@ func (f *fakeChat) Prompt(context.Context, string, string) error {
|
||||
func (f *fakeChat) Stream(ctx context.Context, _ string, ready func(), emit func(protocol.ChatEventFrame) error) error {
|
||||
f.mu.Lock()
|
||||
f.subscribed = true
|
||||
f.streamCalls++
|
||||
readyGate := f.readyGate
|
||||
f.mu.Unlock()
|
||||
if ready != nil {
|
||||
ready()
|
||||
}
|
||||
if f.started != nil {
|
||||
f.startOnce.Do(func() { close(f.started) })
|
||||
}
|
||||
if readyGate != nil {
|
||||
<-readyGate
|
||||
}
|
||||
if ready != nil {
|
||||
ready()
|
||||
}
|
||||
if f.block {
|
||||
<-ctx.Done()
|
||||
if f.done != nil {
|
||||
@@ -116,6 +126,42 @@ func (f *fakeChat) Stream(ctx context.Context, _ string, ready func(), emit func
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestChatHubConcurrentObserversShareStream pins the start-up handoff: two
|
||||
// clients that subscribe before the broker says ready still share one firehose.
|
||||
func TestChatHubConcurrentObserversShareStream(t *testing.T) {
|
||||
broker := scriptedChat()
|
||||
broker.block = true
|
||||
broker.started = make(chan struct{})
|
||||
gate := make(chan struct{})
|
||||
broker.readyGate = gate
|
||||
hub := newChatHub()
|
||||
hub.mu.Lock()
|
||||
room := hub.roomLocked("ses_1")
|
||||
hub.mu.Unlock()
|
||||
|
||||
errs := make(chan error, 2)
|
||||
go func() { errs <- hub.ensureObserving(room, broker) }()
|
||||
select {
|
||||
case <-broker.started:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("first observer did not start the broker stream")
|
||||
}
|
||||
go func() { errs <- hub.ensureObserving(room, broker) }()
|
||||
close(gate)
|
||||
for range 2 {
|
||||
if err := <-errs; err != nil {
|
||||
t.Fatalf("ensureObserving: %v", err)
|
||||
}
|
||||
}
|
||||
broker.mu.Lock()
|
||||
calls := broker.streamCalls
|
||||
broker.mu.Unlock()
|
||||
if calls != 1 {
|
||||
t.Fatalf("broker Stream calls = %d, want 1", calls)
|
||||
}
|
||||
room.cancel()
|
||||
}
|
||||
|
||||
func (f *fakeChat) Interrupt(context.Context, string) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
@@ -151,6 +197,12 @@ func (f *fakeChat) Reply(_ context.Context, request protocol.ChatReplyRequest) e
|
||||
// Permissions returns the seeded pending requests, or blocks until the caller's
|
||||
// context is canceled when permBlock is set (used to prove the poller stops on
|
||||
// disconnect). Every call is counted so a test can assert polling happened.
|
||||
func (f *fakeChat) Shells(context.Context, string) ([]protocol.ChatShell, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return append([]protocol.ChatShell(nil), f.shells...), f.shellErr
|
||||
}
|
||||
|
||||
func (f *fakeChat) Permissions(ctx context.Context, sessionID string) ([]protocol.ChatPermissionFrame, error) {
|
||||
f.mu.Lock()
|
||||
f.permissionCalls++
|
||||
@@ -682,6 +734,9 @@ func TestChatInterruptEndsRunningPrompt(t *testing.T) {
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/permission/request":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"location":{"directory":"/work"},"data":[]}`))
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/shell":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":[]}`))
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/event":
|
||||
flusher, ok := w.(http.Flusher)
|
||||
if !ok {
|
||||
@@ -995,6 +1050,7 @@ func TestChatPromptStreamsShellFrame(t *testing.T) {
|
||||
// shows the background process instead of nothing until it exits.
|
||||
func TestChatSubscribeReplaysRunningShell(t *testing.T) {
|
||||
broker := scriptedChat()
|
||||
broker.shells = []protocol.ChatShell{{ID: "sh_1", Status: protocol.ChatShellRunning, Command: "sleep 120"}}
|
||||
broker.script = append([]protocol.ChatEventFrame{
|
||||
{SessionID: "ses_1", Kind: protocol.ChatEventShell, Shell: &protocol.ChatShell{ID: "sh_1", Status: protocol.ChatShellRunning, Command: "sleep 120"}},
|
||||
}, broker.script...)
|
||||
@@ -1039,6 +1095,40 @@ func TestChatSubscribeReplaysRunningShell(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatSubscribeKeepsStreamWhenShellReadbackFails ensures a temporary
|
||||
// authoritative readback failure does not make chat unavailable or replay a
|
||||
// stale process as running. The subscriber receives an explicit limitation
|
||||
// while its broker stream remains attached for future lifecycle events.
|
||||
func TestChatSubscribeKeepsStreamWhenShellReadbackFails(t *testing.T) {
|
||||
broker := scriptedChat()
|
||||
broker.block = true
|
||||
broker.shellErr = errors.New("shell endpoint unavailable: https://user:secret@example.invalid private response body")
|
||||
ctx, _, conn := dialChat(t, broker)
|
||||
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSubscribe, ID: "sub1", ChatSubscribe: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ack.Type != protocol.ChatSubscribe || ack.ID != "sub1" {
|
||||
t.Fatalf("subscribe ack = %+v", ack)
|
||||
}
|
||||
var limited protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &limited); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if limited.Type != protocol.Error || limited.Error != "background shell visibility is limited" {
|
||||
t.Fatalf("limited visibility notice = %+v", limited)
|
||||
}
|
||||
broker.mu.Lock()
|
||||
calls := broker.streamCalls
|
||||
broker.mu.Unlock()
|
||||
if calls != 1 {
|
||||
t.Fatalf("broker Stream calls = %d, want 1", calls)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatPromptSubscribesBeforePrompt pins the subscribe-before-prompt order:
|
||||
// the firehose has no replay cursor, so the subscription must be attached before
|
||||
// the prompt is posted or deltas emitted in between are lost.
|
||||
@@ -1079,6 +1169,9 @@ func realAdapterChat(t *testing.T, frames []string) []protocol.ChatEventFrame {
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/permission/request":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"location":{"directory":"/work"},"data":[]}`))
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/shell":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":[]}`))
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/event":
|
||||
streams.Add(1)
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
|
||||
Reference in New Issue
Block a user