Stream ntfy notifications to app clients over /connect

Record every notification published to ntfy (run outbox, stage notify
hooks, chat question/approval/reply) in a persisted app_notifications
outbox and stream it to subscribe_notifications clients with an after
cursor; clear_notifications and ack_attention clear across devices, as do
runs leaving waiting, deleted runs and answered chat questions. Add the
notify.ntfy_enabled setting (default on) to stop only the ntfy publish.
This commit is contained in:
2026-11-01 02:03:58 +00:00
parent 6c7c32078c
commit d1846441cc
20 changed files with 1265 additions and 20 deletions
+42 -2
View File
@@ -67,13 +67,28 @@ func (r *runner) notifyStep(id int64, name, workspace string, policy *protocol.N
}
return true
}
// 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)
recorded := false
ntfyOn := ntfyEnabled(ctx, r.settingsStore())
send := func(filename string, data []byte) {
receipts = append(receipts, protocol.NotificationReceipt{Destination: "system", Status: "sending", Filename: filename})
if !persist() {
return
}
receipt := &receipts[len(receipts)-1]
result, err := stage.PublishNtfy(ctx, "system", title, summary, filename, data, runAttentionClick(id), runAttentionViews(id)...)
if !recorded {
recorded = true
r.notes.record(ctx, runNotificationRecord(id, step.Sequence, appEvent, appKind, title, summary))
}
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, runAttentionClick(id), runAttentionViews(id)...)
if err != nil {
receipt.Status = "failed"
receipt.Diagnostic = err.Error()
@@ -165,6 +180,26 @@ func (r *runner) notifyStep(id int64, name, workspace string, policy *protocol.N
}
}
// 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.
func stageNotification(event, target string) (string, string) {
switch {
case event == protocol.NotifyWaiting:
return protocol.NotifyWaiting, protocol.NotificationRunWaiting
case event == "failure" && target == protocol.FailureEnd:
return "failed", protocol.NotificationRunFailed
case event == "failure":
return "failed", protocol.NotificationStageFailed
case target == protocol.SuccessEnd:
return "succeeded", protocol.NotificationRunSucceeded
}
return "success", protocol.NotificationStageSucceeded
}
// Terminal hooks run only after FinishRun commits. They arbitrate against the
// durable intent just like waiting hooks, never publishing a proposed success.
func (r *runner) notifyTerminalStep(id int64, status string) {
@@ -412,7 +447,12 @@ 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)
result, publishErr := stage.PublishNtfy(ctx, n.Channel, n.Title, n.Body, "", nil, n.Click, runAttentionViews(n.RunID)...)
r.notes.record(ctx, runNotificationRecord(n.RunID, n.Sequence, n.Event, runNotificationKind(n.Event), n.Title, n.Body))
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)...)
}
cancel()
messageID, _ := result["message_id"].(string)
save, done := context.WithTimeout(context.Background(), 5*time.Second)