From b6b13ea97cbc478696b12cf3e11b0bb53994147b Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 26 Oct 2026 18:29:34 +1100 Subject: [PATCH] fix(archied): bound task titles in chat notifications --- internal/app/archied/outcome_post.go | 24 +++++++++++++++--- internal/app/archied/outcome_post_test.go | 31 +++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/internal/app/archied/outcome_post.go b/internal/app/archied/outcome_post.go index f4e4bde26..14dfda26b 100644 --- a/internal/app/archied/outcome_post.go +++ b/internal/app/archied/outcome_post.go @@ -103,13 +103,14 @@ func validOrigin(origin string) bool { } func outcomeText(t *task.Task) string { + title := notificationTitle(t.Title) if kind := taskstate.ApprovalKind(t.Status, t.ReviewGate); kind != "" { - return fmt.Sprintf("Task %d (%s) needs your approval of its %s. Approve or reject it on the task page.", t.ID, t.Title, approvalNoun[kind]) + return fmt.Sprintf("Task %d (%s) needs your approval of its %s. Approve or reject it on the task page.", t.ID, title, approvalNoun[kind]) } if t.Status == task.StatusPROpen { - return fmt.Sprintf("Task %d (%s) opened PR #%d for review.", t.ID, t.Title, t.PRNumber) + return fmt.Sprintf("Task %d (%s) opened PR #%d for review.", t.ID, title, t.PRNumber) } - text := fmt.Sprintf("Task %d (%s) finished: %s", t.ID, t.Title, t.Status) + text := fmt.Sprintf("Task %d (%s) finished: %s", t.ID, title, t.Status) if t.PRNumber > 0 { text += fmt.Sprintf(", PR #%d", t.PRNumber) } @@ -119,6 +120,23 @@ func outcomeText(t *task.Task) string { return text } +// maxNotificationTitleRunes bounds the title a notification echoes. New titles +// are capped at the same length when they are created, but a task from another +// writer -- an issue, a schedule -- can still carry a long one, and the +// notification is one line of chat. +const maxNotificationTitleRunes = 80 + +// notificationTitle reduces a task title to a single bounded line. The title +// is the one part of the notification a writer other than chat controls. +func notificationTitle(title string) string { + line := strings.Join(strings.Fields(title), " ") + runes := []rune(line) + if len(runes) <= maxNotificationTitleRunes { + return line + } + return strings.TrimSpace(string(runes[:maxNotificationTitleRunes])) + "..." +} + // approvalNoun names what an approval kind asks the operator to decide. var approvalNoun = map[string]string{ taskstate.ApprovalPlan: "plan", diff --git a/internal/app/archied/outcome_post_test.go b/internal/app/archied/outcome_post_test.go index b12aafd10..f43369d83 100644 --- a/internal/app/archied/outcome_post_test.go +++ b/internal/app/archied/outcome_post_test.go @@ -4,7 +4,9 @@ import ( "context" "errors" "log/slog" + "strings" "testing" + "unicode/utf8" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -23,6 +25,35 @@ func (s *outcomeStore) MarkOutcomePosted(_ context.Context, id int64, _ string) return nil } +func TestOutcomeTextBoundsTheTitle(t *testing.T) { + title := strings.Repeat("workflow_draft returns InvalidArgument because ", 30) + "\nsecond line" + tests := []struct { + name string + task *task.Task + }{ + {"approval", &task.Task{ID: 7, Title: title, Status: task.StatusWaitingApproval}}, + {"pr open", &task.Task{ID: 7, Title: title, Status: task.StatusPROpen, PRNumber: 6}}, + {"finished", &task.Task{ID: 7, Title: title, Status: task.StatusRejected}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + text := outcomeText(tt.task) + if strings.Contains(text, title) { + t.Fatalf("text echoes the whole title: %q", text) + } + if strings.Contains(text, "\n") { + t.Errorf("text is not one line: %q", text) + } + if got := utf8.RuneCountInString(text); got > 200 { + t.Errorf("text is %d runes, want at most 200: %q", got, text) + } + if !strings.Contains(text, "workflow_draft returns") { + t.Errorf("text dropped the start of the title: %q", text) + } + }) + } +} + func TestPostPendingOutcomes(t *testing.T) { tests := []struct { name string