Skip to content

slack: the Slack plugin: sign-in, DM wakes and replies (BB-38) - #59

Merged
trotterdylan merged 3 commits into
mainfrom
bb/bb-38-bb-plugins-slack-plugin-pr-2-of-3-thr_b28humxp55
Oct 22, 2026
Merged

trotterdylan merged 3 commits into
mainfrom
bb/bb-38-bb-plugins-slack-plugin-pr-2-of-3-thr_b28humxp55

Conversation

@trotterdylan

Copy link
Copy Markdown
Collaborator

Implements BB-38 PR 2 of 3. A new slack plugin so a developer can talk to their own bb from Slack: one team Slack app, routed by bb-gate-hooks (bb-gate/docs/hooks.md, "Slack agent") to the server of the Slack user who sent the DM. Each Slack conversation maps to one bb thread; slack_reply posts back through the service, which holds the bot token.

Merging this rebuilds the bb-server image and stages a restart on every developer's server, including this one. It lands when each owner presses Restart now (or in the 03:00–05:00 US Eastern quiet window). Do not press Restart now from a thread you still need.

Classification

owner-reviewed: it consumes the bb-gate-hooks Slack cross-repo contract and adds a per-developer identity flow (Sign in with Slack), with the user token in a secret setting. The bb-gate side merged in #25; this is the plugin half.

What changes

  • plugins/slack/hooks-client.ts — the subscriber side of bb-gate-hooks for Slack: the SSE stream, POST /v1/identify (source: "slack", no teams), the sign-in start/exchange calls, and the reply/status/title endpoints. The connection id from the hello is what every call names.
  • plugins/slack/server.ts — the plugin. A wake (event: session, source: "slack") maps channel:thread to a bb thread in kv: the first message threads.spawns it, a follow-up threads.sends to it. slack_reply posts through the service with the connection id and the text only. A turn that ends without a reply gets one backstop error reply; the bb thread's title is pushed with assistant.threads.setTitle, and assistant.threads.setStatus shows "is thinking…" while the turn runs. Sign-in serves GET /api/v1/plugins/slack/http/signin/callback and calls POST /v1/slack/signin/exchange with this plugin's own {connection,state,code}; the token lands in a secret setting and is never logged or shown. Sign-out deletes it and reconnects the stream so the service drops the binding.
  • plugins/slack/sessions.ts — the pure conversation→thread record and the wake text, with non-owner text marked untrusted.
  • plugins/slack/app.tsx — the settings section: "Signed in as …", a Sign in with Slack button, Sign out, and the project a Slack conversation starts its thread in.
  • plugins/slack/skills/slack/SKILL.md, plugins/slack/README.md, and a row in the top-level README.

Phase 1 is DMs only; the service routes only DMs, so the asker is the owner. Untrusted marking is still applied.

No bb-gate change here. The three docker/bb-server/Dockerfile plugin-list edits are PR 3 and land only after the pin moves past this merge.

Testing

  • npm run typecheck in plugins/slack: pass.
  • npm test in plugins/slack: pass (29 tests; the plugin is not in the .github/workflows/plugins.yaml matrix, like linear).
    • sessions.test.ts: conversation split, owner/trust, untrusted wake text, long-text clipping.
    • hooks-client.test.ts: SSE parsing, the Slack wake schema, and identify with source: "slack"/teams: [].
    • server.test.ts: sign-in start stores the state against the connection; the callback exchanges the code, stores the token, identifies with it and never logs it; a failed exchange tells the developer to start again; an unstarted state is refused; sign-out forgets the token and identity; the first wake spawns and follow-ups send; non-owner text is marked untrusted; slack_reply posts with the connection id only and is one per message; the backstop fires for a turn that handled the wake and not for one that did not; the project picker.
  • bb plugin build in plugins/slack: pass (validates the manifest and the app bundle).
  • Not tested: a live Slack workspace and a live bb-gate-hooks. The Slack app is created, installed and its secrets sealed by hand, and no agent sees them.

Talk to your own bb from Slack. One team Slack app, routed by
bb-gate-hooks (bb-gate/docs/hooks.md, "Slack agent") to the server of the
Slack user who sent the DM. Each Slack conversation maps to one bb thread
in plugin kv: the first message spawns it, a follow-up is sent to it, and
slack_reply posts back through the service, which holds the bot token. The
pod never holds a Slack bot credential.

Identity is per-developer Sign in with Slack: the callback runs on this
server, the exchange happens in bb-gate-hooks (which holds the client
secret), and the returned user token is stored in a secret setting and
presented at /v1/identify. Sign-out forgets it. A turn that ends without a
reply gets one backstop error reply; the bb thread's title is pushed to the
Slack conversation and the thinking status is set while the turn runs.

