Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Restore cached mention names in ambient judge prompts (#1267)

Regan Bell1451c4a4cf44
diff --git a/src/slack/conversation.ts b/src/slack/conversation.ts
index 442d8036e..63905a607 100644
--- a/src/slack/conversation.ts
+++ b/src/slack/conversation.ts
@@ -1,3 +1,5 @@
+import { resolveMentions } from "./mrkdwn.ts";
+export { resolveMentions } from "./mrkdwn.ts";
import { encodeTs, summarizeReactions, type ReactionTally } from "./reactions.ts";
import { utcMinute } from "../util/time.ts";
import { MAX_ATTACHMENTS_PER_TURN } from "./attachments.ts";
@@ -116,16 +118,6 @@ function arrangeForDisplay(messages: readonly RecentMessage[]): Array<{ message:
return out;
}
-const SLACK_MENTION = /<@([A-Z0-9]+)(?:\|[^>]*)?>/g;
-
-export function resolveMentions(text: string, nameById: ReadonlyMap<string, string> | undefined): string {
- if (!nameById || !text.includes("<@")) return text;
- return text.replace(SLACK_MENTION, (m, id) => {
- const name = nameById.get(id);
- return name ? `@${name}` : m;
- });
-}
-
interface ConversationMember {
id: string;
name: string;
diff --git a/src/slack/mrkdwn.ts b/src/slack/mrkdwn.ts
index 1ebdaf6ee..84ff7683c 100644
--- a/src/slack/mrkdwn.ts
+++ b/src/slack/mrkdwn.ts
@@ -10,6 +10,16 @@ export function resolveMentionsInText(text: string, lookup: (id: string) => stri
});
}
+const SLACK_MENTION = /<@([A-Z0-9]+)(?:\|[^>]*)?>/g;
+
+export function resolveMentions(text: string, nameById: ReadonlyMap<string, string> | undefined): string {
+ if (!nameById || !text.includes("<@")) return text;
+ return text.replace(SLACK_MENTION, (m, id) => {
+ const name = nameById.get(id);
+ return name ? `@${name}` : m;
+ });
+}
+
export function stripMention(text: string, botUserId: string): string {
const withoutMention = botUserId ? text.replace(new RegExp(`<@${botUserId}>`, "g"), "") : text;
return decodeSlackEntities(withoutMention).trim();
diff --git a/src/surface-cache/ambient-judge.ts b/src/surface-cache/ambient-judge.ts
index 08d68c490..d106222be 100644
--- a/src/surface-cache/ambient-judge.ts
+++ b/src/surface-cache/ambient-judge.ts
@@ -1,3 +1,4 @@
+import { resolveMentions } from "../slack/mrkdwn.ts";
import type { CachedMessage } from "./surface-cache.ts";
const AMBIENT_JUDGE_SYSTEM = `You are the ambient mind for a chat container — a thin, cheap observer that decides whether the
@@ -49,7 +50,7 @@ export interface AmbientBatch {
export function renderAmbientPrompt(batch: AmbientBatch): string {
const fmt = (m: CachedMessage): string =>
- `${m.authorName || m.authorId || "someone"}${m.bot ? " (bot)" : ""}: ${m.text}`;
+ `${m.authorName || m.authorId || "someone"}${m.bot ? " (bot)" : ""}: ${resolveMentions(m.text, m.mentions ? new Map(Object.entries(m.mentions)) : undefined)}`;
const fmtBackdrop = (m: CachedMessage): string =>
m.handled || m.mentionsSelf ? `${fmt(m)} [handled by the direct responder — do not re-engage]` : fmt(m);
const shown = (ms: CachedMessage[]): CachedMessage[] => ms.filter((m) => !m.deleted && (m.text ?? "").trim());
diff --git a/test/ambient-judge.test.ts b/test/ambient-judge.test.ts
index d9ee221d0..a2d17c44d 100644
--- a/test/ambient-judge.test.ts
+++ b/test/ambient-judge.test.ts
@@ -91,3 +91,29 @@ test("parseAmbientDecision reads asked_by (only on an engage)", () => {
assert.deepEqual(parseAmbientDecision('{"act": true, "asked_by": " "}'), { act: true });
assert.deepEqual(parseAmbientDecision('{"act": true, "asked_by": "[2.0]"}'), { act: true, askedBy: "2.0" });
});
+
+test("ambient rendering resolves cached mention names without changing stored messages", () => {
+ const message = {
+ container: "C1",
+ ts: "2.0",
+ authorName: "Alice",
+ text: "<@U2> ask <@U3|old label> and <@U9> about @already-named",
+ mentions: { U2: "Bob", U3: "Carol" },
+ createdAt: 2,
+ };
+ const batch = base({
+ messages: [message, { ...message, ts: "3.0", mentions: undefined }],
+ backdrop: [{ ...message, ts: "1.0", handled: true }],
+ });
+ const original = structuredClone(batch);
+ const prompt = renderAmbientPrompt(batch);
+ assert.ok(
+ prompt.includes(
+ "Alice: @Bob ask @Carol and <@U9> about @already-named [handled by the direct responder — do not re-engage]",
+ ),
+ );
+ assert.ok(prompt.includes("[2.0] Alice: @Bob ask @Carol and <@U9> about @already-named"));
+ assert.ok(prompt.includes(`[3.0] Alice: ${message.text}`));
+ assert.ok(prompt.includes("mentioned as <@U00000002>"));
+ assert.deepEqual(batch, original);
+});