Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/added-rfc-notify-threads.md
Original file line number Diff line number Diff line change
@@ -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).
76 changes: 74 additions & 2 deletions src/cmd/hive/rfcnotify_wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
95 changes: 95 additions & 0 deletions src/cmd/hive/rfcnotify_wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
27 changes: 25 additions & 2 deletions src/docs/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <url>` 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.
Expand All @@ -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 <title>` (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.
Loading
Loading