Merge branch 'worktree-agent-ae57d5ca18be911ad'
This commit is contained in:
@@ -474,14 +474,39 @@ func (s *session) emit(frame protocol.ChatEventFrame) {
|
||||
targets = append(targets, ch)
|
||||
}
|
||||
s.mu.Unlock()
|
||||
lifecycle := frameIsLifecycle(frame)
|
||||
for _, ch := range targets {
|
||||
select {
|
||||
case ch <- frame:
|
||||
continue
|
||||
default:
|
||||
}
|
||||
if !lifecycle {
|
||||
continue
|
||||
}
|
||||
// A full watcher may lose a delta (the hub repairs text from
|
||||
// history), but never a step start or end: a lost end leaves the
|
||||
// turn "working" forever. Wait, bounded, for the watcher to drain.
|
||||
timer := time.NewTimer(lifecycleDeliveryTimeout)
|
||||
select {
|
||||
case ch <- frame:
|
||||
case <-timer.C:
|
||||
log.Printf("claude session %s: a watcher did not accept the %s frame within %s; it was dropped", s.id, frame.Kind, lifecycleDeliveryTimeout)
|
||||
}
|
||||
timer.Stop()
|
||||
}
|
||||
}
|
||||
|
||||
// lifecycleDeliveryTimeout bounds how long emit waits for a full watcher to
|
||||
// accept a turn lifecycle frame before it gives up on that watcher.
|
||||
var lifecycleDeliveryTimeout = 5 * time.Second
|
||||
|
||||
// frameIsLifecycle reports whether a frame changes the turn's state, so it
|
||||
// must reach every watcher even when that watcher's buffer is full.
|
||||
func frameIsLifecycle(frame protocol.ChatEventFrame) bool {
|
||||
return frame.Kind == protocol.ChatEventStep || frame.Kind == protocol.ChatEventResync
|
||||
}
|
||||
|
||||
func (s *session) subscribe() chan protocol.ChatEventFrame {
|
||||
ch := make(chan protocol.ChatEventFrame, 256)
|
||||
s.mu.Lock()
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// A watcher whose buffer is full of streamed text may lose a delta, but never
|
||||
// the turn's end: a lost end left the chat "working" forever.
|
||||
func TestEmitDeliversTheTurnEndToAFullWatcher(t *testing.T) {
|
||||
c := newChat(context.Background())
|
||||
t.Cleanup(c.cancel)
|
||||
s := c.sessionFor("ses_full", t.TempDir())
|
||||
ch := s.subscribe()
|
||||
defer s.unsubscribe(ch)
|
||||
for len(ch) < cap(ch) {
|
||||
s.emit(protocol.ChatEventFrame{Kind: protocol.ChatEventText, Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "p", Text: "x"}})
|
||||
}
|
||||
emitted := make(chan struct{})
|
||||
go func() {
|
||||
defer close(emitted)
|
||||
s.emit(protocol.ChatEventFrame{Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, TurnID: "turn_1"}})
|
||||
}()
|
||||
// The watcher is slow, not gone: it drains only after the end was emitted.
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
deadline := time.After(3 * time.Second)
|
||||
for {
|
||||
select {
|
||||
case frame := <-ch:
|
||||
if frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded {
|
||||
<-emitted
|
||||
return
|
||||
}
|
||||
case <-deadline:
|
||||
t.Fatal("the turn end never reached the full watcher")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A watcher that never drains does not hold the CLI reader forever.
|
||||
func TestEmitGivesUpOnAStuckWatcher(t *testing.T) {
|
||||
prior := lifecycleDeliveryTimeout
|
||||
lifecycleDeliveryTimeout = 50 * time.Millisecond
|
||||
t.Cleanup(func() { lifecycleDeliveryTimeout = prior })
|
||||
c := newChat(context.Background())
|
||||
t.Cleanup(c.cancel)
|
||||
s := c.sessionFor("ses_stuck", t.TempDir())
|
||||
ch := s.subscribe()
|
||||
defer s.unsubscribe(ch)
|
||||
for len(ch) < cap(ch) {
|
||||
s.emit(protocol.ChatEventFrame{Kind: protocol.ChatEventText, Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "p", Text: "x"}})
|
||||
}
|
||||
start := time.Now()
|
||||
s.emit(protocol.ChatEventFrame{Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, TurnID: "turn_1"}})
|
||||
if waited := time.Since(start); waited > 2*time.Second {
|
||||
t.Fatalf("emit blocked %s on a stuck watcher", waited)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user