|
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. |
2 | 2 |
|
3 | 3 | A code channel is one session, so state is keyed by the code channel's own |
4 | 4 | channel_id. Each entry tracks the temp working directory the agent cloned into, |
@@ -35,10 +35,12 @@ class CodeChannelSession: |
35 | 35 | history: list = field(default_factory=list) |
36 | 36 | # Messages waiting for the next turn, each starting with who sent it. |
37 | 37 | 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. |
40 | 41 | 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. |
42 | 44 | archived: bool = False |
43 | 45 | # One turn at a time per channel, so turns don't race on history. |
44 | 46 | turn_lock: asyncio.Lock = field(default_factory=asyncio.Lock, repr=False) |
@@ -90,9 +92,16 @@ def put(self, session: CodeChannelSession) -> None: |
90 | 92 | self._store[session.channel_id] = session |
91 | 93 |
|
92 | 94 | 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 | + """ |
94 | 100 | 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: |
96 | 105 | shutil.rmtree(session.workdir, ignore_errors=True) |
97 | 106 |
|
98 | 107 |
|
|
0 commit comments