Skip to content

The agent that grows with you

fix(slack): release a failed handler's fresh ts claim so the turn isn't swallowed

Teknium39a5838f0708
diff --git a/plugins/platforms/slack/adapter.py b/plugins/platforms/slack/adapter.py
index 9451c910a72..cd848c4bc50 100644
--- a/plugins/platforms/slack/adapter.py
+++ b/plugins/platforms/slack/adapter.py
@@ -5946,6 +5946,35 @@ class SlackAdapter(BasePlatformAdapter):
async def _handle_slack_message(
self, event: dict, payload: Optional[dict] = None
+ ) -> None:
+ """Handle an incoming Slack message event.
+
+ Thin guard around :meth:`_handle_slack_message_impl`: the impl claims
+ the message ts on entry (before the slow enrichment awaits) so a link
+ unfurl arriving mid-flight can't become a second turn — but a claim
+ held by an invocation that then RAISES would permanently swallow the
+ message: neither a Slack retry nor a user edit could ever re-drive it.
+ So if this invocation newly claimed the ts and then failed, release
+ the claim before re-raising. A ts that was already claimed before we
+ started (the sequential-suppression case) is left untouched.
+ """
+ _ts = str((event or {}).get("ts") or "")
+ _was_claimed = bool(_ts) and _ts in self._processed_message_ts
+ try:
+ return await self._handle_slack_message_impl(event, payload)
+ except BaseException:
+ if _ts and not _was_claimed and _ts in self._processed_message_ts:
+ self._processed_message_ts.pop(_ts, None)
+ logger.warning(
+ "[%s] handler failed after claiming ts=%s; claim released "
+ "so a retry or edit can re-drive the turn",
+ self.name,
+ _ts,
+ )
+ raise
+
+ async def _handle_slack_message_impl(
+ self, event: dict, payload: Optional[dict] = None
) -> None:
"""Handle an incoming Slack message event."""
# DEBUG entry log — fires BEFORE any filtering so users debugging
diff --git a/tests/gateway/test_slack_unfurl_duplicate_turn.py b/tests/gateway/test_slack_unfurl_duplicate_turn.py
index cb2fc4a8458..22c6cbfe6b0 100644
--- a/tests/gateway/test_slack_unfurl_duplicate_turn.py
+++ b/tests/gateway/test_slack_unfurl_duplicate_turn.py
@@ -239,3 +239,67 @@ class TestClaimHelperBounded:
adapter = _make_adapter(delivered)
adapter._remember_processed_message_ts("")
assert adapter._processed_message_ts == {}
+
+
+class TestClaimReleasedOnFailure:
+ """A claim made by an invocation that raises must not swallow the turn.
+
+ Trade-off pinned here: the entry-claim closes the unfurl race, but if the
+ handler dies mid-enrichment while holding a fresh claim, a Slack retry or
+ a user edit must still be able to re-drive the message. The wrapper in
+ ``_handle_slack_message`` releases only claims taken by the failed
+ invocation itself.
+ """
+
+ def test_failed_first_copy_releases_claim_so_edit_can_redrive(self):
+ delivered = []
+ adapter = _make_adapter(delivered)
+ calls = {"n": 0}
+
+ async def _flaky_user_name_resolve(*a, **k):
+ calls["n"] += 1
+ if calls["n"] == 1:
+ raise RuntimeError("slack api died mid-enrichment")
+ return "richard"
+
+ adapter._resolve_user_name = _flaky_user_name_resolve
+
+ edit = _unfurl_event()
+ edit["message"]["text"] = "<@U0BCLP7DB7B> edited text"
+ edit["message"]["edited"] = {"user": USER, "ts": "1787365412.000000"}
+
+ async def scenario():
+ with pytest.raises(RuntimeError):
+ await adapter._handle_slack_message(_original_event(), _body())
+ # The failed invocation must not leave the ts claimed...
+ assert ORIGINAL_TS not in adapter._processed_message_ts
+ # ...so a user edit of the unanswered message still summons the bot.
+ await adapter._handle_slack_message(edit, _body())
+
+ asyncio.run(scenario())
+ assert len(delivered) == 1
+
+ def test_failure_does_not_release_a_preexisting_claim(self):
+ """Sequential suppression survives a later failing duplicate."""
+ delivered = []
+ adapter = _make_adapter(delivered)
+ adapter._resolve_user_name = AsyncMock(return_value="richard")
+
+ async def scenario():
+ await adapter._handle_slack_message(_original_event(), _body())
+ assert ORIGINAL_TS in adapter._processed_message_ts
+ # A later invocation for the same ts that fails must not strip
+ # the claim the successful turn already holds.
+ adapter._resolve_user_name = AsyncMock(
+ side_effect=RuntimeError("boom")
+ )
+ second = _original_event()
+ second["client_msg_id"] = "cmid-replay"
+ try:
+ await adapter._handle_slack_message(second, _body())
+ except RuntimeError:
+ pass
+ assert ORIGINAL_TS in adapter._processed_message_ts
+
+ asyncio.run(scenario())
+ assert len(delivered) == 1