Files
pipelines/internal/protocol/notifications.go
T
alan fc7dbb0ac0 Remove ntfy: notifications go only through the app notification stream
Server: drop ntfy publishing (stage hooks, run outbox, chat attention, chat
tool notify, auto-switch and schedule notices), the chat push outbox (its
table is dropped at startup), ntfy status/endpoints and PIPELINES_NTFY_*.
The app_notifications stream, subscribe/clear and kinds are unchanged.
Notification settings keep run_finished/chat_needs_you/chat_reply_finished;
ntfy_enabled is accepted and ignored and its stored row is never read.

Stage type=ntfy is rejected: "ntfy stages were removed; notifications go to
the Pipelines apps — use the stage notify hook". The notify hook remains.

Installer: no ntfy build, accounts, config or supervisor child; manifest
gets top-level "mode" (legacy ntfy.mode still read). Reinstall retires an
old ntfy (safe PID-file stop, deletes binary, config, data dir, token, log,
with a note in logs/install.log); uninstall cleans leftovers.

App: ntfy settings section replaced by "Notification events"; texts updated.
Docs: docs/ntfy.md folded into docs/notifications.md; install, docker, cli,
author skill references updated; security review M1 marked resolved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 23:24:24 +00:00

123 lines
5.3 KiB
Go

package protocol
import "fmt"
// App notifications are the operator's notifications, streamed over /connect
// so every Pipelines app can post them itself. Every notification point records
// one notification (persisted, with a stable id), governed by the notification
// settings.
//
// → {"type":"subscribe_notifications","id":"1","after":"run:7:3:waiting"}
// ← {"type":"notifications_subscribed","id":"1","notifications":[{...}],"ids":["chat:ses_x:form:f1"]}
// ← {"type":"notification","notification":{...}} (push)
// ← {"type":"notifications_cleared","ids":["run:7:3:waiting"]} (push)
// → {"type":"clear_notifications","id":"2","ids":["run:7:3:waiting"]}
// ← {"type":"notifications_cleared","id":"2","ids":[...]} (reply: the ids it cleared)
//
// subscribe_notifications replies with every still-active notification newer
// than the optional "after" cursor (a notification id the client already
// holds), oldest first, bounded to the newest MaxReplayedNotifications within
// the store's retention window (7 days); an unknown or pruned cursor replays the whole bounded
// set. The reply's "ids" lists notifications cleared within the retention
// window (bounded) so a reconnecting client can dismiss ones it still shows.
// After the reply the connection receives "notification" and
// "notifications_cleared" pushes in order. Subscribing again replaces the
// subscription.
//
// clear_notifications is the app's dismiss/tap: it clears the named ids, has
// the same effect as the matching ack_attention (a run_waiting id dismisses
// "<run>:Decision", run_failed/stage_failed "<run>:Failed", a chat id marks
// the chat seen) and pushes notifications_cleared to every other subscriber.
// An ack_attention from any client, a run leaving "waiting", a deleted run and
// an answered chat question/approval also clear and push.
const (
SubscribeNotifications = "subscribe_notifications"
NotificationsSubscribed = "notifications_subscribed"
NotificationPush = "notification"
NotificationsCleared = "notifications_cleared"
ClearNotifications = "clear_notifications"
)
// App notification kinds.
const (
NotificationRunWaiting = "run_waiting" // a run needs a human decision
NotificationRunFailed = "run_failed" // a run (or its final stage) failed
NotificationRunSucceeded = "run_succeeded" // a run succeeded
NotificationStageFailed = "stage_failed" // a stage notify hook reported a failure mid-run
NotificationStageSucceeded = "stage_succeeded" // a stage notify hook reported a success mid-run
NotificationChatQuestion = "chat_question" // a chat asked a question
NotificationChatApproval = "chat_approval" // a chat asks for permission/approval
NotificationChatReply = "chat_reply" // a chat finished its reply
// NotificationChatNotify is an agent's own notify tool call. Its priority
// is the tool's: low 2, default 3, high 4.
NotificationChatNotify = "chat_notify"
// NotificationSchedule says a scheduled run was delayed (its workspace was
// busy) or skipped. It is low priority (2).
NotificationSchedule = "schedule"
)
// DefaultNotificationPriority is the default of the 1-5 notification priority.
const DefaultNotificationPriority = 3
// MaxReplayedNotifications bounds a subscribe reply and MaxClearNotificationIDs
// one clear_notifications request.
const (
MaxReplayedNotifications = 100
MaxClearNotificationIDs = 500
maxNotificationIDLength = 512
)
// AppNotification is one operator notification as streamed to app clients.
type AppNotification struct {
// ID is stable and is the id used to clear it: "run:<run>:<step>:<event>"
// or "chat:<session>:<form|permission|reply>:<id>".
ID string `json:"id"`
Kind string `json:"kind"`
Title string `json:"title"`
Message string `json:"message"`
// Click is the app tap target (normally the run or chat deep link).
Click string `json:"click"`
RunClick string `json:"run_click,omitempty"`
Artifact *NotificationArtifact `json:"artifact,omitempty"`
Preview string `json:"preview,omitempty"`
Priority int `json:"priority"`
Tags []string `json:"tags"`
// CreatedAt is RFC3339 (UTC).
CreatedAt string `json:"created_at"`
// Group is "run:<id>" or "chat:<id>" for Android notification grouping.
Group string `json:"group"`
}
// NotificationArtifact is the run artifact opened by an app notification.
type NotificationArtifact struct {
RunID int64 `json:"run_id"`
Sequence int `json:"sequence"`
Path string `json:"path"`
Name string `json:"name"`
URL string `json:"url"`
MediaType string `json:"media_type"`
}
// ValidateClearNotifications checks a clear_notifications id list.
func ValidateClearNotifications(ids []string) ([]string, error) {
if len(ids) == 0 {
return nil, fmt.Errorf("clear_notifications must name at least one id")
}
if len(ids) > MaxClearNotificationIDs {
return nil, fmt.Errorf("clear_notifications may carry at most %d ids", MaxClearNotificationIDs)
}
seen := make(map[string]struct{}, len(ids))
result := make([]string, 0, len(ids))
for _, id := range ids {
if id == "" || len(id) > maxNotificationIDLength {
return nil, fmt.Errorf("invalid notification id %q", id)
}
if _, dup := seen[id]; dup {
continue
}
seen[id] = struct{}{}
result = append(result, id)
}
return result, nil
}