Skip to content

hooks-service: alert-channel history and reply endpoints (BB-420) - #469

Merged
trotterdylan merged 2 commits into
mainfrom
bb/bb-420-alert-channel-history-read-and-thread-rep-thr_sxch52mhsk
Dec 4, 2026
Merged

trotterdylan merged 2 commits into
mainfrom
bb/bb-420-alert-channel-history-read-and-thread-rep-thr_sxch52mhsk

Conversation

@trotterdylan

Copy link
Copy Markdown
Collaborator

Implements BB-420.

Adds two additive, cluster-only (8081) endpoints to the hooks service so a
developer's server can find an Alertmanager-posted alert's original Slack
message and post exactly one threaded reply under it. Both are disabled
unless the deployment sets an allow-list.

Merging to bb-plugins main moves the bb-server pin, builds a new
bb-server image and stages a restart on every developer's server
(pressed under Settings → Plugins → Team setup, or taken in the quiet
window). Every developer's server will restart, including this one. It
also publishes a new ghcr.io/bitcomplete/hooks-service:sha-<sha> image;
the service in the cluster does not change until the sibling bb-gate PR
advances the pinned tag.

Sibling: slice 2 of 2, a bb-gate pull request that advances the
hooks-service tag in kploy/base/hooks.yaml, sets -slack-alert-channels
and updates the README, opened after this merges.

Classification

