3e267d4863
emit now waits (bounded) for a full watcher to accept step and resync frames instead of dropping them. The turn runner also ends its turn once a harness that tracks its turn state has reported idle for the settle period and its probe confirms, asking clients to resync. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
91 lines
2.9 KiB
Go
91 lines
2.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// reportingChat is an observedChat that also reports its turn state without
|
|
// I/O, as Claude Code does.
|
|
type reportingChat struct {
|
|
*observedChat
|
|
rmu sync.Mutex
|
|
running bool
|
|
}
|
|
|
|
func (c *reportingChat) TurnRunning(string) bool {
|
|
c.rmu.Lock()
|
|
defer c.rmu.Unlock()
|
|
return c.running
|
|
}
|
|
|
|
// Prompt starts a turn, as Claude Code's does before it returns.
|
|
func (c *reportingChat) Prompt(ctx context.Context, sessionID, text string) error {
|
|
if err := c.observedChat.Prompt(ctx, sessionID, text); err != nil {
|
|
return err
|
|
}
|
|
c.setRunning(true)
|
|
return nil
|
|
}
|
|
|
|
func (c *reportingChat) setRunning(running bool) {
|
|
c.rmu.Lock()
|
|
c.running = running
|
|
c.rmu.Unlock()
|
|
}
|
|
|
|
// The live incident: a Claude turn's end frame was dropped on the way (a full
|
|
// watcher buffer), so the room's own turn stayed "working" forever and every
|
|
// later prompt queued behind it. Once the harness reports itself idle for the
|
|
// settle period and its probe agrees, the turn is ended, the client told to
|
|
// resync, and the queued prompt delivered. While it reports running, it waits.
|
|
func TestChatRunnerEndsItsTurnWhoseEndFrameWasLost(t *testing.T) {
|
|
fastUnattended(t)
|
|
fastStaleTurnSettle(t)
|
|
t.Setenv("PIPELINES_NTFY_URL", "")
|
|
store := openQueueStore(t, filepath.Join(t.TempDir(), "queue.db"))
|
|
broker := &reportingChat{observedChat: newObservedChat()}
|
|
server := startQueueServer(t, broker, store)
|
|
conn := dialChatConn(t, server.ctx, server.srv)
|
|
broker.setProbe(true)
|
|
sendPrompt(t, server.ctx, conn, "p1", "build it", "op-1")
|
|
waitFor(t, "first prompt", func() bool { return broker.promptCount() == 1 })
|
|
broker.push(stepFrameFor(protocol.ChatStepStarted, "turn_1"))
|
|
waitStepFrame(t, server.ctx, conn, protocol.ChatStepStarted, "turn_1")
|
|
if ack := sendPrompt(t, server.ctx, conn, "p2", "continue", "op-2"); !ack.ChatQueued {
|
|
t.Fatalf("second prompt was not queued behind the running turn: %+v", ack)
|
|
}
|
|
time.Sleep(4 * chatQueueStaleTurnSettle)
|
|
if n := broker.promptCount(); n != 1 {
|
|
t.Fatalf("prompt delivered while the harness reports its turn running: %v", broker.promptTexts())
|
|
}
|
|
// The turn ends in the harness, but its end frame never arrives.
|
|
broker.setRunning(false)
|
|
broker.setProbe(false)
|
|
sawResync := false
|
|
for {
|
|
message := readChatMessage(t, server.ctx, conn)
|
|
if message.Type != protocol.ChatEvent || message.ChatEvent == nil {
|
|
continue
|
|
}
|
|
if message.ChatEvent.Kind == protocol.ChatEventResync {
|
|
sawResync = true
|
|
}
|
|
if step := message.ChatEvent.Step; step != nil && step.Status == protocol.ChatStepEnded && step.TurnID == "turn_1" {
|
|
break
|
|
}
|
|
}
|
|
if !sawResync {
|
|
t.Fatal("the client was not told to resync after the lost end")
|
|
}
|
|
waitFor(t, "queued prompt delivered after the lost end", func() bool { return broker.promptCount() == 2 })
|
|
if texts := broker.promptTexts(); texts[1] != "continue" {
|
|
t.Fatalf("delivered = %v", texts)
|
|
}
|
|
}
|