Discord: check bot permissions before handing a token to a node (#225 S3) #249
No Reviewers
Labels
Clear labels
area/ai-pipeline
area/auth
area/billing
area/correlation
area/discord
area/frontend
area/governance
area/growth
area/infra-deploy
area/legal
area/map
area/mqtt
area/node-ota
area/osint
area/recording-audio
area/sdr-radio
area/transcription
board
board:agenda
minutes:draft
minutes:final
role:ceo
role:cfo
role:cio
role:ciso
role:cmo
role:coo
role:cto
Scene/entity extraction, summarizer, AI providers, embeddings
AuthN/Z, roles, tenancy/org scoping, API keys, credentials
Plans, entitlements, AI spend accounting and caps
Incident correlation, linking, lifecycle, severity, corr_debug
Discord bot, radio bots, voice streams, bot tokens, webhooks
Web UI: Next.js app and the edge-node local dashboard
Board process, minutes, unattended runner, agent tooling
Go-to-market, outreach, cold email, discovery conversations
CI/CD, VM, registry, Firestore rules/indexes deploy, Terraform/Caddy
ToS, retention, licences/DAAs, CAN-SPAM, E&O, name suppression
Live map: pins, paths, tiles, legend, geocoding
Node-C2 MQTT, checkins, commands, node sweeper, dynsec
Node install, enrollment, OTA updater, image skew
External feeds: cameras, 511, ADS-B, AIS, outages, and their licences
Call capture, boundaries, PulseAudio, ring buffer, trimming, attribution
OP25 decode, SDR hardware/tuning, conventional, second SDR, mesh radio
STT, transcript correction, place verification, name accuracy
Board process: minutes and board-filed items
Needs board scoping at the next sitting; CEO removes the label once scoped
One exec pre-read, closed by CEO
CEO ruling; open until enacted
Filed by the CEO
Filed by the CFO
Filed by the CIO
Filed by the CISO
Filed by the CMO
Filed by the COO
Filed by the CTO
kind
behavior-bug
Wrong product behavior; the instrument reads it correctly
kind
measurement-bug
Corrupts the recording/instrument (debug data, evals, health checks), not behavior
priority
p0
Drop everything: prod broken, live exposure, or owner-ruled P0
priority
p1
Next up: blocks a gate, a customer, or core incident quality
priority
p2
Normal: real defect or planned work, schedule in turn
priority
p3
Low: minor, cosmetic, or someday
status
blocked
Waiting on an external party, spend, or another issue
status
in-progress
Branch or PR open against it
status
needs-decision
Waiting on an owner or board ruling before work
status
needs-info
Root cause or repro unknown; needs a reading first
type
bug
Something built is wrong: code, data, or a measurement
type
chore
Maintenance, refactor, tests, pinning, cleanup: no user-visible change
type
decision
A ruling to make or record: board minutes, owner calls, legal reviews
type
docs
Docs, runbooks, CLAUDE.md, skills, SOPs
type
feature
New capability or an extension of an existing one
type
security
Security, privacy, credential or tenant-isolation exposure
type
tracking
Tracker, checklist, initiative or punch-list spanning several changes
No labels
Milestone
No items
No Milestone
Projects
Clear projects
No projects
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: logan/server-26#249
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Slice S3 of #225: the permission check is wired into token picking (MVP backend). Built on origin/main with S1 (#230), S2 (#231) and #227 (several streams per channel) merged.
What changed
streams._start_locked, before the lease. That covers all four entry points: botPOST /discord/streams, web pickerPOST /discord/nodes/{id}/streams,nodes.pydiscord_join, and the reconciler's re-place.pick_tokencould choose (valid, pool or this org's, member or membership unknown, not busy in this server, leased to no node or to a candidate node). No other org's bot is ever asked about this server.discord_perm_check_concurrency(4) at once, all withindiscord_perm_check_budget_seconds(3 s). A bot not answered in time counts as unknown, and its load keeps filling the cache.pick_token(perms=...):_candidate_order, confirmed-ok sorts before unknown, placed after BYO-before-pool.missing_permissionswithdenied=[{name, missing, reason, fix}], plus the invite links.preferred_token_idthat is denied is refused by name, not swapped.lease_for_streamfalls through when its token is denied, so the stream moves to a bot that can speak.DRB Radio 2 can't Speak in #dispatch: denied by #dispatch's override for @Muted. In Discord: Edit Channel → Permissions → add DRB Radio 2 → allow Speak, then try again./joinshows it through_refusal_text(detail + invites; no bot change needed)._mark.bad_channel: a channel that is not a voice channel gets a 400bad_channel, notmissing_permissions(per the S1 review note).systems/{id}.preferred_token_idis now read, uncached, so a change applies to the next start.preferred_token_idstays strict.discord_pool_bot_guild_cap(90) is never offered as an invite. That applies topick_token, voice-check and the bot overview. A full pool bot already in the server is still used. With nothing else left, the result is the newpool_full.handle_actual: the edge codesmissing_permissions,channel_full,not_in_guildandbad_channelcalldiscord_rest.invalidate(token_id, guild_id).PUT /tokens/{id}/prefer/{system_id}:org_id) get a 403.discord.token.prefer.DISCORD_PERM_CHECK_ENABLED=falsemeans no Discord call before a lease. The system-preferred hint still applies, since it makes no Discord call. The config comment says so.tests/conftest.pygets an autouse fixture that answers every Discord call with a connect error. No test can reach discord.com, and existing tests see unknown, which fails open to the old picking.JSON contract (for S4)
409 from any start (
POST /discord/streams,POST /discord/nodes/{id}/streams):detailis one line per bot (up to 3) and then the fix.bad_channel(400)pool_full(409)nodes.pydiscord_joinreturnsHTTPException(409, detail=<same text>).GET /discord/nodes/{node_id}/guilds/{guild_id}/voice-check?refresh=0|1&channel_id=<optional>_org_guilds(node.org_id). Anything else is a 404.state:readyunknownneeds_fixbusyno_botbad_channeluncheckedbots[].status:ok | denied | unknown | not_member | invalid | bad_channel | unchecked.unavailable_reason:busy_here(the bot is already in a voice channel in this server) orother_node.Token data: bot names only for non-admins. A platform admin also gets
token_id. No token strings.user_limitis advisory. REST can't see occupancy, so the edge'schannel_fullstays the enforcement.Channels: the server's directory (
voice_channels). When the directory is empty, the voice channels from a bot's snapshot.refresh=1: at most once per 10 s per uid and server. Past that the cached answer is served withrefreshed: false.GET /discord/guilds/{guild_id}/voice-check?channel_id=&refresh=org_id. Rows carrytoken_id.Not in this slice
/prefer, butGET /tokensis admin-only. I'm filing this as a follow-up (linked on #225).Verification
pytest(drb-c2-core): 800 passed.tests/test_discord_perm_check.py.bad_channel, the preferred hint, the one-bot setup, voice-check scoping and the rate limit, the Q2 matrix, and an end-to-end run through the real REST client on a MockTransport that checks the token never reaches logs or messages.tscwas not run.Not merged; the main session merges after review.
🤖 Generated with Claude Code
start_stream now asks Discord (cached REST snapshots, S2) whether each bot it could pick may View, Connect and Speak in the voice channel, before the lease. Covers all four entry points: bot POST /discord/streams, web picker, nodes discord_join, and the reconciler's re-place. - pick_token: denied bots skipped; confirmed-ok sorts before unknown; unknown is allowed (fail open); all denied -> missing_permissions naming each bot, missing permission, trace reason and fix. The reuse branch falls through when its token is denied. - Bounded: discord_perm_check_concurrency at once, within discord_perm_check_budget_seconds; unanswered = unknown. - Non-voice channel -> 400 bad_channel (not missing_permissions). - systems/{id}.preferred_token_id is now read (plan finding 1), as a hint. - Pool bots at discord_pool_bot_guild_cap are never offered as invites; pool_full when nothing else is left (owner Q3). - handle_actual drops cached access on missing_permissions, channel_full, not_in_guild, bad_channel. - New GET /discord/nodes/{node_id}/guilds/{guild_id}/voice-check (org member, names only) and GET /discord/guilds/{guild_id}/voice-check (service key or admin). - PUT /tokens/{id}/prefer/{system_id}: org owners for their own systems and own BYO bots; shared systems and pool bots stay admin-only (owner Q2). Keeps both pointers single-valued; audited. - Kill switch: DISCORD_PERM_CHECK_ENABLED=false. - tests/conftest.py: autouse fixture so no test ever reaches discord.com. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>Review (S3): pushed
8bdcfcaon this branch.guild_overviewgave a full pool bot an invite link whenever it was in any server (if membertested the list, not this guild). Test added.start_streamnow re-reads membership once onpool_full, as onno_bot_in_guild. Test added./joinkeeps refusing for up to 120 s (cached snapshot).pytest: 802 passed. Not merged.