From d42d7a3534969aac3d3b0570d55a0c03dc213300 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Thu, 12 Nov 2026 18:39:03 +0000 Subject: [PATCH] Attach run artifacts to app notifications --- internal/author/skill/references/format.md | 5 + .../author/skill/references/notifications.md | 7 + internal/pipeline/app_notifications.go | 41 +++++- internal/pipeline/notify.go | 17 ++- internal/pipeline/notify_test.go | 1 + internal/protocol/message.go | 1 + internal/protocol/notifications.go | 22 ++- internal/server/app_notifications.go | 21 ++- internal/server/notify.go | 128 +++++++++++++++++- internal/server/notify_test.go | 41 ++++++ 10 files changed, 266 insertions(+), 18 deletions(-) diff --git a/internal/author/skill/references/format.md b/internal/author/skill/references/format.md index 2cfc5661..c8529af8 100644 --- a/internal/author/skill/references/format.md +++ b/internal/author/skill/references/format.md @@ -185,3 +185,8 @@ once. A run started inside a linked worktree (an item's tree) and every retry use their directory as is. After a successful run whose tree is clean and whose branch is on `main`, the server removes the tree and branch; anything else (a failed run, unlanded commits, stray files) is kept for recovery. + +The `notify` block accepts `on`, optional legacy `destination`, optional +`artifacts=markdown|files`, and optional `open=`. `open` selects +an artifact in the run's shared `public/`; without it, one and only one regular +file there is selected automatically. diff --git a/internal/author/skill/references/notifications.md b/internal/author/skill/references/notifications.md index 0cc88bad..1e26df29 100644 --- a/internal/author/skill/references/notifications.md +++ b/internal/author/skill/references/notifications.md @@ -60,6 +60,13 @@ at most 1 MiB, with at most 10 listed. Absolute/traversal/hidden paths, `secrets database/key paths and `node_modules/` are rejected. This is not content scanning. Do not promise phone reception from structural validation. +`open=` attaches one artifact from the run's shared `public/` directory +to the app notification and makes its tap open that artifact. The pattern is a +relative glob with no absolute path, `..`, or hidden path segment; if several +files match, the most recently modified file wins. Without `open=`, exactly one +regular file in `public/` is attached automatically. Zero or multiple files +produce no attached artifact. This is separate from ntfy's `artifacts=` keys. + ## Optional explicit delivery stage Use native delivery only when the user needs delivery as an explicit routed stage diff --git a/internal/pipeline/app_notifications.go b/internal/pipeline/app_notifications.go index 2cc50158..46e6ba35 100644 --- a/internal/pipeline/app_notifications.go +++ b/internal/pipeline/app_notifications.go @@ -41,13 +41,26 @@ type AppNotificationFilter struct { func (s *Store) initAppNotifications() error { _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS app_notifications ( seq INTEGER PRIMARY KEY AUTOINCREMENT, id TEXT NOT NULL UNIQUE, kind TEXT NOT NULL, -title TEXT NOT NULL, message TEXT NOT NULL, click TEXT NOT NULL, priority INTEGER NOT NULL, +title TEXT NOT NULL, message TEXT NOT NULL, click TEXT NOT NULL, run_click TEXT NOT NULL DEFAULT '', +artifact_json TEXT NOT NULL DEFAULT '', preview TEXT NOT NULL DEFAULT '', priority INTEGER NOT NULL, tags TEXT NOT NULL, grp TEXT NOT NULL, run_id INTEGER NOT NULL DEFAULT 0, session_id TEXT NOT NULL DEFAULT '', created_at INTEGER NOT NULL, cleared_at INTEGER NOT NULL DEFAULT 0); CREATE INDEX IF NOT EXISTS app_notifications_run ON app_notifications(run_id, cleared_at); CREATE INDEX IF NOT EXISTS app_notifications_session ON app_notifications(session_id, cleared_at); CREATE INDEX IF NOT EXISTS app_notifications_created ON app_notifications(created_at);`) - return err + if err != nil { + return err + } + for _, column := range []struct{ name, definition string }{ + {"run_click", "TEXT NOT NULL DEFAULT ''"}, + {"artifact_json", "TEXT NOT NULL DEFAULT ''"}, + {"preview", "TEXT NOT NULL DEFAULT ''"}, + } { + if err := s.addColumnIfMissing("app_notifications", column.name, column.definition); err != nil { + return err + } + } + return nil } // RecordAppNotification stores n once (by id) and reports whether it is new @@ -68,6 +81,14 @@ func (s *Store) RecordAppNotification(ctx context.Context, n AppNotificationReco if err != nil { return n.AppNotification, false, err } + artifact := "" + if n.Artifact != nil { + encoded, err := json.Marshal(n.Artifact) + if err != nil { + return n.AppNotification, false, err + } + artifact = string(encoded) + } tx, err := s.db.BeginTx(ctx, nil) if err != nil { return n.AppNotification, false, err @@ -88,8 +109,8 @@ func (s *Store) RecordAppNotification(ctx context.Context, n AppNotificationReco cleared = nowMillis } } - result, err := tx.ExecContext(ctx, `INSERT INTO app_notifications(id,kind,title,message,click,priority,tags,grp,run_id,session_id,created_at,cleared_at) -VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(id) DO NOTHING`, n.ID, n.Kind, n.Title, n.Message, n.Click, n.Priority, string(tags), n.Group, n.RunID, n.SessionID, nowMillis, cleared) + result, err := tx.ExecContext(ctx, `INSERT INTO app_notifications(id,kind,title,message,click,run_click,artifact_json,preview,priority,tags,grp,run_id,session_id,created_at,cleared_at) +VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(id) DO NOTHING`, n.ID, n.Kind, n.Title, n.Message, n.Click, n.RunClick, artifact, n.Preview, n.Priority, string(tags), n.Group, n.RunID, n.SessionID, nowMillis, cleared) if err != nil { return n.AppNotification, false, err } @@ -113,7 +134,7 @@ func (s *Store) ActiveAppNotifications(ctx context.Context, after string, now ti return nil, nil, err } } - rows, err := s.db.QueryContext(ctx, `SELECT id,kind,title,message,click,priority,tags,grp,created_at FROM app_notifications + rows, err := s.db.QueryContext(ctx, `SELECT id,kind,title,message,click,run_click,artifact_json,preview,priority,tags,grp,created_at FROM app_notifications WHERE cleared_at=0 AND seq>? AND created_at>=? ORDER BY seq DESC LIMIT ?`, afterSeq, cutoff, protocol.MaxReplayedNotifications) if err != nil { return nil, nil, err @@ -121,12 +142,18 @@ WHERE cleared_at=0 AND seq>? AND created_at>=? ORDER BY seq DESC LIMIT ?`, after active = []protocol.AppNotification{} for rows.Next() { var n protocol.AppNotification - var tags string + var artifact, tags string var created int64 - if err := rows.Scan(&n.ID, &n.Kind, &n.Title, &n.Message, &n.Click, &n.Priority, &tags, &n.Group, &created); err != nil { + if err := rows.Scan(&n.ID, &n.Kind, &n.Title, &n.Message, &n.Click, &n.RunClick, &artifact, &n.Preview, &n.Priority, &tags, &n.Group, &created); err != nil { rows.Close() return nil, nil, err } + if artifact != "" { + n.Artifact = new(protocol.NotificationArtifact) + if json.Unmarshal([]byte(artifact), n.Artifact) != nil { + n.Artifact = nil + } + } if json.Unmarshal([]byte(tags), &n.Tags) != nil || n.Tags == nil { n.Tags = []string{} } diff --git a/internal/pipeline/notify.go b/internal/pipeline/notify.go index 442cd029..764b287d 100644 --- a/internal/pipeline/notify.go +++ b/internal/pipeline/notify.go @@ -3,6 +3,7 @@ package pipeline import ( "fmt" "git.a-wan.net/alan/pipelines/internal/protocol" + "path" "regexp" "strings" ) @@ -23,7 +24,7 @@ func parseNotify(text string) (*protocol.NotificationPolicy, error) { return nil, fmt.Errorf("duplicate notify setting %s", key) } switch key { - case "on", "destination", "artifacts": + case "on", "destination", "artifacts", "open": default: return nil, fmt.Errorf("unknown notify setting %s", key) } @@ -59,5 +60,19 @@ func parseNotify(text string) (*protocol.NotificationPolicy, error) { return nil, err } } + if open := values["open"]; open != "" { + if strings.ContainsAny(open, "\\\x00\r\n") || strings.HasPrefix(open, "/") || path.IsAbs(open) || path.Clean(open) != open { + return nil, fmt.Errorf("invalid notify open pattern") + } + for _, part := range strings.Split(open, "/") { + if part == ".." || strings.HasPrefix(part, ".") || part == "" { + return nil, fmt.Errorf("invalid notify open pattern") + } + } + if _, err := path.Match(open, ""); err != nil { + return nil, fmt.Errorf("invalid notify open pattern: %v", err) + } + p.Open = open + } return p, nil } diff --git a/internal/pipeline/notify_test.go b/internal/pipeline/notify_test.go index e73645c9..8f67b59b 100644 --- a/internal/pipeline/notify_test.go +++ b/internal/pipeline/notify_test.go @@ -11,6 +11,7 @@ func TestNotifyPolicyParsing(t *testing.T) { valid bool }{ {"on=success\ndestination=anime", true}, {"on=failure\ndestination=anime\nartifacts=files", true}, {"on=success,failure\ndestination=anime\nartifacts=markdown,files", true}, + {"on=success\nopen=reports/*.md", true}, {"on=success\nopen=/secret", false}, {"on=success\nopen=../secret", false}, {"on=success\nopen=.secret", false}, {"on=success\nopen=[", false}, {"on=error\ndestination=anime", false}, {"on=success,success\ndestination=anime", false}, {"destination=anime", false}, {"on=success", true}, {"on=failure\nartifacts=markdown,files", true}, {"on=success\ndestination=../secret", false}, {"on=success\ndestination=anime\ntoken=secret", false}, {"on=success\ndestination=anime\nartifacts=*", false}, {"on=success\non=failure\ndestination=anime", false}, } { source := strings.Replace(Template([]string{"one"}, true), "# stage one", "# stage one\n```notify\n"+test.text+"\n```", 1) diff --git a/internal/protocol/message.go b/internal/protocol/message.go index d4f927c0..3de91cf7 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -56,6 +56,7 @@ type NotificationPolicy struct { On []string `json:"on"` Destination string `json:"destination"` Artifacts []string `json:"artifacts,omitempty"` + Open string `json:"open,omitempty"` } // Public phone setup only. Never include publishing endpoints or credentials. diff --git a/internal/protocol/notifications.go b/internal/protocol/notifications.go index 4581c104..a8fa32c8 100644 --- a/internal/protocol/notifications.go +++ b/internal/protocol/notifications.go @@ -72,17 +72,29 @@ type AppNotification struct { Kind string `json:"kind"` Title string `json:"title"` Message string `json:"message"` - // Click is the same deep link ntfy gets (pipelines://run/, - // pipelines://chat/). - Click string `json:"click"` - Priority int `json:"priority"` - Tags []string `json:"tags"` + // Click is the app tap target (normally the same run deep link ntfy gets). + 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:" or "chat:" 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 { diff --git a/internal/server/app_notifications.go b/internal/server/app_notifications.go index 1baea3aa..453fdef7 100644 --- a/internal/server/app_notifications.go +++ b/internal/server/app_notifications.go @@ -320,14 +320,31 @@ func runNotificationKind(event string) string { // runNotificationRecord builds the app notification for one run event: // identity "run:::", deep link and group from the run id. -func runNotificationRecord(runID int64, sequence int, event, kind, title, message string) pipeline.AppNotificationRecord { +func runNotificationRecord(runID int64, sequence int, event, kind, title, message string, extras ...interface{}) pipeline.AppNotificationRecord { + var artifact *protocol.NotificationArtifact + var preview string + if len(extras) > 0 { + artifact, _ = extras[0].(*protocol.NotificationArtifact) + } + if len(extras) > 1 { + preview, _ = extras[1].(string) + } + click := runAttentionClick(runID) + runClick := "" + if artifact != nil { + click = notificationClick(runID, sequence, artifact) + runClick = runAttentionClick(runID) + } return pipeline.AppNotificationRecord{ AppNotification: protocol.AppNotification{ ID: fmt.Sprintf("run:%d:%d:%s", runID, sequence, event), Kind: kind, Title: title, Message: message, - Click: runAttentionClick(runID), + Click: click, + RunClick: runClick, + Artifact: artifact, + Preview: preview, Priority: protocol.DefaultNotificationPriority, Tags: []string{}, Group: fmt.Sprintf("run:%d", runID), diff --git a/internal/server/notify.go b/internal/server/notify.go index ae99ec82..7b2ee811 100644 --- a/internal/server/notify.go +++ b/internal/server/notify.go @@ -8,8 +8,13 @@ import ( "git.a-wan.net/alan/pipelines/internal/stage" "io" "log" + "mime" + "net/http" + "net/url" "os" + "path" "path/filepath" + "regexp" "strings" "syscall" "time" @@ -70,6 +75,7 @@ func (r *runner) notifyStep(id int64, name, workspace string, policy *protocol.N // The app stream gets one notification per step event, recorded before // the first ntfy publication; artifacts stay ntfy attachments. appEvent, appKind := stageNotification(event, step.Target) + appArtifact, appPreview := r.resolveNotificationArtifact(id, step.Sequence, policy.Open) recorded := false ntfyOn := ntfyEnabled(ctx, r.settingsStore()) send := func(filename string, data []byte) { @@ -80,7 +86,7 @@ func (r *runner) notifyStep(id int64, name, workspace string, policy *protocol.N receipt := &receipts[len(receipts)-1] if !recorded { recorded = true - r.notes.record(ctx, runNotificationRecord(id, step.Sequence, appEvent, appKind, title, summary)) + r.notes.record(ctx, runNotificationRecord(id, step.Sequence, appEvent, appKind, title, summary, appArtifact, appPreview)) } if !ntfyOn { receipt.Status = "sent" @@ -462,11 +468,15 @@ func (r *runner) deliverRunNotificationBatch(now time.Time, limit int) { // and the web origin is server configuration that can change. n.Click = runAttentionClick(n.RunID) ctx, cancel := context.WithTimeout(r.ctx, 30*time.Second) - r.notes.record(ctx, runNotificationRecord(n.RunID, n.Sequence, n.Event, runNotificationKind(n.Event), n.Title, n.Body)) + artifact, preview := r.resolveNotificationArtifact(n.RunID, n.Sequence, "") + n.Click = notificationClick(n.RunID, n.Sequence, artifact) + r.notes.record(ctx, runNotificationRecord(n.RunID, n.Sequence, n.Event, runNotificationKind(n.Event), n.Title, n.Body, artifact, preview)) var result stage.Values var publishErr error if ntfyEnabled(ctx, r.settingsStore()) { - result, publishErr = ntfyPublish(ctx, n.Channel, n.Title, n.Body, "", nil, n.Click, runAttentionViews(n.RunID)...) + // ntfy retains its existing run-level tap target; the richer artifact + // target is carried by the app notification stream. + result, publishErr = ntfyPublish(ctx, n.Channel, n.Title, n.Body, "", nil, runAttentionClick(n.RunID), runAttentionViews(n.RunID)...) } cancel() messageID, _ := result["message_id"].(string) @@ -536,6 +546,118 @@ func notificationArtifact(workspace, name string) ([]byte, error) { return data, nil } +func notificationClick(runID int64, sequence int, artifact *protocol.NotificationArtifact) string { + if artifact == nil { + return runAttentionClick(runID) + } + parts := strings.Split(artifact.Path, "/") + for i, part := range parts { + parts[i] = url.PathEscape(part) + } + return fmt.Sprintf("pipelines://artifact/%d/%d/%s", runID, sequence, strings.Join(parts, "/")) +} + +// resolveNotificationArtifact selects only files in the run's shared public/. +// It is deliberately independent of the ntfy attachment policy. +func (r *runner) resolveNotificationArtifact(runID int64, sequence int, pattern string) (*protocol.NotificationArtifact, string) { + root := filepath.Join(r.artifactsRoot, fmt.Sprint(runID), "public") + type candidate struct { + path string + info os.FileInfo + } + var candidates []candidate + _ = filepath.Walk(root, func(name string, info os.FileInfo, err error) error { + if err != nil || info == nil { + return nil + } + rel, _ := filepath.Rel(root, name) + if rel == "." { + return nil + } + parts := strings.Split(filepath.ToSlash(rel), "/") + for _, part := range parts { + if strings.HasPrefix(part, ".") { + if info.IsDir() { + return filepath.SkipDir + } + return nil + } + } + if info.Mode()&os.ModeSymlink != 0 { + if info.IsDir() { + return filepath.SkipDir + } + return nil + } + if !info.Mode().IsRegular() { + return nil + } + rel = filepath.ToSlash(rel) + if pattern != "" { + ok, _ := path.Match(pattern, rel) + if !ok { + return nil + } + } + candidates = append(candidates, candidate{rel, info}) + return nil + }) + if pattern == "" && len(candidates) != 1 { + return nil, "" + } + if len(candidates) == 0 { + return nil, "" + } + selected := candidates[0] + for _, c := range candidates[1:] { + if c.info.ModTime().After(selected.info.ModTime()) { + selected = c + } + } + full := filepath.Join(root, filepath.FromSlash(selected.path)) + file, err := os.Open(full) + if err != nil { + return nil, "" + } + defer file.Close() + data, err := io.ReadAll(io.LimitReader(file, 64<<10)) + if err != nil { + return nil, "" + } + media := mime.TypeByExtension(filepath.Ext(selected.path)) + if media == "" { + sample := data + if len(sample) > 512 { + sample = sample[:512] + } + media = http.DetectContentType(sample) + } + a := &protocol.NotificationArtifact{RunID: runID, Sequence: sequence, Path: selected.path, Name: filepath.Base(selected.path), URL: artifactURL(runID, sequence, "public/"+selected.path), MediaType: media} + return a, notificationPreview(data, media, filepath.Ext(selected.path)) +} + +var markdownSyntax = regexp.MustCompile(`[*_` + "`" + `>#\[\]()]`) + +func notificationPreview(data []byte, media, ext string) string { + if !(strings.HasPrefix(media, "text/") || ext == ".md" || ext == ".markdown") { + return "" + } + text := strings.ReplaceAll(string(data), "\r\n", "\n") + lines := strings.Split(text, "\n") + for i, line := range lines { + lines[i] = strings.Join(strings.Fields(markdownSyntax.ReplaceAllString(line, "")), " ") + } + text = strings.TrimSpace(strings.Join(lines, "\n")) + if len([]rune(text)) <= 600 { + return text + } + runes := []rune(strings.TrimRight(text, " \n")) + if len(runes) > 599 { + runes = runes[:599] + } + return strings.TrimRight(string(runes), " \n") + "…" +} + // stageNotificationSummary is a stage notification's one-line body. The // outcome is shown only when the stage recorded one (a waiting human stage has // none yet), and a waiting stage says what it waits for. diff --git a/internal/server/notify_test.go b/internal/server/notify_test.go index dd6debff..4f6ff03e 100644 --- a/internal/server/notify_test.go +++ b/internal/server/notify_test.go @@ -19,6 +19,7 @@ import ( "sync/atomic" "syscall" "testing" + "time" ) func TestStageNotificationEventsAndArtifactAudit(t *testing.T) { @@ -269,6 +270,46 @@ func TestNotificationArtifactPathSafety(t *testing.T) { } } +func TestNotificationArtifactSelectionAndPreview(t *testing.T) { + root := t.TempDir() + public := filepath.Join(root, "9", "public") + if err := os.MkdirAll(filepath.Join(public, "reports"), 0700); err != nil { + t.Fatal(err) + } + old := filepath.Join(public, "reports", "old.md") + newer := filepath.Join(public, "reports", "new.md") + if err := os.WriteFile(old, []byte("old"), 0600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(newer, []byte("# Hello world\n\n"+strings.Repeat("word ", 200)), 0600); err != nil { + t.Fatal(err) + } + oldTime := time.Now().Add(-time.Hour) + if err := os.Chtimes(old, oldTime, oldTime); err != nil { + t.Fatal(err) + } + r := &runner{artifactsRoot: root} + a, preview := r.resolveNotificationArtifact(9, 3, "reports/*.md") + if a == nil || a.Path != "reports/new.md" || a.URL != "/artifacts/9/3/public/reports/new.md" { + t.Fatalf("wrong selected artifact: %+v", a) + } + if len([]rune(preview)) > 600 || !strings.HasPrefix(preview, "Hello world") || !strings.HasSuffix(preview, "…") { + t.Fatalf("wrong preview: %q", preview) + } + if got := notificationClick(9, 3, a); got != "pipelines://artifact/9/3/reports/new.md" { + t.Fatal(got) + } + if got, _ := r.resolveNotificationArtifact(9, 3, ""); got != nil { + t.Fatal("multiple files selected automatically") + } + if err := os.Remove(old); err != nil { + t.Fatal(err) + } + if got, _ := r.resolveNotificationArtifact(9, 3, ""); got == nil || got.Path != "reports/new.md" { + t.Fatal("single artifact not selected automatically") + } +} + // TestRunAttentionEventMapping pins the single decision of whether a run needs // the operator: a run parked on a person (waiting) or a failed run, and nothing // else. A queued, running, succeeded, canceled or interrupted run never notifies.