Persist and broadcast shared attention acknowledgements
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// AttentionAckRetention bounds how long an acknowledgement is kept. The
|
||||
// Needs-you list shows only today's work and a chat watermark is refreshed
|
||||
// whenever the chat is opened, so a month-old acknowledgement protects nothing.
|
||||
const AttentionAckRetention = 30 * 24 * time.Hour
|
||||
|
||||
// initAttentionAcks creates the two additive acknowledgement tables. They are
|
||||
// tables of their own so no run or session schema changes; run_id is kept as a
|
||||
// column so pruning against pipeline_runs uses an integer comparison.
|
||||
func (s *Store) initAttentionAcks() error {
|
||||
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS attention_dismissals (
|
||||
key TEXT PRIMARY KEY, run_id INTEGER NOT NULL, dismissed_at INTEGER NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS chat_seen_watermarks (
|
||||
session_id TEXT PRIMARY KEY, seen_at INTEGER NOT NULL, acked_at INTEGER NOT NULL);`)
|
||||
return err
|
||||
}
|
||||
|
||||
type attentionQuerier interface {
|
||||
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
|
||||
}
|
||||
|
||||
func readAttentionAcks(ctx context.Context, q attentionQuerier) (protocol.AttentionAcks, error) {
|
||||
acks := protocol.AttentionAcks{Dismissed: []string{}, ChatSeen: map[string]int64{}}
|
||||
rows, err := q.QueryContext(ctx, `SELECT key FROM attention_dismissals ORDER BY key`)
|
||||
if err != nil {
|
||||
return acks, err
|
||||
}
|
||||
for rows.Next() {
|
||||
var key string
|
||||
if err := rows.Scan(&key); err != nil {
|
||||
rows.Close()
|
||||
return acks, err
|
||||
}
|
||||
acks.Dismissed = append(acks.Dismissed, key)
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return acks, err
|
||||
}
|
||||
rows, err = q.QueryContext(ctx, `SELECT session_id, seen_at FROM chat_seen_watermarks`)
|
||||
if err != nil {
|
||||
return acks, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var id string
|
||||
var at int64
|
||||
if err := rows.Scan(&id, &at); err != nil {
|
||||
return acks, err
|
||||
}
|
||||
acks.ChatSeen[id] = at
|
||||
}
|
||||
return acks, rows.Err()
|
||||
}
|
||||
|
||||
// AttentionAcks returns every stored acknowledgement.
|
||||
func (s *Store) AttentionAcks(ctx context.Context) (protocol.AttentionAcks, error) {
|
||||
return readAttentionAcks(ctx, s.db)
|
||||
}
|
||||
|
||||
// AckAttention validates and records one acknowledgement, prunes stale rows,
|
||||
// and returns the delta that actually changed the stored set together with the
|
||||
// whole set afterwards.
|
||||
//
|
||||
// A dismissal of a run that does not exist is skipped, and so is a watermark
|
||||
// for a session that is not in the operator registry, so a client cannot grow
|
||||
// the tables with invented ids. A watermark only advances: the stored value is
|
||||
// the maximum of the old value, the client's value and the session's own
|
||||
// registry recency, so a device that opened the chat from a stale list row
|
||||
// still acknowledges everything the server listed at that moment.
|
||||
func (s *Store) AckAttention(ctx context.Context, ack *protocol.AttentionAcks, now time.Time) (delta, all protocol.AttentionAcks, err error) {
|
||||
valid, err := protocol.ValidateAttentionAck(ack)
|
||||
if err != nil {
|
||||
return delta, all, err
|
||||
}
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return delta, all, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
nowMillis := now.UnixMilli()
|
||||
delta = protocol.AttentionAcks{Dismissed: []string{}, ChatSeen: map[string]int64{}}
|
||||
for _, key := range valid.Dismissed {
|
||||
runID, _ := strconv.ParseInt(key[:strings.IndexByte(key, ':')], 10, 64)
|
||||
result, err := tx.ExecContext(ctx, `INSERT INTO attention_dismissals(key, run_id, dismissed_at)
|
||||
SELECT ?, id, ? FROM pipeline_runs WHERE id=? ON CONFLICT(key) DO NOTHING`, key, nowMillis, runID)
|
||||
if err != nil {
|
||||
return delta, all, err
|
||||
}
|
||||
if n, _ := result.RowsAffected(); n > 0 {
|
||||
delta.Dismissed = append(delta.Dismissed, key)
|
||||
}
|
||||
}
|
||||
ids := make([]string, 0, len(valid.ChatSeen))
|
||||
for id := range valid.ChatSeen {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
for _, id := range ids {
|
||||
var recency, stored int64
|
||||
var hasStored bool
|
||||
err := tx.QueryRowContext(ctx, `SELECT last_used_at FROM operator_sessions WHERE session_id=?`, id).Scan(&recency)
|
||||
if err == sql.ErrNoRows {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return delta, all, err
|
||||
}
|
||||
switch err := tx.QueryRowContext(ctx, `SELECT seen_at FROM chat_seen_watermarks WHERE session_id=?`, id).Scan(&stored); err {
|
||||
case nil:
|
||||
hasStored = true
|
||||
case sql.ErrNoRows:
|
||||
default:
|
||||
return delta, all, err
|
||||
}
|
||||
next := max(valid.ChatSeen[id], recency, stored)
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO chat_seen_watermarks(session_id, seen_at, acked_at) VALUES(?,?,?)
|
||||
ON CONFLICT(session_id) DO UPDATE SET seen_at=excluded.seen_at, acked_at=excluded.acked_at`, id, next, nowMillis); err != nil {
|
||||
return delta, all, err
|
||||
}
|
||||
if !hasStored || next > stored {
|
||||
delta.ChatSeen[id] = next
|
||||
}
|
||||
}
|
||||
if err := pruneAttentionAcks(ctx, tx, now); err != nil {
|
||||
return delta, all, err
|
||||
}
|
||||
all, err = readAttentionAcks(ctx, tx)
|
||||
if err != nil {
|
||||
return delta, all, err
|
||||
}
|
||||
return delta, all, tx.Commit()
|
||||
}
|
||||
|
||||
// PruneAttentionAcks drops acknowledgements for runs and sessions that no
|
||||
// longer exist and any older than AttentionAckRetention.
|
||||
func (s *Store) PruneAttentionAcks(ctx context.Context, now time.Time) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err := pruneAttentionAcks(ctx, tx, now); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func pruneAttentionAcks(ctx context.Context, tx *sql.Tx, now time.Time) error {
|
||||
cutoff := now.Add(-AttentionAckRetention).UnixMilli()
|
||||
for _, statement := range []string{
|
||||
`DELETE FROM attention_dismissals WHERE dismissed_at < ? OR run_id NOT IN (SELECT id FROM pipeline_runs)`,
|
||||
`DELETE FROM chat_seen_watermarks WHERE acked_at < ? OR session_id NOT IN (SELECT session_id FROM operator_sessions)`,
|
||||
} {
|
||||
if _, err := tx.ExecContext(ctx, statement, cutoff); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func attentionAckFixture(t *testing.T) (*Store, string, []int64) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "acks.db")
|
||||
s, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { s.Close() })
|
||||
if _, err := s.Submit(ctx, "acks", Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, err := s.List(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
definition, err := s.Get(ctx, items[0].ID, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var runs []int64
|
||||
for i := range 2 {
|
||||
id, _, err := s.CreateRun(ctx, definition, fmt.Sprintf("ack-%d", i), "/work/acks", false, nil, map[string]any{"prompt": "task"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runs = append(runs, id)
|
||||
}
|
||||
if err := s.RecordOperatorSession(ctx, "ses_seen", "/work/acks", "Seen", "operator"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return s, path, runs
|
||||
}
|
||||
|
||||
func TestAttentionAcksPersistAdvanceAndSkipUnknown(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, path, runs := attentionAckFixture(t)
|
||||
empty, err := s.AttentionAcks(ctx)
|
||||
if err != nil || len(empty.Dismissed) != 0 || len(empty.ChatSeen) != 0 {
|
||||
t.Fatalf("fresh acks = %+v, %v", empty, err)
|
||||
}
|
||||
session, _, err := s.OperatorSession(ctx, "ses_seen")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
failed := fmt.Sprintf("%d:Failed", runs[0])
|
||||
decision := fmt.Sprintf("%d:Decision", runs[1])
|
||||
now := time.Now()
|
||||
delta, all, err := s.AckAttention(ctx, &protocol.AttentionAcks{
|
||||
Dismissed: []string{failed, failed, decision, "999999:Failed"},
|
||||
ChatSeen: map[string]int64{"ses_seen": session.UpdatedAt + 5, "ses_gone": 10},
|
||||
}, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wantKeys := []string{decision, failed}
|
||||
if decision > failed {
|
||||
wantKeys = []string{failed, decision}
|
||||
}
|
||||
if !reflect.DeepEqual(delta.Dismissed, wantKeys) || !reflect.DeepEqual(all.Dismissed, wantKeys) {
|
||||
t.Fatalf("dismissed delta=%v all=%v, want %v (unknown run skipped)", delta.Dismissed, all.Dismissed, wantKeys)
|
||||
}
|
||||
if len(delta.ChatSeen) != 1 || delta.ChatSeen["ses_seen"] != session.UpdatedAt+5 || len(all.ChatSeen) != 1 {
|
||||
t.Fatalf("chat seen delta=%v all=%v", delta.ChatSeen, all.ChatSeen)
|
||||
}
|
||||
// A repeated dismissal and an older watermark change nothing: the stored
|
||||
// value never moves backwards and the delta is empty.
|
||||
delta, all, err = s.AckAttention(ctx, &protocol.AttentionAcks{Dismissed: []string{failed}, ChatSeen: map[string]int64{"ses_seen": 1}}, now)
|
||||
if err != nil || !delta.Empty() || all.ChatSeen["ses_seen"] != session.UpdatedAt+5 {
|
||||
t.Fatalf("repeat delta=%+v all=%+v err=%v", delta, all, err)
|
||||
}
|
||||
// A stale client value is lifted to the session's registry recency.
|
||||
recency := session.UpdatedAt + 60_000
|
||||
if _, err := s.db.ExecContext(ctx, `UPDATE operator_sessions SET last_used_at=? WHERE session_id='ses_seen'`, recency); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delta, _, err = s.AckAttention(ctx, &protocol.AttentionAcks{ChatSeen: map[string]int64{"ses_seen": 1}}, now)
|
||||
if err != nil || delta.ChatSeen["ses_seen"] != recency {
|
||||
t.Fatalf("lifted delta=%+v err=%v (recency %d)", delta, err, recency)
|
||||
}
|
||||
if err := s.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reopened, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reopened.Close()
|
||||
again, err := reopened.AttentionAcks(ctx)
|
||||
if err != nil || !reflect.DeepEqual(again.Dismissed, wantKeys) || again.ChatSeen["ses_seen"] == 0 {
|
||||
t.Fatalf("after reopen = %+v, %v", again, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttentionAcksPruneDeletedAndExpired(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, _, runs := attentionAckFixture(t)
|
||||
now := time.Now()
|
||||
old := now.Add(-AttentionAckRetention - time.Hour)
|
||||
if _, _, err := s.AckAttention(ctx, &protocol.AttentionAcks{Dismissed: []string{fmt.Sprintf("%d:Failed", runs[0])}}, old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := s.AckAttention(ctx, &protocol.AttentionAcks{
|
||||
Dismissed: []string{fmt.Sprintf("%d:Failed", runs[1])},
|
||||
ChatSeen: map[string]int64{"ses_seen": 5},
|
||||
}, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
all, _ := s.AttentionAcks(ctx)
|
||||
if len(all.Dismissed) != 1 || all.Dismissed[0] != fmt.Sprintf("%d:Failed", runs[1]) {
|
||||
t.Fatalf("expired dismissal not pruned: %+v", all)
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx, `DELETE FROM pipeline_runs WHERE id=?`, runs[1]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx, `DELETE FROM operator_sessions WHERE session_id='ses_seen'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.PruneAttentionAcks(ctx, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
all, err := s.AttentionAcks(ctx)
|
||||
if err != nil || len(all.Dismissed) != 0 || len(all.ChatSeen) != 0 {
|
||||
t.Fatalf("acks for deleted run/session not pruned: %+v, %v", all, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttentionAcksRejectMalformed(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, _, runs := attentionAckFixture(t)
|
||||
tooMany := make([]string, protocol.MaxAttentionAckItems+1)
|
||||
for i := range tooMany {
|
||||
tooMany[i] = fmt.Sprintf("%d:Failed", i+1)
|
||||
}
|
||||
for name, ack := range map[string]*protocol.AttentionAcks{
|
||||
"nil": nil,
|
||||
"empty": {},
|
||||
"kind": {Dismissed: []string{fmt.Sprintf("%d:Running", runs[0])}},
|
||||
"zero run": {Dismissed: []string{"0:Failed"}},
|
||||
"bare": {Dismissed: []string{"Failed"}},
|
||||
"session id": {ChatSeen: map[string]int64{"../etc": 4}},
|
||||
"non-positive": {ChatSeen: map[string]int64{"ses_seen": 0}},
|
||||
"too many": {Dismissed: tooMany},
|
||||
} {
|
||||
if _, _, err := s.AckAttention(ctx, ack, time.Now()); err == nil {
|
||||
t.Errorf("%s: malformed ack accepted", name)
|
||||
}
|
||||
}
|
||||
all, _ := s.AttentionAcks(ctx)
|
||||
if !all.Empty() {
|
||||
t.Fatalf("a refused ack wrote something: %+v", all)
|
||||
}
|
||||
}
|
||||
@@ -77,6 +77,10 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := s.initAttentionAcks(); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Attention acknowledgements are the operator's in-app "I have seen this"
|
||||
// state, shared by every connected client so clearing on one device clears
|
||||
// everywhere. A client reads the whole set with {"type":"attention_acks"} and
|
||||
// adds to it with
|
||||
//
|
||||
// {"type":"ack_attention","attention_acks":{"dismissed":["42:Failed"],"chat_seen":{"ses_x":1730000000000}}}
|
||||
//
|
||||
// Both reply with type "attention_acks" carrying the whole stored set. After an
|
||||
// accepted ack, every other connection receives an unsolicited
|
||||
// {"type":"attention_acks_changed","attention_acks":{...}} frame holding only
|
||||
// what that ack changed. The set only grows (dismissals are added, watermarks
|
||||
// only advance), so applying frames in any order or twice is harmless.
|
||||
//
|
||||
// A dismissal identity is "<run id>:<Decision|Failed>", the Needs-you entry
|
||||
// identity. Clear all is a batch of the identities the client showed, so an
|
||||
// entry that appears later is never hidden by an earlier clear. A chat_seen
|
||||
// value is the chat session's updated_at (epoch milliseconds) the operator has
|
||||
// read up to.
|
||||
const (
|
||||
GetAttentionAcks = "attention_acks"
|
||||
AckAttention = "ack_attention"
|
||||
AttentionAcksReply = "attention_acks"
|
||||
AttentionAcksChanged = "attention_acks_changed"
|
||||
)
|
||||
|
||||
// MaxAttentionAckItems bounds the dismissals plus watermarks one ack may carry.
|
||||
const MaxAttentionAckItems = 500
|
||||
|
||||
// AttentionAcks is the wire form of the acknowledgement set (or a delta of it).
|
||||
type AttentionAcks struct {
|
||||
Dismissed []string `json:"dismissed"`
|
||||
ChatSeen map[string]int64 `json:"chat_seen"`
|
||||
}
|
||||
|
||||
var attentionKeyPattern = regexp.MustCompile(`^[1-9][0-9]{0,18}:(Decision|Failed)$`)
|
||||
|
||||
// chatSeenIDPattern mirrors the server's chat session id shapes (OpenCode
|
||||
// ses_…, Codex/Claude UUIDs).
|
||||
var chatSeenIDPattern = regexp.MustCompile(`^(?:ses_[A-Za-z0-9_]+|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$`)
|
||||
|
||||
// ValidAttentionKey reports whether key is a "<run id>:<kind>" identity.
|
||||
func ValidAttentionKey(key string) bool { return attentionKeyPattern.MatchString(key) }
|
||||
|
||||
// ValidateAttentionAck checks one ack request: at least one item, at most
|
||||
// MaxAttentionAckItems, every dismissal a valid identity, and every watermark a
|
||||
// valid chat session id with a positive millisecond value. It returns the
|
||||
// dismissals deduplicated and sorted.
|
||||
func ValidateAttentionAck(ack *AttentionAcks) (AttentionAcks, error) {
|
||||
if ack == nil || len(ack.Dismissed)+len(ack.ChatSeen) == 0 {
|
||||
return AttentionAcks{}, fmt.Errorf("attention_acks must name at least one dismissal or chat_seen watermark")
|
||||
}
|
||||
if len(ack.Dismissed)+len(ack.ChatSeen) > MaxAttentionAckItems {
|
||||
return AttentionAcks{}, fmt.Errorf("attention_acks may carry at most %d items", MaxAttentionAckItems)
|
||||
}
|
||||
seen := make(map[string]struct{}, len(ack.Dismissed))
|
||||
result := AttentionAcks{Dismissed: []string{}, ChatSeen: map[string]int64{}}
|
||||
for _, key := range ack.Dismissed {
|
||||
if !ValidAttentionKey(key) {
|
||||
return AttentionAcks{}, fmt.Errorf("invalid attention identity %q", key)
|
||||
}
|
||||
if _, dup := seen[key]; dup {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
result.Dismissed = append(result.Dismissed, key)
|
||||
}
|
||||
sort.Strings(result.Dismissed)
|
||||
for id, at := range ack.ChatSeen {
|
||||
if !chatSeenIDPattern.MatchString(id) {
|
||||
return AttentionAcks{}, fmt.Errorf("invalid chat session id %q", id)
|
||||
}
|
||||
if at <= 0 {
|
||||
return AttentionAcks{}, fmt.Errorf("chat_seen %q must be a positive epoch-millisecond time", id)
|
||||
}
|
||||
result.ChatSeen[id] = at
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Empty reports whether the set carries nothing.
|
||||
func (a AttentionAcks) Empty() bool { return len(a.Dismissed) == 0 && len(a.ChatSeen) == 0 }
|
||||
@@ -0,0 +1,37 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidateAttentionAckDedupesAndRefuses(t *testing.T) {
|
||||
got, err := ValidateAttentionAck(&AttentionAcks{
|
||||
Dismissed: []string{"9:Failed", "12:Decision", "9:Failed"},
|
||||
ChatSeen: map[string]int64{"ses_a1": 7, "0123abcd-0123-4567-89ab-0123456789ab": 8},
|
||||
})
|
||||
if err != nil || !reflect.DeepEqual(got.Dismissed, []string{"12:Decision", "9:Failed"}) || len(got.ChatSeen) != 2 {
|
||||
t.Fatalf("valid ack = %+v, %v", got, err)
|
||||
}
|
||||
for _, bad := range []*AttentionAcks{nil, {}, {Dismissed: []string{"-1:Failed"}}, {Dismissed: []string{"1:failed"}}, {ChatSeen: map[string]int64{"ses_x": -1}}, {ChatSeen: map[string]int64{"ses x": 1}}} {
|
||||
if _, err := ValidateAttentionAck(bad); err == nil {
|
||||
t.Errorf("accepted %+v", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttentionAcksWireShape(t *testing.T) {
|
||||
raw := `{"type":"ack_attention","id":"1","attention_acks":{"dismissed":["3:Failed"],"chat_seen":{"ses_q":5}}}`
|
||||
var message Message
|
||||
if err := json.Unmarshal([]byte(raw), &message); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if message.Type != AckAttention || message.AttentionAcks == nil || message.AttentionAcks.Dismissed[0] != "3:Failed" || message.AttentionAcks.ChatSeen["ses_q"] != 5 {
|
||||
t.Fatalf("decoded = %+v", message)
|
||||
}
|
||||
out, _ := json.Marshal(Message{Type: AttentionAcksChanged, AttentionAcks: &AttentionAcks{Dismissed: []string{}, ChatSeen: map[string]int64{}}})
|
||||
if string(out) == "" || !json.Valid(out) {
|
||||
t.Fatal("unencodable")
|
||||
}
|
||||
}
|
||||
@@ -241,4 +241,8 @@ type Message struct {
|
||||
// NotificationSettings is the set_notification_settings partial update and
|
||||
// the notification_settings reply (every key). See notification_settings.go.
|
||||
NotificationSettings map[string]any `json:"notification_settings,omitempty"`
|
||||
// AttentionAcks is the ack_attention body, the attention_acks reply (the
|
||||
// whole set) and the attention_acks_changed push (the delta). See
|
||||
// attention_acks.go.
|
||||
AttentionAcks *AttentionAcks `json:"attention_acks,omitempty"`
|
||||
}
|
||||
|
||||
@@ -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