From 08af8015e00cf865aa486c7c6b7834ff11da219c Mon Sep 17 00:00:00 2001 From: Eden Zimbelman Date: Mon, 30 Nov 2026 16:09:25 -0800 Subject: [PATCH 1/2] fix: respond to file shares and attachment-only messages in code channels Co-Authored-By: Claude --- .../listeners/events/app_mentioned.py | 4 ++++ claude-agent-sdk/listeners/events/message.py | 8 ++++++-- claude-agent-sdk/tests/test_app_mentioned.py | 12 ++++++++++++ claude-agent-sdk/tests/test_message.py | 19 +++++++++++++++++++ .../listeners/events/app_mentioned.py | 4 ++++ openai-agents-sdk/listeners/events/message.py | 8 ++++++-- openai-agents-sdk/tests/test_app_mentioned.py | 12 ++++++++++++ openai-agents-sdk/tests/test_message.py | 19 +++++++++++++++++++ pydantic-ai/listeners/events/app_mentioned.py | 4 ++++ pydantic-ai/listeners/events/message.py | 8 ++++++-- pydantic-ai/tests/test_app_mentioned.py | 12 ++++++++++++ pydantic-ai/tests/test_message.py | 19 +++++++++++++++++++ 12 files changed, 123 insertions(+), 6 deletions(-) diff --git a/claude-agent-sdk/listeners/events/app_mentioned.py b/claude-agent-sdk/listeners/events/app_mentioned.py index 9a497e4..48b8295 100644 --- a/claude-agent-sdk/listeners/events/app_mentioned.py +++ b/claude-agent-sdk/listeners/events/app_mentioned.py @@ -15,7 +15,11 @@ async def handle_app_mentioned( seeds into a channel the agent just created, which starts its first turn. """ channel_id = context.channel_id + files = event.get("files", []) text = event.get("text", "").replace(f"<@{context.bot_user_id}>", "").strip() + if files: + names = ", ".join(file.get("name") or file["id"] for file in files) + text = f"{text}\n(attachment: {names})".strip() session = await find_session(client, channel_id) if session is not None: diff --git a/claude-agent-sdk/listeners/events/message.py b/claude-agent-sdk/listeners/events/message.py index ab3abc1..f3f7618 100644 --- a/claude-agent-sdk/listeners/events/message.py +++ b/claude-agent-sdk/listeners/events/message.py @@ -13,11 +13,15 @@ async def handle_message( for bigger work. In a code channel the people in it are talking to the agent directly, so every message is a turn. Messages anywhere else are left alone. """ - # Ignore edits, joins, or bot posts. - if event.get("subtype") or event.get("bot_id"): + # Ignore edits, joins, or bot posts, but not shared files. + if event.get("subtype") not in (None, "file_share") or event.get("bot_id"): return mention = f"<@{context.bot_user_id}>" + files = event.get("files", []) text = event.get("text", "").strip() + if files: + names = ", ".join(file.get("name") or file["id"] for file in files) + text = f"{text}\n(attachment: {names})".strip() channel_id = context.channel_id if event.get("channel_type") == "im": diff --git a/claude-agent-sdk/tests/test_app_mentioned.py b/claude-agent-sdk/tests/test_app_mentioned.py index c8b2746..d11711a 100644 --- a/claude-agent-sdk/tests/test_app_mentioned.py +++ b/claude-agent-sdk/tests/test_app_mentioned.py @@ -112,6 +112,18 @@ async def test_empty_mention_explains_itself(self, client, context): assert "Mention me" in client.chat_postMessage.call_args.kwargs["text"] + async def test_mention_with_only_a_file_names_it(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.mention( + client, context, "", files=[{"id": "F123", "name": "trace.png"}] + ) + + assert start_run.call_args.args[1] == "<@U123>: (attachment: trace.png)" + async def test_failed_turn_in_a_code_channel_warns_top_level(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) diff --git a/claude-agent-sdk/tests/test_message.py b/claude-agent-sdk/tests/test_message.py index ec3fef9..9d85bcb 100644 --- a/claude-agent-sdk/tests/test_message.py +++ b/claude-agent-sdk/tests/test_message.py @@ -38,6 +38,25 @@ async def test_message_with_a_mention_is_left_to_app_mention(self, client, conte start_run.assert_not_called() + async def test_file_shared_in_a_code_channel_runs_a_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.send( + client, + context, + "why does this fail?", + subtype="file_share", + files=[{"id": "F123", "name": "trace.png"}], + ) + + assert ( + start_run.call_args.args[1] + == "<@U123>: why does this fail?\n(attachment: trace.png)" + ) + async def test_messages_during_a_turn_run_together_next(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) texts = [] diff --git a/openai-agents-sdk/listeners/events/app_mentioned.py b/openai-agents-sdk/listeners/events/app_mentioned.py index 9a497e4..48b8295 100644 --- a/openai-agents-sdk/listeners/events/app_mentioned.py +++ b/openai-agents-sdk/listeners/events/app_mentioned.py @@ -15,7 +15,11 @@ async def handle_app_mentioned( seeds into a channel the agent just created, which starts its first turn. """ channel_id = context.channel_id + files = event.get("files", []) text = event.get("text", "").replace(f"<@{context.bot_user_id}>", "").strip() + if files: + names = ", ".join(file.get("name") or file["id"] for file in files) + text = f"{text}\n(attachment: {names})".strip() session = await find_session(client, channel_id) if session is not None: diff --git a/openai-agents-sdk/listeners/events/message.py b/openai-agents-sdk/listeners/events/message.py index ab3abc1..f3f7618 100644 --- a/openai-agents-sdk/listeners/events/message.py +++ b/openai-agents-sdk/listeners/events/message.py @@ -13,11 +13,15 @@ async def handle_message( for bigger work. In a code channel the people in it are talking to the agent directly, so every message is a turn. Messages anywhere else are left alone. """ - # Ignore edits, joins, or bot posts. - if event.get("subtype") or event.get("bot_id"): + # Ignore edits, joins, or bot posts, but not shared files. + if event.get("subtype") not in (None, "file_share") or event.get("bot_id"): return mention = f"<@{context.bot_user_id}>" + files = event.get("files", []) text = event.get("text", "").strip() + if files: + names = ", ".join(file.get("name") or file["id"] for file in files) + text = f"{text}\n(attachment: {names})".strip() channel_id = context.channel_id if event.get("channel_type") == "im": diff --git a/openai-agents-sdk/tests/test_app_mentioned.py b/openai-agents-sdk/tests/test_app_mentioned.py index c8b2746..d11711a 100644 --- a/openai-agents-sdk/tests/test_app_mentioned.py +++ b/openai-agents-sdk/tests/test_app_mentioned.py @@ -112,6 +112,18 @@ async def test_empty_mention_explains_itself(self, client, context): assert "Mention me" in client.chat_postMessage.call_args.kwargs["text"] + async def test_mention_with_only_a_file_names_it(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.mention( + client, context, "", files=[{"id": "F123", "name": "trace.png"}] + ) + + assert start_run.call_args.args[1] == "<@U123>: (attachment: trace.png)" + async def test_failed_turn_in_a_code_channel_warns_top_level(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) diff --git a/openai-agents-sdk/tests/test_message.py b/openai-agents-sdk/tests/test_message.py index ec3fef9..9d85bcb 100644 --- a/openai-agents-sdk/tests/test_message.py +++ b/openai-agents-sdk/tests/test_message.py @@ -38,6 +38,25 @@ async def test_message_with_a_mention_is_left_to_app_mention(self, client, conte start_run.assert_not_called() + async def test_file_shared_in_a_code_channel_runs_a_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.send( + client, + context, + "why does this fail?", + subtype="file_share", + files=[{"id": "F123", "name": "trace.png"}], + ) + + assert ( + start_run.call_args.args[1] + == "<@U123>: why does this fail?\n(attachment: trace.png)" + ) + async def test_messages_during_a_turn_run_together_next(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) texts = [] diff --git a/pydantic-ai/listeners/events/app_mentioned.py b/pydantic-ai/listeners/events/app_mentioned.py index 9a497e4..48b8295 100644 --- a/pydantic-ai/listeners/events/app_mentioned.py +++ b/pydantic-ai/listeners/events/app_mentioned.py @@ -15,7 +15,11 @@ async def handle_app_mentioned( seeds into a channel the agent just created, which starts its first turn. """ channel_id = context.channel_id + files = event.get("files", []) text = event.get("text", "").replace(f"<@{context.bot_user_id}>", "").strip() + if files: + names = ", ".join(file.get("name") or file["id"] for file in files) + text = f"{text}\n(attachment: {names})".strip() session = await find_session(client, channel_id) if session is not None: diff --git a/pydantic-ai/listeners/events/message.py b/pydantic-ai/listeners/events/message.py index ab3abc1..f3f7618 100644 --- a/pydantic-ai/listeners/events/message.py +++ b/pydantic-ai/listeners/events/message.py @@ -13,11 +13,15 @@ async def handle_message( for bigger work. In a code channel the people in it are talking to the agent directly, so every message is a turn. Messages anywhere else are left alone. """ - # Ignore edits, joins, or bot posts. - if event.get("subtype") or event.get("bot_id"): + # Ignore edits, joins, or bot posts, but not shared files. + if event.get("subtype") not in (None, "file_share") or event.get("bot_id"): return mention = f"<@{context.bot_user_id}>" + files = event.get("files", []) text = event.get("text", "").strip() + if files: + names = ", ".join(file.get("name") or file["id"] for file in files) + text = f"{text}\n(attachment: {names})".strip() channel_id = context.channel_id if event.get("channel_type") == "im": diff --git a/pydantic-ai/tests/test_app_mentioned.py b/pydantic-ai/tests/test_app_mentioned.py index c8b2746..d11711a 100644 --- a/pydantic-ai/tests/test_app_mentioned.py +++ b/pydantic-ai/tests/test_app_mentioned.py @@ -112,6 +112,18 @@ async def test_empty_mention_explains_itself(self, client, context): assert "Mention me" in client.chat_postMessage.call_args.kwargs["text"] + async def test_mention_with_only_a_file_names_it(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.mention( + client, context, "", files=[{"id": "F123", "name": "trace.png"}] + ) + + assert start_run.call_args.args[1] == "<@U123>: (attachment: trace.png)" + async def test_failed_turn_in_a_code_channel_warns_top_level(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) diff --git a/pydantic-ai/tests/test_message.py b/pydantic-ai/tests/test_message.py index ec3fef9..9d85bcb 100644 --- a/pydantic-ai/tests/test_message.py +++ b/pydantic-ai/tests/test_message.py @@ -38,6 +38,25 @@ async def test_message_with_a_mention_is_left_to_app_mention(self, client, conte start_run.assert_not_called() + async def test_file_shared_in_a_code_channel_runs_a_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.send( + client, + context, + "why does this fail?", + subtype="file_share", + files=[{"id": "F123", "name": "trace.png"}], + ) + + assert ( + start_run.call_args.args[1] + == "<@U123>: why does this fail?\n(attachment: trace.png)" + ) + async def test_messages_during_a_turn_run_together_next(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) texts = [] From 39cb8e0032ddaca4c7892c8f0f0585cbaa13f6da Mon Sep 17 00:00:00 2001 From: Eden Zimbelman Date: Mon, 30 Nov 2026 16:26:36 -0800 Subject: [PATCH 2/2] test: cover file shares without text, edits and app file shares Co-authored-by: Cheng Chen <35309699+cchensh@users.noreply.github.com> Co-Authored-By: Claude --- claude-agent-sdk/tests/test_message.py | 39 +++++++++++++++++++++++++ openai-agents-sdk/tests/test_message.py | 39 +++++++++++++++++++++++++ pydantic-ai/tests/test_message.py | 39 +++++++++++++++++++++++++ 3 files changed, 117 insertions(+) diff --git a/claude-agent-sdk/tests/test_message.py b/claude-agent-sdk/tests/test_message.py index 9d85bcb..db2fdd9 100644 --- a/claude-agent-sdk/tests/test_message.py +++ b/claude-agent-sdk/tests/test_message.py @@ -57,6 +57,45 @@ async def test_file_shared_in_a_code_channel_runs_a_turn(self, client, context): == "<@U123>: why does this fail?\n(attachment: trace.png)" ) + async def test_file_shared_without_text_names_it(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.send( + client, + context, + "", + subtype="file_share", + files=[{"id": "F123", "name": "trace.png"}], + ) + + assert start_run.call_args.args[1] == "<@U123>: (attachment: trace.png)" + + async def test_edited_message_runs_no_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch("agent.turns.start_run") as start_run: + await self.send(client, context, "keep going", subtype="message_changed") + + start_run.assert_not_called() + + async def test_file_shared_by_an_app_runs_no_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch("agent.turns.start_run") as start_run: + await self.send( + client, + context, + "", + subtype="file_share", + bot_id="B123", + files=[{"id": "F123", "name": "trace.png"}], + ) + + start_run.assert_not_called() + async def test_messages_during_a_turn_run_together_next(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) texts = [] diff --git a/openai-agents-sdk/tests/test_message.py b/openai-agents-sdk/tests/test_message.py index 9d85bcb..db2fdd9 100644 --- a/openai-agents-sdk/tests/test_message.py +++ b/openai-agents-sdk/tests/test_message.py @@ -57,6 +57,45 @@ async def test_file_shared_in_a_code_channel_runs_a_turn(self, client, context): == "<@U123>: why does this fail?\n(attachment: trace.png)" ) + async def test_file_shared_without_text_names_it(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.send( + client, + context, + "", + subtype="file_share", + files=[{"id": "F123", "name": "trace.png"}], + ) + + assert start_run.call_args.args[1] == "<@U123>: (attachment: trace.png)" + + async def test_edited_message_runs_no_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch("agent.turns.start_run") as start_run: + await self.send(client, context, "keep going", subtype="message_changed") + + start_run.assert_not_called() + + async def test_file_shared_by_an_app_runs_no_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch("agent.turns.start_run") as start_run: + await self.send( + client, + context, + "", + subtype="file_share", + bot_id="B123", + files=[{"id": "F123", "name": "trace.png"}], + ) + + start_run.assert_not_called() + async def test_messages_during_a_turn_run_together_next(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) texts = [] diff --git a/pydantic-ai/tests/test_message.py b/pydantic-ai/tests/test_message.py index 9d85bcb..db2fdd9 100644 --- a/pydantic-ai/tests/test_message.py +++ b/pydantic-ai/tests/test_message.py @@ -57,6 +57,45 @@ async def test_file_shared_in_a_code_channel_runs_a_turn(self, client, context): == "<@U123>: why does this fail?\n(attachment: trace.png)" ) + async def test_file_shared_without_text_names_it(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.send( + client, + context, + "", + subtype="file_share", + files=[{"id": "F123", "name": "trace.png"}], + ) + + assert start_run.call_args.args[1] == "<@U123>: (attachment: trace.png)" + + async def test_edited_message_runs_no_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch("agent.turns.start_run") as start_run: + await self.send(client, context, "keep going", subtype="message_changed") + + start_run.assert_not_called() + + async def test_file_shared_by_an_app_runs_no_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch("agent.turns.start_run") as start_run: + await self.send( + client, + context, + "", + subtype="file_share", + bot_id="B123", + files=[{"id": "F123", "name": "trace.png"}], + ) + + start_run.assert_not_called() + async def test_messages_during_a_turn_run_together_next(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) texts = []