perf(core): speed up routing, pairing, slack, and security scans

This commit is contained in:
Peter Steinberger
2026-03-02 21:07:34 +00:00
parent 3a08e69a05
commit 5a32a66aa8
11 changed files with 462 additions and 38 deletions

View File

@@ -254,6 +254,7 @@ export async function authorizeSlackSystemEventSender(params: {
channelId,
channelName,
channels: params.ctx.channelsConfig,
channelKeys: params.ctx.channelsConfigKeys,
defaultRequireMention: params.ctx.defaultRequireMention,
});
const channelUsersAllowlistConfigured =

View File

@@ -89,11 +89,12 @@ export function resolveSlackChannelConfig(params: {
channelId: string;
channelName?: string;
channels?: SlackChannelConfigEntries;
channelKeys?: string[];
defaultRequireMention?: boolean;
}): SlackChannelConfigResolved | null {
const { channelId, channelName, channels, defaultRequireMention } = params;
const { channelId, channelName, channels, channelKeys, defaultRequireMention } = params;
const entries = channels ?? {};
const keys = Object.keys(entries);
const keys = channelKeys ?? Object.keys(entries);
const normalizedName = channelName ? normalizeSlackSlug(channelName) : "";
const directName = channelName ? channelName.trim() : "";
// Slack always delivers channel IDs in uppercase (e.g. C0ABC12345) but

View File

@@ -78,6 +78,7 @@ export type SlackMonitorContext = {
groupDmChannels: string[];
defaultRequireMention: boolean;
channelsConfig?: SlackChannelConfigEntries;
channelsConfigKeys: string[];
groupPolicy: GroupPolicy;
useAccessGroups: boolean;
reactionMode: SlackReactionNotificationMode;
@@ -173,6 +174,7 @@ export function createSlackMonitorContext(params: {
const groupDmChannelsLower = normalizeAllowListLower(groupDmChannels);
const defaultRequireMention = params.defaultRequireMention ?? true;
const hasChannelAllowlistConfig = Object.keys(params.channelsConfig ?? {}).length > 0;
const channelsConfigKeys = Object.keys(params.channelsConfig ?? {});
const markMessageSeen = (channelId: string | undefined, ts?: string) => {
if (!channelId || !ts) {
@@ -331,6 +333,7 @@ export function createSlackMonitorContext(params: {
channelId: p.channelId,
channelName: p.channelName,
channels: params.channelsConfig,
channelKeys: channelsConfigKeys,
defaultRequireMention,
});
const channelMatchMeta = formatAllowlistMatchMeta(channelConfig);
@@ -413,6 +416,7 @@ export function createSlackMonitorContext(params: {
groupDmChannels,
defaultRequireMention,
channelsConfig: params.channelsConfig,
channelsConfigKeys,
groupPolicy: params.groupPolicy,
useAccessGroups: params.useAccessGroups,
reactionMode: params.reactionMode,

View File

@@ -108,6 +108,7 @@ export async function prepareSlackMessage(params: {
channelId: message.channel,
channelName,
channels: ctx.channelsConfig,
channelKeys: ctx.channelsConfigKeys,
defaultRequireMention: ctx.defaultRequireMention,
})
: null;
@@ -251,15 +252,29 @@ export async function prepareSlackMessage(params: {
hasSlackThreadParticipation(account.accountId, message.channel, message.thread_ts)),
);
const sender = message.user ? await ctx.resolveUserName(message.user) : null;
const senderName =
sender?.name ?? message.username?.trim() ?? message.user ?? message.bot_id ?? "unknown";
let resolvedSenderName = message.username?.trim() || undefined;
const resolveSenderName = async (): Promise<string> => {
if (resolvedSenderName) {
return resolvedSenderName;
}
if (message.user) {
const sender = await ctx.resolveUserName(message.user);
const normalized = sender?.name?.trim();
if (normalized) {
resolvedSenderName = normalized;
return resolvedSenderName;
}
}
resolvedSenderName = message.user ?? message.bot_id ?? "unknown";
return resolvedSenderName;
};
const senderNameForAuth = ctx.allowNameMatching ? await resolveSenderName() : undefined;
const channelUserAuthorized = isRoom
? resolveSlackUserAllowed({
allowList: channelConfig?.users,
userId: senderId,
userName: senderName,
userName: senderNameForAuth,
allowNameMatching: ctx.allowNameMatching,
})
: true;
@@ -279,7 +294,7 @@ export async function prepareSlackMessage(params: {
const ownerAuthorized = resolveSlackAllowListMatch({
allowList: allowFromLower,
id: senderId,
name: senderName,
name: senderNameForAuth,
allowNameMatching: ctx.allowNameMatching,
}).allowed;
const channelUsersAllowlistConfigured =
@@ -289,7 +304,7 @@ export async function prepareSlackMessage(params: {
? resolveSlackUserAllowed({
allowList: channelConfig?.users,
userId: senderId,
userName: senderName,
userName: senderNameForAuth,
allowNameMatching: ctx.allowNameMatching,
})
: false;
@@ -350,7 +365,7 @@ export async function prepareSlackMessage(params: {
limit: ctx.historyLimit,
entry: pendingBody
? {
sender: senderName,
sender: await resolveSenderName(),
body: pendingBody,
timestamp: message.ts ? Math.round(Number(message.ts) * 1000) : undefined,
messageId: message.ts,
@@ -455,6 +470,7 @@ export async function prepareSlackMessage(params: {
: null;
const roomLabel = channelName ? `#${channelName}` : `#${message.channel}`;
const senderName = await resolveSenderName();
const preview = rawBody.replace(/\s+/g, " ").slice(0, 160);
const inboundLabel = isDirectMessage
? `Slack DM from ${senderName}`

View File

@@ -385,11 +385,11 @@ export async function registerSlackMonitorSlashCommands(params: {
channelId: command.channel_id,
channelName: channelInfo?.name,
channels: ctx.channelsConfig,
channelKeys: ctx.channelsConfigKeys,
defaultRequireMention: ctx.defaultRequireMention,
});
if (ctx.useAccessGroups) {
const channelAllowlistConfigured =
Boolean(ctx.channelsConfig) && Object.keys(ctx.channelsConfig ?? {}).length > 0;
const channelAllowlistConfigured = (ctx.channelsConfigKeys?.length ?? 0) > 0;
const channelAllowed = channelConfig?.allowed !== false;
if (
!isSlackChannelAllowedByPolicy({