Skip to content

The agent that grows with you

fix(slack): bound nested attachment block text across the whole unfurl array

SilverNine709aaf97643c
diff --git a/plugins/platforms/slack/adapter.py b/plugins/platforms/slack/adapter.py
index beaaac13177..01a32c4dc32 100644
--- a/plugins/platforms/slack/adapter.py
+++ b/plugins/platforms/slack/adapter.py
@@ -541,6 +541,11 @@ def _extract_text_from_slack_blocks(blocks: list) -> str:
#: with whatever was pasted. 20k chars comfortably covers real tables while
#: staying well under Slack's own 40k message ceiling.
_SLACK_TABLE_MAX_CHARS = 20_000
+#: One ceiling for every ``attachments[].blocks[]`` projection in a message. Slack allows 20
+#: attachments each with its own blocks, so a per-attachment cap alone still grows 20x; the
+#: top-level ``blocks`` path is capped once (``_serialize_slack_blocks_for_agent``) and this
+#: keeps the unfurl path in the same order of magnitude.
+_SLACK_UNFURL_BLOCKS_MAX_CHARS = 6000
def _collect_slack_table_cell_text(value: Any) -> str:
@@ -4122,6 +4127,7 @@ class SlackAdapter(BasePlatformAdapter):
own content and is skipped. Dedup matches the rendered section, not the bare URL (which is
usually already in the user's text while the preview body is not)."""
att_parts: list[str] = []
+ blocks_budget = _SLACK_UNFURL_BLOCKS_MAX_CHARS
for att in slack_attachments:
att_title = att.get("title", "")
att_url = att.get("title_link", "") or att.get("from_url", "")
@@ -4139,8 +4145,15 @@ class SlackAdapter(BasePlatformAdapter):
body = body[:497] + "..."
# Pasted tables arrive as ``table`` blocks in ``attachments[].blocks[]``, absent from
# ``text``/``fallback``/files; without this the agent sees only the sentence before them.
- nested_text = _extract_text_from_slack_blocks(att.get("blocks") or [])
+ # The budget is shared across the whole array: a 20-attachment alert must not project
+ # 20x what a single one does, and a spent budget still leaves the header visible.
+ nested_text = ""
+ if blocks_budget > 0:
+ nested_text = _extract_text_from_slack_blocks(att.get("blocks") or [])
+ if len(nested_text) > blocks_budget:
+ nested_text = nested_text[:blocks_budget].rstrip() + "\n... [truncated]"
if nested_text and nested_text not in body:
+ blocks_budget -= len(nested_text)
body = f"{body}\n{nested_text}".strip() if body else nested_text
if header:
section = f"{header}\n {body}" if body else header
diff --git a/tests/gateway/test_slack_unfurl_nested_blocks_budget.py b/tests/gateway/test_slack_unfurl_nested_blocks_budget.py
new file mode 100644
index 00000000000..2bb9eb1ed38
--- /dev/null
+++ b/tests/gateway/test_slack_unfurl_nested_blocks_budget.py
@@ -0,0 +1,38 @@
+"""Nested ``attachments[].blocks[]`` text in link unfurls shares one bounded budget.
+
+The live inbound path (``_append_link_unfurls``) renders each attachment's Block Kit
+``blocks`` into the message the agent reads. Slack allows up to 20 attachments per message,
+each carrying its own blocks, so without a shared ceiling the projection grows linearly with
+attachment count while the top-level ``blocks`` path is capped once.
+"""
+
+from plugins.platforms.slack.adapter import SlackAdapter
+
+
+def _rich_blocks(sections: int = 8, size: int = 400) -> list:
+ return [{"type": "rich_text", "elements": [
+ {"type": "rich_text_section", "elements": [{"type": "text", "text": "x" * size}]}
+ for _ in range(sections)]}]
+
+
+def _attachment(i: int) -> dict:
+ return {"title": f"Alert {i}", "title_link": "https://example.com/a", "blocks": _rich_blocks()}
+
+
+def test_nested_block_text_stops_growing_with_attachment_count():
+ sizes = {n: len(SlackAdapter._append_link_unfurls("intro", [_attachment(i) for i in range(n)]))
+ for n in (1, 5, 20)}
+ # Unbounded, 20 attachments cost 20x one (each ~3.2K of block text). Bounded, going from
+ # 5 to 20 attachments adds only their headers, not another 15 bodies of block text.
+ per_attachment_growth = (sizes[20] - sizes[5]) / 15
+ assert per_attachment_growth < 200, sizes
+ assert sizes[20] < 3 * sizes[1], sizes
+ assert "[truncated]" in SlackAdapter._append_link_unfurls("intro", [_attachment(i) for i in range(20)])
+
+
+def test_first_attachment_keeps_its_body_and_headers_survive_exhaustion():
+ out = SlackAdapter._append_link_unfurls("intro", [_attachment(i) for i in range(20)])
+ # The common single-alert shape is unchanged: attachment 0's block text is fully present.
+ assert "x" * 400 in out
+ # Later attachments still announce themselves even when the block budget is spent.
+ assert "📎 [Alert 19](https://example.com/a)" in out