Phase 1 is DMs only. No bb-gate change here: the Dockerfile plugin list is
the next PR.
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Independent review running in bb thread @thread:thr_fxddsp94g9.

_— from bb thread

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 1 (GLM 5.3, at 4e39039)

Owner review required: yes — this is the consumer half of the newly-landed bb-gate-hooks Slack contract (bb-gate/docs/hooks.md, "Slack agent"), and it introduces a new per-developer identity flow whose token is stored on the pod in a secret setting (a new credential for every server, a bigDecisions entry). @trotterdylan, this one needs your approval once the blockers below are fixed.

Blockers

  1. plugins/slack/server.ts:361-368 (the onSession catch path) — any failure of threads.send clears the session→thread mapping, so a transient error (server busy, a hiccup in the local RPC) permanently orphans the conversation's bb thread: the next Slack message silently starts a fresh thread with no context, which breaks the ticket's "follow-ups threads.send to the same one". Only a thread that is truly gone should clear the mapping — the stewards plugin's send() (plugins/stewards/server.ts:866-875) does exactly this distinction (NOT_FOUND/404/410 ⇒ gone, everything else ⇒ keep). The blanket clear is also redundant with the thread.archived/thread.deleted listeners just below it, which already forget deleted threads. Fix: clear only on a gone-shaped error and keep the mapping otherwise (the backstop already tells the user about the failure).
  2. plugins/slack/sessions.ts:82-84 + server.ts:400-406 (assistant_thread_context_changed) — the wake text for a context change tells the agent "If a reply is needed, use the slack_reply tool; otherwise end the turn", but because that text names the session, the turn counts as having handled the wake, so a turn that (per your own instruction) ends without slack_reply always triggers the backstop: every routine context change posts "The bb thread finished its turn without sending a reply here. Send another message to try again." into the user's DM — plus a wasted bb turn to produce it. Either don't hand context_changed to the bb thread / don't count it as needing a reply (mark it answered on arrival, or skip the backstop for it), or instruct a one-line acknowledgment so a reply always comes.

Suggestions

  • server.ts:230 (onSession is not awaited by the stream) — two wakes for the same session that interleave (the hooks-client dispatches onSession without awaiting) can both see threadId === null during the first spawn's round-trip and both spawn, leaving two threads and two replies for one message. A per-session promise chain (or re-reading the map for a thread id before spawning) would close the window. Low likelihood, but cheap to fix.
  • hooks-client.ts:249-263 (identifyNow on 401) — a refused token (revoked/expired, per the contract "401 means sign in again") only logs a warning; the settings page keeps saying "Signed in as …" while the user's DMs fall to the service's no-server reply. Clearing the stored identity (and announcing) on 401 would make the page truthful. (The linear plugin has the same gap, so this may be a deliberate team-wide choice.)
  • hooks-client.test.ts:87 — the test wake sends routedTo: "owner"; the schema field is routedAs. It passes only because the schema is .loose() with a default, so the test isn't exercising the wire shape it thinks it is. Fix the typo.
  • server.ts:395-398 (prunePending) — expired pending sign-in states are pruned only inside signinStart; they linger in kv until the next sign-in attempt. Pruning on the stream's hello (or a timer) would keep kv tidy.
  • Criterion 11, "code single use" — the mechanism is tested (pending deleted before the exchange, unknown state refused) but there is no direct test that a second callback with the same state is refused. One extra assertion would pin it.

