fix(chat): settle authoritative turns and make capabilities explicit

This commit is contained in:
2026-10-20 15:45:09 +01:00
parent 016fcb552a
commit e504780976
16 changed files with 615 additions and 56 deletions
+76 -1
View File
@@ -6,6 +6,7 @@ import (
"net/http/httptest"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@@ -16,6 +17,71 @@ import (
"git.a-wan.net/alan/pipelines/internal/protocol"
)
type reconnectingChat struct {
*channelChat
streams atomic.Int32
disconnect chan struct{}
reconnected chan struct{}
}
func (c *reconnectingChat) Stream(ctx context.Context, id string, ready func(), emit func(protocol.ChatEventFrame) error) error {
n := c.streams.Add(1)
if ready != nil {
ready()
}
if n == 1 {
select {
case <-c.disconnect:
return errors.New("test transport disconnect")
case <-ctx.Done():
return ctx.Err()
}
}
if n == 2 {
close(c.reconnected)
}
for {
select {
case <-ctx.Done():
return ctx.Err()
case frame := <-c.frames:
if err := emit(frame); err != nil {
return err
}
}
}
}
func TestChatStreamDisconnectPreservesQueuedTurn(t *testing.T) {
broker := &reconnectingChat{channelChat: newChannelChat(), disconnect: make(chan struct{}), reconnected: make(chan struct{})}
ctx, _, _, conns := dialChatServer(t, Options{Chat: broker}, 1)
conn := conns[0]
for _, text := range []string{"one", "two"} {
writeChat(t, ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: text, ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: text}})
ack := readUntil(t, ctx, conn, protocol.ChatPrompt)
if ack.ChatQueued != (text == "two") {
t.Fatalf("ack = %+v", ack)
}
}
close(broker.disconnect)
select {
case <-broker.reconnected:
case <-ctx.Done():
t.Fatal("stream did not reconnect")
}
if broker.promptCount() != 1 {
t.Fatal("transport loss advanced queue")
}
broker.push(protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded}})
deadline := time.Now().Add(time.Second)
for broker.promptCount() < 2 && time.Now().Before(deadline) {
time.Sleep(5 * time.Millisecond)
}
if broker.promptCount() != 2 {
t.Fatal("authoritative completion did not advance queue")
}
}
// channelChat is a broker whose stream is driven frame-by-frame from a channel,
// so a test can hold a turn in flight and prove prompt serialisation, fan-out and
// exactly-once approval behaviour without a real OpenCode service.
@@ -286,7 +352,16 @@ func TestChatPromptSerialisedPerSession(t *testing.T) {
t.Fatalf("prompts forwarded = %d, want 1 while the first turn is in flight", broker.promptCount())
}
// End the first turn; the queued prompt is then posted.
// Provider/tool steps are not foreground-drain completion. Neither step
// boundary may start the queued prompt, including after a subscriber attach.
for i := 0; i < 2; i++ {
broker.push(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, Index: i + 1, Intermediate: true}})
time.Sleep(30 * time.Millisecond)
if broker.promptCount() != 1 {
t.Fatalf("intermediate step advanced queue: %v", broker.promptTexts())
}
}
// End the first turn authoritatively; the queued prompt is then posted.
broker.push(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, Index: 1}})
deadline := time.Now().Add(3 * time.Second)
for broker.promptCount() < 2 {