Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Preserve canonical Slack mention IDs in mirror storage (#1249)

Regan Bell66488ee31345
diff --git a/src/slack/history.ts b/src/slack/history.ts
index 442550074..48d20043f 100644
--- a/src/slack/history.ts
+++ b/src/slack/history.ts
@@ -1,7 +1,7 @@
import { slackMessageToIngestEvent } from "./mirror.ts";
import { messageWithForwardedContent } from "./forwards.ts";
import type { SlackContextSource } from "./config.ts";
-import { decodeSlackEntities, resolveMentionsInText } from "./lib.ts";
+import { decodeSlackEntities } from "./lib.ts";
import { slackHistoryRateLimitMessage } from "./history-rate-limit.ts";
import type { SlackCoreClient } from "../api/slack-core-client.ts";
import type { CachedMessage } from "../surface-cache/types.ts";
@@ -136,8 +136,7 @@ export function createSlackHistoryReader(deps: {
if (!stored || stored.deleted) continue;
storedMessages++;
const content = messageWithForwardedContent(message);
- const normalize = (text: string) => resolveMentionsInText(text, (id) => stored.mentions?.[id]);
- const textMatches = normalize(stored.text) === normalize(decodeSlackEntities(content.text));
+ const textMatches = stored.text === decodeSlackEntities(content.text);
const parentMatches =
stored.sub === (message.thread_ts && message.thread_ts !== message.ts ? message.thread_ts : undefined);
const files = (values: Array<{ id?: string; name?: string; mimetype?: string }>) =>
diff --git a/src/slack/mirror.ts b/src/slack/mirror.ts
index bb4f18838..10f1d5eb3 100644
--- a/src/slack/mirror.ts
+++ b/src/slack/mirror.ts
@@ -1,5 +1,5 @@
import { swallow } from "../util/errors.ts";
-import { decodeSlackEntities, mentionsBot, resolveMentionsInText } from "./lib.ts";
+import { decodeSlackEntities, mentionsBot } from "./lib.ts";
import { messageWithForwardedContent } from "./forwards.ts";
import type { SlackCoreClient } from "../api/slack-core-client.ts";
import type { IngestEvent } from "../surface-cache/surface-cache.ts";
@@ -9,7 +9,6 @@ import { MAX_NAME_LOOKUPS } from "./conversation-view.ts";
export interface Mirror {
pushSurfaceEvents(events: IngestEvent[]): Promise<void>;
- resolveTextMentions(client: any, text: string): Promise<{ text: string; mentions: Record<string, string> }>;
mirrorMessageEvent(
m: Partial<SlackMessageEvent>,
client: any,
@@ -83,10 +82,7 @@ export function createMirror(deps: {
await core.ingestSurfaceEvents(events, { name: ids.botHandle, mentionId: ids.botUserId });
}
- async function resolveTextMentions(
- client: any,
- text: string,
- ): Promise<{ text: string; mentions: Record<string, string> }> {
+ async function resolveMentionNames(client: any, text: string): Promise<Record<string, string>> {
const mentionIds = new Set<string>();
for (const match of text.matchAll(/<@(U\w+)(?:\|[^>]*)?>/g)) mentionIds.add(match[1] as string);
const names = new Map<string, string>();
@@ -100,7 +96,7 @@ export function createMirror(deps: {
}
}),
);
- return { text: resolveMentionsInText(text, (id) => names.get(id)), mentions: Object.fromEntries(names) };
+ return Object.fromEntries(names);
}
async function mirrorMessageEvent(
@@ -138,9 +134,9 @@ export function createMirror(deps: {
}
const content = messageWithForwardedContent(m);
const raw = content.text;
- const { text, mentions } = await resolveTextMentions(client, decodeSlackEntities(raw));
- await pushSurfaceEvents([slackMessageToIngestEvent(m, ids, { ...opts, text, mentions, kind })]);
+ const mentions = await resolveMentionNames(client, decodeSlackEntities(raw));
+ await pushSurfaceEvents([slackMessageToIngestEvent(m, ids, { ...opts, mentions, kind })]);
}
- return { pushSurfaceEvents, resolveTextMentions, mirrorMessageEvent };
+ return { pushSurfaceEvents, mirrorMessageEvent };
}
diff --git a/test/slack-mirror-fidelity.test.ts b/test/slack-mirror-fidelity.test.ts
index bce90e4da..4b58908af 100644
--- a/test/slack-mirror-fidelity.test.ts
+++ b/test/slack-mirror-fidelity.test.ts
@@ -1,9 +1,10 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { createMemorySurfaceCache, createPostgresSurfaceCache } from "../src/surface-cache/surface-cache.ts";
-import { slackMessageToIngestEvent } from "../src/slack/mirror.ts";
+import { createMirror, slackMessageToIngestEvent } from "../src/slack/mirror.ts";
import { toEvent } from "../src/api/routes/surface-cache.ts";
import { createSlackHistoryReader } from "../src/slack/history.ts";
+import { buildContextWindow } from "../src/slack/conversation.ts";
import { createConversationSerializer } from "../src/slack/conversation-view.ts";
import type { SlackCoreClient } from "../src/api/slack-core-client.ts";
import type { BotIdentity, Directory } from "../src/slack/directory.ts";
@@ -150,3 +151,50 @@ test("live and shadow do not repair their own comparison source", async () => {
assert.equal(writes, 0);
}
});
+
+test("event mirroring preserves mention IDs for current-context rendering", async () => {
+ const cache = createMemorySurfaceCache();
+ const core = {
+ ingestSurfaceEvents: async (events) => {
+ await cache.ingest(events);
+ },
+ readSurfaceMessages: cache.readMessages,
+ } as SlackCoreClient;
+ const mentionDirectory = {
+ classifyUserCached: async () => ({ actor: { displayName: "Old name" } }),
+ } as unknown as Directory;
+ const mirror = createMirror({
+ core,
+ ids,
+ directory: mentionDirectory,
+ externalParticipantsEnabled: async () => true,
+ });
+ const raw = {
+ channel: "C",
+ channel_type: "channel" as const,
+ ts: "1000",
+ text: "Hi <@U1> and <@U2> &amp; welcome",
+ user: "U1",
+ };
+ await mirror.mirrorMessageEvent(raw, {});
+ const stored = (await cache.readMessages("C"))[0]!;
+ assert.equal(stored.text, "Hi <@U1> and <@U2> & welcome");
+ assert.deepEqual(stored.mentions, { U1: "Old name", U2: "Old name" });
+ const shaped = [];
+ for (const source of ["live", "mirror"] as const) {
+ const readHistory = createSlackHistoryReader({ core, ids, source });
+ const serializer = createConversationSerializer({
+ ids,
+ directory: mentionDirectory,
+ externalParticipantsEnabled: async () => true,
+ readHistory,
+ });
+ const page = await readHistory({ conversations: { history: async () => ({ messages: [raw] }) } }, "C");
+ shaped.push(await serializer.shapeRecentMessages({}, page.raw, "", new Map([["U1", "New name"]])));
+ }
+ assert.deepEqual(shaped[1], shaped[0]);
+ assert.equal(shaped[1]![0]!.text, "Hi <@U1> and <@U2> & welcome");
+ assert.equal(shaped[1]![0]!.name, "New name");
+ const context = buildContextWindow(shaped[1]!, { count: 20, nameById: new Map([["U1", "New name"]]) });
+ assert.equal(context.messages[0]!.text, "Hi @New name and <@U2> & welcome");
+});
diff --git a/test/slack-mirror-soak.test.ts b/test/slack-mirror-soak.test.ts
index 2d8b7f683..3fdda6104 100644
--- a/test/slack-mirror-soak.test.ts
+++ b/test/slack-mirror-soak.test.ts
@@ -204,7 +204,7 @@ test("shadow distinguishes stored messages with wrong parents from ingestion los
assert.equal(result.matchingMessages, 1);
});
-test("shadow normalizes mentions without erasing literal entity or attachment differences", async (t) => {
+test("shadow compares canonical mention IDs without erasing literal entity or attachment differences", async (t) => {
const { createMemorySurfaceCache } = await import("../src/surface-cache/surface-cache.ts");
const cache = createMemorySurfaceCache();
await cache.ingest([
@@ -383,3 +383,20 @@ test("shadow freezes live messages and attachments before callers modify their c
assert.equal(comparison.fileMismatches, 0);
assert.equal(comparison.liveMessagesMissingFromStorage, 0);
});
+
+test("shadow reports legacy rewritten mentions as a difference", async (t) => {
+ const { createMemorySurfaceCache } = await import("../src/surface-cache/surface-cache.ts");
+ const cache = createMemorySurfaceCache();
+ await cache.ingest([{ container: "C", ts: "1", text: "Hi @Alice", mentions: { U1: "Alice" } }]);
+ const logs: string[] = [];
+ t.mock.method(console, "info", (line: string) => logs.push(line));
+ const read = createSlackHistoryReader({
+ ids,
+ source: "shadow",
+ core: { readSurfaceMessages: cache.readMessages } as unknown as SlackCoreClient,
+ });
+ await read({ conversations: { history: async () => ({ messages: [{ ts: "1", text: "Hi <@U1>" }] }) } }, "C");
+ await new Promise((resolve) => setImmediate(resolve));
+ assert.equal(JSON.parse(logs[0]!).textMismatches, 1);
+ assert.equal(JSON.parse(logs[0]!).matchingMessages, 0);
+});