owner-reviewed: the hooks service's subscriber API and its contract
(hooks-service/docs/hooks.md). The decide list's decide: user entry
"Identity and access changes (… the hooks service's webhook verification,
subscriber API, app token, leases and session ownership)" names
bb-plugins/hooks-service/**, and a user entry wins.

What changes

  • cmd/hooks-service/main.go: new -slack-alert-channels flag (env
    SLACK_ALERT_CHANNELS), a comma-separated channel-id allow-list. Empty
    (the default) disables both endpoints.
  • hooks/slack.go: GET /v1/slack/alert-channel/history and
    POST /v1/slack/alert-channel/reply, modelled on
    handleEphemeral/allowedChannel.
    • History: an identified connection (403 not-identified), a channel
      on the allow-list (403 channel-not-allowed), limit 1..50 (default
      20) and an optional oldest; calls conversations.history with the bot
      token and returns ts, thread_ts, user, bot_id, text and each
      attachment's title/text/fallback/fields, nothing else. Every
      returned ts is remembered as matched for (connection, channel), in
      memory, 24h TTL, bounded in size.
    • Reply: text only (no blocks, no reply_broadcast), 32 KiB cap; 403
      parent-not-matched unless parent_ts was returned by a history read
      for the same connection and channel; the only write is one
      chat.postMessage with thread_ts. The same request_id is 200
      {"duplicate":true} without posting; a different one is 409
      already-replied.
  • hooks/server.go: the two routes on the subscriber mux, beside the other
    /v1/slack routes.
  • hooks/slack_test.go: tests for the disabled flag, the unallowed channel,
    the unmatched parent (including a match by another connection and one for
    another channel), the duplicate/conflict reply, and the single
    chat.postMessage payload; the existing suite passes.
  • hooks-service/docs/hooks.md: both endpoints, auth, allow-list, limits
    and an error table.
  • hooks-service/README.md: the new flag in the flags table. The
    bot-scope list already names channels:history and groups:history.

Testing

  • cd hooks-service && gofmt -l . && go vet ./... && go test -race ./...: pass
    (hooks package 22.5s, sentryx 0.02s).
  • Exercised the endpoints through the real subscriber mux and the fake
    Slack API: TestSlackAlertChannelDisabled,
    TestSlackAlertChannelHistory, TestSlackAlertChannelReply.
  • Not tested: a live Slack conversations.history call, and the by-hand bot
    scopes channels:history/groups:history are not yet confirmed installed
    on the team app (owner step, raised when this PR opens).

Out of scope

The BB-419 consumer, the stewards triage logic, any write other than the one
thread reply, persistence of match or reply state, and any change to the
session reply path.

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Independent review running in bb thread @thread:thr_pzmqa3ux6g.

— from bb thread thr_sxch52mhsk

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 1 (Opus 5.5, at d5654eb)

Owner review required: yes. This changes the hooks service's subscriber API and adds a new use of the bot token (channel history reads, and replies under messages bb did not start). The decide-list entry "Identity and access changes (… the hooks service's webhook verification, subscriber API, app token, leases and session ownership …)" is decide: user and wins over the project contract entry for docs/hooks.md. This escalation is binding: a project-steward bb-approval will not meet the gate, and the owner's unsigned approval will. @trotterdylan, this one needs your approval. The spec was approved on 2026-12-04, so I am not asking for a product decision.

Blockers

  1. hooks/slack.go handleAlertReply: a retry gets duplicate:true even when nothing was posted, and the release path is untested. claimAlertReply records the claim before postMessage runs, and the post can take up to the 10s client timeout. If a repeat with the same request_id arrives during that window, it gets 200 {"duplicate":true}. If the first post then fails, releaseAlertReply drops the claim. The caller has now been told its reply is posted when it never was, and it will not retry, because a duplicate response means "done". A different request_id sent in the same window gets 409 already-replied for a reply that never happened. Suggested fix: keep the claim in a pending state until the post returns, and answer a repeat during that state with a retryable error (for example 409 in-flight), added to the error table. Neither the release-on-failure path nor this case has a test. Using the existing f.api.postError, add a test where the first post fails, a retry with the same request_id posts exactly once, and a repeat during an in-flight post is not reported as duplicate.
  2. hooks/slack.go alertParentMatched: no test covers lapsed matches. The spec and docs/hooks.md both say a matched ts that has lapsed is refused with parent-not-matched, but no test checks it. Slack.Now can be set in tests. Advance it past DefaultSessionKeep and assert the 403 and that no Slack call is made.

Suggestions

  • handleAlertReply posts with r.Context(). If the caller disconnects partway through, the post is cancelled with an unknown outcome and the claim is released, so a retry can post twice. Using context.WithoutCancel(r.Context()) for the post closes that gap.

Tested

  • Ran: cd hooks-service && gofmt -l . && go vet ./... && go test -race ./... (pass).

— from bb thread thr_pzmqa3ux6g

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Addressed round 1 at b07643c3:

  • Blocker 1 (claim recorded before the post; a retry could be told duplicate:true when nothing was posted, and the release path untested): fixed in b07643c3. The claim now carries a pending state. While the first post is in flight, a repeat gets 409 {"error":"in-flight"} (retryable), not duplicate:true, and a different request_id in that window is also in-flight, not already-replied. completeAlertReply flips the claim to posted only after chat.postMessage returns; releaseAlertReply drops it on a Slack failure, so a retry with the same request_id posts. Added TestSlackAlertChannelReplyRetry: a failed post followed by a same-request_id retry posts exactly once, and a repeat during an in-flight post gets in-flight (asserted with a blocking fake post). Documented in-flight in the reply section and the error table.
  • Blocker 2 (no lapsed-match test): fixed in b07643c3. Added TestSlackAlertChannelMatchLapses, which advances Now past DefaultSessionKeep and asserts 403 parent-not-matched with no Slack call. It re-identifies first, because the connection identity's lease is shorter than the match TTL, so the match is the only thing that lapsed.
  • Suggestion (post on r.Context()): taken in b07643c3. The post now uses context.WithoutCancel(r.Context()), so a caller disconnect cannot cancel a post whose outcome is unknown and let a retry double-post.

Ran: cd hooks-service && gofmt -l . && go vet ./... && go test -race ./... (pass; hooks 25.5s).

— from bb thread thr_sxch52mhsk

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 2 (Opus 5.5, at b07643c)

Owner review required: yes. This changes the hooks service's subscriber API and adds a new use of the bot token. The decide-list entry "Identity and access changes (… the hooks service's webhook verification, subscriber API, app token, leases and session ownership …)" is decide: user. This escalation is binding: a project-steward bb-approval will not meet the gate, and the owner's unsigned approval will. @trotterdylan, this one needs your approval.

Resolved since round 1

  • Blocker 1 (round 1): resolved by b07643c. The claim stays pending until the post returns. A repeat during the post gets 409 in-flight, which is in the docs' error table. A failed post releases the claim. TestSlackAlertChannelReplyRetry covers both cases.
  • Blocker 2 (round 1): resolved by b07643c. TestSlackAlertChannelMatchLapses checks that an expired match is refused with no post.
  • Suggestion (round 1): taken. The post now runs on context.WithoutCancel.

Tested

  • Ran: cd hooks-service && gofmt -l . && go vet ./... && go test -race ./... (pass), and -count=5 -run TestSlackAlertChannel (pass, no flakes).

Looks good.

— from bb thread thr_pzmqa3ux6g

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Approved b07643c from the Stewards page

@trotterdylan
trotterdylan merged commit 455c5e6 into main Dec 4, 2026
2 checks passed
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Merged: owner-reviewed lane, reviewer approved b07643c, checks green, approved by the owner, escalated by the reviewer to the owner.
merge commit 455c5e6
Note: merging this moves the pin and stages a restart for every developer's server (Restart now, or in the 03:00–05:00 ET quiet window).
dispatched bb-server.yaml on bitcomplete/bb-gate@main

@trotterdylan
trotterdylan deleted the bb/bb-420-alert-channel-history-read-and-thread-rep-thr_sxch52mhsk branch December 4, 2026 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant