Skip to content

Commit a84239f

Browse files
authored
fix: keep a separate agent session for each person in a thread (#319)
* fix: reply once to a thread reply that mentions the agent A thread reply that @-mentions the agent fires both app_mention and message, so the agent ran twice and posted two replies. The message listener now leaves those replies to app_mention. * fix: keep a separate agent session for each person in a thread Sessions were keyed by channel and thread, so anyone replying in a channel thread resumed the session of the person who started it, including results Casey fetched with that person's user token. Sessions are now keyed by channel, thread, and user. Casey auto-replies in a thread only to people with their own session, and others start one with an @mention. * test: check that a DM mentioning the agent still runs it * refactor: name the own session check and test session keys for each person
1 parent 1a5d7ad commit a84239f

22 files changed

Lines changed: 482 additions & 56 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Once Casey is running, there are several ways to interact:
8282

8383
* **App Home** — Open Casey in Slack and click the _Home_ tab. Choose from common issue categories, describe your issue in the modal, and Casey starts a DM thread with a resolution.
8484
* **Direct Messages** — Open a DM with Casey. Pick a suggested prompt or describe your issue, and Casey will reply in a thread.
85-
* **Channel @mentions** — Invite Casey to a channel by typing `/invite @Casey`, then type `@Casey` followed by your issue. Casey responds in a thread so the channel stays clean.
85+
* **Channel @mentions** — Invite Casey to a channel by typing `/invite @Casey`, then type `@Casey` followed by your issue. Casey responds in a thread so the channel stays clean. Casey replies to follow-ups from the person who mentioned it and anyone else can mention `@Casey` to start their own conversation.
8686
* **Assistant Panel** — Click _Add Agent_ in the top-right corner of Slack, select Casey, and choose a suggested prompt like _Reset Password_, _Request Access_, or _Network Issues_.
8787

8888
For full setup instructions, pick a framework above and follow the README in that directory.

‎claude-agent-sdk/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ Once Casey is running, there are several ways to interact:
286286

287287
**Direct Messages** — Open a DM with Casey. You'll see suggested prompts like _Reset Password_, _Request Access_, and _Network Issues_ — pick one or describe your own issue. Casey will react with :eyes: while processing, then reply in a thread. Send follow-up messages in the same thread and Casey will maintain the full conversation context.
288288

289-
**Channel @mentions** — Invite Casey to a channel by typing `/invite @Casey` in the message box, then type `@Casey` followed by your issue. Casey responds in a thread so the channel stays clean.
289+
**Channel @mentions** — Invite Casey to a channel by typing `/invite @Casey` in the message box, then type `@Casey` followed by your issue. Casey responds in a thread so the channel stays clean. In that thread, Casey replies to follow-up messages from the person who mentioned it. Anyone else in the thread can mention `@Casey` to start their own conversation and Casey keeps each person's conversation separate.
290290

291291
**Assistant Panel** — Click _Add Agent_ in the top-right corner of Slack, select Casey from the list, then pick a suggested prompt or type a message.
292292

@@ -352,7 +352,7 @@ The `tools` directory contains five IT support tools defined using the `@tool` d
352352

353353
### `/thread_context`
354354

355-
The `store.py` file implements a thread-safe in-memory session ID store, keyed by channel and thread. The Claude Agent SDK manages conversation history server-side via sessions, so only session IDs need to be tracked locally for resuming conversations.
355+
The `store.py` file implements a thread-safe in-memory session ID store, keyed by channel, thread, and user, so each person in a thread has their own conversation. The Claude Agent SDK manages conversation history server-side via sessions, so only session IDs need to be tracked locally for resuming conversations.
356356

357357
## Troubleshooting
358358

‎claude-agent-sdk/listeners/events/app_mentioned.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ async def handle_app_mentioned(
5858
)
5959

6060
# Get session ID for conversation context
61-
existing_session_id = session_store.get_session(channel_id, thread_ts)
61+
existing_session_id = session_store.get_session(
62+
channel_id, thread_ts, context.user_id
63+
)
6264

6365
# Run the agent with deps for tool access
6466
deps = CaseyDeps(
@@ -81,7 +83,9 @@ async def handle_app_mentioned(
8183

8284
# Store session ID for future context
8385
if new_session_id:
84-
session_store.set_session(channel_id, thread_ts, new_session_id)
86+
session_store.set_session(
87+
channel_id, thread_ts, context.user_id, new_session_id
88+
)
8589

8690
except Exception as e:
8791
logger.exception("Failed to handle app mention")

‎claude-agent-sdk/listeners/events/message.py‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ async def handle_message(
4242
elif is_thread_reply:
4343
if f"<@{context.bot_user_id}>" in event.get("text", ""):
4444
return
45-
# Channel thread replies are handled only if the bot is already engaged
46-
session = session_store.get_session(context.channel_id, event["thread_ts"])
47-
if session is None:
45+
own_session = session_store.get_session(
46+
context.channel_id, event["thread_ts"], context.user_id
47+
)
48+
if own_session is None:
4849
return
4950
else:
5051
# Top-level channel messages are handled by app_mentioned
@@ -55,8 +56,15 @@ async def handle_message(
5556
text = event.get("text", "")
5657
thread_ts = event.get("thread_ts") or event["ts"]
5758

59+
# For issue submissions the bot posted the message, so the real
60+
# user_id comes from the metadata rather than the event context.
61+
if is_issue_submission:
62+
user_id = event["metadata"]["event_payload"]["user_id"]
63+
else:
64+
user_id = context.user_id
65+
5866
# Get session ID for conversation context
59-
existing_session_id = session_store.get_session(channel_id, thread_ts)
67+
existing_session_id = session_store.get_session(channel_id, thread_ts, user_id)
6068

6169
# Add eyes reaction only to the first message (DMs only — channel
6270
# threads already have the reaction from the initial app_mention)
@@ -79,13 +87,6 @@ async def handle_message(
7987
],
8088
)
8189

82-
# For issue submissions the bot posted the message, so the real
83-
# user_id comes from the metadata rather than the event context.
84-
if is_issue_submission:
85-
user_id = event["metadata"]["event_payload"]["user_id"]
86-
else:
87-
user_id = context.user_id
88-
8990
# Run the agent with deps for tool access
9091
deps = CaseyDeps(
9192
client=client,
@@ -107,7 +108,7 @@ async def handle_message(
107108

108109
# Store session ID for future context
109110
if new_session_id:
110-
session_store.set_session(channel_id, thread_ts, new_session_id)
111+
session_store.set_session(channel_id, thread_ts, user_id, new_session_id)
111112

112113
except Exception as e:
113114
logger.exception("Failed to handle message")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import logging
2+
from unittest.mock import AsyncMock, Mock, patch
3+
4+
import pytest
5+
from slack_bolt.context.async_context import AsyncBoltContext
6+
from slack_sdk.web.async_client import AsyncWebClient
7+
8+
from listeners.events.app_mentioned import handle_app_mentioned
9+
from thread_context import session_store
10+
11+
test_logger = logging.getLogger(__name__)
12+
13+
14+
class TestAppMentioned:
15+
def setup_method(self):
16+
session_store.set_session("C234", "1.0", "U123", "session-1")
17+
self.fake_client = Mock(AsyncWebClient)
18+
self.fake_client.reactions_add = AsyncMock()
19+
self.fake_context = Mock(AsyncBoltContext)
20+
self.fake_context.bot_user_id = "UBOT"
21+
self.fake_context.channel_id = "C234"
22+
self.fake_context.user_id = "U456"
23+
24+
@pytest.mark.asyncio
25+
async def test_starts_a_separate_session_for_a_second_person_in_the_thread(self):
26+
with patch(
27+
"listeners.events.app_mentioned.run_casey_agent",
28+
AsyncMock(return_value=("Hi", "session-2")),
29+
) as fake_run_agent:
30+
await handle_app_mentioned(
31+
client=self.fake_client,
32+
context=self.fake_context,
33+
event={
34+
"channel": "C234",
35+
"text": "<@UBOT> what did you find?",
36+
"thread_ts": "1.0",
37+
"ts": "3.0",
38+
"user": "U456",
39+
},
40+
logger=test_logger,
41+
say=AsyncMock(),
42+
say_stream=AsyncMock(
43+
return_value=Mock(append=AsyncMock(), stop=AsyncMock())
44+
),
45+
set_status=AsyncMock(),
46+
)
47+
48+
assert fake_run_agent.call_args.kwargs["session_id"] is None
49+
assert session_store.get_session("C234", "1.0", "U456") == "session-2"
50+
assert session_store.get_session("C234", "1.0", "U123") == "session-1"

‎claude-agent-sdk/tests/test_message.py‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class TestMessage:
1515
def setup_method(self):
16-
session_store.set_session("C123", "1.0", "session-1")
16+
session_store.set_session("C123", "1.0", "U123", "session-1")
1717
self.fake_client = Mock(AsyncWebClient)
1818
self.fake_client.reactions_add = AsyncMock()
1919
self.fake_context = Mock(AsyncBoltContext)
@@ -65,3 +65,73 @@ async def test_runs_agent_for_dm_that_mentions_the_agent(self):
6565
)
6666

6767
fake_run_agent.assert_called_once()
68+
69+
@pytest.mark.asyncio
70+
async def test_skips_thread_reply_from_someone_without_their_own_session(self):
71+
self.fake_context.user_id = "U456"
72+
73+
fake_run_agent = await self.handle("what did you find?", user="U456")
74+
75+
fake_run_agent.assert_not_called()
76+
77+
@pytest.mark.asyncio
78+
async def test_stores_the_new_session_under_the_person_who_replied(self):
79+
session_store.set_session("C123", "1.0", "U123", "session-1")
80+
81+
with patch(
82+
"listeners.events.message.run_casey_agent",
83+
AsyncMock(return_value=("Hi", "session-2")),
84+
):
85+
await handle_message(
86+
client=self.fake_client,
87+
context=self.fake_context,
88+
event={
89+
"channel_type": "channel",
90+
"text": "and then?",
91+
"thread_ts": "1.0",
92+
"ts": "2.0",
93+
"user": "U123",
94+
},
95+
logger=test_logger,
96+
say=AsyncMock(),
97+
say_stream=AsyncMock(
98+
return_value=Mock(append=AsyncMock(), stop=AsyncMock())
99+
),
100+
set_status=AsyncMock(),
101+
)
102+
103+
assert session_store.get_session("C123", "1.0", "U123") == "session-2"
104+
assert session_store.get_session("C123", "1.0", "U456") is None
105+
106+
@pytest.mark.asyncio
107+
async def test_stores_an_issue_submission_session_under_the_submitter(self):
108+
self.fake_context.channel_id = "D123"
109+
self.fake_context.user_id = "UBOT"
110+
111+
with patch(
112+
"listeners.events.message.run_casey_agent",
113+
AsyncMock(return_value=("Hi", "session-2")),
114+
):
115+
await handle_message(
116+
client=self.fake_client,
117+
context=self.fake_context,
118+
event={
119+
"bot_id": "B123",
120+
"channel_type": "im",
121+
"metadata": {
122+
"event_type": "issue_submission",
123+
"event_payload": {"user_id": "U789"},
124+
},
125+
"text": "*Category:* Hardware",
126+
"ts": "5.0",
127+
},
128+
logger=test_logger,
129+
say=AsyncMock(),
130+
say_stream=AsyncMock(
131+
return_value=Mock(append=AsyncMock(), stop=AsyncMock())
132+
),
133+
set_status=AsyncMock(),
134+
)
135+
136+
assert session_store.get_session("D123", "5.0", "U789") == "session-2"
137+
assert session_store.get_session("D123", "5.0", "UBOT") is None
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from thread_context.store import SessionStore
2+
3+
4+
class TestSessionStore:
5+
def test_keeps_different_users_in_the_same_thread_independent(self):
6+
store = SessionStore()
7+
store.set_session("C1", "T1", "U1", "sid-1")
8+
9+
assert store.get_session("C1", "T1", "U2") is None
10+
11+
store.set_session("C1", "T1", "U2", "sid-2")
12+
13+
assert store.get_session("C1", "T1", "U1") == "sid-1"
14+
assert store.get_session("C1", "T1", "U2") == "sid-2"

‎claude-agent-sdk/thread_context/store.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@
55
class SessionStore:
66
"""Thread-safe in-memory session ID store.
77
8-
Stores Claude Agent SDK session IDs keyed by (channel_id, thread_ts).
98
The SDK manages conversation history server-side via sessions, so we
109
only need to track session IDs for resuming conversations.
1110
Includes TTL-based cleanup and a maximum entry limit.
1211
"""
1312

1413
def __init__(self, ttl_seconds: int = 86400, max_entries: int = 1000):
15-
self._store: dict[tuple[str, str], dict] = {}
14+
self._store: dict[tuple[str, str, str], dict] = {}
1615
self._lock = threading.Lock()
1716
self._ttl_seconds = ttl_seconds
1817
self._max_entries = max_entries
1918

20-
def get_session(self, channel_id: str, thread_ts: str) -> str | None:
19+
def get_session(self, channel_id: str, thread_ts: str, user_id: str) -> str | None:
2120
"""Retrieve session ID for a thread.
2221
2322
Returns None if no session exists or if the session has expired.
2423
"""
25-
key = (channel_id, thread_ts)
24+
key = (channel_id, thread_ts, user_id)
2625
with self._lock:
2726
entry = self._store.get(key)
2827
if entry is None:
@@ -32,9 +31,11 @@ def get_session(self, channel_id: str, thread_ts: str) -> str | None:
3231
return None
3332
return entry["session_id"]
3433

35-
def set_session(self, channel_id: str, thread_ts: str, session_id: str) -> None:
34+
def set_session(
35+
self, channel_id: str, thread_ts: str, user_id: str, session_id: str
36+
) -> None:
3637
"""Store session ID for a thread."""
37-
key = (channel_id, thread_ts)
38+
key = (channel_id, thread_ts, user_id)
3839
with self._lock:
3940
self._store[key] = {
4041
"session_id": session_id,

‎openai-agents-sdk/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ Once Casey is running, there are several ways to interact:
286286

287287
**Direct Messages** — Open a DM with Casey. You'll see suggested prompts like _Reset Password_, _Request Access_, and _Network Issues_ — pick one or describe your own issue. Casey will react with :eyes: while processing, then reply in a thread. Send follow-up messages in the same thread and Casey will maintain the full conversation context.
288288

289-
**Channel @mentions** — Invite Casey to a channel by typing `/invite @Casey` in the message box, then type `@Casey` followed by your issue. Casey responds in a thread so the channel stays clean.
289+
**Channel @mentions** — Invite Casey to a channel by typing `/invite @Casey` in the message box, then type `@Casey` followed by your issue. Casey responds in a thread so the channel stays clean. In that thread, Casey replies to follow-up messages from the person who mentioned it. Anyone else in the thread can mention `@Casey` to start their own conversation and Casey keeps each person's conversation separate.
290290

291291
**Assistant Panel** — Click _Add Agent_ in the top-right corner of Slack, select Casey from the list, then pick a suggested prompt or type a message.
292292

@@ -348,7 +348,7 @@ The `tools` directory contains five IT support tools that the agent can call dur
348348

349349
### `/thread_context`
350350

351-
The `store.py` file implements a thread-safe in-memory conversation history store, keyed by channel and thread. This enables multi-turn conversations where Casey remembers previous context within a thread.
351+
The `store.py` file implements a thread-safe in-memory conversation history store, keyed by channel, thread, and user, so each person in a thread has their own conversation. This enables multi-turn conversations where Casey remembers previous context within a thread.
352352

353353
## Troubleshooting
354354

‎openai-agents-sdk/listeners/events/app_mentioned.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def handle_app_mentioned(
5656
)
5757

5858
# Get conversation history
59-
history = conversation_store.get_history(channel_id, thread_ts)
59+
history = conversation_store.get_history(channel_id, thread_ts, context.user_id)
6060

6161
# Build input for the agent
6262
if history:
@@ -82,7 +82,9 @@ def handle_app_mentioned(
8282
streamer.stop(blocks=feedback_blocks)
8383

8484
# Store conversation history
85-
conversation_store.set_history(channel_id, thread_ts, result.to_input_list())
85+
conversation_store.set_history(
86+
channel_id, thread_ts, context.user_id, result.to_input_list()
87+
)
8688

8789
except Exception as e:
8890
logger.exception("Failed to handle app mention")

0 commit comments

Comments
 (0)