From 35e2a216ab5c0fe28545e697dda671db8c0ba51d Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 21 Nov 2026 15:18:16 +1100 Subject: [PATCH] fix(runs): refuse a message.send step whose channel can't send to a chat (#2085) --- modules/deliveries/deliveries.go | 7 ++++++ modules/runs/message.go | 3 +++ modules/runs/runs.go | 21 +++++++++++++++++ modules/runs/steps_test.go | 39 ++++++++++++++++++++++++++++++-- 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/modules/deliveries/deliveries.go b/modules/deliveries/deliveries.go index dd8f878b0..a76b0126d 100644 --- a/modules/deliveries/deliveries.go +++ b/modules/deliveries/deliveries.go @@ -140,6 +140,13 @@ func (m *Module) Notify(ctx context.Context, n Notification) error { }) } +// Reaches reports whether channel is a running connector that declares the +// delivery to one chat. +func (m *Module) Reaches(channel string) bool { + c, ok := m.channels.Get(channel) + return ok && c.Delivers(deliveryChat) +} + // Send delivers text to one chat on the named channel, and returns when the // channel has accepted it. The key makes the delivery idempotent on the // channel, so a step that is replayed after a crash does not send twice. diff --git a/modules/runs/message.go b/modules/runs/message.go index 07bc4cca5..c845abc19 100644 --- a/modules/runs/message.go +++ b/modules/runs/message.go @@ -9,6 +9,9 @@ import ( // Messenger delivers text to one chat on a messaging channel. type Messenger interface { + // Reaches reports whether channel is running and can send to a chat. A + // workflow that sends through any other channel can't be saved. + Reaches(channel string) bool Send(ctx context.Context, channel, chatID, text, key string) error } diff --git a/modules/runs/runs.go b/modules/runs/runs.go index 3d0c3bfcf..06a6b6ce3 100644 --- a/modules/runs/runs.go +++ b/modules/runs/runs.go @@ -144,6 +144,9 @@ func (e *Engine) validateDocument(ctx context.Context, body string) (Document, e } var problems []Problem for i, step := range doc.Steps { + if step.Type == typeMessage { + problems = append(problems, e.checkMessageChannel(i, step)...) + } if step.Type != typeAgent { continue } @@ -171,6 +174,24 @@ func (e *Engine) validateDocument(ctx context.Context, body string) (Document, e return doc, nil } +// checkMessageChannel refuses a message.send step whose channel can't send to +// a chat, so the workflow fails when it is saved instead of when it runs. A +// step with invalid settings is already reported by the document check. +func (e *Engine) checkMessageChannel(index int, step Step) []Problem { + var s messageSettings + if decodeSettings(step.Settings, &s) != nil || strings.TrimSpace(s.Channel) == "" { + return nil + } + deps, err := e.steps() + if err == nil && deps.Messenger != nil && deps.Messenger.Reaches(s.Channel) { + return nil + } + return []Problem{{ + Path: fmt.Sprintf("steps[%d].settings.channel", index), + Message: fmt.Sprintf("channel %q isn't an accepted connector that sends messages to a chat", s.Channel), + }} +} + // Has reports whether a workflow is stored under name. func (e *Engine) Has(ctx context.Context, orgID, name string) (bool, error) { _, err := e.db.Workflow(ctx, orgID, name, 0) diff --git a/modules/runs/steps_test.go b/modules/runs/steps_test.go index f11351f48..1c74bbcbe 100644 --- a/modules/runs/steps_test.go +++ b/modules/runs/steps_test.go @@ -6,6 +6,8 @@ import ( "encoding/json/jsontext" json "encoding/json/v2" "errors" + "fmt" + "log/slog" "slices" "strings" "testing" @@ -107,10 +109,16 @@ func (n *notices) Notify(_ context.Context, msg deliveries.Notification) error { return nil } -// chats records the messages a workflow sends. +// chats records the messages a workflow sends. Its only channel that can send +// to a chat is "chat", as a connector declaring the chat delivery would be. type chats struct{ sent []string } +func (*chats) Reaches(channel string) bool { return channel == "chat" } + func (c *chats) Send(_ context.Context, channel, chatID, text, _ string) error { + if channel != "chat" { + return fmt.Errorf("connector %q declares no chat delivery", channel) + } c.sent = append(c.sent, channel+"/"+chatID+": "+text) return nil } @@ -418,7 +426,7 @@ steps: told := ¬ices{} sent := &chats{} wf := Deps{Settings: onePersonPath{}, Toolbox: anyBox{}, Judge: judge, Approver: ask, Notifier: told, Messenger: sent}.Compile(doc) - work := &callWork{signalAfter: signalAfter{arrives: 1}} + work := &callWork{arrives: 1} work.bodies = tt.bodies run := store.Run{ID: uuid.New(), Dry: tt.dry} rc := newContext(t.Context(), work, run, uuid.New(), nil) @@ -509,6 +517,33 @@ steps: } } +// TestSavingChecksMessageChannel guards the message.send contract: a workflow +// that sends through a channel no accepted connector declares the chat +// delivery for is refused when it is saved, not when it runs. +func TestSavingChecksMessageChannel(t *testing.T) { + tests := []struct { + name string + channel string + want string + }{ + {name: "a channel that sends to a chat is accepted", channel: "chat"}, + {name: "a channel with no chat delivery is refused", channel: "mail", want: `steps[0].settings.channel: channel "mail" isn't an accepted connector that sends messages to a chat`}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + engine := newEngine(nil, &fakeWork{}, slog.New(slog.DiscardHandler), Options{}) + engine.Use(Deps{Settings: onePersonPath{}, Messenger: &chats{}}) + _, err := engine.validateDocument(t.Context(), "steps:\n - {type: message.send, settings: {channel: "+tt.channel+", chat_id: ops, text: hi}}\n") + switch { + case tt.want == "" && err != nil: + t.Fatalf("validateDocument: %v", err) + case tt.want != "" && (err == nil || !strings.Contains(err.Error(), tt.want)): + t.Fatalf("validateDocument error = %v, want %q", err, tt.want) + } + }) + } +} + // stepsBeforeRetry is what a retry from the named step keeps: the steps // recorded before it began, and the markers written before it was entered. It // mirrors store.Retry.