Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): slack - identify the installed agent reply boundary (#1112)

Andrew Barbabd4397b66749
diff --git a/.changeset/tidy-agents-reply.md b/.changeset/tidy-agents-reply.md
new file mode 100644
index 000000000..17952c7b6
--- /dev/null
+++ b/.changeset/tidy-agents-reply.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Fixed Slack `threadContext` with `since: "last-agent-reply"` so only replies from the installed app move the context boundary. Replies from other bots remain part of the incremental thread context, are labeled `bot` instead of `agent` in the injected transcript, and their file uploads are now eligible for mention attachment lookback.
diff --git a/docs/channels/slack.mdx b/docs/channels/slack.mdx
index 0f188e730..300d9beab 100644
--- a/docs/channels/slack.mdx
+++ b/docs/channels/slack.mdx
@@ -153,7 +153,7 @@ export default slackChannel({
`since` sets the boundary for what each mention injects and accepts three values:
- `"thread-root"` (the default): every prior message in the thread, on every mention. `threadContext: {}` behaves the same.
-- `"last-agent-reply"`: only messages after the agent's last reply, keeping repeated mentions incremental.
+- `"last-agent-reply"`: only messages after this installed agent's last reply, keeping repeated mentions incremental. Replies from other Slack bots do not move the boundary.
- A predicate `(message: SlackThreadMessage) => boolean`: only messages after the last one it matches, such as "since the last message that mentioned a particular user".
`threadContext` performs one `conversations.replies` request for each triggering thread reply and requires the matching Slack history scope. Omit it when the agent should see only direct mentions. `loadThreadContextMessages` remains available when you need custom filtering or non-model processing of the raw thread messages.
diff --git a/packages/eve/src/public/channels/slack/api.test.ts b/packages/eve/src/public/channels/slack/api.test.ts
index cca341609..8a6d8c240 100644
--- a/packages/eve/src/public/channels/slack/api.test.ts
+++ b/packages/eve/src/public/channels/slack/api.test.ts
@@ -439,7 +439,7 @@ describe("SlackThread.refresh", () => {
botId: "B01",
ts: "1700000001.000000",
threadTs: "1700000000.000001",
- isMe: true,
+ isMe: false,
});
const firstMessage = thread.recentMessages[0]!;
@@ -448,6 +448,56 @@ describe("SlackThread.refresh", () => {
expect("author" in firstMessage).toBe(false);
expect("metadata" in firstMessage).toBe(false);
});
+
+ it("marks only replies from the bound Slack app as mine", async () => {
+ mock.fetch.mockImplementation(async (input: string | URL | Request) => {
+ if (String(input) === "https://slack.com/api/conversations.replies") {
+ return new Response(
+ JSON.stringify({
+ ok: true,
+ messages: [
+ {
+ app_id: "A_SELF",
+ bot_id: "B_SELF",
+ text: "own user-attributed reply",
+ ts: "1.1",
+ user: "U_SELF",
+ },
+ {
+ app_id: "A_OTHER",
+ bot_id: "B_OTHER",
+ text: "other bot reply",
+ ts: "1.2",
+ user: "U_OTHER",
+ },
+ {
+ app_id: "A_SELF",
+ bot_id: "B_SELF",
+ text: "own app-attributed reply",
+ ts: "1.3",
+ },
+ ],
+ }),
+ { headers: { "content-type": "application/json" } },
+ );
+ }
+ return new Response(JSON.stringify({ ok: true }), {
+ headers: { "content-type": "application/json" },
+ });
+ });
+ const { thread } = buildSlackBinding({
+ appId: "A_SELF",
+ botToken: "xoxb-test",
+ botUserId: "U_SELF",
+ channelId: "C01",
+ threadTs: "1.0",
+ teamId: undefined,
+ });
+
+ await thread.refresh();
+
+ expect(thread.recentMessages.map((message) => message.isMe)).toEqual([true, false, true]);
+ });
});
describe("SlackThread.listParticipants", () => {
diff --git a/packages/eve/src/public/channels/slack/api.ts b/packages/eve/src/public/channels/slack/api.ts
index 738f97235..c44639b11 100644
--- a/packages/eve/src/public/channels/slack/api.ts
+++ b/packages/eve/src/public/channels/slack/api.ts
@@ -195,6 +195,13 @@ export interface SlackThreadMessage {
readonly botId: string | undefined;
readonly ts: string;
readonly threadTs: string;
+ /**
+ * Whether this message was authored by the Slack app bound to the thread.
+ * Classification requires the binding to carry the app identity (bot user
+ * id or app id). Inbound event bindings do; bindings rebuilt from session
+ * state or interaction payloads currently do not and mark every message
+ * `false`.
+ */
readonly isMe: boolean;
readonly raw: Record<string, unknown>;
}
@@ -368,7 +375,11 @@ interface SlackBinding {
* anchor.
*/
export function buildSlackBinding(input: {
+ /** Slack app id used to identify this app's fetched thread replies. */
+ readonly appId?: string;
readonly botToken: SlackBotToken | undefined;
+ /** Slack bot user id used to identify this app's fetched thread replies. */
+ readonly botUserId?: string;
readonly channelId: string;
readonly threadTs: string;
readonly teamId: string | undefined;
@@ -499,7 +510,12 @@ export function buildSlackBinding(input: {
ts: currentThreadTs,
});
for (const raw of response.messages as Record<string, unknown>[]) {
- messages.push(parseThreadMessage(raw, currentThreadTs));
+ messages.push(
+ parseThreadMessage(raw, currentThreadTs, {
+ appId: input.appId,
+ botUserId: input.botUserId,
+ }),
+ );
}
} catch (error) {
logError(log, "refresh threw — swallowed", error, { channelId: input.channelId });
@@ -603,12 +619,20 @@ function normalizeFileData(data: FileUpload["data"]): SlackFileUpload["data"] {
function parseThreadMessage(
raw: Record<string, unknown>,
threadRootTs: string,
+ identity: {
+ readonly appId: string | undefined;
+ readonly botUserId: string | undefined;
+ },
): SlackThreadMessage {
const text = typeof raw.text === "string" ? raw.text : "";
const ts = typeof raw.ts === "string" ? raw.ts : "";
const threadTs = typeof raw.thread_ts === "string" ? raw.thread_ts : threadRootTs;
const user = typeof raw.user === "string" ? raw.user : undefined;
const botId = typeof raw.bot_id === "string" ? raw.bot_id : undefined;
+ const appId = typeof raw.app_id === "string" ? raw.app_id : undefined;
+ const isMe =
+ (identity.botUserId !== undefined && user === identity.botUserId) ||
+ (identity.appId !== undefined && appId === identity.appId);
return {
text,
markdown: slackMrkdwnToGfm(text),
@@ -616,7 +640,7 @@ function parseThreadMessage(
botId,
ts,
threadTs,
- isMe: botId !== undefined,
+ isMe,
raw,
};
}
diff --git a/packages/eve/src/public/channels/slack/attachments.ts b/packages/eve/src/public/channels/slack/attachments.ts
index 9c1c6fe24..f2c5e9a74 100644
--- a/packages/eve/src/public/channels/slack/attachments.ts
+++ b/packages/eve/src/public/channels/slack/attachments.ts
@@ -72,11 +72,12 @@ function toSlackFilePart(attachment: SlackAttachment, index: number): FilePart |
* Prefers attachments on the triggering mention (the common case: user
* uploads a file and mentions the bot in the same message). When the
* mention has none, refreshes the thread via {@link SlackThread.refresh}
- * and picks the latest non-bot message's attachments — covering the
- * case where a user dropped a file in the thread first, then mentioned
- * the bot in a follow-up. Any error during refresh is logged and treated
- * as "no attachments" so the text portion of the mention still gets
- * delivered.
+ * and picks the attachments of the latest message this app did not
+ * author (the thread message `isMe` classification) — covering the case
+ * where a user or another bot dropped a file in the thread first, then a
+ * user mentioned the bot in a follow-up. Any error during refresh is logged
+ * and treated as "no attachments" so the text portion of the mention
+ * still gets delivered.
*
* Skips the thread-history lookback when the policy disables uploads,
* since the refresh can't surface anything we'd deliver.
diff --git a/packages/eve/src/public/channels/slack/slackChannel.test.ts b/packages/eve/src/public/channels/slack/slackChannel.test.ts
index 7d279af84..63f71660d 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.test.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.test.ts
@@ -1206,9 +1206,9 @@ describe("slackChannel() inbound mention pipeline", () => {
expect(options.title).toBe("hello");
});
- it("adds one ID-attributed thread transcript without extra profile lookups", async () => {
+ it("uses only this app's reply as the incremental thread context boundary", async () => {
const threadTs = "1700000000.000001";
- const currentTs = "1700000000.000005";
+ const currentTs = "1700000000.000006";
fetchMock.mockImplementation(async (request: string | URL | Request) => {
const url = String(request);
if (url.includes("conversations.replies")) {
@@ -1218,10 +1218,12 @@ describe("slackChannel() inbound mention pipeline", () => {
messages: [
{ user: "U_ROOT", text: "root", ts: threadTs, thread_ts: threadTs },
{
+ app_id: "A01",
bot_id: "B_AGENT",
text: "What changed?",
ts: "1700000000.000002",
thread_ts: threadTs,
+ user: "U_BOT",
},
{
user: "U_BACKEND",
@@ -1229,10 +1231,18 @@ describe("slackChannel() inbound mention pipeline", () => {
ts: "1700000000.000003",
thread_ts: threadTs,
},
+ {
+ app_id: "A_OTHER",
+ bot_id: "B_OTHER",
+ text: "Other bot follow-up.",
+ ts: "1700000000.000004",
+ thread_ts: threadTs,
+ user: "U_OTHER_BOT",
+ },
{
user: "U_FRONTEND",
text: "I changed the UI.",
- ts: "1700000000.000004",
+ ts: "1700000000.000005",
thread_ts: threadTs,
},
{
@@ -1255,18 +1265,25 @@ describe("slackChannel() inbound mention pipeline", () => {
onAppMention: () => ({ auth: null }),
threadContext: { since: "last-agent-reply" },
});
- const { body } = buildMentionBody({
- threadTs,
- ts: currentTs,
- text: "Summarize ownership.",
- user: "U_CURRENT",
- });
+ const body = buildEventBody(
+ {
+ channel: "C01",
+ event_ts: currentTs,
+ text: "Summarize ownership.",
+ thread_ts: threadTs,
+ ts: currentTs,
+ type: "app_mention",
+ user: "U_CURRENT",
+ },
+ { authorizations: [{ is_bot: true, user_id: "U_BOT" }] },
+ );
const { send } = await firePost(channel, buildSignedRequest({ body }));
const [{ message }] = send.mock.calls[0]! as [{ message: string }];
expect(message).toContain("<slack_thread_context>");
expect(message).toContain("sender_id: U_BACKEND");
+ expect(message).toContain("sender_id: U_OTHER_BOT");
expect(message).toContain("sender_id: U_FRONTEND");
expect(message).toContain("sender_id: U_CURRENT");
expect(message).not.toContain("sender_id: U_ROOT");
diff --git a/packages/eve/src/public/channels/slack/slackChannel.ts b/packages/eve/src/public/channels/slack/slackChannel.ts
index 6052c308b..198f6f61a 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.ts
@@ -780,6 +780,8 @@ async function handleEventPost(input: {
}
if (envelope === null) return new Response("ok");
+ const appId = typeof envelope.api_app_id === "string" ? envelope.api_app_id : undefined;
+ const botUserId = slackEventBotUserId(envelope);
// Handler precedence, in fall-through order:
// 1) an authored mention/DM handler for its own event kind,
@@ -791,10 +793,12 @@ async function handleEventPost(input: {
if (payload.kind === "app_mention" || payload.kind === "direct_message") {
const kind = payload.kind;
const message = slackMessageFromWebhookPayload(payload);
- if (message !== null && !isSelfAuthoredSlackMessage(envelope, message)) {
+ if (message !== null && !isSelfAuthoredSlackMessage({ appId, botUserId }, message)) {
const dispatchMessageWith =
(handler: NonNullable<SlackChannelConfig["onAppMention"]>) => () =>
dispatchInboundMessage({
+ appId,
+ botUserId,
credentials: config.credentials,
handler,
kind,
@@ -808,7 +812,8 @@ async function handleEventPost(input: {
if (handler !== undefined) {
dispatch = () =>
dispatchSlackMessage({
- botUserId: slackEventBotUserId(envelope),
+ appId,
+ botUserId,
credentials: config.credentials,
handler,
kind,
@@ -828,13 +833,13 @@ async function handleEventPost(input: {
if (dispatch === null && config.onMessage !== undefined) {
const message = parseMessageEvent(envelope);
- if (message !== null && !isSelfAuthoredSlackMessage(envelope, message)) {
- const botUserId = slackEventBotUserId(envelope);
+ if (message !== null && !isSelfAuthoredSlackMessage({ appId, botUserId }, message)) {
// Slack also emits message.channels for an app mention. The app_mention
// callback owns that user action so the generic message is not duplicated.
if (botUserId === undefined || !message.text.includes(`<@${botUserId}`)) {
dispatch = () =>
dispatchSlackMessage({
+ appId,
botUserId,
credentials: config.credentials,
handler: config.onMessage!,
@@ -884,19 +889,21 @@ async function handleEventPost(input: {
return new Response("ok");
}
-function isSelfAuthoredSlackMessage(envelope: SlackEventEnvelope, message: SlackMessage): boolean {
- const botUserId = slackEventBotUserId(envelope);
- if (botUserId !== undefined && message.author?.userId === botUserId) {
+function isSelfAuthoredSlackMessage(
+ identity: { readonly appId: string | undefined; readonly botUserId: string | undefined },
+ message: SlackMessage,
+): boolean {
+ if (identity.botUserId !== undefined && message.author?.userId === identity.botUserId) {
return true;
}
// App-authored message events can omit `user`, so match Slack's app identity
// as a fallback. Other bots keep flowing to authored onMessage handlers.
- const apiAppId = typeof envelope.api_app_id === "string" ? envelope.api_app_id : undefined;
- return apiAppId !== undefined && message.raw.app_id === apiAppId;
+ return identity.appId !== undefined && message.raw.app_id === identity.appId;
}
async function dispatchSlackMessage(input: {
+ readonly appId: string | undefined;
readonly botUserId: string | undefined;
readonly credentials: SlackChannelCredentials | undefined;
readonly handler: NonNullable<SlackChannelConfig["onMessage"]>;
@@ -910,7 +917,9 @@ async function dispatchSlackMessage(input: {
readonly uploadPolicy: UploadPolicy;
}): Promise<void> {
const { thread, slack } = buildSlackBinding({
+ appId: input.appId,
botToken: input.credentials?.botToken,
+ botUserId: input.botUserId,
channelId: input.message.channelId,
threadTs: input.message.threadTs,
teamId: input.message.teamId,
@@ -1030,6 +1039,8 @@ async function verifyInbound(
* handler never crashes the webhook ACK.
*/
async function dispatchInboundMessage(input: {
+ readonly appId: string | undefined;
+ readonly botUserId: string | undefined;
readonly kind: "app_mention" | "direct_message";
readonly message: SlackMessage;
readonly handler:
@@ -1042,7 +1053,9 @@ async function dispatchInboundMessage(input: {
}): Promise<void> {
const { message, kind } = input;
const { thread, slack } = buildSlackBinding({
+ appId: input.appId,
botToken: input.credentials?.botToken,
+ botUserId: input.botUserId,
channelId: message.channelId,
threadTs: message.threadTs,
teamId: message.teamId,