Persist and broadcast shared attention acknowledgements
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// attentionAckStore persists the shared in-app acknowledgements.
|
||||
// *pipeline.Store implements it.
|
||||
type attentionAckStore interface {
|
||||
AttentionAcks(context.Context) (protocol.AttentionAcks, error)
|
||||
AckAttention(context.Context, *protocol.AttentionAcks, time.Time) (delta, all protocol.AttentionAcks, err error)
|
||||
}
|
||||
|
||||
// attentionAckHub fans an accepted acknowledgement out to every other open
|
||||
// connection, so a dismissal on one device disappears on the others live.
|
||||
// It holds each connection's serialized write function, never the socket.
|
||||
type attentionAckHub struct {
|
||||
mu sync.Mutex
|
||||
clients map[string]func(protocol.Message) error
|
||||
}
|
||||
|
||||
func newAttentionAckHub() *attentionAckHub {
|
||||
return &attentionAckHub{clients: map[string]func(protocol.Message) error{}}
|
||||
}
|
||||
|
||||
func (h *attentionAckHub) attach(clientID string, write func(protocol.Message) error) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.clients[clientID] = write
|
||||
}
|
||||
|
||||
func (h *attentionAckHub) detach(clientID string) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
delete(h.clients, clientID)
|
||||
}
|
||||
|
||||
// broadcast pushes delta to every connection except the sender. Each write
|
||||
// runs on its own goroutine (bounded by the connection's own write timeout), so
|
||||
// one slow client cannot delay the sender's reply or the other clients.
|
||||
func (h *attentionAckHub) broadcast(sender string, delta protocol.AttentionAcks) {
|
||||
h.mu.Lock()
|
||||
targets := make([]func(protocol.Message) error, 0, len(h.clients))
|
||||
for id, write := range h.clients {
|
||||
if id != sender {
|
||||
targets = append(targets, write)
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
for _, write := range targets {
|
||||
frame := delta
|
||||
go func() {
|
||||
_ = write(protocol.Message{Type: protocol.AttentionAcksChanged, AttentionAcks: &frame})
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// handleAttentionAcks answers attention_acks and ack_attention. It reports
|
||||
// whether request was one of them. An ack that changes nothing (already
|
||||
// dismissed, or a watermark that does not advance) replies with the whole set
|
||||
// but pushes nothing.
|
||||
func handleAttentionAcks(ctx context.Context, request protocol.Message, store attentionAckStore, hub *attentionAckHub, clientID string, write func(protocol.Message) error) bool {
|
||||
if request.Type != protocol.GetAttentionAcks && request.Type != protocol.AckAttention {
|
||||
return false
|
||||
}
|
||||
if store == nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "attention acknowledgements are unavailable: no store"})
|
||||
return true
|
||||
}
|
||||
var all, delta protocol.AttentionAcks
|
||||
var err error
|
||||
if request.Type == protocol.GetAttentionAcks {
|
||||
all, err = store.AttentionAcks(ctx)
|
||||
} else {
|
||||
delta, all, err = store.AckAttention(ctx, request.AttentionAcks, time.Now())
|
||||
}
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return true
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.AttentionAcksReply, ID: request.ID, AttentionAcks: &all})
|
||||
if hub != nil && !delta.Empty() {
|
||||
hub.broadcast(clientID, delta)
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func dialAckClient(t *testing.T, ctx context.Context, url string) *websocket.Conn {
|
||||
t.Helper()
|
||||
conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(url, "http")+"/connect", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { conn.CloseNow() })
|
||||
var welcome protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &welcome); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return conn
|
||||
}
|
||||
|
||||
func ackExchange(t *testing.T, ctx context.Context, conn *websocket.Conn, request protocol.Message) protocol.Message {
|
||||
t.Helper()
|
||||
if err := wsjson.Write(ctx, conn, request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var reply protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &reply); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return reply
|
||||
}
|
||||
|
||||
func TestAttentionAcksSyncAcrossConnections(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
store := newOperatorStore(t)
|
||||
if _, err := store.Submit(ctx, "acks", pipeline.Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, err := store.List(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
definition, err := store.Get(ctx, items[0].ID, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
run, _, err := store.CreateRun(ctx, definition, "ack-run", t.TempDir(), false, nil, map[string]any{"prompt": "task"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.RecordOperatorSession(ctx, "ses_sync", "/work", "Sync", "operator"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
handlerCtx, stop := context.WithCancel(ctx)
|
||||
handler := Handler(handlerCtx, store)
|
||||
defer func() { stop(); WaitRuns(handler) }()
|
||||
srv := httptest.NewServer(handler)
|
||||
defer srv.Close()
|
||||
web := dialAckClient(t, ctx, srv.URL)
|
||||
phone := dialAckClient(t, ctx, srv.URL)
|
||||
|
||||
initial := ackExchange(t, ctx, phone, protocol.Message{Type: protocol.GetAttentionAcks, ID: "get"})
|
||||
if initial.Type != protocol.AttentionAcksReply || initial.ID != "get" || initial.AttentionAcks == nil || !initial.AttentionAcks.Empty() {
|
||||
t.Fatalf("initial = %+v", initial)
|
||||
}
|
||||
key := fmt.Sprintf("%d:Failed", run)
|
||||
reply := ackExchange(t, ctx, web, protocol.Message{Type: protocol.AckAttention, ID: "ack", AttentionAcks: &protocol.AttentionAcks{
|
||||
Dismissed: []string{key},
|
||||
ChatSeen: map[string]int64{"ses_sync": 1},
|
||||
}})
|
||||
if reply.Type != protocol.AttentionAcksReply || reply.ID != "ack" || len(reply.AttentionAcks.Dismissed) != 1 || reply.AttentionAcks.ChatSeen["ses_sync"] == 0 {
|
||||
t.Fatalf("ack reply = %+v", reply)
|
||||
}
|
||||
var pushed protocol.Message
|
||||
if err := wsjson.Read(ctx, phone, &pushed); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pushed.Type != protocol.AttentionAcksChanged || pushed.ID != "" || pushed.AttentionAcks == nil ||
|
||||
len(pushed.AttentionAcks.Dismissed) != 1 || pushed.AttentionAcks.Dismissed[0] != key ||
|
||||
pushed.AttentionAcks.ChatSeen["ses_sync"] != reply.AttentionAcks.ChatSeen["ses_sync"] {
|
||||
t.Fatalf("pushed = %+v", pushed)
|
||||
}
|
||||
|
||||
// Malformed acks are refused and push nothing: the next frame the phone
|
||||
// reads is the reply to its own request.
|
||||
for _, bad := range []*protocol.AttentionAcks{nil, {Dismissed: []string{"1:Running"}}, {ChatSeen: map[string]int64{"bad id": 3}}} {
|
||||
refused := ackExchange(t, ctx, web, protocol.Message{Type: protocol.AckAttention, ID: "bad", AttentionAcks: bad})
|
||||
if refused.Type != protocol.Error || refused.Error == "" || refused.AttentionAcks != nil {
|
||||
t.Fatalf("malformed ack accepted: %+v", refused)
|
||||
}
|
||||
}
|
||||
// A repeated ack changes nothing and pushes nothing.
|
||||
ackExchange(t, ctx, web, protocol.Message{Type: protocol.AckAttention, ID: "again", AttentionAcks: &protocol.AttentionAcks{Dismissed: []string{key}}})
|
||||
after := ackExchange(t, ctx, phone, protocol.Message{Type: protocol.GetAttentionAcks, ID: "after"})
|
||||
if after.Type != protocol.AttentionAcksReply || after.ID != "after" || len(after.AttentionAcks.Dismissed) != 1 {
|
||||
t.Fatalf("phone read after malformed/repeated acks = %+v", after)
|
||||
}
|
||||
}
|
||||
@@ -113,6 +113,11 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
settingsStore = stores[0]
|
||||
}
|
||||
hub.settings = settingsStore
|
||||
acks := newAttentionAckHub()
|
||||
var ackStore attentionAckStore
|
||||
if len(stores) > 0 {
|
||||
ackStore = stores[0]
|
||||
}
|
||||
mux.HandleFunc("GET /artifacts/{run}/{step}/{name...}", func(w http.ResponseWriter, r *http.Request) {
|
||||
if runs == nil {
|
||||
http.NotFound(w, r)
|
||||
@@ -180,6 +185,10 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
return
|
||||
}
|
||||
}
|
||||
// Shared attention acknowledgements reach every open connection, after
|
||||
// the welcome frame so a push never precedes it.
|
||||
acks.attach(clientID, write)
|
||||
defer acks.detach(clientID)
|
||||
for {
|
||||
var request protocol.Message
|
||||
if err := wsjson.Read(connCtx, conn, &request); err != nil {
|
||||
@@ -196,6 +205,9 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
if handleNotificationSettings(ctx, request, settingsStore, write) {
|
||||
continue
|
||||
}
|
||||
if handleAttentionAcks(ctx, request, ackStore, acks, clientID, write) {
|
||||
continue
|
||||
}
|
||||
if handleRunEvents(request, events, runs, clientID, write) {
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user