Skip to content

The agent that grows with you

fix(discord): re-authorize durable role grants through the ingress gate resolver

unsupportedpastels68b3b6af2ead
diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.py
index f8c8cef0330..ba42d10b961 100644
--- a/plugins/platforms/discord/adapter.py
+++ b/plugins/platforms/discord/adapter.py
@@ -4998,12 +4998,12 @@ class DiscordAdapter(DiscordAuthorizationMixin, DiscordMediaMixin, DiscordThread
return snap[name] or default
return _scoped_gate_env(name, default)
- def _gate_raw(self, extra_key: str, env_key: str):
- """Resolve one gate value: env/snapshot first (legacy precedence), then extra."""
- val = self._gate_env(env_key)
+ def _gate_raw(self, extra_key: str, env_key: str, config=None):
+ """Resolve one gate value: env/snapshot first (legacy precedence), then extra; *config* = live re-read."""
+ val = self._gate_env(env_key) if config is None else _scoped_gate_env(env_key)
if val:
return val
- extra = getattr(getattr(self, "config", None), "extra", None)
+ extra = getattr(getattr(self, "config", None) if config is None else config, "extra", None)
if isinstance(extra, dict):
return extra.get(extra_key)
return None
diff --git a/plugins/platforms/discord/adapter_authorization.py b/plugins/platforms/discord/adapter_authorization.py
index 1c776973890..603076963ec 100644
--- a/plugins/platforms/discord/adapter_authorization.py
+++ b/plugins/platforms/discord/adapter_authorization.py
@@ -4,14 +4,15 @@ import asyncio
class DiscordAuthorizationMixin:
async def reauthorize_native_roles(self, source):
- from plugins.platforms.discord.adapter import _read_dm_role_auth_guild, _scoped_gate_env
- from hermes_cli.config import read_raw_config
+ from plugins.platforms.discord.adapter import _read_dm_role_auth_guild
+ from gateway.config import Platform, PlatformConfig, load_gateway_config
from discord import HTTPException
- # Unlike ingress caches, recovery reads current transport-scoped policy.
+ # Unlike ingress caches, recovery reads current transport-scoped policy, resolved like
+ # ingress: the same gate resolver over the Discord config the loader builds now.
def policy():
- cfg = (read_raw_config() or {}).get('discord', {}) or {}
- raw = _scoped_gate_env('DISCORD_ALLOWED_ROLES') or cfg.get('allowed_roles', [])
+ current = load_gateway_config().platforms.get(Platform.DISCORD) or PlatformConfig()
+ raw = self._gate_raw('allowed_roles', 'DISCORD_ALLOWED_ROLES', config=current)
roles = {int(str(item).strip()) for item in self._gate_csv_set(raw)
if str(item).strip().isdigit()}
guild_id = _read_dm_role_auth_guild() if source.chat_type == 'dm' else source.scope_id
diff --git a/tests/plugins/platforms/test_discord_reauthorize_allowed_roles.py b/tests/plugins/platforms/test_discord_reauthorize_allowed_roles.py
new file mode 100644
index 00000000000..500d79086a0
--- /dev/null
+++ b/tests/plugins/platforms/test_discord_reauthorize_allowed_roles.py
@@ -0,0 +1,96 @@
+"""Durable Discord re-authorization resolves ``allowed_roles`` like ingress, from current config (#139148).
+
+Ingress reads the gate through ``DiscordAdapter._gate_raw``: scoped env first, then the Discord
+``PlatformConfig.extra`` that ``load_gateway_config()`` builds from every YAML spelling. Recovery
+must apply that same resolution to the config as it stands now, so a revocation still takes effect.
+"""
+
+import asyncio
+import importlib
+import sys
+from types import ModuleType, SimpleNamespace
+
+import hermes_yaml as yaml
+import pytest
+
+from gateway.config import Platform, PlatformConfig
+from plugins.platforms.discord.adapter import DiscordAdapter
+
+GUILD_ID = 222222
+USER_ID = "42"
+SOURCE = SimpleNamespace(chat_type="group", scope_id=str(GUILD_ID), user_id=USER_ID)
+
+
[email protected](autouse=True)
+def _home(tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ # The default-profile YAML bridge seeds DISCORD_ALLOWED_ROLES first-writer-wins; undo it per test.
+ monkeypatch.delenv("DISCORD_ALLOWED_ROLES", raising=False)
+ if getattr(sys.modules.get("discord"), "HTTPException", None) is None:
+ try:
+ importlib.import_module("discord")
+ except ImportError:
+ stub = ModuleType("discord")
+ stub.HTTPException = type("HTTPException", (Exception,), {})
+ monkeypatch.setitem(sys.modules, "discord", stub)
+ return tmp_path
+
+
+def _write_config(home, config):
+ (home / "config.yaml").write_text(yaml.safe_dump(config), encoding="utf-8")
+
+
+def _adapter(role_id, connect_extra=None):
+ """An adapter whose guild member holds *role_id*; *connect_extra* is its connect-time extra."""
+ adapter = object.__new__(DiscordAdapter)
+ adapter.platform = Platform.DISCORD
+ adapter.config = PlatformConfig(enabled=True, extra=dict(connect_extra or {}))
+ adapter._gate_env_snapshot = None
+ member = SimpleNamespace(id=int(USER_ID), roles=[SimpleNamespace(id=role_id)])
+
+ async def fetch_member(_uid):
+ return member
+
+ guild = SimpleNamespace(id=GUILD_ID, fetch_member=fetch_member)
+ member.guild = guild
+ adapter._client = SimpleNamespace(get_guild=lambda gid: guild if gid == GUILD_ID else None)
+ return adapter
+
+
+def _reauthorize(adapter):
+ return asyncio.run(adapter.reauthorize_native_roles(SOURCE))
+
+
+def test_flat_platforms_allowed_roles_authorize(_home):
+ _write_config(_home, {"platforms": {"discord": {"enabled": True, "allowed_roles": ["2"]}}})
+ assert _reauthorize(_adapter(2)) is True
+
+
+def test_platforms_extra_allowed_roles_authorize(_home):
+ _write_config(_home, {"platforms": {"discord": {"enabled": True, "extra": {"allowed_roles": ["2"]}}}})
+ assert _reauthorize(_adapter(2)) is True
+
+
[email protected]("role_id, expected", [(2, True), (3, False)])
+def test_platforms_extra_beats_top_level_block_like_ingress(_home, role_id, expected):
+ _write_config(_home, {
+ "discord": {"allowed_roles": ["3"]},
+ "platforms": {"discord": {"enabled": True, "extra": {"allowed_roles": ["2"]}}},
+ })
+ assert _reauthorize(_adapter(role_id)) is expected
+
+
+def test_legacy_top_level_allowed_roles_still_authorize(_home):
+ _write_config(_home, {"discord": {"allowed_roles": ["2"]}, "platforms": {"discord": {"enabled": True}}})
+ assert _reauthorize(_adapter(2)) is True
+
+
+def test_revocation_in_current_config_beats_connect_time_extra(_home):
+ _write_config(_home, {"platforms": {"discord": {"enabled": True, "allowed_roles": ["9"]}}})
+ assert _reauthorize(_adapter(2, connect_extra={"allowed_roles": "2"})) is False
+
+
+def test_scoped_env_allowlist_wins_over_config(_home, monkeypatch):
+ _write_config(_home, {"platforms": {"discord": {"enabled": True, "allowed_roles": ["2"]}}})
+ monkeypatch.setenv("DISCORD_ALLOWED_ROLES", "9")
+ assert _reauthorize(_adapter(2)) is False