Skip to content

Commit 93ac2b4

Browse files
feat(slack): per-thread session isolation for DM auto-threading (openclaw#26849)
* feat(slack): create thread sessions for auto-threaded DM messages When replyToMode="all", every top-level message starts a new Slack thread. Previously, only subsequent replies in that thread got an isolated session (via 🧵<threadTs> suffix). The initial message fell back to the base DM session, mixing context across unrelated conversations. Now, when replyToMode="all" and a message is not already a thread reply, the message's own ts is used as the threadId for session key resolution. This gives the initial message AND all subsequent thread replies the same isolated session. This enables per-thread session isolation for Slack DMs — each new message starts its own thread and session, keeping conversations separate. * Slack: fix auto-thread session key mode check and add changelog --------- Co-authored-by: Tak Hoffman <[email protected]>
1 parent 746688d commit 93ac2b4

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ Docs: https://docs.openclaw.ai
246246
- Slack/Threading: resolve `replyToMode` per incoming message using chat-type-aware account config (`replyToModeByChatType` and legacy `dm.replyToMode`) so DM/channel reply threading honors overrides instead of always using monitor startup defaults. (#24717) Thanks @dbachelder.
247247
- Slack/Threading: track bot participation in message threads (per account/channel/thread) so follow-up messages in those threads can be handled without requiring repeated @mentions, while preserving mention-gating behavior for unrelated threads. (#29165) Thanks @luijoc.
248248
- Slack/Threading: stop forcing tool-call reply mode to `all` based on `ThreadLabel` alone; now force thread reply mode only when an explicit thread target exists (`MessageThreadId`/`ReplyToId`), so DM `replyToModeByChatType.direct` overrides are honored outside real thread replies. (#26251) Thanks @dbachelder.
249+
- Slack/Threading: when `replyToMode="all"` auto-threads top-level Slack DMs, seed the thread session key from the message `ts` so the initial message and later replies share the same isolated `:thread:` session instead of falling back to base DM context. (#26849) Thanks @calder-sandy.
249250
- Agents/Subagents delivery: refactor subagent completion announce dispatch into an explicit queue/direct/fallback state machine, recover outbound channel-plugin resolution in cold/stale plugin-registry states across announce/message/gateway send paths, finalize cleanup bookkeeping when announce flow rejects, and treat Telegram sends without `message_id` as delivery failures (instead of false-success `"unknown"` IDs). (#26867, #25961, #26803, #25069, #26741) Thanks @SmithLabsLLC and @docaohieu2808.
250251
- Telegram/Webhook: pre-initialize webhook bots, switch webhook processing to callback-mode JSON handling, and preserve full near-limit payload reads under delayed handlers to prevent webhook request hangs and dropped updates. (#26156)
251252
- Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl.

src/slack/monitor/message-handler/prepare.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,32 @@ describe("slack prepareSlackMessage inbound contract", () => {
712712
expect(prepared!.ctxPayload.Body).not.toContain("thread_ts");
713713
expect(prepared!.ctxPayload.Body).not.toContain("parent_user_id");
714714
});
715+
716+
it("creates thread session for top-level DM when replyToMode=all", async () => {
717+
const { storePath } = makeTmpStorePath();
718+
const slackCtx = createInboundSlackCtx({
719+
cfg: {
720+
session: { store: storePath },
721+
channels: { slack: { enabled: true, replyToMode: "all" } },
722+
} as OpenClawConfig,
723+
replyToMode: "all",
724+
});
725+
// oxlint-disable-next-line typescript/no-explicit-any
726+
slackCtx.resolveUserName = async () => ({ name: "Alice" }) as any;
727+
728+
const message = createSlackMessage({ ts: "500.000" });
729+
const prepared = await prepareMessageWith(
730+
slackCtx,
731+
createSlackAccount({ replyToMode: "all" }),
732+
message,
733+
);
734+
735+
expect(prepared).toBeTruthy();
736+
// Session key should include :thread:500.000 for the auto-threaded message
737+
expect(prepared!.ctxPayload.SessionKey).toContain(":thread:500.000");
738+
// MessageThreadId should be set for the reply
739+
expect(prepared!.ctxPayload.MessageThreadId).toBe("500.000");
740+
});
715741
});
716742

717743
describe("prepareSlackMessage sender prefix", () => {

src/slack/monitor/message-handler/prepare.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,19 @@ export async function prepareSlackMessage(params: {
181181
const threadContext = resolveSlackThreadContext({ message, replyToMode });
182182
const threadTs = threadContext.incomingThreadTs;
183183
const isThreadReply = threadContext.isThreadReply;
184+
// When replyToMode="all", every top-level message starts a new thread.
185+
// Use its own ts as threadId so the initial message AND subsequent replies
186+
// in that thread share an isolated session (instead of falling back to the
187+
// base DM/channel session for the first message).
188+
const autoThreadId =
189+
!isThreadReply && replyToMode === "all" && threadContext.messageTs
190+
? threadContext.messageTs
191+
: undefined;
184192
const threadKeys = resolveThreadSessionKeys({
185193
baseSessionKey,
186-
threadId: isThreadReply ? threadTs : undefined,
187-
parentSessionKey: isThreadReply && ctx.threadInheritParent ? baseSessionKey : undefined,
194+
threadId: isThreadReply ? threadTs : autoThreadId,
195+
parentSessionKey:
196+
(isThreadReply || autoThreadId) && ctx.threadInheritParent ? baseSessionKey : undefined,
188197
});
189198
const sessionKey = threadKeys.sessionKey;
190199
const historyKey =

0 commit comments

Comments
 (0)