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>
This commit is contained in:
+31
-218
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
"io"
|
||||
"log"
|
||||
"mime"
|
||||
@@ -16,7 +15,6 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
@@ -62,149 +60,41 @@ func (r *runner) notifyStep(id int64, name, workspace string, policy *protocol.N
|
||||
summary := stageNotificationSummary(name, id, step.Stage, event, step.Outcome)
|
||||
summary = truncateUTF8(summary, 1800)
|
||||
title := fmt.Sprintf("Pipeline stage %s", event)
|
||||
receipts := []protocol.NotificationReceipt{}
|
||||
persist := func() bool {
|
||||
save, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := r.store.SaveNotifications(save, id, step.Sequence, receipts); err != nil {
|
||||
log.Printf("notification audit run=%d step=%d: %v", id, step.Sequence, err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
// The app stream gets one notification per step event, recorded before
|
||||
// the first ntfy publication; artifacts stay ntfy attachments.
|
||||
// The app stream gets one notification per step event.
|
||||
appEvent, appKind := stageNotification(event, step.Target)
|
||||
// A `link=` file decides the tap target; it is never the attached artifact.
|
||||
link, linkFile, linkDiagnostic := r.resolveNotificationLink(id, policy.Link)
|
||||
appArtifact, appPreview := r.resolveNotificationArtifact(id, step.Sequence, policy.Open, linkFile)
|
||||
recorded := false
|
||||
ntfyOn := ntfyEnabled(ctx, r.settingsStore())
|
||||
views := runAttentionViews(id)
|
||||
ntfyClick, linkNote := ntfyLinkClick(id, link, views)
|
||||
if ntfyOn && linkDiagnostic == "" {
|
||||
linkDiagnostic = linkNote
|
||||
}
|
||||
send := func(filename string, data []byte) {
|
||||
receipts = append(receipts, protocol.NotificationReceipt{Destination: "system", Status: "sending", Filename: filename})
|
||||
if !persist() {
|
||||
return
|
||||
receipt := protocol.NotificationReceipt{Destination: "app", Status: "sent", Diagnostic: linkDiagnostic}
|
||||
if !r.notes.record(ctx, withNotificationLink(runNotificationRecord(id, step.Sequence, appEvent, appKind, title, summary, appArtifact, appPreview), link)) {
|
||||
receipt.Status = "failed"
|
||||
if receipt.Diagnostic != "" {
|
||||
receipt.Diagnostic += "; "
|
||||
}
|
||||
receipt := &receipts[len(receipts)-1]
|
||||
if !recorded {
|
||||
recorded = true
|
||||
r.notes.record(ctx, withNotificationLink(runNotificationRecord(id, step.Sequence, appEvent, appKind, title, summary, appArtifact, appPreview), link))
|
||||
}
|
||||
if !ntfyOn {
|
||||
receipt.Status = "sent"
|
||||
receipt.Diagnostic = "ntfy disabled; delivered to app clients only"
|
||||
persist()
|
||||
return
|
||||
}
|
||||
result, err := ntfyPublish(ctx, "system", title, summary, filename, data, ntfyClick, views...)
|
||||
if err != nil {
|
||||
receipt.Status = "failed"
|
||||
receipt.Diagnostic = err.Error()
|
||||
} else {
|
||||
receipt.Status = "sent"
|
||||
receipt.MessageID, _ = result["message_id"].(string)
|
||||
receipt.AttachmentURL, _ = result["attachment_url"].(string)
|
||||
}
|
||||
persist()
|
||||
}
|
||||
reject := func(filename, diagnostic string) {
|
||||
receipts = append(receipts, protocol.NotificationReceipt{Destination: "system", Status: "failed", Filename: filename, Diagnostic: diagnostic})
|
||||
persist()
|
||||
}
|
||||
sentArtifact := false
|
||||
for _, kind := range policy.Artifacts {
|
||||
switch kind {
|
||||
case "markdown":
|
||||
if markdown, ok := step.Output["markdown"].(string); ok && strings.TrimSpace(markdown) != "" {
|
||||
sentArtifact = true
|
||||
send("digest.md", []byte(markdown))
|
||||
}
|
||||
case "files":
|
||||
raw, exists := step.Output["artifacts"]
|
||||
if !exists {
|
||||
continue
|
||||
}
|
||||
var names []string
|
||||
switch files := raw.(type) {
|
||||
case []string:
|
||||
names = files
|
||||
case []any:
|
||||
for _, value := range files {
|
||||
name, ok := value.(string)
|
||||
if !ok {
|
||||
reject("", "output.artifacts must be an array of relative file paths")
|
||||
names = nil
|
||||
break
|
||||
}
|
||||
names = append(names, name)
|
||||
}
|
||||
default:
|
||||
reject("", "output.artifacts must be an array of relative file paths")
|
||||
}
|
||||
if len(names) > 10 {
|
||||
reject("", "at most 10 file artifacts can be notified")
|
||||
continue
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, name := range names {
|
||||
if seen[name] {
|
||||
continue
|
||||
}
|
||||
seen[name] = true
|
||||
data, err := notificationArtifact(workspace, name)
|
||||
if err != nil {
|
||||
reject(name, err.Error())
|
||||
continue
|
||||
}
|
||||
sentArtifact = true
|
||||
send(filepath.Base(name), data)
|
||||
}
|
||||
}
|
||||
}
|
||||
// No produced artifacts still merits an event, especially on failures.
|
||||
if !sentArtifact {
|
||||
send("", nil)
|
||||
}
|
||||
// The link note is recorded once, never as the run-level failure reason.
|
||||
failure := "stage notification publication failed"
|
||||
if len(receipts) > 0 && receipts[0].Diagnostic != "" {
|
||||
failure = receipts[0].Diagnostic
|
||||
}
|
||||
if linkDiagnostic != "" && len(receipts) > 0 {
|
||||
first := &receipts[0]
|
||||
if first.Diagnostic != "" {
|
||||
first.Diagnostic += "; "
|
||||
}
|
||||
first.Diagnostic += linkDiagnostic
|
||||
persist()
|
||||
}
|
||||
// Only a successful publish covers the run alert. A failed stage publisher
|
||||
// must not swallow the durable run-level fallback and its retries.
|
||||
for _, receipt := range receipts {
|
||||
if receipt.Status == "sent" {
|
||||
covered := stageAttentionEvent(event)
|
||||
if event == "success" && step.Target == protocol.SuccessEnd {
|
||||
covered = "succeeded"
|
||||
}
|
||||
r.markAttentionSent(id, step.Sequence, covered)
|
||||
return
|
||||
receipt.Diagnostic += "the app notification was not recorded"
|
||||
}
|
||||
save, done := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
if err := r.store.SaveNotifications(save, id, step.Sequence, []protocol.NotificationReceipt{receipt}); err != nil {
|
||||
log.Printf("notification audit run=%d step=%d: %v", id, step.Sequence, err)
|
||||
}
|
||||
done()
|
||||
if receipt.Status == "sent" {
|
||||
covered := stageAttentionEvent(event)
|
||||
if event == "success" && step.Target == protocol.SuccessEnd {
|
||||
covered = "succeeded"
|
||||
}
|
||||
r.markAttentionSent(id, step.Sequence, covered)
|
||||
return
|
||||
}
|
||||
// A hook that recorded nothing must not swallow the durable run-level
|
||||
// fallback and its retries.
|
||||
if lease != nil {
|
||||
if err := r.store.FinishRunNotification(r.ctx, *lease, time.Now().Unix(), "", fmt.Errorf("%s", failure)); err != nil {
|
||||
if err := r.store.FinishRunNotification(r.ctx, *lease, time.Now().Unix(), "", fmt.Errorf("stage notification was not recorded")); err != nil {
|
||||
log.Printf("release stage notification run=%d: %v", id, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ntfyPublish is the run notification publisher; tests may replace it.
|
||||
var ntfyPublish = stage.PublishNtfy
|
||||
|
||||
// stageNotification maps a stage hook event to the app notification event
|
||||
// (the id suffix, shared with the run outbox for the same run event so the
|
||||
// two never both appear) and kind.
|
||||
@@ -360,35 +250,11 @@ func runAttentionClick(id int64) string {
|
||||
return fmt.Sprintf("pipelines://run/%d", id)
|
||||
}
|
||||
|
||||
// runAttentionViews is the optional "Open in browser" action for a run.
|
||||
func runAttentionViews(id int64) []stage.NtfyView {
|
||||
return webAttentionViews(fmt.Sprintf("/runs/%d", id))
|
||||
}
|
||||
|
||||
// webAttentionViews returns an "Open in browser" ntfy action for path on the
|
||||
// operator's authenticated web origin, or nothing when none is configured.
|
||||
// Never infer a public address from a request Host, the unauthenticated API
|
||||
// listener or the ntfy server URL.
|
||||
func webAttentionViews(path string) []stage.NtfyView {
|
||||
raw := os.Getenv("PIPELINES_WEB_PUBLIC_URL")
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
base := publicNtfyBase(raw)
|
||||
if base == "" {
|
||||
log.Printf("notification web link unavailable: PIPELINES_WEB_PUBLIC_URL must be an HTTP(S) origin without credentials, query or fragment")
|
||||
return nil
|
||||
}
|
||||
return []stage.NtfyView{{Label: "Open in browser", URL: base + path}}
|
||||
}
|
||||
|
||||
// notifyRun publishes one run-level attention notification to the operator's
|
||||
// already-configured "system" destination. It is the run counterpart of
|
||||
// notifyStep and reuses the same PublishNtfy machinery, destination and token
|
||||
// handling, so no second publisher, topic or service is introduced. The run id
|
||||
// and pipeline name identify the run and the click action opens it in the app.
|
||||
// Attempts are persisted before publication. Publisher acknowledgement is not
|
||||
// proof of device reception; a crash in that acknowledgement window can repeat.
|
||||
// notifyRun records one run-level attention notification on the app
|
||||
// notification stream. It is the run counterpart of notifyStep. The run id and
|
||||
// pipeline name identify the run and the click action opens it in the app.
|
||||
// The event goes through the durable run notification outbox, so a crash
|
||||
// before it is recorded is recovered after a restart.
|
||||
func (r *runner) notifyRun(id int64, name, status string, sequence int, currentStage, diagnostic string) {
|
||||
if r.notifyMode(id) == "off" {
|
||||
return
|
||||
@@ -488,20 +354,13 @@ func (r *runner) deliverRunNotificationBatch(now time.Time, limit int) {
|
||||
link, linkFile := r.stageHookLink(*n)
|
||||
artifact, preview := r.resolveNotificationArtifact(n.RunID, n.Sequence, "", linkFile)
|
||||
n.Click = notificationClick(n.RunID, n.Sequence, artifact)
|
||||
r.notes.record(ctx, withNotificationLink(runNotificationRecord(n.RunID, n.Sequence, n.Event, runNotificationKind(n.Event), n.Title, n.Body, artifact, preview), link))
|
||||
var result stage.Values
|
||||
var publishErr error
|
||||
if ntfyEnabled(ctx, r.settingsStore()) {
|
||||
// ntfy retains its existing run-level tap target (or the hook's link);
|
||||
// the richer artifact target is carried by the app notification stream.
|
||||
views := runAttentionViews(n.RunID)
|
||||
click, _ := ntfyLinkClick(n.RunID, link, views)
|
||||
result, publishErr = ntfyPublish(ctx, n.Channel, n.Title, n.Body, "", nil, click, views...)
|
||||
var recordErr error
|
||||
if r.notes != nil && !r.notes.record(ctx, withNotificationLink(runNotificationRecord(n.RunID, n.Sequence, n.Event, runNotificationKind(n.Event), n.Title, n.Body, artifact, preview), link)) {
|
||||
recordErr = fmt.Errorf("app notification was not recorded")
|
||||
}
|
||||
cancel()
|
||||
messageID, _ := result["message_id"].(string)
|
||||
save, done := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
err = r.store.FinishRunNotification(save, *n, now.Add(time.Since(started)).Unix(), messageID, publishErr)
|
||||
err = r.store.FinishRunNotification(save, *n, now.Add(time.Since(started)).Unix(), "", recordErr)
|
||||
done()
|
||||
if err != nil {
|
||||
log.Printf("save run notification run=%d: %v", n.RunID, err)
|
||||
@@ -521,51 +380,6 @@ func truncateUTF8(text string, max int) string {
|
||||
return text
|
||||
}
|
||||
|
||||
func notificationArtifact(workspace, name string) ([]byte, error) {
|
||||
if name == "" || filepath.IsAbs(name) || filepath.Clean(name) != name || strings.ContainsAny(name, "\\\x00\r\n") {
|
||||
return nil, fmt.Errorf("artifact must be a clean workspace-relative file path")
|
||||
}
|
||||
for _, part := range strings.Split(filepath.ToSlash(name), "/") {
|
||||
lower := strings.ToLower(part)
|
||||
if strings.HasPrefix(part, ".") || lower == "secrets" || lower == "node_modules" || strings.HasSuffix(lower, ".pem") || strings.HasSuffix(lower, ".key") || strings.HasSuffix(lower, ".db") || strings.HasSuffix(lower, ".db-wal") || strings.HasSuffix(lower, ".db-shm") || strings.HasSuffix(lower, ".sqlite") || strings.HasSuffix(lower, ".sqlite3") {
|
||||
return nil, fmt.Errorf("artifact path is protected")
|
||||
}
|
||||
}
|
||||
root, err := filepath.EvalSymlinks(workspace)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("artifact workspace unavailable")
|
||||
}
|
||||
// os.Root prevents symlink traversal outside the workspace; disallow symlinks
|
||||
// altogether so aliases cannot bypass protected path names within it either.
|
||||
current := root
|
||||
for _, part := range strings.Split(name, string(os.PathSeparator)) {
|
||||
current = filepath.Join(current, part)
|
||||
info, err := os.Lstat(current)
|
||||
if err != nil || info.Mode()&os.ModeSymlink != 0 {
|
||||
return nil, fmt.Errorf("artifact is missing or uses a symlink")
|
||||
}
|
||||
}
|
||||
scoped, err := os.OpenRoot(root)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("artifact workspace unavailable")
|
||||
}
|
||||
defer scoped.Close()
|
||||
file, err := scoped.OpenFile(name, os.O_RDONLY|syscall.O_NONBLOCK|syscall.O_NOFOLLOW, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read artifact")
|
||||
}
|
||||
defer file.Close()
|
||||
info, err := file.Stat()
|
||||
if err != nil || !info.Mode().IsRegular() || info.Size() > 1<<20 {
|
||||
return nil, fmt.Errorf("artifact must be a regular file at most 1 MiB")
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(file, (1<<20)+1))
|
||||
if err != nil || len(data) > 1<<20 {
|
||||
return nil, fmt.Errorf("cannot read artifact within size limit")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func notificationClick(runID int64, sequence int, artifact *protocol.NotificationArtifact) string {
|
||||
if artifact == nil {
|
||||
return runAttentionClick(runID)
|
||||
@@ -578,7 +392,6 @@ func notificationClick(runID int64, sequence int, artifact *protocol.Notificatio
|
||||
}
|
||||
|
||||
// resolveNotificationArtifact selects only files in the run's shared public/.
|
||||
// It is deliberately independent of the ntfy attachment policy.
|
||||
// The exclude file (a `link=` file) is never selected.
|
||||
func (r *runner) resolveNotificationArtifact(runID int64, sequence int, pattern, exclude string) (*protocol.NotificationArtifact, string) {
|
||||
selected := r.publicNotificationFile(runID, pattern, exclude)
|
||||
|
||||
Reference in New Issue
Block a user