fix(slack): route system events via binding-aware session keys

This commit is contained in:
Vincent Koc
2026-03-04 01:11:17 -05:00
parent dfb4cb87f9
commit 6af782d2d9

View File

@@ -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" },