Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Stop stripping the bot mention from Slack messages (#2256)

Joshua Franced7ccedfe7766
diff --git a/src/slack/lib.ts b/src/slack/lib.ts
index 0afe629b2..1b4a7094a 100644
--- a/src/slack/lib.ts
+++ b/src/slack/lib.ts
@@ -4,7 +4,6 @@ export { safeChunks } from "./safe-cut.ts";
export {
decodeSlackEntities,
resolveMentionsInText,
- stripMention,
neutralizeMassMentions,
neutralizeMentions,
wireMentionKeys,
diff --git a/src/slack/message-gating.ts b/src/slack/message-gating.ts
index 634970ed2..509cc1331 100644
--- a/src/slack/message-gating.ts
+++ b/src/slack/message-gating.ts
@@ -199,8 +199,12 @@ export async function dedupedRun(
return true;
}
-export function isBareStop(text: string): boolean {
- return /^stop[.!]?$/i.test(text.trim());
+export function isBareStop(text: string, botHandle = ""): boolean {
+ const trimmed = text.trim();
+ const tag = botHandle ? `@${botHandle}` : "";
+ const afterTag = tag && trimmed.startsWith(tag) ? trimmed.slice(tag.length) : undefined;
+ const rest = afterTag !== undefined && /^\s/.test(afterTag) ? afterTag : trimmed;
+ return /^stop[.!]?$/i.test(rest.trim());
}
export function createInFlightThreadMap(): {
@@ -220,12 +224,13 @@ export function createInFlightThreadMap(): {
export async function maybeInterceptStop(opts: {
text: string;
+ botHandle?: string;
threadRef: string;
getInFlightRun: (threadRef: string) => string | undefined | Promise<string | undefined>;
signalAbort: (runId: string) => Promise<void>;
stopConversation?: (threadRef: string) => Promise<boolean>;
}): Promise<boolean> {
- if (!isBareStop(opts.text)) return false;
+ if (!isBareStop(opts.text, opts.botHandle)) return false;
if (opts.stopConversation) return opts.stopConversation(opts.threadRef);
const runId = await opts.getInFlightRun(opts.threadRef);
if (!runId) return false;
diff --git a/src/slack/mrkdwn.ts b/src/slack/mrkdwn.ts
index 84ff7683c..afba0000a 100644
--- a/src/slack/mrkdwn.ts
+++ b/src/slack/mrkdwn.ts
@@ -20,11 +20,6 @@ export function resolveMentions(text: string, nameById: ReadonlyMap<string, stri
});
}
-export function stripMention(text: string, botUserId: string): string {
- const withoutMention = botUserId ? text.replace(new RegExp(`<@${botUserId}>`, "g"), "") : text;
- return decodeSlackEntities(withoutMention).trim();
-}
-
const BOLD_SENTINEL = String.fromCharCode(1);
const STASH_OPEN = String.fromCharCode(0);
diff --git a/src/slack/turn-handler.ts b/src/slack/turn-handler.ts
index 4e8340a54..a42684d17 100644
--- a/src/slack/turn-handler.ts
+++ b/src/slack/turn-handler.ts
@@ -52,10 +52,10 @@ import {
refusalDelivery,
refusalNote,
renderConversationView,
+ resolveMentions,
resolveReactionTargets,
shouldSurfaceReaction,
slackReplyArgs,
- stripMention,
threadHasBotStake,
toSlackMrkdwn,
uploadAttachments,
@@ -216,7 +216,10 @@ export function createTurnHandler(deps: {
if (deps.externalAccess && inc.unprompted) inc = { ...inc, unprompted: false };
if (deps.allowActor && !deps.allowActor(actor)) return;
const timezone = classified.timezone;
- const text = stripMention(inc.rawText, ids.botUserId);
+ const text = resolveMentions(
+ decodeSlackEntities(inc.rawText).trim(),
+ ids.botUserId && ids.botHandle ? new Map([[ids.botUserId, ids.botHandle]]) : undefined,
+ );
if (!hasContent(text, inc.files)) return;
let audience: ActorAssertion[] = [actor];
@@ -368,6 +371,7 @@ export function createTurnHandler(deps: {
if (!inc.unprompted) {
const intercepted = await maybeInterceptStop({
text,
+ botHandle: ids.botHandle,
threadRef,
getInFlightRun: async (ref) =>
(await core.activeRunForThread(ref).catch(swallowAs("slack: active-run lookup", undefined))) ??
diff --git a/test/slack-index.integration.test.ts b/test/slack-index.integration.test.ts
index 89066ee11..2038365ab 100644
--- a/test/slack-index.integration.test.ts
+++ b/test/slack-index.integration.test.ts
@@ -1239,7 +1239,7 @@ test("an internal channel mention carries the complete audience and thread conte
f.client.messagesByChannel.set("C1", [event]);
await f.app.emitEvent("app_mention", event);
assert.equal(f.core.turns.length, 1);
- assert.equal(f.core.turns[0].text, "status?");
+ assert.equal(f.core.turns[0].text, "@qmbot status?");
assert.equal(f.core.turns[0].conversation.threadRef, "ch:C1:104.1");
assert.equal(f.core.turns[0].conversation.channelRef, "C1");
assert.deepEqual(f.core.turns[0].conversation.audience.map((a: any) => a.externalId).sort(), ["U1", "U2"]);
diff --git a/test/slack-message-gating.test.ts b/test/slack-message-gating.test.ts
index 1ca6f514a..9489d1ef1 100644
--- a/test/slack-message-gating.test.ts
+++ b/test/slack-message-gating.test.ts
@@ -205,6 +205,8 @@ test("createDeduper suppresses a redelivery of the same message with a different
test("isBareStop accepts case-insensitive stop with optional trailing . or !", () => {
for (const t of ["stop", "Stop", "STOP", " stop ", "stop.", "stop!"]) assert.equal(isBareStop(t), true, t);
for (const t of ["please stop the job", "stop it", "stop?", "stopping", ""]) assert.equal(isBareStop(t), false, t);
+ for (const t of ["@qm stop", "@qm Stop!"]) assert.equal(isBareStop(t, "qm"), true, t);
+ for (const t of ["@qm stop it", "@qmstop", "@alice stop", "@here stop"]) assert.equal(isBareStop(t, "qm"), false, t);
});
test("maybeInterceptStop: bare stop with in-flight run signals abort and suppresses the turn", async () => {
diff --git a/test/slack-mrkdwn.test.ts b/test/slack-mrkdwn.test.ts
index db6b630ec..3c6b9df68 100644
--- a/test/slack-mrkdwn.test.ts
+++ b/test/slack-mrkdwn.test.ts
@@ -2,7 +2,6 @@ import { test } from "node:test";
import assert from "node:assert/strict";
import {
decodeSlackEntities,
- stripMention,
toSlackMrkdwn,
slackSectionBlocks,
resolveMentionsInText,
@@ -20,10 +19,8 @@ test("inlineCode clamps long text and neutralizes backticks", () => {
assert.match(clamped, /\.\.\.`$/);
});
-test("decodeSlackEntities / stripMention", () => {
+test("decodeSlackEntities", () => {
assert.equal(decodeSlackEntities("echo a &gt; b &amp;&amp; c"), "echo a > b && c");
- assert.equal(stripMention("<@BOT> hello", "BOT"), "hello");
- assert.equal(stripMention("<@BOT>", "BOT"), "");
});
test("toSlackMrkdwn: bold uses single * (the screenshot bug: **x** rendered literally)", () => {
diff --git a/test/slack-unprompted-ack.test.ts b/test/slack-unprompted-ack.test.ts
index e3babdd03..3cb874d1c 100644
--- a/test/slack-unprompted-ack.test.ts
+++ b/test/slack-unprompted-ack.test.ts
@@ -5,7 +5,7 @@ import { createConversationSerializer } from "../src/slack/conversation-view.ts"
import { createThreadTracker, createDeduper } from "../src/slack/lib.ts";
import type { BotIdentity, Directory } from "../src/slack/directory.ts";
-const ids = { botUserId: "UBOT", ownBotId: "BBOT" } as BotIdentity;
+const ids = { botUserId: "UBOT", ownBotId: "BBOT", botHandle: "qm" } as BotIdentity;
const actor = { externalId: "U1", displayName: "Teammate" };
const directory = {
classifyUserCached: async () => ({ actor }),
@@ -174,3 +174,17 @@ test("waitRun signals replying only once the run has passed detection and begun
});
assert.equal(replying, 1);
});
+
+test("a mention-only thread reply starts a turn with the tag intact", async () => {
+ const h = harness(async () => ({ status: "ok", reply: "On it." }));
+ await h.handler.handleIncoming({ ...h.followup, unprompted: false, rawText: "<@UBOT>" }, h.client);
+ assert.equal(h.turns.length, 1);
+ assert.equal(h.turns[0].text, "@qm");
+ assert.deepEqual(h.posts, ["On it."]);
+});
+
+test("the bot's own mention reaches the agent instead of being stripped", async () => {
+ const h = harness(async () => ({ status: "ok", reply: "Done." }));
+ await h.handler.handleIncoming({ ...h.followup, unprompted: false, rawText: "<@UBOT> check &amp; report" }, h.client);
+ assert.equal(h.turns[0].text, "@qm check & report");
+});