From 6af782d2d96fe938220384e188feb3bb0da54d35 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 4 Mar 2026 01:11:17 -0500 Subject: [PATCH] fix(slack): route system events via binding-aware session keys --- src/slack/monitor/context.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/slack/monitor/context.ts b/src/slack/monitor/context.ts index 84633320427..1d75af03650 100644 --- a/src/slack/monitor/context.ts +++ b/src/slack/monitor/context.ts @@ -7,6 +7,7 @@ import type { DmPolicy, GroupPolicy } from "../../config/types.js"; import { logVerbose } from "../../globals.js"; import { createDedupeCache } from "../../infra/dedupe.js"; import { getChildLogger } from "../../logging.js"; +import { resolveAgentRoute } from "../../routing/resolve-route.js"; import type { RuntimeEnv } from "../../runtime.js"; import type { SlackMessageEvent } from "../types.js"; import { normalizeAllowList, normalizeAllowListLower, normalizeSlackSlug } from "./allow-list.js"; @@ -62,6 +63,7 @@ export type SlackMonitorContext = { resolveSlackSystemEventSessionKey: (params: { channelId?: string | null; channelType?: string | null; + senderId?: string | null; }) => string; isChannelAllowed: (params: { channelId?: string; @@ -151,6 +153,7 @@ export function createSlackMonitorContext(params: { const resolveSlackSystemEventSessionKey = (p: { channelId?: string | null; channelType?: string | null; + senderId?: string | null; }) => { const channelId = p.channelId?.trim() ?? ""; if (!channelId) { @@ -165,6 +168,27 @@ export function createSlackMonitorContext(params: { ? `slack:group:${channelId}` : `slack:channel:${channelId}`; const chatType = isDirectMessage ? "direct" : isGroup ? "group" : "channel"; + const senderId = p.senderId?.trim() ?? ""; + + // Resolve through shared channel/account bindings so system events route to + // the same agent session as regular inbound messages. + try { + const peerKind = isDirectMessage ? "direct" : isGroup ? "group" : "channel"; + const peerId = isDirectMessage ? senderId : channelId; + if (peerId) { + const route = resolveAgentRoute({ + cfg: params.cfg, + channel: "slack", + accountId: params.accountId, + teamId: params.teamId, + peer: { kind: peerKind, id: peerId }, + }); + return route.sessionKey; + } + } catch { + // Fall through to legacy key derivation. + } + return resolveSessionKey( params.sessionScope, { From: from, ChatType: chatType, Provider: "slack" },