Skip to content

The agent that grows with you

fix: an answered Discord approval whose click edit fails still retires its card

kshitijk4pooraecf04f3d8a7
diff --git a/plugins/platforms/discord/adapter_exec_approval_view.py b/plugins/platforms/discord/adapter_exec_approval_view.py
index aa3af1c00c4..4c4ae829653 100644
--- a/plugins/platforms/discord/adapter_exec_approval_view.py
+++ b/plugins/platforms/discord/adapter_exec_approval_view.py
@@ -135,9 +135,17 @@ def build_exec_approval_view(discord, base, *, unauthorized):
return
color = discord.Color.dark_grey()
label = t("platform.discord.approval.expired")
- await self._finalize_embed(
- interaction, color,
- t("platform.discord.approval.by_user", label=label, user=interaction.user.display_name) if count else label)
+ try:
+ await self._finalize_embed(
+ interaction, color,
+ t("platform.discord.approval.by_user", label=label, user=interaction.user.display_name) if count else label)
+ except Exception:
+ if count:
+ # The answer is recorded but this click's edit failed, and the observer skips cards
+ # whose click owns the edit: stamp the card through its message instead.
+ self._backend_settled = False
+ await self.retire("resolved")
+ raise
# Decorator labels are placeholders; ``_localize_buttons`` in __init__ sets the real text.
@discord.ui.button(label="Allow Once", style=discord.ButtonStyle.green)
diff --git a/tests/gateway/test_discord_approval_lifecycle.py b/tests/gateway/test_discord_approval_lifecycle.py
index 1b3b554268b..65931cc5b02 100644
--- a/tests/gateway/test_discord_approval_lifecycle.py
+++ b/tests/gateway/test_discord_approval_lifecycle.py
@@ -57,8 +57,44 @@ async def test_click_stops_view_even_if_interaction_edit_fails(monkeypatch, edit
await asyncio.sleep(0)
await asyncio.gather(*adapter._background_tasks)
assert view.is_finished()
- assert message.embeds[0].footer == footer
- message.edit.assert_not_awaited()
+ if edit_fails:
+ # The recorded answer must not leave a pending-looking card: it is stamped through the message.
+ message.edit.assert_awaited_once()
+ assert message.edit.await_args.kwargs["view"] is None
+ footer = message.embeds[0].footer
+ assert (footer["text"] if isinstance(footer, dict) else footer.text) == t(
+ "platform.discord.approval.already_resolved")
+ else:
+ assert message.embeds[0].footer == footer
+ message.edit.assert_not_awaited()
+
+
+async def test_observer_that_fires_before_a_failed_click_edit_still_sees_the_card_retired(monkeypatch):
+ """The waiter usually wakes before Discord rejects the edit, so the observer runs while the click
+ still owns the card and skips it; the click itself must then retire the card."""
+ adapter, view, entry, message = await _pending_card(monkeypatch)
+ monkeypatch.setattr(view, "_gate", AsyncMock(return_value=True))
+
+ async def edit_message(**_kwargs):
+ entry.observers[0]("resolved")
+ await asyncio.sleep(0)
+ await asyncio.gather(*adapter._background_tasks)
+ message.edit.assert_not_awaited() # the observer skipped: the click owned the edit
+ raise RuntimeError("Discord unavailable")
+
+ interaction = SimpleNamespace(
+ message=message, user=SimpleNamespace(display_name="tester"),
+ response=SimpleNamespace(edit_message=edit_message))
+
+ with pytest.raises(RuntimeError, match="Discord unavailable"):
+ await view._resolve(interaction, "once", 2, "platform.discord.approval.resolved_once")
+
+ message.edit.assert_awaited_once()
+ assert message.edit.await_args.kwargs["view"] is None
+ footer = message.embeds[0].footer
+ assert (footer["text"] if isinstance(footer, dict) else footer.text) == t(
+ "platform.discord.approval.already_resolved")
@pytest.mark.asyncio