Persist and broadcast shared attention acknowledgements

This commit is contained in:
2026-10-31 12:23:51 +00:00
parent 480f8843eb
commit f4b3ef17cc
9 changed files with 684 additions and 0 deletions
+89
View File
@@ -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 }
+37
View File
@@ -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")
}
}
+4
View File
@@ -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"`
}