Notify link=: review fixes

- ntfy uses the link only beside a web run view; otherwise it keeps the
  run click and the receipt says why (app click stays the link).
- The outbox retry of a hook's failed publication re-resolves the stage's
  link=, so the ntfy and app tap targets keep it.
- A valid link file is never the attached artifact.
- Notification artifacts are read through os.OpenRoot with O_NOFOLLOW.
- The link note is recorded once, on the first receipt, and never becomes
  the run-level failure reason.
- Android: a recents relaunch does not reopen the link, and a run thread of
  several entries opens the run rather than the latest external page.
- Docs and the protocol comment say "first non-empty line".
This commit is contained in:
2026-12-02 12:29:46 +00:00
parent f5f31b7bcd
commit 0c75ff1b0d
10 changed files with 401 additions and 109 deletions
+47 -41
View File
@@ -75,22 +75,16 @@ 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)
// A `link=` file decides the tap target (ntfy and app alike); the run stays
// reachable through the app's run action and ntfy's browser view.
click := runAttentionClick(id)
link, linkDiagnostic := r.resolveNotificationLink(id, policy.Link)
if link != "" {
click = link
}
note := func(diagnostic string) string {
if linkDiagnostic == "" || diagnostic == "" {
return diagnostic + linkDiagnostic
}
return diagnostic + "; " + linkDiagnostic
}
// 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() {
@@ -99,25 +93,20 @@ func (r *runner) notifyStep(id int64, name, workspace string, policy *protocol.N
receipt := &receipts[len(receipts)-1]
if !recorded {
recorded = true
record := runNotificationRecord(id, step.Sequence, appEvent, appKind, title, summary, appArtifact, appPreview)
if link != "" {
record.Click, record.RunClick = link, runAttentionClick(id)
}
r.notes.record(ctx, record)
r.notes.record(ctx, withNotificationLink(runNotificationRecord(id, step.Sequence, appEvent, appKind, title, summary, appArtifact, appPreview), link))
}
if !ntfyOn {
receipt.Status = "sent"
receipt.Diagnostic = note("ntfy disabled; delivered to app clients only")
receipt.Diagnostic = "ntfy disabled; delivered to app clients only"
persist()
return
}
result, err := ntfyPublish(ctx, "system", title, summary, filename, data, click, runAttentionViews(id)...)
result, err := ntfyPublish(ctx, "system", title, summary, filename, data, ntfyClick, views...)
if err != nil {
receipt.Status = "failed"
receipt.Diagnostic = note(err.Error())
receipt.Diagnostic = err.Error()
} else {
receipt.Status = "sent"
receipt.Diagnostic = note("")
receipt.MessageID, _ = result["message_id"].(string)
receipt.AttachmentURL, _ = result["attachment_url"].(string)
}
@@ -181,6 +170,19 @@ func (r *runner) notifyStep(id int64, name, workspace string, policy *protocol.N
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 {
@@ -194,11 +196,7 @@ func (r *runner) notifyStep(id int64, name, workspace string, policy *protocol.N
}
}
if lease != nil {
diagnostic := "stage notification publication failed"
if len(receipts) > 0 && receipts[0].Diagnostic != "" {
diagnostic = receipts[0].Diagnostic
}
if err := r.store.FinishRunNotification(r.ctx, *lease, time.Now().Unix(), "", fmt.Errorf("%s", diagnostic)); err != nil {
if err := r.store.FinishRunNotification(r.ctx, *lease, time.Now().Unix(), "", fmt.Errorf("%s", failure)); err != nil {
log.Printf("release stage notification run=%d: %v", id, err)
}
}
@@ -486,15 +484,19 @@ 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)
artifact, preview := r.resolveNotificationArtifact(n.RunID, n.Sequence, "")
// A retry of a stage hook's failed publication keeps the hook's link.
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, runNotificationRecord(n.RunID, n.Sequence, n.Event, runNotificationKind(n.Event), n.Title, n.Body, artifact, preview))
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; 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)...)
// 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...)
}
cancel()
messageID, _ := result["message_id"].(string)
@@ -577,13 +579,13 @@ 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.
func (r *runner) resolveNotificationArtifact(runID int64, sequence int, pattern string) (*protocol.NotificationArtifact, string) {
selected := r.publicNotificationFile(runID, pattern)
// 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)
if selected == "" {
return nil, ""
}
full := filepath.Join(r.artifactsRoot, fmt.Sprint(runID), "public", filepath.FromSlash(selected))
file, err := os.Open(full)
file, _, err := openPublicFile(filepath.Join(r.artifactsRoot, fmt.Sprint(runID), "public"), selected)
if err != nil {
return nil, ""
}
@@ -604,10 +606,11 @@ func (r *runner) resolveNotificationArtifact(runID int64, sequence int, pattern
return a, notificationPreview(data, media, filepath.Ext(selected))
}
// publicNotificationFile picks a regular, non-hidden, non-symlinked file in the
// run's shared public/ as a slash path: the most recently modified match of
// pattern, or without a pattern the only file there. "" when none qualifies.
func (r *runner) publicNotificationFile(runID int64, pattern string) string {
// publicNotificationFile picks a regular, non-hidden, non-symlinked file other
// than exclude in the run's shared public/ as a slash path: the most recently
// modified match of pattern, or without a pattern the only such file there.
// "" when none qualifies.
func (r *runner) publicNotificationFile(runID int64, pattern, exclude string) string {
root := filepath.Join(r.artifactsRoot, fmt.Sprint(runID), "public")
type candidate struct {
path string
@@ -641,6 +644,9 @@ func (r *runner) publicNotificationFile(runID int64, pattern string) string {
return nil
}
rel = filepath.ToSlash(rel)
if rel == exclude {
return nil
}
if pattern != "" {
ok, _ := path.Match(pattern, rel)
if !ok {