diff --git a/changelog.d/added-rfc-notify-threads.md b/changelog.d/added-rfc-notify-threads.md new file mode 100644 index 0000000000..e18484a0ec --- /dev/null +++ b/changelog.d/added-rfc-notify-threads.md @@ -0,0 +1 @@ +- With `rfc_threads: true` on the designated `rfc_announcer`, each RFC now gets one Discord thread under its opened card, with a durable per-RFC message/thread mapping in `/data/rfc-notify.json` (state version 2; version 1 files load unchanged), a write-ahead intent plus Discord `enforce_nonce` and a bounded 50-message history lookup so a crash or retry never posts a second card, later closed/ready/doc-accepted events posted into the thread (closed and ready also echoed once to the channel), and an opt-in `rfc_backlink_comment` that comments the thread link on the RFC once, only after the thread exists (#15939). diff --git a/src/cmd/hive/rfcnotify_wire.go b/src/cmd/hive/rfcnotify_wire.go index 685803bd10..ae96268a6c 100644 --- a/src/cmd/hive/rfcnotify_wire.go +++ b/src/cmd/hive/rfcnotify_wire.go @@ -56,6 +56,10 @@ type rfcNotifyState struct { // newPoster builds the Discord poster; nil means discordRFCPoster. // Tests inject a recorder. newPoster func(token, channel string) rfcnotify.Poster + // threads is the Discord surface of threads mode, built for channel; + // newThreads builds it (nil means discordRFCThreads). Tests inject a fake. + threads rfcnotify.Threads + newThreads func(token, channel string) rfcnotify.Threads } // rfcNotifyState lazily builds the boot's notice state. @@ -128,6 +132,17 @@ func (s *rfcNotifyState) ensure(cfg *config.Config, repos int, logger *slog.Logg if s.post == nil || s.channel != channel { s.post = newPoster(token, channel) s.channel = channel + s.threads = nil + } + if gates.threads { + if s.threads == nil { + newThreads := s.newThreads + if newThreads == nil { + newThreads = discordRFCThreads(logger) + } + s.threads = newThreads(token, channel) + } + s.sweeper.SetThreads(s.threads) } return true } @@ -141,6 +156,53 @@ func discordRFCPoster(logger *slog.Logger) func(token, channel string) rfcnotify } } +// discordRFCThreads builds the threads-mode Discord surface for the RFC +// channel. It posts through the bot token like discordRFCPoster, without +// starting the command bot. +func discordRFCThreads(logger *slog.Logger) func(token, channel string) rfcnotify.Threads { + return func(token, channel string) rfcnotify.Threads { + return rfcDiscordThreads{bot: discord.NewBot(discord.Config{Token: token}, logger), channel: channel} + } +} + +// rfcDiscordThreads adapts a discord.Bot to rfcnotify.Threads for one channel. +type rfcDiscordThreads struct { + bot *discord.Bot + channel string +} + +func (d rfcDiscordThreads) Post(content, nonce string) (string, error) { + return d.bot.PostMessageNonce(d.channel, content, nonce) +} + +func (d rfcDiscordThreads) StartThread(messageID, name string) (string, error) { + return d.bot.StartThreadFromMessage(d.channel, messageID, name) +} + +func (d rfcDiscordThreads) Send(threadID, content string) error { + return d.bot.SendMessageTo(threadID, content) +} + +func (d rfcDiscordThreads) Echo(content string) error { + return d.bot.SendMessageTo(d.channel, content) +} + +func (d rfcDiscordThreads) ThreadURL(threadID string) (string, error) { + return d.bot.ThreadURL(threadID) +} + +func (d rfcDiscordThreads) Recent(limit int) ([]rfcnotify.Message, error) { + msgs, err := d.bot.RecentMessages(d.channel, limit) + if err != nil { + return nil, err + } + out := make([]rfcnotify.Message, 0, len(msgs)) + for _, m := range msgs { + out = append(out, rfcnotify.Message{ID: m.ID, Content: m.Content, Nonce: m.Nonce, Self: m.Self}) + } + return out, nil +} + type rfcRepoVisibility struct { private bool at time.Time @@ -224,6 +286,13 @@ func newRFCNotifySource(c *github.Client, state *rfcNotifyState) rfcnotify.Sourc return rfcNotifySource{c: c, state: state} } +// Comment implements rfcnotify.Commenter for the opt-in thread backlink. It +// goes through the per-issue bot comment budget, so a backlink can never +// flood an issue. +func (s rfcNotifySource) Comment(ctx context.Context, repo string, number int, body string) error { + return s.c.CreateIssueCommentBudgeted(ctx, repo, number, body) +} + func (s rfcNotifySource) ListOpen(ctx context.Context) ([]rfcnotify.Issue, error) { snaps, err := s.c.ListIssueSnapshots(ctx, github.IssueSnapshotOptions{State: "open"}) return s.markPrivate(ctx, rfcNotifyIssues(snaps)), err @@ -381,8 +450,11 @@ func runRFCNotifySweep(ctx context.Context, src rfcnotify.Source, repos int, cfg dashSrv.AuditLogRecord("system", "rfc-notify", fmt.Sprintf("channel=%s %s", state.channel, res.Posted[i]), "", ev.Issue.Repo, ev.Issue.Number) } } - if len(res.Posted) > 0 || res.Errors > 0 || res.Withheld > 0 || res.Unresolved > 0 { - logger.Info("rfc notify sweep", "posted", len(res.Posted), "errors", res.Errors, "withheld", res.Withheld, "unresolved", res.Unresolved) + if dashSrv != nil && (res.Threads > 0 || res.Reconciled > 0 || res.Backlinks > 0 || res.Errors > 0) { + dashSrv.AuditLogRecord("system", "rfc-notify", fmt.Sprintf("channel=%s threads=%d reconciled=%d backlinks=%d errors=%d", state.channel, res.Threads, res.Reconciled, res.Backlinks, res.Errors), "", "", 0) + } + if len(res.Posted) > 0 || res.Errors > 0 || res.Withheld > 0 || res.Unresolved > 0 || res.Threads > 0 || res.Reconciled > 0 || res.Backlinks > 0 { + logger.Info("rfc notify sweep", "posted", len(res.Posted), "errors", res.Errors, "withheld", res.Withheld, "unresolved", res.Unresolved, "threads", res.Threads, "reconciled", res.Reconciled, "backlinks", res.Backlinks) } return res } diff --git a/src/cmd/hive/rfcnotify_wire_test.go b/src/cmd/hive/rfcnotify_wire_test.go index 5731bdb755..2ef4b4f65f 100644 --- a/src/cmd/hive/rfcnotify_wire_test.go +++ b/src/cmd/hive/rfcnotify_wire_test.go @@ -502,3 +502,98 @@ func TestRunRFCNotifySweepRFCPRs(t *testing.T) { t.Fatalf("merge: %+v posted=%q", res, posted) } } + +// rfcNotifyFakeThreads records what threads mode sends to Discord. +type rfcNotifyFakeThreads struct { + posts, echoes []string + threads []string + sends []string +} + +func (f *rfcNotifyFakeThreads) Post(content, nonce string) (string, error) { + f.posts = append(f.posts, content+"|"+nonce) + return "m1", nil +} + +func (f *rfcNotifyFakeThreads) StartThread(messageID, name string) (string, error) { + f.threads = append(f.threads, messageID+"|"+name) + return "t1", nil +} + +func (f *rfcNotifyFakeThreads) Send(threadID, content string) error { + f.sends = append(f.sends, threadID+"|"+content) + return nil +} + +func (f *rfcNotifyFakeThreads) Echo(content string) error { + f.echoes = append(f.echoes, content) + return nil +} + +func (f *rfcNotifyFakeThreads) Recent(int) ([]rfcnotify.Message, error) { return nil, nil } + +func (f *rfcNotifyFakeThreads) ThreadURL(id string) (string, error) { return "https://d/" + id, nil } + +func TestRunRFCNotifySweepThreadsMode(t *testing.T) { + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + var lines []string + state := rfcNotifyRecorder(t, &lines) + fake := &rfcNotifyFakeThreads{} + var builtFor []string + state.newThreads = func(token, channel string) rfcnotify.Threads { + builtFor = append(builtFor, token+"@"+channel) + return fake + } + src := &rfcNotifyFakeSource{open: []rfcnotify.Issue{{Repo: "o/r", Number: 1, Title: "RFC: a", URL: "u", State: "open", CreatedAt: time.Now().Add(-time.Hour)}}} + cfg := rfcNotifyConfig("tok", "42") + cfg.Notifications.Discord.RFCThreads = true + cfg.Notifications.Discord.RFCAnnouncer = true + runRFCNotifySweep(context.Background(), src, 1, cfg, nil, state, logger) + + fresh := rfcnotify.Issue{Repo: "o/r", Number: 2, Title: "RFC: b", URL: "v", State: "open", CreatedAt: time.Now().Add(time.Hour)} + src.updated = []rfcnotify.Issue{fresh} + forcePoll(state) + res := runRFCNotifySweep(context.Background(), src, 1, cfg, nil, state, logger) + if res.Threads != 1 || len(fake.posts) != 1 || len(fake.threads) != 1 || !strings.HasPrefix(fake.threads[0], "m1|#2 RFC: b") { + t.Fatalf("threads mode: %+v posts=%v threads=%v", res, fake.posts, fake.threads) + } + if len(lines) != 0 { + t.Fatalf("threads mode must not use the line poster: %v", lines) + } + if len(builtFor) != 1 || builtFor[0] != "tok@42" { + t.Fatalf("threads built for %v", builtFor) + } + // A channel change rebuilds the Discord surface for the new channel. + cfg.Notifications.Discord.RFCChannelID = "43" + forcePoll(state) + src.updated = nil + runRFCNotifySweep(context.Background(), src, 1, cfg, nil, state, logger) + if len(builtFor) != 2 || builtFor[1] != "tok@43" { + t.Fatalf("threads not rebuilt for the new channel: %v", builtFor) + } +} + +func TestRunRFCNotifySweepThreadsNeedAnnouncer(t *testing.T) { + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + var lines []string + state := rfcNotifyRecorder(t, &lines) + fake := &rfcNotifyFakeThreads{} + state.newThreads = func(string, string) rfcnotify.Threads { return fake } + src := &rfcNotifyFakeSource{open: []rfcnotify.Issue{{Repo: "o/r", Number: 1, Title: "RFC: a", URL: "u", State: "open", CreatedAt: time.Now().Add(-time.Hour)}}} + cfg := rfcNotifyConfig("tok", "42") + cfg.Notifications.Discord.RFCThreads = true + runRFCNotifySweep(context.Background(), src, 1, cfg, nil, state, logger) + src.updated = []rfcnotify.Issue{{Repo: "o/r", Number: 2, Title: "RFC: b", URL: "v", State: "open", CreatedAt: time.Now().Add(time.Hour)}} + forcePoll(state) + res := runRFCNotifySweep(context.Background(), src, 1, cfg, nil, state, logger) + if res.Withheld != 1 || len(fake.posts)+len(fake.echoes)+len(fake.threads) != 0 || len(lines) != 0 { + t.Fatalf("a hive that is not the announcer must not call Discord: %+v %+v", res, fake) + } +} + +func TestRFCNotifySourceCommentWithoutClient(t *testing.T) { + var c rfcnotify.Commenter = rfcNotifySource{} + if err := c.Comment(context.Background(), "o/r", 1, "Discussion thread: x"); !errors.Is(err, github.ErrNoGitHubClient) { + t.Fatalf("err = %v, want ErrNoGitHubClient", err) + } +} diff --git a/src/docs/notifications.md b/src/docs/notifications.md index f0adf091b2..9cf58d3b1e 100644 --- a/src/docs/notifications.md +++ b/src/docs/notifications.md @@ -211,9 +211,9 @@ These keys sit next to `rfc_channel_id`. All of them default off, and with none | Key | Default | Effect | | --- | --- | --- | | `rfc_format` | `""` (= `line`) | `line` posts one line per transition. `card` selects rich mode; card rendering lands in a later change. Any other value is a config error. | -| `rfc_threads` | `false` | Rich mode: one Discord thread per RFC (rendering lands later). | +| `rfc_threads` | `false` | Rich mode: one Discord thread per RFC in the existing text channel (see [Threads](#one-thread-per-rfc-rfc_threads)). Needs `rfc_announcer`. | | `rfc_announcer` | `false` | Marks this hive as the one designated announcer for `rfc_channel_id`. Rich mode posts only from the announcer. | -| `rfc_backlink_comment` | `false` | Parsed now; a later change uses it to comment the thread link back on the RFC. | +| `rfc_backlink_comment` | `false` | With `rfc_threads`, comments `Discussion thread: ` on the RFC once, after the thread exists. | | `rfc_include_private` | `false` | Authorizes posting RFCs from private repositories to the channel. | - **Announcer rule.** Rich mode means `rfc_format: card` or `rfc_threads: true`. A hive in rich mode without `rfc_announcer: true` posts nothing and logs `rfc notify: rich mode requires rfc_announcer; not posting` once. It keeps recording transitions, so turning on the announcer later does not backfill old ones. Set `rfc_announcer: true` on exactly one hive per channel. Line mode ignores the key. @@ -229,3 +229,26 @@ notifications: rfc_announcer: true # only on the one announcing hive rfc_include_private: false # default: private-repo RFCs are not posted ``` + +#### One thread per RFC (`rfc_threads`) + +On the announcer, `rfc_threads: true` keeps the text channel (no Forum) and turns each RFC into a card plus a thread: + +- **Opened** posts the card, then starts a thread on it named `#N ` (cut to Discord's 100 characters). The card and the thread ID are stored per RFC in `/data/rfc-notify.json`, which is now state version 2; a version 1 file loads unchanged, and a file written by a newer hive is refused rather than downgraded. +- **Closed, ready to implement and doc accepted** post into the RFC's thread. Closed and ready also echo one line to the channel. An RFC announced before threads were enabled has no thread and keeps getting the plain channel line; it is never back-filled. +- **Idempotency.** Before the card is posted the hive saves a write-ahead intent with a deterministic nonce (a hash of `repo#number` plus `opened`, 25 characters) and sends it with Discord's `nonce` and `enforce_nonce`, so a retry inside Discord's dedup window returns the same message. Channel history is never treated as a claim between hives; the single announcer is the only writer. +- **Reconciliation.** At startup, and whenever an RFC has an intent without a confirmed card or a card without a thread, the hive does one lookup of the channel's last 50 messages for a message of its own bot with the card's nonce or exact text. A match is adopted (no second card); with no match the card is re-posted once more with the same nonce. A failed lookup re-posts nothing. Card posts and thread starts are each tried at most 3 times across polls and restarts, then logged and left alone. Only RFCs this hive began announcing are reconciled, so there is no historical burst. +- **Backlink (`rfc_backlink_comment: true`).** Once the thread is confirmed the hive comments `Discussion thread: <thread URL>` on the RFC, through the per-issue bot comment budget. The flag is saved after it succeeds, so restarts do not repeat it; a failure leaves the flag unset and is retried up to 3 times, then logged. Default off: the hive never comments on issues otherwise. +- **Rate limits and failures.** Every Discord call honours a `429` `retry_after` with at most 3 bounded retries (each wait capped at 15 seconds). Failures count in the sweep's `errors`, are logged as warnings, and are written to the audit log as `rfc-notify` lines with `threads`, `reconciled`, `backlinks` and `errors`. + +```yaml +notifications: + discord: + bot_token: ${DISCORD_BOT_TOKEN} + rfc_channel_id: "1540404487099777136" + rfc_threads: true + rfc_announcer: true # only on the one announcing hive + rfc_backlink_comment: false # opt in to comment the thread link on the RFC +``` + +The bot needs the `Create Public Threads` and `Send Messages in Threads` permissions, plus `Read Message History` for reconciliation. diff --git a/src/pkg/discord/bot.go b/src/pkg/discord/bot.go index c123a32254..edc2b5868f 100644 --- a/src/pkg/discord/bot.go +++ b/src/pkg/discord/bot.go @@ -9,6 +9,7 @@ import ( "log/slog" "net/http" "net/url" + "strconv" "strings" "sync" "time" @@ -28,8 +29,27 @@ const ( discordMsgLimit = 1900 sseReconnectBase = 5 * time.Second sseReconnectMax = 60 * time.Second + + // nonceLimit is Discord's longest message nonce. + nonceLimit = 25 + // threadNameLimit is Discord's longest thread name, in characters. + threadNameLimit = 100 + // historyLimit bounds RecentMessages; crash recovery never reads further back. + historyLimit = 50 + // maxRateLimitRetries bounds the retries of one call after a 429. + maxRateLimitRetries = 3 + // maxRetryAfter caps a single 429 wait. + maxRetryAfter = 15 * time.Second + // threadExistsCode is Discord's error code for a message that already + // has a thread. + threadExistsCode = "160004" + // maxResponseBytes caps how much of a response body is read. + maxResponseBytes = 1 << 20 ) +// rateLimitSleep waits out a 429; tests replace it. +var rateLimitSleep = time.Sleep + // AgentIdentity holds the Discord display metadata for an agent. type AgentIdentity struct { Emoji string @@ -102,6 +122,11 @@ type Bot struct { msgQueue chan msgItem lastState *statusSnapshot lastTopic string + + // self is the bot's own user ID, resolved on first use. + self string + // guilds maps a thread ID to its guild, for thread links. + guilds map[string]string } type msgItem struct { @@ -202,36 +227,245 @@ func (b *Bot) SendMessageTo(channelID, content string) error { if strings.TrimSpace(channelID) == "" { return fmt.Errorf("discord send: channel id not configured") } - payload := map[string]any{ + return b.do(http.MethodPost, fmt.Sprintf("%s/channels/%s/messages", discordAPIBase, channelID), messagePayload(content), nil) +} + +// messagePayload is a plain-text message that cannot ping anyone. +func messagePayload(content string) map[string]any { + return map[string]any{ "content": content, "allowed_mentions": map[string]any{"parse": []string{}}, } - data, err := json.Marshal(payload) - if err != nil { - return err +} + +// PostMessageNonce posts content to channelID with a Discord nonce and +// enforce_nonce, so a retry inside Discord's dedup window returns the +// message the first attempt created instead of a second one. It returns the +// message ID. The nonce is at most 25 characters. +func (b *Bot) PostMessageNonce(channelID, content, nonce string) (string, error) { + if strings.TrimSpace(channelID) == "" { + return "", fmt.Errorf("discord send: channel id not configured") + } + if len(nonce) > nonceLimit { + return "", fmt.Errorf("discord send: nonce is %d characters, the limit is %d", len(nonce), nonceLimit) } + payload := messagePayload(content) + if nonce != "" { + payload["nonce"] = nonce + payload["enforce_nonce"] = true + } + var msg struct { + ID string `json:"id"` + } + if err := b.do(http.MethodPost, fmt.Sprintf("%s/channels/%s/messages", discordAPIBase, channelID), payload, &msg); err != nil { + return "", err + } + if msg.ID == "" { + return "", fmt.Errorf("discord send: response carried no message id") + } + return msg.ID, nil +} - url := fmt.Sprintf("%s/channels/%s/messages", discordAPIBase, channelID) - req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data)) - if err != nil { - return err +// StartThreadFromMessage opens a public thread on an existing message and +// returns the thread ID. Threads are channels, so SendMessageTo works on it. +// The name is truncated to Discord's 100 characters. +func (b *Bot) StartThreadFromMessage(channelID, messageID, name string) (string, error) { + name = truncateRunes(strings.TrimSpace(name), threadNameLimit) + if strings.TrimSpace(channelID) == "" || strings.TrimSpace(messageID) == "" || name == "" { + return "", fmt.Errorf("discord thread: channel id, message id and name are required") + } + var th struct { + ID string `json:"id"` + GuildID string `json:"guild_id"` + } + url := fmt.Sprintf("%s/channels/%s/messages/%s/threads", discordAPIBase, channelID, messageID) + if err := b.do(http.MethodPost, url, map[string]any{"name": name}, &th); err != nil { + // 160004: a thread already exists on this message (an earlier call + // created it but its answer was lost). A thread started from a + // message shares that message's ID, so the retry is already done. + if strings.Contains(err.Error(), threadExistsCode) { + return messageID, nil + } + return "", err + } + if th.ID == "" { + return "", fmt.Errorf("discord thread: response carried no thread id") } + b.rememberGuild(th.ID, th.GuildID) + return th.ID, nil +} - req.Header.Set("Authorization", "Bot "+b.token) - req.Header.Set("Content-Type", "application/json") +// ChannelMessage is one message from a channel's history. +type ChannelMessage struct { + ID string + Content string + // Nonce is the nonce the sender attached, when Discord returned it. + Nonce string + // Self is true when this bot authored the message. + Self bool +} - resp, err := b.client.Do(req) +// RecentMessages returns up to limit (at most 50) of the newest messages in +// channelID. It exists for crash recovery (finding a card this bot already +// posted), not for scanning history. +func (b *Bot) RecentMessages(channelID string, limit int) ([]ChannelMessage, error) { + if strings.TrimSpace(channelID) == "" { + return nil, fmt.Errorf("discord history: channel id not configured") + } + if limit <= 0 || limit > historyLimit { + limit = historyLimit + } + self, err := b.selfID() if err != nil { - return fmt.Errorf("discord send: %w", err) + return nil, err } - defer func() { _ = resp.Body.Close() }() + var raw []struct { + ID string `json:"id"` + Content string `json:"content"` + Nonce any `json:"nonce"` + Author struct { + ID string `json:"id"` + } `json:"author"` + } + if err := b.do(http.MethodGet, fmt.Sprintf("%s/channels/%s/messages?limit=%d", discordAPIBase, channelID, limit), nil, &raw); err != nil { + return nil, err + } + out := make([]ChannelMessage, 0, len(raw)) + for _, m := range raw { + nonce, _ := m.Nonce.(string) + out = append(out, ChannelMessage{ + ID: m.ID, Content: m.Content, Nonce: nonce, + Self: m.Author.ID == self, + }) + } + return out, nil +} - if resp.StatusCode >= 400 { - body, _ := io.ReadAll(resp.Body) - return fmt.Errorf("discord API %d: %s", resp.StatusCode, string(body)) +// selfID resolves, once, the bot's own user ID. +func (b *Bot) selfID() (string, error) { + b.mu.RLock() + id := b.self + b.mu.RUnlock() + if id != "" { + return id, nil + } + var me struct { + ID string `json:"id"` + } + if err := b.do(http.MethodGet, discordAPIBase+"/users/@me", nil, &me); err != nil { + return "", fmt.Errorf("discord identity: %w", err) } + if me.ID == "" { + return "", fmt.Errorf("discord identity: response carried no user id") + } + b.mu.Lock() + b.self = me.ID + b.mu.Unlock() + return me.ID, nil +} - return nil +// ThreadURL is the link to a thread, built from the guild the thread lives +// in. The guild is learned when the thread is created or, failing that, +// looked up once. +func (b *Bot) ThreadURL(threadID string) (string, error) { + if strings.TrimSpace(threadID) == "" { + return "", fmt.Errorf("discord thread url: thread id is required") + } + b.mu.RLock() + guild := b.guilds[threadID] + b.mu.RUnlock() + if guild == "" { + var ch struct { + GuildID string `json:"guild_id"` + } + if err := b.do(http.MethodGet, fmt.Sprintf("%s/channels/%s", discordAPIBase, threadID), nil, &ch); err != nil { + return "", err + } + if ch.GuildID == "" { + return "", fmt.Errorf("discord thread url: thread %s has no guild", threadID) + } + guild = ch.GuildID + b.rememberGuild(threadID, guild) + } + return fmt.Sprintf("https://discord.com/channels/%s/%s", guild, threadID), nil +} + +func (b *Bot) rememberGuild(threadID, guildID string) { + if guildID == "" { + return + } + b.mu.Lock() + defer b.mu.Unlock() + if b.guilds == nil { + b.guilds = map[string]string{} + } + b.guilds[threadID] = guildID +} + +// do sends one Discord REST call with the bot token. A 429 waits out the +// advertised retry_after (each wait capped at maxRetryAfter) and retries at +// most maxRateLimitRetries times; any other status >= 400 is an error. out, +// when non-nil, receives the decoded JSON body. +func (b *Bot) do(method, url string, payload, out any) error { + var data []byte + if payload != nil { + var err error + if data, err = json.Marshal(payload); err != nil { + return err + } + } + for attempt := 0; ; attempt++ { + var body io.Reader + if data != nil { + body = bytes.NewReader(data) + } + req, err := http.NewRequest(method, url, body) + if err != nil { + return err + } + req.Header.Set("Authorization", "Bot "+b.token) + if data != nil { + req.Header.Set("Content-Type", "application/json") + } + resp, err := b.client.Do(req) + if err != nil { + return fmt.Errorf("discord send: %w", err) + } + raw, _ := io.ReadAll(io.LimitReader(resp.Body, maxResponseBytes)) + _ = resp.Body.Close() + + if resp.StatusCode == http.StatusTooManyRequests && attempt < maxRateLimitRetries { + rateLimitSleep(retryAfter(resp, raw)) + continue + } + if resp.StatusCode >= 400 { + return fmt.Errorf("discord API %d: %s", resp.StatusCode, string(raw)) + } + if out != nil && len(bytes.TrimSpace(raw)) > 0 { + if err := json.Unmarshal(raw, out); err != nil { + return fmt.Errorf("discord response: %w", err) + } + } + return nil + } +} + +// retryAfter reads Discord's 429 wait from the JSON body, then the +// Retry-After header, defaulting to one second, capped at maxRetryAfter. +func retryAfter(resp *http.Response, body []byte) time.Duration { + wait := time.Second + var rl struct { + RetryAfter float64 `json:"retry_after"` + } + if json.Unmarshal(body, &rl) == nil && rl.RetryAfter > 0 { + wait = time.Duration(rl.RetryAfter * float64(time.Second)) + } else if secs, err := strconv.ParseFloat(strings.TrimSpace(resp.Header.Get("Retry-After")), 64); err == nil && secs > 0 { + wait = time.Duration(secs * float64(time.Second)) + } + if wait > maxRetryAfter { + wait = maxRetryAfter + } + return wait } func (b *Bot) enqueue(content string) { diff --git a/src/pkg/discord/threads_test.go b/src/pkg/discord/threads_test.go new file mode 100644 index 0000000000..867e4a5d4b --- /dev/null +++ b/src/pkg/discord/threads_test.go @@ -0,0 +1,276 @@ +package discord + +import ( + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync/atomic" + "testing" + "time" +) + +// noRateLimitSleep stops the 429 tests from waiting out retry_after. +func noRateLimitSleep(t *testing.T) *[]time.Duration { + t.Helper() + var slept []time.Duration + prev := rateLimitSleep + rateLimitSleep = func(d time.Duration) { slept = append(slept, d) } + t.Cleanup(func() { rateLimitSleep = prev }) + return &slept +} + +func TestPostMessageNonce_SendsEnforcedNonceAndReturnsID(t *testing.T) { + var path string + var got map[string]any + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path = r.URL.Path + raw, _ := io.ReadAll(r.Body) + _ = json.Unmarshal(raw, &got) + _, _ = w.Write([]byte(`{"id":"m-1"}`)) + })) + defer ts.Close() + + id, err := newTestBot(ts, "status").PostMessageNonce("rfc", "RFC opened", "abc123") + if err != nil { + t.Fatalf("PostMessageNonce: %v", err) + } + if id != "m-1" { + t.Fatalf("id = %q, want m-1", id) + } + if !strings.HasSuffix(path, "/channels/rfc/messages") { + t.Fatalf("path = %q", path) + } + if got["nonce"] != "abc123" || got["enforce_nonce"] != true { + t.Fatalf("nonce payload = %v, want nonce abc123 with enforce_nonce true", got) + } + if mentions, _ := got["allowed_mentions"].(map[string]any); mentions == nil { + t.Fatalf("allowed_mentions missing: %v", got) + } +} + +func TestPostMessageNonce_Errors(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte(`{}`)) + })) + defer ts.Close() + b := newTestBot(ts, "status") + if _, err := b.PostMessageNonce(" ", "x", "n"); err == nil { + t.Fatal("empty channel must error") + } + if _, err := b.PostMessageNonce("rfc", "x", "n"); err == nil { + t.Fatal("a response without an id must error") + } + if _, err := b.PostMessageNonce("rfc", "x", strings.Repeat("n", 26)); err == nil { + t.Fatal("a nonce over 25 characters must error") + } +} + +func TestStartThreadFromMessage_PostsNameAndReturnsThreadID(t *testing.T) { + var path, method string + var got map[string]any + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path, method = r.URL.Path, r.Method + raw, _ := io.ReadAll(r.Body) + _ = json.Unmarshal(raw, &got) + _, _ = w.Write([]byte(`{"id":"t-9","guild_id":"g-1"}`)) + })) + defer ts.Close() + + long := strings.Repeat("é", 150) + id, err := newTestBot(ts, "status").StartThreadFromMessage("rfc", "m-1", long) + if err != nil { + t.Fatalf("StartThreadFromMessage: %v", err) + } + if id != "t-9" { + t.Fatalf("id = %q, want t-9", id) + } + if method != http.MethodPost || !strings.HasSuffix(path, "/channels/rfc/messages/m-1/threads") { + t.Fatalf("%s %s", method, path) + } + if name, _ := got["name"].(string); len([]rune(name)) != threadNameLimit { + t.Fatalf("name has %d runes, want %d", len([]rune(name)), threadNameLimit) + } +} + +func TestStartThreadFromMessage_Errors(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusForbidden) + _, _ = w.Write([]byte(`{"message":"Missing Permissions"}`)) + })) + defer ts.Close() + b := newTestBot(ts, "status") + if _, err := b.StartThreadFromMessage("", "m", "n"); err == nil { + t.Fatal("empty channel must error") + } + if _, err := b.StartThreadFromMessage("c", "", "n"); err == nil { + t.Fatal("empty message must error") + } + if _, err := b.StartThreadFromMessage("c", "m", " "); err == nil { + t.Fatal("empty name must error") + } + if _, err := b.StartThreadFromMessage("c", "m", "n"); err == nil || !strings.Contains(err.Error(), "403") { + t.Fatalf("err = %v, want the 403", err) + } +} + +func TestRecentMessages_BoundsLimitAndMarksOwnMessages(t *testing.T) { + var query string + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case strings.HasSuffix(r.URL.Path, "/users/@me"): + _, _ = w.Write([]byte(`{"id":"bot-1"}`)) + case strings.HasSuffix(r.URL.Path, "/channels/rfc/messages"): + query = r.URL.RawQuery + _, _ = w.Write([]byte(`[{"id":"2","content":"mine","nonce":"n-2","author":{"id":"bot-1","bot":true}},{"id":"1","content":"theirs","nonce":5,"author":{"id":"other","bot":true}}]`)) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + defer ts.Close() + + msgs, err := newTestBot(ts, "status").RecentMessages("rfc", 500) + if err != nil { + t.Fatalf("RecentMessages: %v", err) + } + if query != "limit=50" { + t.Fatalf("query = %q, want the limit capped at 50", query) + } + if len(msgs) != 2 { + t.Fatalf("got %d messages, want 2", len(msgs)) + } + if !msgs[0].Self || msgs[0].Nonce != "n-2" || msgs[0].Content != "mine" { + t.Fatalf("own message = %+v", msgs[0]) + } + if msgs[1].Self { + t.Fatalf("another bot's message must not count as ours: %+v", msgs[1]) + } +} + +func TestRecentMessages_SelfLookupFailure(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusUnauthorized) + })) + defer ts.Close() + if _, err := newTestBot(ts, "status").RecentMessages("rfc", 10); err == nil { + t.Fatal("an unresolved bot identity must error rather than adopt foreign messages") + } + if _, err := newTestBot(ts, "status").RecentMessages(" ", 10); err == nil { + t.Fatal("empty channel must error") + } +} + +func TestThreadURL_UsesGuildOfTheThreadAndCaches(t *testing.T) { + var calls atomic.Int32 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls.Add(1) + _, _ = w.Write([]byte(`{"id":"t-9","guild_id":"g-1"}`)) + })) + defer ts.Close() + b := newTestBot(ts, "status") + for i := 0; i < 2; i++ { + u, err := b.ThreadURL("t-9") + if err != nil { + t.Fatalf("ThreadURL: %v", err) + } + if u != "https://discord.com/channels/g-1/t-9" { + t.Fatalf("url = %q", u) + } + } + if calls.Load() != 1 { + t.Fatalf("%d channel lookups, want the guild cached after the first", calls.Load()) + } +} + +func TestThreadURL_NoGuild(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte(`{"id":"t-9"}`)) + })) + defer ts.Close() + if _, err := newTestBot(ts, "status").ThreadURL("t-9"); err == nil { + t.Fatal("a thread without a guild id has no URL") + } + if _, err := newTestBot(ts, "status").ThreadURL(""); err == nil { + t.Fatal("empty thread id must error") + } +} + +func TestRateLimit_RetriesAfterRetryAfterThenSucceeds(t *testing.T) { + slept := noRateLimitSleep(t) + var calls atomic.Int32 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if calls.Add(1) == 1 { + w.WriteHeader(http.StatusTooManyRequests) + _, _ = w.Write([]byte(`{"retry_after":1.5,"global":false}`)) + return + } + _, _ = w.Write([]byte(`{"id":"m-2"}`)) + })) + defer ts.Close() + + id, err := newTestBot(ts, "status").PostMessageNonce("rfc", "x", "n") + if err != nil || id != "m-2" { + t.Fatalf("id=%q err=%v, want m-2 after one retry", id, err) + } + if calls.Load() != 2 { + t.Fatalf("%d requests, want 2", calls.Load()) + } + if len(*slept) != 1 || (*slept)[0] != 1500*time.Millisecond { + t.Fatalf("slept %v, want one 1.5s wait", *slept) + } +} + +func TestRateLimit_GivesUpAfterBoundedRetries(t *testing.T) { + slept := noRateLimitSleep(t) + var calls atomic.Int32 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls.Add(1) + w.Header().Set("Retry-After", "3600") + w.WriteHeader(http.StatusTooManyRequests) + })) + defer ts.Close() + + if _, err := newTestBot(ts, "status").StartThreadFromMessage("c", "m", "n"); err == nil || !strings.Contains(err.Error(), "429") { + t.Fatalf("err = %v, want the 429 surfaced", err) + } + if want := int32(maxRateLimitRetries + 1); calls.Load() != want { + t.Fatalf("%d requests, want %d", calls.Load(), want) + } + for _, d := range *slept { + if d > maxRetryAfter { + t.Fatalf("waited %v, want each wait capped at %v", d, maxRetryAfter) + } + } +} + +func TestSendMessageTo_RetriesOn429(t *testing.T) { + noRateLimitSleep(t) + var calls atomic.Int32 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if calls.Add(1) == 1 { + w.WriteHeader(http.StatusTooManyRequests) + return + } + w.WriteHeader(http.StatusOK) + })) + defer ts.Close() + if err := newTestBot(ts, "status").SendMessageTo("rfc", "x"); err != nil { + t.Fatalf("SendMessageTo: %v", err) + } + if calls.Load() != 2 { + t.Fatalf("%d requests, want 2", calls.Load()) + } +} + +func TestStartThreadFromMessage_ExistingThreadSharesTheMessageID(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"code":160004,"message":"A thread has already been created for this message"}`)) + })) + defer ts.Close() + id, err := newTestBot(ts, "status").StartThreadFromMessage("rfc", "m-1", "#1 x") + if err != nil || id != "m-1" { + t.Fatalf("id=%q err=%v, want the message id as the existing thread", id, err) + } +} diff --git a/src/pkg/rfcnotify/detector.go b/src/pkg/rfcnotify/detector.go index c48321e240..63ed1757ae 100644 --- a/src/pkg/rfcnotify/detector.go +++ b/src/pkg/rfcnotify/detector.go @@ -13,8 +13,10 @@ import ( ) const ( - // stateVersion guards the on-disk shape. - stateVersion = 1 + // stateVersion guards the on-disk shape. Version 2 adds the per-RFC + // Discord mapping (issueRecord.MessageID and friends); a version 1 file + // loads unchanged because every new field is optional. + stateVersion = 2 // closedRetention is how long a closed issue's record is kept so a // late re-read of the same close cannot post it twice. closedRetention = 30 * 24 * time.Hour @@ -31,6 +33,45 @@ type issueRecord struct { LastSeen time.Time `json:"last_seen"` // PR marks an RFC document pull request. PR bool `json:"pr,omitempty"` + + // The fields below are the durable Discord mapping (threads mode). An + // RFC announced before threads existed carries none of them and is never + // back-filled with a thread. + + // AnnounceState is "" (no announcement owned by this hive), StatePending + // (intent recorded, card not confirmed), StatePosted (card confirmed, no + // thread) or StateThreaded. + AnnounceState string `json:"announce_state,omitempty"` + // Nonce is the deterministic Discord nonce of the opened card. + Nonce string `json:"nonce,omitempty"` + // MessageID is the Discord message of the opened card. + MessageID string `json:"message_id,omitempty"` + // ThreadID is the Discord thread started on that message. + ThreadID string `json:"thread_id,omitempty"` + // OpenText and ThreadName are what a retry re-posts and re-creates with. + OpenText string `json:"open_text,omitempty"` + ThreadName string `json:"thread_name,omitempty"` + // BacklinkPosted is set once the opt-in GitHub comment went out. + BacklinkPosted bool `json:"backlink_posted,omitempty"` + // PostTries, ThreadTries and BacklinkTries bound the retries of each step. + PostTries int `json:"post_tries,omitempty"` + ThreadTries int `json:"thread_tries,omitempty"` + BacklinkTries int `json:"backlink_tries,omitempty"` +} + +// Values of issueRecord.AnnounceState. +const ( + StatePending = "pending" + StatePosted = "posted" + StateThreaded = "threaded" +) + +// inherit carries the Discord mapping of the previous record over to a +// record rebuilt from a fresh snapshot. +func (r *issueRecord) inherit(prev *issueRecord) { + r.AnnounceState, r.Nonce, r.MessageID, r.ThreadID = prev.AnnounceState, prev.Nonce, prev.MessageID, prev.ThreadID + r.OpenText, r.ThreadName, r.BacklinkPosted = prev.OpenText, prev.ThreadName, prev.BacklinkPosted + r.PostTries, r.ThreadTries, r.BacklinkTries = prev.PostTries, prev.ThreadTries, prev.BacklinkTries } // State is the persisted detector memory. @@ -72,6 +113,9 @@ func Load(path string) (*Detector, error) { if err := json.Unmarshal(data, &st); err != nil { return nil, fmt.Errorf("parse rfc notify state: %w", err) } + if st.Version > stateVersion { + return nil, fmt.Errorf("rfc notify state %s is version %d, newer than the supported %d", path, st.Version, stateVersion) + } if st.Issues == nil { st.Issues = map[string]*issueRecord{} } @@ -81,11 +125,14 @@ func Load(path string) (*Detector, error) { } // Save writes the state to path (0600, directory created as needed). -func (d *Detector) Save(path string) error { +func (d *Detector) Save(path string) error { return writeState(path, d.state) } + +// writeState persists st atomically. +func writeState(path string, st State) error { if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { return fmt.Errorf("create rfc notify state dir: %w", err) } - data, err := json.MarshalIndent(d.state, "", " ") + data, err := json.MarshalIndent(st, "", " ") if err != nil { return fmt.Errorf("marshal rfc notify state: %w", err) } @@ -147,6 +194,9 @@ func (d *Detector) Observe(issues []Issue, now time.Time) []Event { key := is.Key() prev, known := d.state.Issues[key] cur := recordOf(is, now) + if known { + cur.inherit(prev) + } switch { case is.IsPR: if ev, ok := d.observePR(is, prev, known); ok { @@ -249,16 +299,34 @@ func (d *Detector) prune(now time.Time) { } // clone copies the state so an Observe can be partially undone. -func (d *Detector) clone() State { - out := d.state - out.Issues = make(map[string]*issueRecord, len(d.state.Issues)) - for k, v := range d.state.Issues { +func (d *Detector) clone() State { return cloneState(d.state) } + +func cloneState(st State) State { + out := st + out.Issues = make(map[string]*issueRecord, len(st.Issues)) + for k, v := range st.Issues { rec := *v out.Issues[k] = &rec } return out } +// checkpoint persists base with key's current record folded in. base is the +// pre-poll state advanced only by what has been delivered so far (same +// watermark as before the poll), so a crash mid-batch re-observes every +// event not yet delivered while keeping the write-ahead intent and the +// mapping of the ones that were. +func (d *Detector) checkpoint(path string, base *State, key string) error { + if path == "" { + return nil + } + if rec, ok := d.state.Issues[key]; ok { + cp := *rec + base.Issues[key] = &cp + } + return writeState(path, *base) +} + // rollback restores the records of the issues behind unposted events to // what they were in before, and moves the watermark back so the next poll // lists them again. Records of issues whose events were posted are kept. diff --git a/src/pkg/rfcnotify/sweeper.go b/src/pkg/rfcnotify/sweeper.go index 4c8d1b5e63..1417bbe88e 100644 --- a/src/pkg/rfcnotify/sweeper.go +++ b/src/pkg/rfcnotify/sweeper.go @@ -63,8 +63,9 @@ type Options struct { // Announcer marks this hive as the designated announcer for the // channel. Rich mode posts nothing without it; line mode ignores it. Announcer bool - // Backlink opts into a GitHub comment linking the RFC thread; carried - // for a later change and unused today. + // Backlink opts into one GitHub comment linking the RFC's Discord thread, + // posted only after the thread exists and at most once per RFC. It needs + // a Source that implements Commenter. Backlink bool // IncludePrivate authorizes posting issues whose Private is true. IncludePrivate bool @@ -94,6 +95,10 @@ type Result struct { // Unresolved counts events whose repository visibility could not be // determined; they stay unannounced and are retried on the next poll. Unresolved int + // Threads counts Discord threads started, Reconciled counts cards + // adopted from channel history after a crash, and Backlinks counts GitHub + // backlink comments posted (threads mode). + Threads, Reconciled, Backlinks int } // Sweeper owns the detector and its cadence. @@ -103,6 +108,8 @@ type Sweeper struct { lastRun time.Time // warnedAnnouncer makes the rich-mode announcer warning log once. warnedAnnouncer bool + // threads is the Discord surface of threads mode; see SetThreads. + threads Threads } // NewSweeper loads the state at opts.StatePath (an unreadable file logs @@ -132,6 +139,16 @@ func NewSweeper(opts Options) *Sweeper { return &Sweeper{opts: opts, det: det} } +// SetThreads attaches the Discord surface threads mode posts through. +// Without it, Options.Threads falls back to the one-line poster. +func (s *Sweeper) SetThreads(t Threads) { s.threads = t } + +// threadsActive reports whether this run owns Discord threads: the option, +// the designated-announcer gate and a Discord surface are all present. +func (s *Sweeper) threadsActive() bool { + return s.opts.Threads && s.opts.Announcer && s.threads != nil +} + // Detector exposes the sweeper's detector for inspection. func (s *Sweeper) Detector() *Detector { return s.det } @@ -164,6 +181,12 @@ func (s *Sweeper) Run(ctx context.Context, src Source, post Poster) (Result, err return res, s.save() } + if s.threadsActive() { + // Settle cards and threads a crash or a failed call left half-done + // before diffing, so they never wait on a new transition. + s.reconcile(&res) + } + since := s.det.Watermark().Add(-s.opts.Slack) issues, err := src.ListUpdatedSince(ctx, since) if err != nil { @@ -186,6 +209,11 @@ func (s *Sweeper) Run(ctx context.Context, src Source, post Poster) (Result, err res.Withheld = len(events) return res, s.save() } + var tr *threadRun + if s.threadsActive() { + base := cloneState(before) + tr = &threadRun{s: s, res: &res, base: &base} + } for i, ev := range events { if ev.Issue.VisibilityUnknown && !s.opts.IncludePrivate { res.Unresolved++ @@ -199,7 +227,20 @@ func (s *Sweeper) Run(ctx context.Context, src Source, post Poster) (Result, err continue } msg := Format(ev, s.opts.IncludeRepo) - if err := post(msg); err != nil { + if tr != nil { + if keep, err := tr.deliver(ev, msg); err != nil { + res.Errors++ + s.opts.Logger.Warn("rfc notify: post failed", "issue", ev.Issue.Key(), "kind", ev.Kind, "error", err) + // A card whose write-ahead intent is saved is retried by + // reconciliation, so only the events after it are replayed. + if keep { + s.det.rollback(before, events[i+1:]) + } else { + s.det.rollback(before, events[i:]) + } + break + } + } else if err := post(msg); err != nil { res.Errors++ s.opts.Logger.Warn("rfc notify: post failed", "issue", ev.Issue.Key(), "kind", ev.Kind, "error", err) // Keep what was announced, forget what was not: replay the @@ -210,6 +251,9 @@ func (s *Sweeper) Run(ctx context.Context, src Source, post Poster) (Result, err res.Posted = append(res.Posted, msg) res.Events = append(res.Events, ev) } + if tr != nil { + s.backlinks(ctx, src, &res) + } return res, s.save() } diff --git a/src/pkg/rfcnotify/thread_test.go b/src/pkg/rfcnotify/thread_test.go new file mode 100644 index 0000000000..7c86d4da51 --- /dev/null +++ b/src/pkg/rfcnotify/thread_test.go @@ -0,0 +1,581 @@ +package rfcnotify + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +// fakeThreads is a scripted Discord: a message log plus failure switches. +type fakeThreads struct { + posts []fakePost + postErrs []error // consumed one per Post call; nil entries succeed + nextMsg int + startErr error + started map[string]string // message id -> thread id + startNames []string + sends []fakeSend + sendErr error + echoes []string + recent []Message + recentErr error + recentCalls int + recentLimit int + urlErr error +} + +type fakePost struct{ content, nonce string } +type fakeSend struct{ thread, content string } + +func (f *fakeThreads) Post(content, nonce string) (string, error) { + f.posts = append(f.posts, fakePost{content, nonce}) + if len(f.postErrs) > 0 { + err := f.postErrs[0] + f.postErrs = f.postErrs[1:] + if err != nil { + return "", err + } + } + f.nextMsg++ + return fmt.Sprintf("msg-%d", f.nextMsg), nil +} + +func (f *fakeThreads) StartThread(messageID, name string) (string, error) { + f.startNames = append(f.startNames, name) + if f.startErr != nil { + return "", f.startErr + } + if f.started == nil { + f.started = map[string]string{} + } + id := "thr-" + messageID + f.started[messageID] = id + return id, nil +} + +func (f *fakeThreads) Send(threadID, content string) error { + f.sends = append(f.sends, fakeSend{threadID, content}) + return f.sendErr +} + +func (f *fakeThreads) Echo(content string) error { + f.echoes = append(f.echoes, content) + return nil +} + +func (f *fakeThreads) Recent(limit int) ([]Message, error) { + f.recentCalls++ + f.recentLimit = limit + return f.recent, f.recentErr +} + +func (f *fakeThreads) ThreadURL(threadID string) (string, error) { + if f.urlErr != nil { + return "", f.urlErr + } + return "https://discord.com/channels/g/" + threadID, nil +} + +func (f *fakeThreads) calls() int { + return len(f.posts) + len(f.startNames) + len(f.sends) + len(f.echoes) + f.recentCalls +} + +// commentSource is a fakeSource that can also comment on GitHub. +type commentSource struct { + *fakeSource + comments []string + errs []error +} + +func (c *commentSource) Comment(_ context.Context, repo string, number int, body string) error { + c.comments = append(c.comments, fmt.Sprintf("%s#%d %s", repo, number, body)) + if len(c.errs) > 0 { + err := c.errs[0] + c.errs = c.errs[1:] + return err + } + return nil +} + +// threadRig is a threads-mode announcer over a state file in a temp dir. +type threadRig struct { + t *testing.T + path string + now time.Time + src *commentSource + th *fakeThreads + sw *Sweeper + opts Options +} + +func newThreadRig(t *testing.T, opts Options) *threadRig { + t.Helper() + r := &threadRig{t: t, path: filepath.Join(t.TempDir(), "rfc-notify.json"), now: t0, th: &fakeThreads{}} + r.src = &commentSource{fakeSource: &fakeSource{open: []Issue{rfc(1, "open"), rfc(2, "open")}}} + opts.StatePath, opts.Logger, opts.Threads = r.path, quiet, true + opts.Now = func() time.Time { return r.now } + r.opts = opts + r.sw = r.newSweeper() + r.run() // seed + return r +} + +func (r *threadRig) newSweeper() *Sweeper { + sw := NewSweeper(r.opts) + sw.SetThreads(r.th) + return sw +} + +// restart simulates a process restart over the same state file. +func (r *threadRig) restart() { r.sw = r.newSweeper() } + +func (r *threadRig) run() Result { + r.t.Helper() + r.now = r.now.Add(DefaultInterval) + res, err := r.sw.Run(context.Background(), r.src, func(string) error { + r.t.Fatal("line poster used in threads mode") + return nil + }) + if err != nil { + r.t.Fatalf("Run: %v", err) + } + return res +} + +func (r *threadRig) record(key string) issueRecord { + r.t.Helper() + d, err := Load(r.path) + if err != nil { + r.t.Fatal(err) + } + rec := d.state.Issues[key] + if rec == nil { + r.t.Fatalf("no record for %s", key) + } + return *rec +} + +// openRFC makes RFC n appear as newly opened on the next run. +func (r *threadRig) openRFC(n int) Issue { + is := rfc(n, "open") + is.CreatedAt = r.now.Add(time.Second) + r.src.updated = []Issue{is} + return is +} + +const key3 = "hivecommons/hive#3" + +func TestThreads_OpenedPostsOneCardAndStartsOneThread(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + is := r.openRFC(3) + res := r.run() + if res.Errors != 0 || len(r.th.posts) != 1 { + t.Fatalf("res=%+v posts=%v", res, r.th.posts) + } + card := r.th.posts[0] + if card.content != Format(Event{Kind: KindOpened, Issue: is}, false) { + t.Fatalf("card = %q", card.content) + } + if card.nonce == "" || len(card.nonce) > 25 { + t.Fatalf("nonce %q must be set and at most 25 characters", card.nonce) + } + if len(r.th.startNames) != 1 || !strings.HasPrefix(r.th.startNames[0], "#3 RFC: thing") { + t.Fatalf("thread names = %v", r.th.startNames) + } + rec := r.record(key3) + if rec.AnnounceState != StateThreaded || rec.MessageID != "msg-1" || rec.ThreadID != "thr-msg-1" || rec.Nonce != card.nonce { + t.Fatalf("record = %+v", rec) + } + if res.Threads != 1 { + t.Fatalf("Threads = %d, want 1", res.Threads) + } +} + +func TestThreads_NonceIsDeterministicPerRFC(t *testing.T) { + if a, b := announceNonce(key3), announceNonce(key3); a != b || len(a) > 25 { + t.Fatalf("nonce %q / %q", a, b) + } + if announceNonce(key3) == announceNonce("hivecommons/hive#4") { + t.Fatal("different RFCs must not share a nonce") + } +} + +func TestThreads_ThreadNameTruncatedTo100(t *testing.T) { + name := threadName(Issue{Number: 7, Title: strings.Repeat("é", 300)}) + if len([]rune(name)) != 100 || !strings.HasPrefix(name, "#7 ") { + t.Fatalf("name = %q (%d runes)", name, len([]rune(name))) + } +} + +func TestThreads_ClosedPostsIntoThreadAndEchoesOnce(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.openRFC(3) + r.run() + closed := withReason(rfc(3, "closed"), "completed") + r.src.updated = []Issue{closed} + res := r.run() + if len(r.th.posts) != 1 { + t.Fatalf("a close must not post a second card: %v", r.th.posts) + } + want := Format(Event{Kind: KindClosed, Issue: closed}, false) + if len(r.th.sends) != 1 || r.th.sends[0] != (fakeSend{"thr-msg-1", want}) { + t.Fatalf("thread sends = %v", r.th.sends) + } + if len(r.th.echoes) != 1 || r.th.echoes[0] != want { + t.Fatalf("channel echoes = %v", r.th.echoes) + } + if len(res.Posted) != 1 { + t.Fatalf("Posted = %v", res.Posted) + } +} + +func TestThreads_ReadyPostsIntoThreadAndEchoes(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + held := rfc(3, "open", "hold") + held.CreatedAt = r.now.Add(time.Second) + r.src.updated = []Issue{held} + r.run() + lifted := rfc(3, "open") + r.src.updated = []Issue{lifted} + r.run() + want := Format(Event{Kind: KindReady, Issue: lifted}, false) + if len(r.th.sends) != 1 || r.th.sends[0] != (fakeSend{"thr-msg-1", want}) { + t.Fatalf("sends = %v", r.th.sends) + } + if len(r.th.echoes) != 1 || r.th.echoes[0] != want { + t.Fatalf("echoes = %v", r.th.echoes) + } +} + +func TestThreads_LegacyRFCWithoutThreadGetsAChannelLineNotABackfilledThread(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.src.updated = []Issue{withReason(rfc(1, "closed"), "completed")} // seeded before threads existed + r.run() + if len(r.th.startNames) != 0 || len(r.th.posts) != 0 { + t.Fatalf("legacy RFC must not get a card or thread: posts=%v threads=%v", r.th.posts, r.th.startNames) + } + if len(r.th.echoes) != 1 || !strings.HasPrefix(r.th.echoes[0], "RFC closed") { + t.Fatalf("echoes = %v", r.th.echoes) + } +} + +func TestThreads_WriteAheadIntentSurvivesAFailedPost(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.th.postErrs = []error{errors.New("discord down")} + is := r.openRFC(3) + res := r.run() + if res.Errors != 1 || len(res.Posted) != 0 { + t.Fatalf("res = %+v", res) + } + rec := r.record(key3) + if rec.AnnounceState != StatePending || rec.Nonce != announceNonce(key3) || rec.MessageID != "" { + t.Fatalf("intent not persisted: %+v", rec) + } + if rec.OpenText != Format(Event{Kind: KindOpened, Issue: is}, false) { + t.Fatalf("intent lost the card text: %q", rec.OpenText) + } +} + +func TestThreads_PendingRetryRepostsWithSameNonceAndThreads(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.th.postErrs = []error{errors.New("discord down")} + r.openRFC(3) + r.run() + r.src.updated = nil + res := r.run() // retry: lookup finds nothing, re-post once + if len(r.th.posts) != 2 || r.th.posts[0].nonce != r.th.posts[1].nonce || r.th.posts[0].content != r.th.posts[1].content { + t.Fatalf("retry must re-post the same card and nonce: %v", r.th.posts) + } + if r.th.recentCalls != 1 || r.th.recentLimit != 50 { + t.Fatalf("lookup calls=%d limit=%d, want one lookup of 50", r.th.recentCalls, r.th.recentLimit) + } + if rec := r.record(key3); rec.AnnounceState != StateThreaded { + t.Fatalf("record = %+v", rec) + } + if res.Threads != 1 { + t.Fatalf("Threads = %d", res.Threads) + } +} + +func TestThreads_RestartAfterPostBeforeThreadAttachesExistingMessage(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.th.startErr = errors.New("missing permissions") + r.openRFC(3) + res := r.run() + if res.Errors != 1 || r.record(key3).AnnounceState != StatePosted { + t.Fatalf("res=%+v rec=%+v", res, r.record(key3)) + } + r.th.startErr = nil + r.src.updated = nil + r.restart() + r.run() + if len(r.th.posts) != 1 { + t.Fatalf("restart posted a second card: %v", r.th.posts) + } + if rec := r.record(key3); rec.AnnounceState != StateThreaded || rec.ThreadID != "thr-msg-1" { + t.Fatalf("record = %+v", rec) + } + if r.th.recentCalls != 0 { + t.Fatalf("a known message needs no history lookup, got %d", r.th.recentCalls) + } +} + +func TestThreads_CrashBeforeMessageIDStoredAdoptsMessageFromHistory(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.th.postErrs = []error{errors.New("timeout after Discord accepted it")} + is := r.openRFC(3) + r.run() + card := Format(Event{Kind: KindOpened, Issue: is}, false) + r.th.recent = []Message{ + {ID: "other", Content: "unrelated", Self: true}, + {ID: "foreign", Content: card, Self: false}, // someone else's copy is not ours + {ID: "m-adopted", Content: card, Self: true}, + } + r.src.updated = nil + r.restart() + r.run() + if len(r.th.posts) != 1 { + t.Fatalf("an existing card must not be posted again: %v", r.th.posts) + } + if rec := r.record(key3); rec.MessageID != "m-adopted" || rec.ThreadID != "thr-m-adopted" || rec.AnnounceState != StateThreaded { + t.Fatalf("record = %+v", rec) + } +} + +func TestThreads_HistoryMatchOnNonce(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.th.postErrs = []error{errors.New("timeout")} + r.openRFC(3) + r.run() + r.th.recent = []Message{{ID: "m-n", Content: "edited text", Nonce: announceNonce(key3), Self: true}} + r.src.updated = nil + r.run() + if len(r.th.posts) != 1 || r.record(key3).MessageID != "m-n" { + t.Fatalf("posts=%v rec=%+v", r.th.posts, r.record(key3)) + } +} + +func TestThreads_HistoryLookupFailureNeverReposts(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.th.postErrs = []error{errors.New("timeout")} + r.openRFC(3) + r.run() + r.th.recentErr = errors.New("history unavailable") + r.src.updated = nil + res := r.run() + if len(r.th.posts) != 1 || res.Errors != 1 { + t.Fatalf("an unresolved lookup must not duplicate the card: posts=%v res=%+v", r.th.posts, res) + } +} + +func TestThreads_PendingRepostIsBounded(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.th.postErrs = []error{errors.New("e"), errors.New("e"), errors.New("e"), errors.New("e"), errors.New("e"), errors.New("e")} + r.openRFC(3) + for i := 0; i < 6; i++ { + r.run() + r.src.updated = nil + } + if len(r.th.posts) != maxPostTries { + t.Fatalf("%d posts, want the retries bounded at %d", len(r.th.posts), maxPostTries) + } +} + +func TestThreads_ReconcileNeverScansWithoutAnIntent(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.run() + r.run() + if r.th.calls() != 0 { + t.Fatalf("seeded records must never be reconciled or backfilled: %d Discord calls", r.th.calls()) + } +} + +func TestThreads_ThreadStartRetryIsBounded(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.th.startErr = errors.New("no") + r.openRFC(3) + for i := 0; i < 6; i++ { + r.run() + r.src.updated = nil + } + if len(r.th.startNames) != maxThreadTries { + t.Fatalf("%d thread attempts, want %d", len(r.th.startNames), maxThreadTries) + } +} + +func TestThreads_NonAnnouncerNeverTouchesDiscord(t *testing.T) { + r := newThreadRig(t, Options{Announcer: false}) + r.openRFC(3) + res := r.run() + r.src.updated = nil + r.restart() + r.run() + if r.th.calls() != 0 || res.Withheld != 1 { + t.Fatalf("calls=%d res=%+v", r.th.calls(), res) + } + if len(r.src.comments) != 0 { + t.Fatalf("comments = %v", r.src.comments) + } +} + +func TestThreads_FailedThreadPostRollsBackSoItRetries(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.openRFC(3) + r.run() + closed := withReason(rfc(3, "closed"), "completed") + r.src.updated = []Issue{closed} + r.th.sendErr = errors.New("rate limited") + res := r.run() + if res.Errors != 1 || len(res.Posted) != 0 { + t.Fatalf("res = %+v", res) + } + r.th.sendErr = nil + res = r.run() + if len(res.Posted) != 1 || len(r.th.echoes) != 1 { + t.Fatalf("the close must be retried exactly once: res=%+v echoes=%v", res, r.th.echoes) + } +} + +func TestThreads_DocAcceptedPostsIntoLinkedRFCThread(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.openRFC(3) + r.run() + pr := Issue{ + Repo: "hivecommons/hive", Number: 40, Title: "📝 proposal: thing", URL: "https://github.com/hivecommons/hive/pull/40", + State: "closed", IsPR: true, Body: "Refs #3", CreatedAt: r.now.Add(-time.Hour), MergedAt: r.now.Add(time.Second), + } + r.src.updated = []Issue{pr} + r.run() + if len(r.th.sends) != 1 || r.th.sends[0].thread != "thr-msg-1" || !strings.HasPrefix(r.th.sends[0].content, "RFC doc accepted (RFC #3)") { + t.Fatalf("sends = %v", r.th.sends) + } +} + +func TestThreads_BacklinkPostedOnceAfterThreadAndNotAcrossRestarts(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true, Backlink: true}) + r.openRFC(3) + r.run() + want := "hivecommons/hive#3 Discussion thread: https://discord.com/channels/g/thr-msg-1" + if len(r.src.comments) != 1 || r.src.comments[0] != want { + t.Fatalf("comments = %v, want [%s]", r.src.comments, want) + } + if !r.record(key3).BacklinkPosted { + t.Fatal("BacklinkPosted not persisted") + } + r.src.updated = nil + r.run() + r.restart() + r.run() + if len(r.src.comments) != 1 { + t.Fatalf("backlink repeated: %v", r.src.comments) + } +} + +func TestThreads_BacklinkOffByDefault(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true}) + r.openRFC(3) + r.run() + if len(r.src.comments) != 0 || r.record(key3).BacklinkPosted { + t.Fatalf("comments = %v", r.src.comments) + } +} + +func TestThreads_BacklinkWaitsForTheThread(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true, Backlink: true}) + r.th.startErr = errors.New("no thread") + r.openRFC(3) + r.run() + if len(r.src.comments) != 0 { + t.Fatalf("backlink posted before the thread existed: %v", r.src.comments) + } + r.th.startErr = nil + r.src.updated = nil + r.run() + if len(r.src.comments) != 1 { + t.Fatalf("comments = %v, want one once the thread exists", r.src.comments) + } +} + +func TestThreads_BacklinkFailureIsBoundedAndLeavesFlagUnset(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true, Backlink: true}) + r.src.errs = []error{errors.New("403"), errors.New("403"), errors.New("403"), errors.New("403"), errors.New("403")} + r.openRFC(3) + for i := 0; i < 5; i++ { + r.run() + r.src.updated = nil + } + if len(r.src.comments) != maxBacklinkTries { + t.Fatalf("%d attempts, want %d", len(r.src.comments), maxBacklinkTries) + } + if r.record(key3).BacklinkPosted { + t.Fatal("flag must stay unset on failure") + } +} + +func TestThreads_BacklinkNeedsACommenterSource(t *testing.T) { + r := newThreadRig(t, Options{Announcer: true, Backlink: true}) + r.openRFC(3) + r.now = r.now.Add(DefaultInterval) + if _, err := r.sw.Run(context.Background(), r.src.fakeSource, noopPost); err != nil { + t.Fatal(err) + } + if len(r.src.comments) != 0 { + t.Fatalf("comments = %v", r.src.comments) + } +} + +func noopPost(string) error { return nil } + +func TestThreads_V1StateLoadsUnchangedAndSavesAsV2(t *testing.T) { + path := filepath.Join(t.TempDir(), "rfc-notify.json") + v1 := `{"version":1,"seeded":true,"watermark":"2026-10-27T12:00:00Z","issues":{"hivecommons/hive#1":{"open":true,"held":true,"actionable":false,"ready_announced":true,"last_seen":"2026-10-27T12:00:00Z"}}}` + if err := os.WriteFile(path, []byte(v1), 0o600); err != nil { + t.Fatal(err) + } + d, err := Load(path) + if err != nil { + t.Fatalf("Load v1: %v", err) + } + rec := d.state.Issues["hivecommons/hive#1"] + if rec == nil || !rec.Open || !rec.Held || !rec.ReadyAnnounced || rec.AnnounceState != "" || rec.MessageID != "" || rec.ThreadID != "" { + t.Fatalf("v1 record = %+v", rec) + } + if !d.Seeded() || !d.Watermark().Equal(t0) { + t.Fatal("v1 seed/watermark lost") + } + if err := d.Save(path); err != nil { + t.Fatal(err) + } + data, _ := os.ReadFile(path) + if !strings.Contains(string(data), `"version": 2`) { + t.Fatalf("saved state is not v2:\n%s", data) + } +} + +func TestThreads_NewerStateVersionIsRefused(t *testing.T) { + path := filepath.Join(t.TempDir(), "rfc-notify.json") + if err := os.WriteFile(path, []byte(`{"version":3,"seeded":true,"issues":{}}`), 0o600); err != nil { + t.Fatal(err) + } + if _, err := Load(path); err == nil { + t.Fatal("a state written by a newer hive must not be silently downgraded") + } +} + +func TestThreads_ObserveKeepsTheMappingAcrossPolls(t *testing.T) { + d := NewDetector() + d.Seed([]Issue{rfc(1, "open")}, t0) + d.state.Issues["hivecommons/hive#1"].MessageID = "m" + d.state.Issues["hivecommons/hive#1"].ThreadID = "t" + d.state.Issues["hivecommons/hive#1"].AnnounceState = StateThreaded + d.Observe([]Issue{rfc(1, "open", "hold")}, t0.Add(time.Minute)) + rec := d.state.Issues["hivecommons/hive#1"] + if rec.MessageID != "m" || rec.ThreadID != "t" || rec.AnnounceState != StateThreaded { + t.Fatalf("mapping dropped by Observe: %+v", rec) + } +} diff --git a/src/pkg/rfcnotify/threads.go b/src/pkg/rfcnotify/threads.go new file mode 100644 index 0000000000..28dffdf70a --- /dev/null +++ b/src/pkg/rfcnotify/threads.go @@ -0,0 +1,316 @@ +package rfcnotify + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "fmt" + "sort" + "strconv" + "strings" +) + +const ( + // ReconcileLookback is how many of the newest channel messages one + // reconciliation reads. It never reads further back. + ReconcileLookback = 50 + // maxPostTries, maxThreadTries and maxBacklinkTries bound the attempts of + // each step across polls and restarts; after them the step is given up + // with a warning. + maxPostTries = 3 + maxThreadTries = 3 + maxBacklinkTries = 3 + // nonceLen is Discord's longest message nonce. + nonceLen = 25 + // threadNameLen is Discord's longest thread name, in characters. + threadNameLen = 100 +) + +// Message is one message from the announcement channel's history. +type Message struct { + ID string + Content string + // Nonce is the nonce the sender attached, when Discord returned it. + Nonce string + // Self is true when the announcing bot authored the message. + Self bool +} + +// Threads is the Discord surface of threads mode, bound to one channel. +type Threads interface { + // Post sends the opened card to the channel with an enforced nonce and + // returns its message ID. + Post(content, nonce string) (messageID string, err error) + // StartThread opens a thread on a card and returns the thread ID. + StartThread(messageID, name string) (threadID string, err error) + // Send posts into a thread. + Send(threadID, content string) error + // Echo posts one line to the channel itself. + Echo(content string) error + // Recent returns up to limit of the channel's newest messages. + Recent(limit int) ([]Message, error) + // ThreadURL is the link to a thread. + ThreadURL(threadID string) (string, error) +} + +// Commenter is an optional extension of Source that comments on a GitHub +// issue or pull request, for the opt-in backlink. +type Commenter interface { + Comment(ctx context.Context, repo string, number int, body string) error +} + +// announceNonce is the deterministic write-ahead nonce of an RFC's opened +// card: a hash of "repo#number" plus "opened", at most nonceLen characters. +func announceNonce(key string) string { + sum := sha256.Sum256([]byte(key + "opened")) + return hex.EncodeToString(sum[:])[:nonceLen] +} + +// threadName is "#N" plus the title, cut to Discord's thread-name limit. +func threadName(is Issue) string { + title := strings.TrimSpace(is.Title) + if d := strings.TrimSpace(is.DocTitle); d != "" { + title = d + } + r := []rune(fmt.Sprintf("#%d %s", is.Number, title)) + if len(r) > threadNameLen { + r = r[:threadNameLen] + } + return strings.TrimSpace(string(r)) +} + +// threadRun is the threads-mode half of one Run. base is the state to +// checkpoint into mid-batch (the pre-poll state plus what was delivered); +// nil means persist the whole state, which is right outside a diff. +type threadRun struct { + s *Sweeper + res *Result + base *State +} + +// persist saves the record of key; an error means the write-ahead guarantee +// does not hold and the caller must not go on to call Discord. +func (t *threadRun) persist(key string) error { + if t.s.opts.StatePath == "" { + return nil + } + if t.base == nil { + return t.s.save() + } + return t.s.det.checkpoint(t.s.opts.StatePath, t.base, key) +} + +// deliver announces one event. keep reports that the event's record, with +// its saved intent, must survive the failure described by err. +func (t *threadRun) deliver(ev Event, msg string) (keep bool, err error) { + key := ev.Issue.Key() + if ev.Kind == KindOpened { + rec := t.s.det.state.Issues[key] + if rec == nil { + return false, t.s.threads.Echo(msg) + } + if rec.AnnounceState == "" { + rec.Nonce, rec.AnnounceState = announceNonce(key), StatePending + rec.OpenText, rec.ThreadName = msg, threadName(ev.Issue) + if err := t.persist(key); err != nil { + return false, fmt.Errorf("save announcement intent: %w", err) + } + } + if rec.AnnounceState == StatePending { + if err := t.postCard(key, rec); err != nil { + return true, err + } + } + if rec.MessageID != "" && rec.ThreadID == "" { + if err := t.startThread(key, rec); err != nil { + t.res.Errors++ + t.s.opts.Logger.Warn("rfc notify: thread not started; retrying later", "issue", key, "error", err) + } + } + return false, t.persist(key) + } + + target := t.s.det.state.Issues[ev.Thread] + if target == nil || target.ThreadID == "" { + // Announced before threads existed, or the thread is not up yet: + // the plain channel line, never a back-filled thread. + if err := t.s.threads.Echo(msg); err != nil { + return false, err + } + return false, t.persist(key) + } + if err := t.s.threads.Send(target.ThreadID, msg); err != nil { + return false, err + } + if ev.Kind == KindClosed || ev.Kind == KindReady { + if err := t.s.threads.Echo(msg); err != nil { + t.res.Errors++ + t.s.opts.Logger.Warn("rfc notify: channel echo failed", "issue", key, "kind", ev.Kind, "error", err) + } + } + return false, t.persist(key) +} + +// postCard sends the opened card with the record's nonce. The attempt is +// counted and saved first so a crash cannot make the retries unbounded. +func (t *threadRun) postCard(key string, rec *issueRecord) error { + rec.PostTries++ + if err := t.persist(key); err != nil { + return fmt.Errorf("save post attempt: %w", err) + } + id, err := t.s.threads.Post(rec.OpenText, rec.Nonce) + if err != nil { + if rec.PostTries >= maxPostTries { + t.s.opts.Logger.Warn("rfc notify: giving up on the opened card", "issue", key, "tries", rec.PostTries) + } + return err + } + rec.MessageID, rec.AnnounceState = id, StatePosted + return t.persist(key) +} + +// startThread opens the RFC's thread on its confirmed card. +func (t *threadRun) startThread(key string, rec *issueRecord) error { + rec.ThreadTries++ + if err := t.persist(key); err != nil { + return fmt.Errorf("save thread attempt: %w", err) + } + id, err := t.s.threads.StartThread(rec.MessageID, rec.ThreadName) + if err != nil { + if rec.ThreadTries >= maxThreadTries { + t.s.opts.Logger.Warn("rfc notify: giving up on the thread", "issue", key, "tries", rec.ThreadTries) + } + return err + } + rec.ThreadID, rec.AnnounceState = id, StateThreaded + t.res.Threads++ + return t.persist(key) +} + +// reconcile settles records a crash or a failed call left without a card or +// a thread. It does one bounded history lookup, only when a record has no +// confirmed card, and only for records this hive started announcing (those +// carrying an AnnounceState), so it never backfills. A found card is +// adopted; a missing one is re-posted with the same nonce, within the retry +// bound; an unreadable history re-posts nothing. +func (s *Sweeper) reconcile(res *Result) { + var keys []string + for key, rec := range s.det.state.Issues { + if (rec.AnnounceState == StatePending || rec.AnnounceState == StatePosted) && rec.ThreadID == "" { + keys = append(keys, key) + } + } + if len(keys) == 0 { + return + } + sort.Strings(keys) + t := &threadRun{s: s, res: res} + var recent []Message + lookedUp, lookupOK := false, false + for _, key := range keys { + rec := s.det.state.Issues[key] + if rec.MessageID == "" { + if !lookedUp { + lookedUp = true + var err error + if recent, err = s.threads.Recent(ReconcileLookback); err != nil { + res.Errors++ + s.opts.Logger.Warn("rfc notify: history lookup failed; not re-posting", "error", err) + } else { + lookupOK = true + } + } + if !lookupOK { + continue + } + if m, ok := findCard(recent, rec); ok { + rec.MessageID, rec.AnnounceState = m.ID, StatePosted + res.Reconciled++ + s.opts.Logger.Info("rfc notify: adopted existing card", "issue", key, "message", m.ID) + if err := t.persist(key); err != nil { + s.opts.Logger.Warn("rfc notify: save failed", "issue", key, "error", err) + continue + } + } else if rec.PostTries < maxPostTries { + if err := t.postCard(key, rec); err != nil { + res.Errors++ + s.opts.Logger.Warn("rfc notify: re-post failed", "issue", key, "error", err) + continue + } + } else { + continue + } + } + if rec.MessageID != "" && rec.ThreadID == "" && rec.ThreadTries < maxThreadTries { + if err := t.startThread(key, rec); err != nil { + res.Errors++ + s.opts.Logger.Warn("rfc notify: thread not started; retrying later", "issue", key, "error", err) + } + } + } +} + +// findCard returns the bot's own message that is rec's opened card: same +// nonce, or exactly the card text (Discord does not always echo nonces in +// history). +func findCard(recent []Message, rec *issueRecord) (Message, bool) { + for _, m := range recent { + if !m.Self { + continue + } + if (rec.Nonce != "" && m.Nonce == rec.Nonce) || (rec.OpenText != "" && m.Content == rec.OpenText) { + return m, true + } + } + return Message{}, false +} + +// backlinks posts the opt-in GitHub comment for every threaded RFC that has +// not had one, bounded by maxBacklinkTries. The attempt is counted and saved +// before the comment goes out. +func (s *Sweeper) backlinks(ctx context.Context, src Source, res *Result) { + if !s.opts.Backlink { + return + } + cm, ok := src.(Commenter) + if !ok { + return + } + var keys []string + for key, rec := range s.det.state.Issues { + if rec.AnnounceState == StateThreaded && rec.ThreadID != "" && !rec.BacklinkPosted && rec.BacklinkTries < maxBacklinkTries { + keys = append(keys, key) + } + } + sort.Strings(keys) + for _, key := range keys { + rec := s.det.state.Issues[key] + repo, num, found := strings.Cut(key, "#") + n, err := strconv.Atoi(num) + if !found || err != nil { + continue + } + rec.BacklinkTries++ + if err := s.save(); err != nil { + s.opts.Logger.Warn("rfc notify: save failed; backlink not posted", "issue", key, "error", err) + rec.BacklinkTries-- + continue + } + url, err := s.threads.ThreadURL(rec.ThreadID) + if err == nil { + err = cm.Comment(ctx, repo, n, "Discussion thread: "+url) + } + if err != nil { + res.Errors++ + s.opts.Logger.Warn("rfc notify: backlink comment failed", "issue", key, "tries", rec.BacklinkTries, "error", err) + continue + } + rec.BacklinkPosted = true + res.Backlinks++ + // Save at once so a crash cannot repeat the comment; the flag stays + // set even if this write fails. + if err := s.save(); err != nil { + s.opts.Logger.Warn("rfc notify: save failed after backlink", "issue", key, "error", err) + } + } +}