Discord: several radio streams per voice channel; adding never overwrites (#213)

Owner-reported on live: adding a radio channel for a server + voice channel
that already had one (different label, system, even bot) overwrote the
existing stream and made its bot leave, because stream_id was guild:channel.

- New streams get guild:channel:<12 hex> ids (leases.new_stream_id); old
  guild:channel docs keep working for read/stop/remove/node reports. Route
  validators accept both (leases.STREAM_ID_PATTERN).
- start_stream: no stream_id = ADD. Same system already on in that channel
  is idempotent (already_on, label/mode untouched); a stopped one of that
  system is restarted; anything else is a new doc. stream_id = explicit
  restart/edit (admin Start button, reconciler), incl. moving to another bot.
- preferred_token_id is strict: bot already in this server -> 409
  preferred_busy naming the stream; on another node -> preferred_other_node;
  unusable -> preferred_unavailable. No silent swap. pool_exhausted now says
  one-bot-per-server and to add + invite another bot.
- One-token-per-guild was already enforced inside the lease transaction;
  now covered by race tests. Per-guild asyncio lock serialises the
  already-on / server-cap check with the create (guild half of #223).
- Stop by stream_id (POST /discord/guilds/{id}/stop {stream_id}); admin UI
  Stop/Start/Remove act on the row's stream; node picker lists every stream
  in a channel and adds beside other systems.
- Bot: /leave stream: (autocomplete) stops one; /leave channel: with several
  streams asks which unless all:True; bare /leave still stops the server.
  /status and /join name each stream's bot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Logan Cusano
2026-10-05 00:30:40 -04:00
co-authored by Claude Opus 5.5
parent 16a609e35b
commit 8af410aede
15 changed files with 913 additions and 113 deletions
+62 -3
View File
@@ -32,7 +32,15 @@ Firestore layout:
in_use / assigned_node_id / assigned_at — legacy mirrors of ``lease``,
still written so the old /tokens page and any reader of them stay right.
discord_streams/{guild_id}:{channel_id} — see discord_streams.py.
discord_streams/{stream_id} — see discord_streams.py. ``stream_id`` is
``{guild_id}:{channel_id}:{suffix}`` (server-26#213 follow-up: several
streams may share one voice channel, each with its own bot); docs created
before that are plain ``{guild_id}:{channel_id}`` and keep working.
Several streams in ONE guild therefore always hold DIFFERENT tokens: a token's
lease lists its streams and ``pick_token`` skips any token that already has a
stream in the guild. Both the read and the write happen inside the lease
transaction, so two concurrent starts in one guild can never share a token.
"""
from __future__ import annotations
@@ -63,10 +71,23 @@ def invite_url(application_id: Optional[str], guild_id: Optional[str] = None) ->
return url
# Old ids are ``guild:channel``; new ones add ``:<hex>``. Safe as a Firestore
# doc id (no "/") and as a URL path segment. Every route validates against it.
STREAM_ID_PATTERN = r"^[0-9]{1,25}:[0-9]{1,25}(?::[0-9a-f]{8,32})?$"
def stream_id_for(guild_id: str, channel_id: str) -> str:
"""The LEGACY one-stream-per-channel id. Only for reading docs created
before several streams could share a channel — never for a new stream."""
return f"{guild_id}:{channel_id}"
def new_stream_id(guild_id: str, channel_id: str) -> str:
"""A fresh id for a new stream. Unique per stream, so adding a radio
channel can never land on (and overwrite) an existing one."""
return f"{guild_id}:{channel_id}:{uuid.uuid4().hex[:12]}"
def guild_of(stream_id: str) -> str:
return stream_id.split(":", 1)[0]
@@ -81,10 +102,15 @@ class LeaseResult:
token_id: Optional[str] = None
lease_id: Optional[str] = None
reused: bool = False
# error codes: pool_empty | no_bot_in_guild | pool_exhausted
# error codes: pool_empty | no_bot_in_guild | pool_exhausted |
# preferred_busy (the chosen bot already has a stream in this guild) |
# preferred_other_node (the chosen bot is logged in on another node) |
# preferred_unavailable (unknown, invalid, or another org's bot)
error: Optional[str] = None
# bots that would work if invited to this guild: [{name, invite_url}]
invite: list = field(default_factory=list)
# preferred_* errors: {token_name, busy_stream_id, node_id}
detail: dict = field(default_factory=dict)
def _lease_streams(tok: dict) -> list:
@@ -124,9 +150,15 @@ def pick_token(
preferred_token_id: Optional[str] = None,
exclude_stream_id: Optional[str] = None,
) -> LeaseResult:
"""Pure choice over a snapshot of bot_tokens. No I/O — unit tested directly."""
"""Pure choice over a snapshot of bot_tokens. No I/O — unit tested directly.
A ``preferred_token_id`` is a demand, not a hint: if that bot cannot take
the stream the result says why instead of quietly using another bot."""
if not tokens:
return LeaseResult(ok=False, error="pool_empty")
if preferred_token_id:
return _pick_preferred(tokens, preferred_token_id, node_id=node_id, guild_id=guild_id,
org_id=org_id, exclude_stream_id=exclude_stream_id)
usable: list[tuple] = []
not_member: list[dict] = []
for tok_id, tok in tokens.items():
@@ -155,6 +187,31 @@ def pick_token(
return LeaseResult(ok=False, error="pool_exhausted")
def _pick_preferred(tokens: dict[str, dict], tok_id: str, *, node_id: str, guild_id: str,
org_id: Optional[str], exclude_stream_id: Optional[str]) -> LeaseResult:
tok = tokens.get(tok_id)
owner = (tok or {}).get("org_id")
if not tok or tok.get("valid") is False or (owner is not None and owner != org_id):
return LeaseResult(ok=False, error="preferred_unavailable",
detail={"token_name": (tok or {}).get("name") or tok_id})
name = tok.get("name") or tok_id
busy = [s for s in _lease_streams(tok) if s != exclude_stream_id and guild_of(s) == guild_id]
if busy:
# One voice connection per bot per guild (Discord): never move the
# bot off another stream to serve this one.
return LeaseResult(ok=False, error="preferred_busy",
detail={"token_name": name, "busy_stream_id": busy[0]})
lease = tok.get("lease") or {}
if lease.get("node_id") and lease["node_id"] != node_id:
return LeaseResult(ok=False, error="preferred_other_node",
detail={"token_name": name, "node_id": lease["node_id"]})
members = tok.get("guild_ids")
if members is not None and guild_id not in members:
return LeaseResult(ok=False, error="no_bot_in_guild",
invite=[{"name": name, "invite_url": invite_url(tok.get("application_id"), guild_id)}])
return LeaseResult(ok=True, token_id=tok_id)
async def lease_for_stream(
stream_id: str,
*,
@@ -186,6 +243,8 @@ async def lease_for_stream(
and (prev_tok.get("lease") or {}).get("node_id") == node_id
and stream_id in _lease_streams(prev_tok)
and prev_tok.get("valid") is not False
# An explicit edit to a different bot moves the stream to it.
and (not preferred_token_id or preferred_token_id == prev_tok_id)
):
lease = prev_tok["lease"]
if lease.get("releasing"):
+148 -22
View File
@@ -1,8 +1,14 @@
"""
Discord radio channels as desired state (server-26#203, #204; node-26#25).
One ``discord_streams/{guild_id}:{channel_id}`` doc per Discord voice channel
that should carry radio. Fields:
One ``discord_streams/{stream_id}`` doc per radio stream. A Discord voice
channel may carry SEVERAL streams (server-26#213 follow-up, owner decision
2026-10-05): each has its own radio bot and plays its own system, so a channel
can hold e.g. County P25 on one bot and City Fire on another. ``stream_id`` is
``{guild_id}:{channel_id}:{suffix}`` (``discord_leases.new_stream_id``); docs
from before are ``{guild_id}:{channel_id}`` and are read, stopped and removed
exactly the same way. Adding a stream never reuses another stream's doc except
the same system in the same channel (idempotent re-add / restart). Fields:
org_id, guild_id, channel_id, channel_name, guild_name,
system_id, system_name,
@@ -201,12 +207,14 @@ async def publish_desired(node_id: str) -> bool:
# ---------------------------------------------------------------------------
class StreamError(Exception):
def __init__(self, code: str, message: str, invite: Optional[list] = None, status: int = 409):
def __init__(self, code: str, message: str, invite: Optional[list] = None, status: int = 409,
extra: Optional[dict] = None):
super().__init__(message)
self.code = code
self.message = message
self.invite = invite or []
self.status = status
self.extra = extra or {}
ERROR_TEXT = {
@@ -214,16 +222,69 @@ ERROR_TEXT = {
"no_node": "No online node in your organization is listening to that system right now.",
"pool_empty": "No radio bots have been added to DRB yet. A platform admin adds them under Radio bots.",
"no_bot_in_guild": "No free radio bot is a member of this server. Invite one with the link below, then try again.",
"pool_exhausted": "Every radio bot that can join this server is already busy. Stop another radio channel or add a bot.",
"pool_exhausted": ("Every radio bot that can join this server is already busy — a bot can be in only one voice "
"channel per server, and a bot carrying radio on another node can't be shared. Add another "
"radio bot to DRB and invite it to this server, or stop a radio channel here."),
"limit": "This server already has its maximum number of radio channels. Stop one with /leave first.",
"preferred_unavailable": "That radio bot can't be used here (it was removed, failed validation, or belongs to "
"another organization). Pick another bot, or let DRB choose any free one.",
"not_found": "That radio channel no longer exists.",
}
# One c2-core process. Serialising start_stream per guild makes "is this
# system already playing in this channel?" + "is the server at its cap?" +
# create a real check-then-write: two concurrent adds of the same system to
# one channel land on ONE stream, and two adds can't both take the last slot.
_guild_locks: dict[str, asyncio.Lock] = {}
def _guild_lock(guild_id: str) -> asyncio.Lock:
lock = _guild_locks.get(guild_id)
if lock is None:
lock = _guild_locks[guild_id] = asyncio.Lock()
return lock
def _stream_label(s: dict) -> str:
where = f"#{s['channel_name']}" if s.get("channel_name") else f"channel {s.get('channel_id')}"
return f"{s.get('system_name') or s.get('system_id') or 'radio'} in {where}"
async def find_channel_stream(guild_id: str, channel_id: str, system_id: str) -> Optional[dict]:
"""The stream already carrying ``system_id`` in this voice channel: a live
one first, else the most recently updated stopped one (restarted instead
of piling up a new doc per /join + /leave). None if there is neither."""
same = [s for s in await fstore.collection_list(STREAMS, guild_id=str(guild_id))
if str(s.get("channel_id")) == str(channel_id) and s.get("system_id") == system_id and s.get("stream_id")]
live = sorted((s for s in same if s.get("desired") == "on"), key=lambda s: s.get("created_at") or "")
if live:
return live[0]
same.sort(key=lambda s: s.get("updated_at") or "", reverse=True)
return same[0] if same else None
async def _lease_error(res: leases.LeaseResult) -> "StreamError":
code = res.error or "no_node"
d = res.detail or {}
bot = d.get("token_name") or "That radio bot"
if code == "preferred_busy":
busy = (await fstore.doc_get(STREAMS, d["busy_stream_id"]) if d.get("busy_stream_id") else None) or {}
what = _stream_label(busy) if busy else "another radio channel"
return StreamError(code, f"{bot} is already playing {what} in this server. A bot can be in only one voice "
"channel per server — pick another bot, or let DRB choose any free one.",
extra={"busy_stream_id": d.get("busy_stream_id")})
if code == "preferred_other_node":
node = (await fstore.doc_get("nodes", d["node_id"]) if d.get("node_id") else None) or {}
return StreamError(code, f"{bot} is carrying radio on another node ({node.get('name') or d.get('node_id')}) "
"that can't serve this channel. Pick another bot, or let DRB choose any free one.")
return StreamError(code, ERROR_TEXT.get(code, code), invite=res.invite)
async def start_stream(
*,
org_id: str,
guild_id: str,
channel_id: str,
guild_id: Optional[str] = None,
channel_id: Optional[str] = None,
system_id: str,
mode: str = "adhoc",
channel_name: Optional[str] = None,
@@ -232,16 +293,67 @@ async def start_stream(
node_id: Optional[str] = None,
preferred_token_id: Optional[str] = None,
max_streams: Optional[int] = None,
stream_id: Optional[str] = None,
) -> dict:
"""Make ``guild_id:channel_id`` carry ``system_id``. Idempotent: re-running
it for a channel that is already on reuses the node and the lease."""
"""Put ``system_id`` on a voice channel.
* ``stream_id`` given — restart / edit THAT stream (the reconciler, the
admin page's Start button). Its guild and channel come from the doc.
* otherwise — ADD a stream to ``guild_id:channel_id``. If this system is
already on in that channel the existing stream is returned (idempotent
re-add, ``already_on``); a stopped one with this system is restarted;
anything else gets a brand-new stream id, so another system's stream in
the same channel is never touched.
``preferred_token_id`` is strict: a bot already in this server, on another
node, or unusable is refused with a reason, never silently swapped.
"""
if mode not in ("persistent", "adhoc"):
raise StreamError("bad_mode", "mode must be persistent or adhoc", status=400)
if not org_id:
raise StreamError("no_org", ERROR_TEXT["no_org"], status=403)
existing: dict = {}
if stream_id:
existing = await fstore.doc_get(STREAMS, stream_id) or {}
if not existing:
raise StreamError("not_found", ERROR_TEXT["not_found"], status=404)
doc_guild, doc_channel = str(existing.get("guild_id")), str(existing.get("channel_id"))
if (guild_id and str(guild_id) != doc_guild) or (channel_id and str(channel_id) != doc_channel):
raise StreamError("bad_stream", "That radio channel belongs to a different server or voice channel.",
status=400)
guild_id, channel_id = doc_guild, doc_channel
if not guild_id or not channel_id:
raise StreamError("bad_stream", "guild_id and channel_id are required.", status=400)
guild_id, channel_id = str(guild_id), str(channel_id)
stream_id = leases.stream_id_for(guild_id, channel_id)
existing = await fstore.doc_get(STREAMS, stream_id) or {}
async with _guild_lock(guild_id):
return await _start_locked(
org_id=org_id, guild_id=guild_id, channel_id=channel_id, system_id=system_id, mode=mode,
channel_name=channel_name, guild_name=guild_name, created_by=created_by, node_id=node_id,
preferred_token_id=preferred_token_id, max_streams=max_streams, stream_id=stream_id,
existing=existing,
)
async def _start_locked(*, org_id: str, guild_id: str, channel_id: str, system_id: str, mode: str,
channel_name: Optional[str], guild_name: Optional[str], created_by: Optional[str],
node_id: Optional[str], preferred_token_id: Optional[str], max_streams: Optional[int],
stream_id: Optional[str], existing: dict) -> dict:
already_on = False
if stream_id:
existing = await fstore.doc_get(STREAMS, stream_id) or existing # re-read under the lock
else:
existing = await find_channel_stream(guild_id, channel_id, system_id) or {}
if existing.get("desired") == "on":
already_on = True
if preferred_token_id and existing.get("token_id") and existing["token_id"] != preferred_token_id:
tok = await fstore.doc_get(leases.TOKENS, existing["token_id"]) or {}
raise StreamError("duplicate", f"{_stream_label(existing)} is already playing on radio bot "
f"{tok.get('name') or existing['token_id']}. Remove that one "
"first, or pick another system.",
extra={"busy_stream_id": existing["stream_id"]})
# A re-add is not an edit: keep the running stream's label and mode.
channel_name, mode = None, existing.get("mode") or mode
stream_id = existing.get("stream_id") or leases.new_stream_id(guild_id, channel_id)
if max_streams is not None and existing.get("desired") != "on":
live = [s for s in await fstore.collection_list(STREAMS, guild_id=guild_id) if s.get("desired") == "on"]
@@ -263,6 +375,12 @@ async def start_stream(
cur = existing.get("node_id")
if cur and existing.get("system_id") == system_id:
candidates.sort(key=lambda n: 0 if n["node_id"] == cur else 1)
if preferred_token_id:
# A chosen bot can only run on the node it is logged in on.
tok = await fstore.doc_get(leases.TOKENS, preferred_token_id) or {}
held_on = (tok.get("lease") or {}).get("node_id")
if held_on:
candidates.sort(key=lambda n: 0 if n["node_id"] == held_on else 1)
if not candidates:
raise StreamError("no_node", ERROR_TEXT["no_node"])
@@ -306,13 +424,16 @@ async def start_stream(
if prev_node and prev_node != node["node_id"]:
await publish_desired(prev_node)
logger.info(f"Discord stream {stream_id} -> node {node['node_id']} token {res.token_id}"
f" ({'reused' if res.reused else 'new'} lease)")
f" ({'reused' if res.reused else 'new'} lease{', already on' if already_on else ''})")
return {**existing, **fields, "node_id": node["node_id"], "node_name": node.get("name"),
"token_id": res.token_id, "lease_id": res.lease_id, "reused": res.reused,
"delivered": delivered}
"delivered": delivered, "already_on": already_on}
if res.error in ("preferred_busy", "preferred_unavailable"):
raise await _lease_error(res) # true on every node — no point trying the rest
last = res
code = last.error if last and last.error else "no_node"
raise StreamError(code, ERROR_TEXT.get(code, code), invite=last.invite if last else [])
if last is None:
raise StreamError("no_node", ERROR_TEXT["no_node"])
raise await _lease_error(last)
async def stop_stream(stream_id: str, reason: str = "stopped") -> bool:
@@ -395,14 +516,19 @@ async def remove_guild(guild_id: str, by: str) -> dict:
"guild_name": link.get("guild_name")}
async def stop_guild(guild_id: str, reason: str, channel_id: Optional[str] = None) -> list[str]:
"""Stop every live stream in ONE Discord server (optionally one channel).
The /leave fix for server-26#203: never touches another server's streams."""
async def stop_guild(guild_id: str, reason: str, channel_id: Optional[str] = None,
stream_id: Optional[str] = None) -> list[str]:
"""Stop live streams in ONE Discord server: all of them, every stream in
one voice channel, or one stream by id. The /leave fix for server-26#203:
never touches another server's streams (a ``stream_id`` from another
server simply matches nothing)."""
stopped = []
for s in await fstore.collection_list(STREAMS, guild_id=str(guild_id)):
if s.get("desired") != "on":
continue
if channel_id and s.get("channel_id") != str(channel_id):
if channel_id and str(s.get("channel_id")) != str(channel_id):
continue
if stream_id and s.get("stream_id") != stream_id:
continue
await stop_stream(s["stream_id"], reason)
stopped.append(s["stream_id"])
@@ -456,8 +582,8 @@ async def handle_actual(node_id: str, payload: dict) -> None:
recheck: set = set()
if isinstance(reported, dict):
for stream_id, st in list(reported.items())[:50]:
if not isinstance(st, dict):
continue
if not isinstance(st, dict) or "/" in str(stream_id):
continue # "/" would be a Firestore sub-path, never a stream id
doc = await fstore.doc_get(STREAMS, str(stream_id))
if not doc or doc.get("node_id") != node_id or doc.get("desired") != "on":
continue
@@ -552,8 +678,8 @@ async def reconcile_once() -> None:
continue
org_id = s.get("org_id")
try:
await start_stream(org_id=org_id, guild_id=s["guild_id"], channel_id=s["channel_id"],
system_id=s["system_id"], mode="persistent")
await start_stream(org_id=org_id, stream_id=s["stream_id"], system_id=s["system_id"],
mode="persistent")
except StreamError as e:
await _mark(s, "error", e.message)