Multiplayer agent harness for work.
Preserve Slack mirror history across system messages and reactions (#1237)
Regan Bell4872fd275fdc
diff --git a/src/slack/events.ts b/src/slack/events.tsindex 183136e87..703f3c1b4 100644--- a/src/slack/events.ts+++ b/src/slack/events.ts@@ -18,6 +18,7 @@ import type { DenyResponder } from "./allow-from.ts";import { swallowAs } from "../util/errors.ts";import type { Mirror } from "./mirror.ts";import type { TurnHandler } from "./turn-handler.ts";+import { shouldMirrorMessage } from "./message-gating.ts";interface EventArgs {event: unknown;@@ -168,14 +169,12 @@ export function registerSlackEvents(const privacyChange = channelPrivacyChange(m);if (privacyChange) {await forceDirectorySync(client, privacyChange.channel);- return;}if (isGroupMembershipMessage(m)) {await forceDirectorySync(client);- return;}const ackGate = context.ackGate;- if (m.channel && m.ts && !m.subtype && !(m.bot_id || m.subtype === "bot_message")) {+ if (m.channel && m.ts && !m.hidden && !m.subtype && !(m.bot_id || m.subtype === "bot_message")) {deps.inboxMessage?.(client, {channel: m.channel,ts: m.ts,@@ -212,8 +211,9 @@ export function registerSlackEvents(if (m.subtype === "message_changed" && m.message) {const textChanged = m.previous_message?.text === undefined || m.previous_message.text !== m.message.text;const editedAt = Number(m.message.edited?.ts) || (textChanged ? Number(m.ts) : 0);- if (shouldProcessMessage(m.message, "", ""))+ if (shouldMirrorMessage(m.message))await mirrorMessageEvent({ ...m.message, channel: m.channel, channel_type: m.channel_type }, client, {+ ...(!shouldProcessMessage(m.message, "", "") ? { handled: true } : {}),...(editedAt > 0 ? { editedAt: Math.round(editedAt * 1000) } : {}),...(m.channel_type === "im" ? { kind: "dm" as const } : {}),});@@ -237,11 +237,14 @@ export function registerSlackEvents(]);return;}- if (ownMessage(m) && shouldProcessMessage(m, "", "")) {- await mirrorMessageEvent(m, client, m.channel_type === "im" ? { kind: "dm" } : {});+ if (!shouldMirrorMessage(m)) return;+ if (ownMessage(m) || !shouldProcessMessage(m, ids.botUserId, ids.ownBotId)) {+ await mirrorMessageEvent(m, client, {+ handled: true,+ ...(m.channel_type === "im" ? { kind: "dm" } : {}),+ });return;}- if (!shouldProcessMessage(m, ids.botUserId, ids.ownBotId)) return;if (m.channel_type === "im") {const identity = await eventIdentity(client, m);diff --git a/src/slack/message-gating.ts b/src/slack/message-gating.tsindex 4550ce40c..c33c9f5a2 100644--- a/src/slack/message-gating.ts+++ b/src/slack/message-gating.ts@@ -35,6 +35,14 @@ export function shouldProcessMessage(return true;}+export function shouldMirrorMessage(m: { hidden?: boolean; subtype?: string; ts?: string }): boolean {+ return (+ Boolean(m.ts) &&+ m.hidden !== true &&+ !["message_changed", "message_deleted", "message_replied", "tombstone"].includes(m.subtype ?? "")+ );+}+export function isGroupMembershipMessage(m: { channel_type?: string; subtype?: string }): boolean {return m.channel_type === "mpim" && (m.subtype === "group_join" || m.subtype === "group_leave");}diff --git a/src/slack/payloads.ts b/src/slack/payloads.tsindex 6147d7a35..73933413e 100644--- a/src/slack/payloads.ts+++ b/src/slack/payloads.ts@@ -22,6 +22,7 @@ export interface SlackMessageEvent {channel: string;channel_type?: string;subtype?: string;+ hidden?: boolean;user?: string;username?: string;bot_id?: string;@@ -56,6 +57,7 @@ export function parseMessageEvent(event: unknown): SlackMessageEvent {channel: coerced(e.channel) ?? "",channel_type: str(e.channel_type),subtype: str(e.subtype),+ hidden: e.hidden === true,user: str(e.user),username: str(e.username),bot_id: str(e.bot_id),diff --git a/src/slack/turn-handler.ts b/src/slack/turn-handler.tsindex b511035eb..3706c5871 100644--- a/src/slack/turn-handler.ts+++ b/src/slack/turn-handler.ts@@ -401,7 +401,7 @@ export function createTurnHandler(deps: {await ack?.settle().catch(swallowAs("slack: ack settle", undefined));};- {+ if (!inc.synthetic) {const containerName = inc.kind === "dm" ? actor.displayName?.trim() || undefined : channelName;await mirrorMessageEvent({diff --git a/test/slack-mirror-ingestion.test.ts b/test/slack-mirror-ingestion.test.tsindex 8ba5331be..3300dc563 100644--- a/test/slack-mirror-ingestion.test.ts+++ b/test/slack-mirror-ingestion.test.ts@@ -29,7 +29,12 @@ function fixture() {const events: any[] = [];const dispatches: any[] = [];+ const refreshes: any[] = [];+ const inbox: any[] = [];const directory = options.directory ?? {+ forceDirectorySync: async (...args: any[]) => {+ refreshes.push(args);+ },getChannelInfo: async () => ({ id: "C1" }),allInternalRosters: async () =>new Map([@@ -65,6 +70,9 @@ function fixture(});app.error(async () => {});registerSlackEvents(app, {+ inboxMessage: (...args: any[]) => {+ inbox.push(args);+ },ids,mirror,directory,@@ -87,9 +95,83 @@ function fixture(});gate?.gate.persisted();}- return { mirror, events, dispatches, fire };+ return { mirror, events, dispatches, refreshes, inbox, fire };}+test("visible Slack system messages and their edits mirror without agent dispatch", async () => {+ const f = fixture();+ for (const subtype of [+ "channel_join",+ "channel_leave",+ "channel_topic",+ "channel_purpose",+ "channel_name",+ "channel_convert_to_private",+ "channel_convert_to_public",+ "group_join",+ "group_leave",+ ]) {+ const base = {+ type: "message",+ subtype,+ channel: "C1",+ channel_type: subtype.startsWith("group_") ? "mpim" : "channel",+ user: "U1",+ ts: "3.000001",+ text: "system context",+ };+ await f.fire(base);+ await f.fire({+ type: "message",+ subtype: "message_changed",+ channel: "C1",+ channel_type: base.channel_type,+ ts: "4.000001",+ message: { ...base, text: "updated context", edited: { ts: "4.000001" } },+ });+ }+ assert.equal(f.events.length, 18);+ assert.equal(f.refreshes.length, 4);+ assert.equal(f.dispatches.length, 0);+ assert.ok(f.events.every((e) => e.handled === true));+ assert.ok(f.events.filter((_, i) => i % 2 === 1).every((e) => e.text === "updated context" && e.editedAt === 4000));+});++test("hidden messages never enter context or dispatch", async () => {+ const f = fixture();+ const message = {+ type: "message",+ channel: "C1",+ channel_type: "channel",+ user: "U1",+ ts: "3.000001",+ text: "hidden control",+ hidden: true,+ };+ await f.fire(message);+ await f.fire({ ...message, subtype: "channel_topic" });+ await f.fire({ ...message, hidden: false, subtype: "message_replied" });+ await f.fire({ type: "message", subtype: "message_changed", channel: "C1", channel_type: "channel", message });+ assert.deepEqual(f.events, []);+ assert.deepEqual(f.dispatches, []);+ assert.deepEqual(f.inbox, []);+});++test("a system-message edit arriving first is already handled", async () => {+ const f = fixture();+ await f.fire({+ type: "message",+ subtype: "message_changed",+ channel: "C1",+ channel_type: "channel",+ ts: "4.000001",+ message: { subtype: "channel_topic", ts: "3.000001", user: "U1", text: "new topic", edited: { ts: "4.000001" } },+ });+ assert.equal(f.events.length, 1);+ assert.equal(f.events[0].handled, true);+ assert.deepEqual(f.dispatches, []);+});+test("real Bolt ingests own channel/DM messages and edits without responses or self reactions", async () => {const f = fixture();for (const channel of ["C1", "D1"]) {@@ -442,3 +524,46 @@ test("stake fallback bounds Slack replies while omitted cutoff and negative tracassert.equal(await handler.botHasStakeInThread(client, "C1", "5"), false);assert.deepEqual(calls[1], { channel: "C1", ts: "5", limit: 200 });});++test("reaction turns preserve the reacted message in the mirror", async () => {+ const cache = createMemorySurfaceCache();+ const f = fixture({+ ingest: async (events) => {+ await cache.ingest(events);+ },+ });+ const client = { reactions: { get: async () => ({ message: { text: "Original bot answer", user: "UBOT" } }) } };+ await f.mirror.mirrorMessageEvent(+ { channel: "D1", channel_type: "im", ts: "100.001", text: "Original bot answer", user: "UBOT" },+ client,+ { kind: "dm" },+ );+ const before = await cache.readMessages("D1", { limit: 10 });+ const turns: any[] = [];+ const handler = createTurnHandler({+ core: {},+ flow: {+ callCore: async (turn: any) => {+ turns.push(turn);+ return { status: "silent" };+ },+ },+ directory: { classifyUserCached: async () => ({ actor: { externalId: "U1", displayName: "Teammate" } }) },+ mirror: f.mirror,+ ids,+ threads: createThreadTracker(),+ deduper: createDeduper(),+ } as any);+ for (const added of [true, false]) {+ await handler.handleReactionEvent(+ { user: "U1", reaction: "thumbsup", item_user: "UBOT", item: { type: "message", channel: "D1", ts: "100.001" } },+ `reaction-${added}`,+ client,+ added,+ );+ }+ assert.equal(turns.length, 2);+ assert.match(turns[0].text, /\[Slack reaction\].*reacted/);+ assert.match(turns[1].text, /\[Slack reaction\].*removed/);+ assert.deepEqual(await cache.readMessages("D1", { limit: 10 }), before);+});