run attention: publish to ntfy and surface what needs the operator
Nothing told the operator that a run was waiting for a person or had failed; they found out by going looking. A run that needs attention now reaches the ntfy they already run, and the app answers "what needs me now" in one live, ordered surface. Server (smallest change, reusing the existing publisher): - internal/server/notify.go adds notifyRun, called from finishRunState (failed) and when a run parks on a human (runner.go runLoop). It publishes to the same already-configured `system` destination through the unchanged stage.PublishNtfy, so there is no second publisher, topic or service and the token handling is the stage's own. runAttentionEvent maps only waiting and failed; queued, running, succeeded, canceled and interrupted publish nothing. - No storms: runner.attentionSent records sequence:event per run before publishing, so a run that stays waiting is never republished on a re-request or restart, and a stage notify policy that already covers the same event marks it first so the run-level default is suppressed. The set is dropped when the run is terminal. A delivery failure is logged, never fails the run, and is not retried. - The payload names the run and pipeline and carries a click action pipelines://run/<id> (validated: absolute URL, no userinfo, no fragment, no credentials). PublishNtfy gained the optional click parameter; existing callers pass "". App: - AndroidManifest/MainActivity declare the pipelines://run/<id> deep link (singleTop); runIdFromDeepLink parses it and PipelinesApp opens that run once, clearing the link. It opens a run; it starts nothing. - ui/Attention.kt is the pure model (status -> Decision/Failed/Finished or nothing, ordered by kind then newest-first) and ui/components/AttentionSurface.kt is the floating indicator plus bottom sheet. It is derived from the same run events the server already pushes, so an entry appears with no refresh and clears when resolved; a finished run clears once looked at, and any entry can be dismissed, persisted in SettingsRepository. The existing destinations, pill and bottom strip are untouched and no competing navigation surface is added. Checked before committing: - Packaged authoring skill references: internal/author/skill/references/ notifications.md documents the automatic run-attention notifications and the app surface. The only packaged "template" carrying notify is that reference; no pipeline definition template exists to update and install.go's managed file list is unchanged (notifications.md already existed). - Repository guidelines: docs/ntfy.md gains an "Automatic run-attention notifications" section and no longer claims "No block means no notifications"; the top-level README phone-notifications note now says a waiting/failed run notifies automatically; android/README.md gains the dated feature section and supersedes the previous section's note that the attention surface was absent. - docker.md, install docs and the CLI/TUI docs describe mechanisms this change does not alter; left as they are. Tests: go test ./internal/server/... ./internal/stage/... ./internal/author/... all ok; notify_test.go covers the event mapping and the real HTTP delivery (destination, topic, click, no duplicate). Android :app:testDebugUnitTest -> BUILD SUCCESSFUL, 58 suites / 531 tests / 0 failures (new AttentionTest, 13 cases, covering ordering, appearing from a push, clearing on resolution, dismissal, the empty case, persistence and the deep link). Device screenshots and the phone's own notification are visual review's per the brief's device policy. Scope: android/ plus the smallest server change; no new dependency, no run started from the app, no installer or Docker change.
This commit is contained in:
+27
-3
@@ -79,7 +79,7 @@ func (NtfyStage) Run(ctx context.Context, def Definition, input Values) (Result,
|
||||
if strings.ContainsAny(title, "\r\n") || strings.ContainsAny(summary, "\r\n") {
|
||||
return Result{}, fmt.Errorf("ntfy title and summary must be single-line strings")
|
||||
}
|
||||
delivery, err := PublishNtfy(ctx, def.Configs["destination"], title, summary, "digest.md", []byte(markdown))
|
||||
delivery, err := PublishNtfy(ctx, def.Configs["destination"], title, summary, "digest.md", []byte(markdown), "")
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
@@ -89,11 +89,17 @@ func (NtfyStage) Run(ctx context.Context, def Definition, input Values) (Result,
|
||||
}
|
||||
|
||||
// PublishNtfy is shared by native delivery stages and stage notification hooks.
|
||||
// An empty filename publishes an event without an attachment.
|
||||
func PublishNtfy(ctx context.Context, destination, title, summary, filename string, data []byte) (Values, error) {
|
||||
// An empty filename publishes an event without an attachment. A non-empty click
|
||||
// is an optional ntfy Click action (an absolute URL) so a tap on the notification
|
||||
// opens the destination the operator should act on; it is validated here and
|
||||
// never carries credentials.
|
||||
func PublishNtfy(ctx context.Context, destination, title, summary, filename string, data []byte, click string) (Values, error) {
|
||||
if !destinationName.MatchString(destination) || strings.TrimSpace(title) == "" || strings.TrimSpace(summary) == "" || len(title) > 256 || len(summary) > 2048 || !utf8.ValidString(title) || !utf8.ValidString(summary) || strings.ContainsAny(title+summary, "\r\n\x00") {
|
||||
return nil, fmt.Errorf("invalid ntfy notification metadata")
|
||||
}
|
||||
if !validNtfyClick(click) {
|
||||
return nil, fmt.Errorf("invalid ntfy click action")
|
||||
}
|
||||
if len(data) > 1<<20 || (filename != "" && (filename != filepath.Base(filename) || filename == "." || strings.ContainsAny(filename, "/\\\r\n\x00") || len(filename) > 200)) {
|
||||
return nil, fmt.Errorf("invalid ntfy attachment")
|
||||
}
|
||||
@@ -145,6 +151,9 @@ func PublishNtfy(ctx context.Context, destination, title, summary, filename stri
|
||||
}
|
||||
request.Header.Set("Title", mime.BEncoding.Encode("utf-8", title))
|
||||
request.Header.Set("Message", mime.BEncoding.Encode("utf-8", summary))
|
||||
if click != "" {
|
||||
request.Header.Set("Click", click)
|
||||
}
|
||||
if filename != "" {
|
||||
request.Header.Set("Filename", mime.BEncoding.Encode("utf-8", filename))
|
||||
}
|
||||
@@ -189,3 +198,18 @@ func PublishNtfy(ctx context.Context, destination, title, summary, filename stri
|
||||
}
|
||||
return Values{"destination": destination, "message_id": ack.ID, "attachment_url": ack.Attachment.URL, "filename": filename}, nil
|
||||
}
|
||||
|
||||
// validNtfyClick accepts an empty click or an absolute URL the ntfy server will
|
||||
// hand to the operating system on tap: a scheme, no credentials, no fragment,
|
||||
// no control characters and a bounded length. The value is an operator-visible
|
||||
// deep link, never a place to smuggle a token.
|
||||
func validNtfyClick(click string) bool {
|
||||
if click == "" {
|
||||
return true
|
||||
}
|
||||
if len(click) > 2048 || !utf8.ValidString(click) || strings.ContainsAny(click, "\r\n\t\x00 ") {
|
||||
return false
|
||||
}
|
||||
u, err := url.Parse(click)
|
||||
return err == nil && u.Scheme != "" && u.User == nil && u.Fragment == ""
|
||||
}
|
||||
|
||||
@@ -32,16 +32,16 @@ func TestNtfyTokenFile(t *testing.T) {
|
||||
t.Setenv("PIPELINES_NTFY_ANIME_URL", srv.URL+"/anime")
|
||||
t.Setenv("PIPELINES_NTFY_ANIME_TOKEN", "")
|
||||
t.Setenv("PIPELINES_NTFY_ANIME_TOKEN_FILE", path)
|
||||
if _, err := PublishNtfy(context.Background(), "anime", "Title", "Summary", "", nil); err != nil {
|
||||
if _, err := PublishNtfy(context.Background(), "anime", "Title", "Summary", "", nil, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PIPELINES_NTFY_ANIME_TOKEN", "other-token")
|
||||
if _, err := PublishNtfy(context.Background(), "anime", "Title", "Summary", "", nil); err == nil {
|
||||
if _, err := PublishNtfy(context.Background(), "anime", "Title", "Summary", "", nil, ""); err == nil {
|
||||
t.Fatal("ambiguous token sources accepted")
|
||||
}
|
||||
t.Setenv("PIPELINES_NTFY_ANIME_TOKEN", "")
|
||||
os.WriteFile(path, nil, 0600)
|
||||
if _, err := PublishNtfy(context.Background(), "anime", "Title", "Summary", "", nil); err == nil {
|
||||
if _, err := PublishNtfy(context.Background(), "anime", "Title", "Summary", "", nil, ""); err == nil {
|
||||
t.Fatal("empty token accepted")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user