Checked

  • Ran, in plugins/slack (not in the .github/workflows/plugins.yaml matrix, so CI never runs it): npm run typecheck (pass), npm test (29 tests, 3 files, pass), bb plugin build (pass). Quoted results are clean; nothing red to report.
  • Verified against the ticket's acceptance criteria:
    • AC5 (mapping/spawn/send/slack_reply/status/title/backstop/settings state): met, except the two blockers above (transient-error mapping loss; context-change backstop noise).
    • AC6 (vitest for mapping/backstop/tool): met — sessions.test.ts, hooks-client.test.ts, server.test.ts cover the mapping, the backstop's handled/not-handled distinction and the tool's once-per-message rule; they fail if the behaviour is reverted.
    • AC9, plugin half (present the token correctly, re-identify after every hello): met — identify body is {connection, source: "slack", token, teams: []} exactly as docs/hooks.md specifies, re-identified on every hello and on the renewal interval; tested. The rejections themselves are the service's (PR 1).
    • AC10 ("Signed in as …" / Sign-in button): met — app.tsx shows both, plus the hooks connection state.
    • AC11 (sign-in flow end to end, state checked, code single use, token never logged): met in the plugin's half — the flow is tested from signinStart through the callback to the stored token and identify; the state is validated by shape and kv lookup, unknown states refused, the pending state is deleted before the exchange, and the token never appears in any log entry (asserted). The Slack side of the exchange is the service's, tested in PR 1.
    • Revision 2: untrusted marking is applied even though phase 1 routes DMs only (tested with a non-owner wake); replies carry only the connection id, and the service's not-routed refusal is surfaced to the agent; the pod holds no Slack credential — only the developer's own sign-in token in a secret setting, exactly as the contract prescribes.
  • Verified against the contract (bb-gate/docs/hooks.md, "Slack agent"): identify, sign-in start/exchange, wake, reply/status/title bodies and paths all match; the callback is served at /signin/callback (the /api/v1/plugins/slack/http prefix) with auth: "none", the state is single-use and connection-bound, a failed exchange tells the user to start again (including the not-yours case), sign-out drops the token and reconnects so the service forgets the binding, and the 24 h session TTL matches the service's routing window. bb-gate is untouched by this PR (no bb-gate files in the diff), and the three Dockerfile plugin-list edits are correctly left for PR 3.

— from bb thread thr_fxddsp94g9

…context change (BB-38)

Review round 1: a transient threads.send failure no longer clears the
conversation's thread, so the next message continues it; only a gone-shaped
error (404/410/NOT_FOUND) clears the mapping and starts a fresh thread. An
assistant_thread_context_changed wake records the conversation but runs no
turn and expects no reply, so it cannot trip the backstop. Wakes are
serialized per conversation so two quick messages cannot both spawn. A 401
from /v1/identify forgets the token and identity; pending sign-in states are
pruned on every hello; the test's routedAs typo is fixed and a second
callback with a consumed state is asserted refused.
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Round 1 addressed in 7b1de53. Every point:

Blockers

  1. Transient threads.send no longer clears the mapping — sendToThread distinguishes gone (NOT_FOUND/404/410) from transient. A transient error keeps the thread and posts the "could not deliver" backstop; only a gone-shaped error forgets the mapping and starts a fresh thread. Test: "keeps the conversation's thread on a transient send error, and starts a new one only when it is gone".
  2. assistant_thread_context_changed no longer runs a turn: handleSession records the conversation (keeping it mapped and alive) but does not increment the reply count, does not set the thinking status, and does not send to the thread, so the backstop cannot fire for it. Test: "records a context change without running a turn or expecting a reply".

Suggestions

  • Concurrent wakes: fixed — wakes are serialized per conversation through sessionChains, so two quick messages cannot both see threadId === null and spawn.
  • 401 identify: fixed — identifyNow reports a refused token to the plugin, which forgets the stored token and identity and reconnects, so the settings page stops claiming "Signed in as …". Test: "forgets the token when the service refuses it on identify". (I took this for Slack; whether linear should follow is a separate call.)
  • Test typo: fixed — routedTo → routedAs.
  • Pending sign-in pruning: fixed — pruned on every hello via onHello, as well as before a new start.
  • Code single use: added — the exchange test now asserts a second callback with the same state is refused and no second exchange happens.

npm run typecheck, npm test (32 tests), and bb plugin build all pass on the new head.

— from bb thread thr_b28humxp55

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 2 (GLM 5.3, at 7b1de53)

Owner review required: yes — same lane as round 1: the consumer half of the newly-landed bb-gate-hooks Slack contract, plus a new per-developer credential (the sign-in token) stored on the pod in a secret setting. @trotterdylan, the blockers from round 1 are fixed; this is ready for your approval.

Blockers

None.

Suggestions

  • server.ts:177-189 (forgetSignin): a 401 arriving from an identify of an old token can race a just-completed sign-in that stored a new one (the old identify's failure lands after the callback's success and clears the fresh token). Self-healing — the user signs in again — and very unlikely, so not worth blocking; noting it only for completeness.

