Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(slack): do not flow through edits and deletions to `onMessage` hook (#4152)

Ben Pankow19a2b4fd413e
diff --git a/.changeset/slack-onmessage-skip-edits.md b/.changeset/slack-onmessage-skip-edits.md
new file mode 100644
index 000000000..04c31b4a6
--- /dev/null
+++ b/.changeset/slack-onmessage-skip-edits.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Slack `onMessage` now receives only newly posted messages. Edits, deletions, and other system message events go to `onEvent`, so updating the agent's own HITL card in a DM no longer starts a stray session.
diff --git a/docs/channels/slack.mdx b/docs/channels/slack.mdx
index 6d7233130..dabb5df24 100644
--- a/docs/channels/slack.mdx
+++ b/docs/channels/slack.mdx
@@ -152,7 +152,7 @@ controls observability content capture, not access to the channel. See
Message hooks return `{ auth }` to dispatch, `null` to drop, or `{ auth, context }` to inject background into history. Public conversations use the returned `title`, or the triggering message text when `title` is omitted. DMs and private channels always use `Private message` so the run title does not expose message details.
-- `onMessage(ctx, message)` handles Slack `message` events. eve drops messages authored by the installed app before this hook runs, preventing self-reply loops. Messages from other bots remain visible; check `message.author?.isBot` when those should also be ignored. `ctx.isBotMentioned()` and `ctx.isSubscribed()` support mention and active-thread policies.
+- `onMessage(ctx, message)` handles newly posted Slack messages. eve drops messages authored by the installed app before this hook runs, preventing self-reply loops. Messages from other bots remain visible; check `message.author?.isBot` when those should also be ignored. `ctx.isBotMentioned()` and `ctx.isSubscribed()` support mention and active-thread policies.
- `onAppMention(ctx, message)` handles only `app_mention` and takes precedence over `onMessage`. Its default derives workspace-scoped auth.
- `onDirectMessage(ctx, message)` handles only DMs and takes precedence over `onMessage`. Bot-authored messages and edits are filtered first; Slack requires `message.im` and `im:history`.
diff --git a/packages/eve/src/public/channels/slack/inbound.test.ts b/packages/eve/src/public/channels/slack/inbound.test.ts
index 4dfd4beb0..281110aab 100644
--- a/packages/eve/src/public/channels/slack/inbound.test.ts
+++ b/packages/eve/src/public/channels/slack/inbound.test.ts
@@ -210,7 +210,7 @@ describe("parseAppMentionEvent", () => {
});
describe("parseMessageEvent", () => {
- it("preserves bot and subtype message events for onMessage", () => {
+ it("preserves bot messages for onMessage", () => {
const bot = parseMessageEvent({
type: "event_callback",
event: {
@@ -222,20 +222,33 @@ describe("parseMessageEvent", () => {
ts: "2.0",
},
});
- const subtype = parseMessageEvent({
- type: "event_callback",
- event: {
- type: "message",
- subtype: "message_changed",
- text: "edited",
- channel: "C01",
- ts: "3.0",
- },
- });
expect(bot?.author?.isBot).toBe(true);
- expect(subtype?.raw.subtype).toBe("message_changed");
});
+
+ it.each(["bot_message", "file_share", "me_message", "thread_broadcast"])(
+ "keeps the posted-message subtype %s",
+ (subtype) => {
+ const message = parseMessageEvent({
+ type: "event_callback",
+ event: { type: "message", subtype, user: "U01", text: "hi", channel: "C01", ts: "2.0" },
+ });
+
+ expect(message?.raw.subtype).toBe(subtype);
+ },
+ );
+
+ it.each(["message_changed", "message_deleted", "channel_join"])(
+ "drops the system subtype %s",
+ (subtype) => {
+ const message = parseMessageEvent({
+ type: "event_callback",
+ event: { type: "message", subtype, text: "edited", channel: "C01", ts: "3.0" },
+ });
+
+ expect(message).toBeNull();
+ },
+ );
});
describe("parseDirectMessageEvent", () => {
diff --git a/packages/eve/src/public/channels/slack/inbound.ts b/packages/eve/src/public/channels/slack/inbound.ts
index 78c2a06ba..588bcae9a 100644
--- a/packages/eve/src/public/channels/slack/inbound.ts
+++ b/packages/eve/src/public/channels/slack/inbound.ts
@@ -240,12 +240,30 @@ export function slackEventInstallationTeamId(envelope: SlackEventCallback): stri
return authorizations.find((entry) => typeof entry.team_id === "string")?.team_id;
}
-/** Parses a Slack message event without applying bot or subtype policy. */
+/**
+ * Parses a Slack message event that posts a new message. Bot-authored messages
+ * remain; edits, deletions, joins, and other system events return `null`.
+ */
export function parseMessageEvent(envelope: SlackEventCallback): SlackMessage | null {
if (envelope.type !== "event_callback") return null;
const event = envelope.event;
if (!event || event.type !== "message") return null;
- return buildSlackMessage(event as SlackMessageEvent, envelope.team_id);
+ const message = event as SlackMessageEvent;
+ if (!isPostedMessageSubtype(message.subtype)) return null;
+ return buildSlackMessage(message, envelope.team_id);
+}
+
+// Change events such as `message_changed` carry the edit's own `ts` and nest
+// the author under `message`, so they cannot be routed as new messages.
+const POSTED_MESSAGE_SUBTYPES: ReadonlySet<string> = new Set([
+ "bot_message",
+ "file_share",
+ "me_message",
+ "thread_broadcast",
+]);
+
+function isPostedMessageSubtype(subtype: string | undefined): boolean {
+ return subtype === undefined || subtype === "" || POSTED_MESSAGE_SUBTYPES.has(subtype);
}
export function parseDirectMessageEvent(envelope: SlackEventCallback): SlackMessage | null {
diff --git a/packages/eve/src/public/channels/slack/slackChannel.test.ts b/packages/eve/src/public/channels/slack/slackChannel.test.ts
index 53ef8d82c..1dd452d55 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.test.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.test.ts
@@ -3410,6 +3410,72 @@ describe("slackChannel() onMessage", () => {
expect(send).not.toHaveBeenCalled();
});
+ // Slack reports chat.update and chat.delete on the app's own HITL card as
+ // message events whose author is nested under `message`/`previous_message`.
+ it.each([
+ [
+ "message_changed",
+ {
+ channel: "D01",
+ channel_type: "im",
+ hidden: true,
+ message: {
+ app_id: "A01",
+ bot_id: "B01",
+ text: "Approved",
+ ts: "1700000000.000200",
+ type: "message",
+ user: "U_BOT",
+ },
+ subtype: "message_changed",
+ ts: "1700000000.000300",
+ type: "message",
+ },
+ ],
+ [
+ "message_deleted",
+ {
+ channel: "D01",
+ channel_type: "im",
+ deleted_ts: "1700000000.000200",
+ hidden: true,
+ previous_message: {
+ app_id: "A01",
+ bot_id: "B01",
+ text: "Approve?",
+ ts: "1700000000.000200",
+ type: "message",
+ user: "U_BOT",
+ },
+ subtype: "message_deleted",
+ ts: "1700000000.000400",
+ type: "message",
+ },
+ ],
+ ])("routes %s to onEvent instead of the documented DM policy", async (_label, event) => {
+ const onEvent = vi.fn(() => {});
+ const channel = slackChannel({
+ credentials: { botToken: "xoxb-test", signingSecret: SIGNING_SECRET },
+ async onMessage(ctx, message) {
+ if (message.author?.isBot) return null;
+ return (await ctx.isDMOrPrivateChannel()) ||
+ ctx.isBotMentioned() ||
+ (await ctx.isSubscribed())
+ ? { auth: null }
+ : null;
+ },
+ onEvent,
+ });
+ const body = buildEventBody(event, {
+ authorizations: [{ is_bot: true, user_id: "U_BOT" }],
+ });
+
+ const { send } = await firePost(channel, buildSignedRequest({ body }));
+
+ expect(send).not.toHaveBeenCalled();
+ expect(onEvent).toHaveBeenCalledTimes(1);
+ });
+
it("gives specialized message hooks precedence", async () => {
const onAppMention = vi.fn(() => null);
const onMessage = vi.fn(() => ({ auth: null }));
diff --git a/packages/eve/src/public/channels/slack/slackChannel.ts b/packages/eve/src/public/channels/slack/slackChannel.ts
index 5ced6533b..c8ddbf173 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.ts
@@ -1139,7 +1139,7 @@ async function handleEventPost(input: {
}
// Specialized handlers retain their bot/subtype filters. onMessage receives
- // structurally valid messages except this app's own; onEvent is the raw fallback.
+ // newly posted messages except this app's own; onEvent is the raw fallback.
const onEvent = config.onEvent;
if (dispatch === null && onEvent !== undefined) {
dispatch = () =>