Skip to content

Commit 21a03ed

Browse files
zimegClaude
andcommitted
fix: quiet a turn still running when its channel is archived from Slack
Discarding a session now marks it archived and stopped, so a turn still running calls no more tools and doesn't put the session back with a deleted clone when it finishes. Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
1 parent 72233a8 commit 21a03ed

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎openai-agents-sdk/agent/sessions.py‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""In-memory store mapping a code channel to its working session.
1+
"""Code channel sessions: their state, the deps each agent run gets, and the store.
22
33
A code channel is one session, so state is keyed by the code channel's own
44
channel_id. Each entry tracks the temp working directory the agent cloned into,
@@ -35,10 +35,12 @@ class CodeChannelSession:
3535
history: list = field(default_factory=list)
3636
# Messages waiting for the next turn, each starting with who sent it.
3737
inbox: list[str] = field(default_factory=list)
38-
# Set by the native stop button. Checked before posting and before every
39-
# tool call so a stopped session goes quiet; cleared when the user starts a new turn.
38+
# Set by the native stop button, or when the session is discarded. Checked
39+
# before posting and before every tool call so a stopped session goes quiet;
40+
# cleared when the user starts a new turn.
4041
stopped: bool = False
41-
# Set once the agent archives the channel; nothing more is posted after that.
42+
# Set once the channel is archived, by the agent or from Slack; nothing more
43+
# is posted after that.
4244
archived: bool = False
4345
# One turn at a time per channel, so turns don't race on history.
4446
turn_lock: asyncio.Lock = field(default_factory=asyncio.Lock, repr=False)
@@ -90,9 +92,16 @@ def put(self, session: CodeChannelSession) -> None:
9092
self._store[session.channel_id] = session
9193

9294
def discard(self, channel_id: str) -> None:
93-
"""Remove a session and clean up its working directory."""
95+
"""Remove a session and clean up its working directory.
96+
97+
The session is also marked archived and stopped, so a turn still running
98+
in it calls no more tools and doesn't put it back when it finishes.
99+
"""
94100
session = self._store.pop(channel_id, None)
95-
if session is not None and session.workdir:
101+
if session is None:
102+
return
103+
session.archived = session.stopped = True
104+
if session.workdir:
96105
shutil.rmtree(session.workdir, ignore_errors=True)
97106

98107

‎openai-agents-sdk/tests/test_channel_archive.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from unittest.mock import Mock
22

33
from agent.approvals import PendingApproval, pending_store
4-
from agent.sessions import CodeChannelSession, session_store
4+
from agent.outcome import RunOutcome
5+
from agent.sessions import AgentDeps, CodeChannelSession, session_store
6+
from agent.turns import deliver_outcome
57
from listeners.events.channel_archive import handle_channel_archive
68

79

@@ -22,3 +24,20 @@ async def test_archiving_from_slack_forgets_the_session(self, context, tmp_path)
2224
assert session_store.get("C_CODE") is None
2325
assert pending_store.pop("C_CODE", "9.1") is None
2426
assert not workdir.exists()
27+
28+
async def test_a_turn_running_when_slack_archives_stays_quiet(
29+
self, client, context
30+
):
31+
session = CodeChannelSession(channel_id="C_CODE")
32+
session_store.put(session)
33+
34+
await handle_channel_archive(context=context, event={"channel": "C_CODE"})
35+
# The turn that was running finishes after the archive.
36+
await deliver_outcome(
37+
outcome=RunOutcome(final_output="Done."),
38+
deps=AgentDeps(client=client, session=session),
39+
)
40+
41+
assert session.archived and session.stopped
42+
assert session_store.get("C_CODE") is None
43+
client.chat_postMessage.assert_not_called()

0 commit comments

Comments
 (0)