Skip to content

Commit 5a48bd7

Browse files
John-Linclaude
andcommitted
Fix DM session key to use channel ID instead of message ts
Each DM message had a unique ts, so consecutive messages in a DM created separate sessions — the bot had no memory between turns. DMs now use channel ID as the session key so the full DM conversation shares one history. Thread sessions are unchanged (still keyed by thread_ts). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 5cc81ce commit 5a48bd7

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

bot/slack.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,13 @@ async def _process_message(self, event, say):
101101
display_name = await self._get_display_name(user_id)
102102
user_text = f"[{display_name}] {user_text}"
103103

104+
# DMs use the channel ID as session key so all messages share one conversation.
105+
# Threads use thread_ts so each thread has its own isolated conversation.
104106
thread_ts = event.get("thread_ts", event.get("ts"))
107+
session_key = event["channel"] if event.get("channel_type") == "im" else thread_ts
105108

106109
try:
107-
asst_text = await self.agent.run(thread_ts, user_text)
110+
asst_text = await self.agent.run(session_key, user_text)
108111
mrkdwn_text = markdown_to_slack_mrkdwn(str(asst_text))
109112
try:
110113
await say(text=mrkdwn_text, channel=channel, thread_ts=thread_ts)

tests/test_slack.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ async def test_mention_strips_bot_mention(self, bot):
110110

111111
class TestHandleMessage:
112112
@pytest.mark.anyio
113-
async def test_dm_calls_agent(self, bot):
113+
async def test_dm_uses_channel_as_session_key(self, bot):
114+
"""DM messages must use channel ID as session key so consecutive messages share history."""
114115
message = {
115116
"channel": "D001",
116117
"channel_type": "im",
@@ -123,7 +124,23 @@ async def test_dm_calls_agent(self, bot):
123124

124125
await bot.handle_message(message, say, ack)
125126

126-
bot.agent.run.assert_called_once_with("1234567890.123456", "[Alice] hello")
127+
bot.agent.run.assert_called_once_with("D001", "[Alice] hello")
128+
129+
@pytest.mark.anyio
130+
async def test_dm_consecutive_messages_share_session(self, bot):
131+
"""Two consecutive DM messages must use the same session key."""
132+
say = AsyncMock()
133+
ack = AsyncMock()
134+
135+
for ts in ("1111.111", "2222.222"):
136+
await bot.handle_message(
137+
{"channel": "D001", "channel_type": "im", "user": "U123", "text": "msg", "ts": ts},
138+
say,
139+
ack,
140+
)
141+
142+
keys = [call.args[0] for call in bot.agent.run.call_args_list]
143+
assert keys[0] == keys[1] == "D001"
127144

128145
@pytest.mark.anyio
129146
async def test_non_dm_is_ignored(self, bot):

0 commit comments

Comments
 (0)