Notify: an agent's notify reaches every connected app
The notify tool now records a chat_notify app notification with its own id per call (no dedupe of repeated words) and the tool's priority, so every subscribed client gets it and a reconnecting client replays it. ntfy delivery is unchanged; the tool result names where it went. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -51,6 +51,7 @@ type appNotificationFixture struct {
|
||||
ctx context.Context
|
||||
store *pipeline.Store
|
||||
runner *runner
|
||||
hub *chatHub
|
||||
url string
|
||||
def *protocol.PipelineDetail
|
||||
}
|
||||
@@ -74,7 +75,7 @@ func newAppNotificationFixture(t *testing.T, ctx context.Context) *appNotificati
|
||||
t.Cleanup(func() { stop(); WaitRuns(handler) })
|
||||
srv := httptest.NewServer(handler)
|
||||
t.Cleanup(srv.Close)
|
||||
return &appNotificationFixture{t: t, ctx: ctx, store: store, runner: handler.(*runHandler).runner, url: srv.URL, def: def}
|
||||
return &appNotificationFixture{t: t, ctx: ctx, store: store, runner: handler.(*runHandler).runner, hub: handler.(*runHandler).hub, url: srv.URL, def: def}
|
||||
}
|
||||
|
||||
func TestToolNotificationRecordsAppNotificationWhenNtfyDisabled(t *testing.T) {
|
||||
@@ -83,19 +84,86 @@ func TestToolNotificationRecordsAppNotificationWhenNtfyDisabled(t *testing.T) {
|
||||
if _, err := store.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyNtfyEnabled: false}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PIPELINES_NTFY_URL", "http://ntfy.invalid/pipelines")
|
||||
hub := &chatHub{notes: newAppNotifier(store), settings: store}
|
||||
if ok, diagnostic := hub.publishToolNotification(ctx, "session-1", "Build failed", "Please inspect the run"); !ok || diagnostic != "" {
|
||||
t.Fatalf("tool notification result = %v, %q", ok, diagnostic)
|
||||
got := hub.publishToolNotification(ctx, "session-1", chatToolNotice{ID: "shown_1", Title: "Build failed", Message: "Please inspect the run", Priority: "low"})
|
||||
if !got.App || got.Phone || got.Detail != "phone notifications (ntfy) are turned off" {
|
||||
t.Fatalf("tool notification result = %+v", got)
|
||||
}
|
||||
active, _, err := store.ActiveAppNotifications(ctx, "", time.Now())
|
||||
if err != nil || len(active) != 1 {
|
||||
t.Fatalf("app notifications = %+v, %v", active, err)
|
||||
}
|
||||
if active[0].Kind != protocol.NotificationChatReply || active[0].Title != "Build failed" || active[0].Message != "Please inspect the run" {
|
||||
t.Fatalf("wrong tool notification: %+v", active[0])
|
||||
if n := active[0]; n.ID != "chat:session-1:notify:shown_1" || n.Kind != protocol.NotificationChatNotify || n.Title != "Build failed" ||
|
||||
n.Message != "Please inspect the run" || n.Priority != 2 || n.Group != "chat:session-1" || n.Click != "pipelines://chat/session-1" {
|
||||
t.Fatalf("wrong tool notification: %+v", n)
|
||||
}
|
||||
}
|
||||
|
||||
// An agent's notify reaches every connected app, not only the clients watching
|
||||
// that chat, and a client that connects later replays the ones still active.
|
||||
func TestToolNotificationBroadcastsAndReplaysToReconnectingClients(t *testing.T) {
|
||||
t.Setenv("PIPELINES_NTFY_URL", "")
|
||||
t.Setenv("PIPELINES_WEB_PUBLIC_URL", "")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
f := newAppNotificationFixture(t, ctx)
|
||||
hub := f.handlerHub()
|
||||
web, desktop := dialAckClient(t, ctx, f.url), dialAckClient(t, ctx, f.url)
|
||||
subscribeNotifications(t, ctx, web, "")
|
||||
subscribeNotifications(t, ctx, desktop, "")
|
||||
|
||||
notice := chatToolNotice{ID: "shown_a", Title: "Done", Message: "Build finished", Priority: "high"}
|
||||
if got := hub.publishToolNotification(ctx, "ses_x", notice); !got.App || got.Phone || got.Detail == "" {
|
||||
t.Fatalf("delivery = %+v", got)
|
||||
}
|
||||
for name, conn := range map[string]*websocket.Conn{"web": web, "desktop": desktop} {
|
||||
push := readFrame(t, ctx, conn, protocol.NotificationPush).Notification
|
||||
if push == nil || push.ID != "chat:ses_x:notify:shown_a" || push.Kind != protocol.NotificationChatNotify || push.Priority != 4 ||
|
||||
push.Title != "Done" || push.Message != "Build finished" || push.Group != "chat:ses_x" {
|
||||
t.Fatalf("%s push = %+v", name, push)
|
||||
}
|
||||
}
|
||||
// The same words again are a new notification, not a duplicate.
|
||||
notice.ID = "shown_b"
|
||||
hub.publishToolNotification(ctx, "ses_x", notice)
|
||||
if push := readFrame(t, ctx, web, protocol.NotificationPush).Notification; push == nil || push.ID != "chat:ses_x:notify:shown_b" {
|
||||
t.Fatalf("second push = %+v", push)
|
||||
}
|
||||
|
||||
// A client that was away replays what it missed, after its cursor.
|
||||
late := dialAckClient(t, ctx, f.url)
|
||||
if all := subscribeNotifications(t, ctx, late, ""); len(all.Notifications) != 2 || all.Notifications[0].ID != "chat:ses_x:notify:shown_a" {
|
||||
t.Fatalf("replay = %+v", all.Notifications)
|
||||
}
|
||||
late.CloseNow()
|
||||
late = dialAckClient(t, ctx, f.url)
|
||||
if missed := subscribeNotifications(t, ctx, late, "chat:ses_x:notify:shown_a"); len(missed.Notifications) != 1 || missed.Notifications[0].ID != "chat:ses_x:notify:shown_b" {
|
||||
t.Fatalf("replay after cursor = %+v", missed.Notifications)
|
||||
}
|
||||
|
||||
// Opened on one device: the chat is seen, so none of its notifications
|
||||
// replay anywhere.
|
||||
if err := wsjson.Write(ctx, web, protocol.Message{Type: protocol.ClearNotifications, ID: "clr", IDs: []string{"chat:ses_x:notify:shown_a"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reply := readFrame(t, ctx, web, protocol.NotificationsCleared); len(reply.IDs) != 1 {
|
||||
t.Fatalf("clear reply = %+v", reply)
|
||||
}
|
||||
again := dialAckClient(t, ctx, f.url)
|
||||
if left := subscribeNotifications(t, ctx, again, ""); len(left.Notifications) != 0 {
|
||||
t.Fatalf("replay after clear = %+v", left.Notifications)
|
||||
}
|
||||
}
|
||||
|
||||
func (f *appNotificationFixture) handlerHub() *chatHub {
|
||||
f.t.Helper()
|
||||
if f.hub == nil {
|
||||
f.t.Fatal("the handler has no chat hub")
|
||||
}
|
||||
return f.hub
|
||||
}
|
||||
|
||||
// failRun creates a "Notify me" run and fails it, which commits a durable run
|
||||
// notification intent in the same transaction, then drains the outbox.
|
||||
func (f *appNotificationFixture) failRun(name string) int64 {
|
||||
|
||||
Reference in New Issue
Block a user