Resolved since round 1

  • Blocker 1 (any send error cleared the session→thread mapping): resolved — sendToThread distinguishes gone (NOT_FOUND/404/410 → clear and respawn) from transient (keep the mapping, backstop "could not deliver"), exactly the stewards distinction; tested both ways.
  • Blocker 2 (assistant_thread_context_changed always tripped the backstop): resolved — a context change is recorded without counting as a message (needsReply false: no events increment, no turn, no status, no reply expected), so the backstop cannot fire for it; tested.
  • Suggestion (per-session serialization): resolved — sessionChains runs one wake at a time per conversation, swallows a previous run's failure, and cleans up its entry.
  • Suggestion (401 identify left "Signed in as …" on the page): resolved — onTokenRefused forgets the token and identity, reconnects so the service drops the binding, and the test asserts the page flips and the token never appears in logs.
  • Suggestion (routedTo test typo): resolved.
  • Suggestion (pending states pruned only in signinStart): resolved — pruned on every hello.
  • Suggestion (no direct replay test for a consumed state): resolved — a second callback with the same state is asserted refused with only one exchange.

Checked

  • Ran, in plugins/slack (still not in the .github/workflows/plugins.yaml matrix, so CI never runs it): npm run typecheck (pass), npm test (32 tests, 3 files, pass), bb plugin build (pass).
  • Re-reviewed the new changes themselves: the gone/respawn path re-checks the record after clearing before spawning (serialized per session, so no interleave), a respawn failure leaves the mapping null so the next message starts fresh, and markHanded/spawnThread keep the backstop's handedAt bookkeeping on both the spawn and send paths.
  • Verified against the ticket's acceptance criteria: unchanged from round 1 — AC5 (mapping/spawn/send/slack_reply/status/title/backstop/settings state), AC6, AC9 plugin half, AC10, AC11 and revision 2 all met; the two round-1 exceptions are the now-fixed blockers.
  • Verified against the contract (bb-gate/docs/hooks.md, "Slack agent"): unchanged from round 1 — identify/start/exchange/wake/reply/status/title shapes all match, the callback flow and its failure pages match, sign-out and a refused token both drop the stream so the service forgets the binding, and bb-gate remains untouched.
  • Lane re-classified from the diff: owner-reviewed stands (new cross-repo contract consumer half, new per-developer credential in a secret setting); the fixes added no new triggers.

— from bb thread thr_fxddsp94g9

Review round 2's one suggestion: onTokenRefused now names the token the
service refused, and forgetSignin leaves the setting alone when a different
token has been stored since (a sign-in that completed while the old
identify was in flight). Tested.
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Round 2's one suggestion taken in 5c744bb: onTokenRefused now names the token the service refused, and forgetSignin leaves the setting alone when a different (newer) token has been stored since, so an old identify's 401 cannot clear a just-completed sign-in. Test: "does not clear a newer token when an old one is refused".

npm run typecheck, npm test (33 tests), and bb plugin build pass. The round-2 approval stands; this is the suggestion the review named, not a scope change. Waiting on @trotterdylan.

— from bb thread thr_b28humxp55

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 3 (GLM 5.3, at 5c744bb)

Owner review required: yes — unchanged lane. @trotterdylan, the round-2 approval stands on this head; this is the one to approve.

Blockers

None.

Resolved since round 2

  • The 401 race (an old token's refused identify could clear a newer token stored by a sign-in that completed mid-flight): resolved — onTokenRefused now names the refused token and forgetSignin skips clearing when the stored token is a different (newer) one. The sign-out path still clears unconditionally. Tested, including that the page stays signed in after the mid-flight swap.

Checked

  • Ran, in plugins/slack (still not in the CI matrix): npm run typecheck (pass), npm test (33 tests, 3 files, pass), bb plugin build (pass).
  • Reviewed the push against round 2: it is the one suggestion from that round and nothing else; no scope or behaviour changed beyond it, so the round-2 approved verdict stands and this marker repeats it on 5c744bb. Lane unchanged: owner-reviewed, waiting on @trotterdylan.

— from bb thread thr_fxddsp94g9

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

LGTM

@trotterdylan
trotterdylan merged commit 596c0b2 into main Oct 22, 2026
20 checks passed
@trotterdylan
trotterdylan deleted the bb/bb-38-bb-plugins-slack-plugin-pr-2-of-3-thr_b28humxp55 branch October 22, 2026 20:48
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Merged: owner-reviewed, approved by trotterdylan on 5c744bb (LGTM comment, 2026-10-22T20:47:41Z); independent reviewer approved 5c744bb; all checks green. Squash-merged as 596c0b2.

Merging re-pins bb-plugins main in bb-gate, rebuilds the bb-server image and stages a restart on every developer's server (including this one) for each owner to press Restart now, or in the 03:00–05:00 US Eastern quiet window. Do not press Restart now from a thread you still need. The docker/bb-server/Dockerfile plugin-list edits are PR 3 and follow once the pin moves past this merge.

— from bb thread thr_b28humxp55

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