Skip to content

Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.

TypeScript

feat(agents): tell an agent when a message @-mentions it by name (#88)

Xialie Zhuang784ebe7c759e
diff --git a/server/src/__tests__/scheduler-mentions.test.ts b/server/src/__tests__/scheduler-mentions.test.ts
new file mode 100644
index 0000000..13fd76a
--- /dev/null
+++ b/server/src/__tests__/scheduler-mentions.test.ts
@@ -0,0 +1,85 @@
+/**
+ * `mentionedAgentIds` — the deterministic "this message names you" signal.
+ *
+ * It reuses the exact-token rule `shouldDeliverToMutedAgent` already uses, so
+ * "addressed to" means one thing everywhere. It is a PROMPT signal only: the
+ * recipient set is unchanged, deliberately. Narrowing the fan-out to mentioned
+ * agents is unsafe — a mentioned BYOA agent with a closed laptop has its wake
+ * deferred while the peers who would have covered were never woken, and an
+ * excluded peer who posts anything else auto-acks its read cursor to NOW
+ * (`cumora reply`), putting the human's message permanently behind the cursor.
+ *
+ * Run: node --import tsx --test server/src/__tests__/scheduler-mentions.test.ts
+ */
+import { after, test } from 'node:test'
+import assert from 'node:assert/strict'
+import { mentionedAgentIds, shouldDeliverToMutedAgent } from '../agents/scheduler.js'
+import { pool } from '../db/pool.js'
+
+after(async () => {
+ // scheduler.ts transitively imports redis.ts which opens connections — without
+ // this the runner never exits and CI cancels the job rather than failing it.
+ try { await pool.end() } catch { /* ignore */ }
+ try {
+ const { redis, sub } = await import('../redis.js')
+ redis.disconnect()
+ sub.disconnect()
+ } catch { /* ignore */ }
+})
+
+const MEMBERS = ['nova-12', 'iris-9', 'atlas-7']
+
+test('an exact mention selects that agent', () => {
+ assert.deepEqual(mentionedAgentIds('please check this @nova-12', MEMBERS), ['nova-12'])
+})
+
+test('mentions are case-insensitive', () => {
+ assert.deepEqual(mentionedAgentIds('ping @NOVA-12, please', MEMBERS), ['nova-12'])
+})
+
+test('several mentions all come back, in member order', () => {
+ assert.deepEqual(mentionedAgentIds('@atlas-7 and @nova-12 pair on this', MEMBERS), ['nova-12', 'atlas-7'])
+})
+
+test('an unmentioned room yields nobody', () => {
+ assert.deepEqual(mentionedAgentIds('ordinary room chatter', MEMBERS), [])
+})
+
+test('a longer id is not matched by a shorter one', () => {
+ // The trap `shouldDeliverToMutedAgent` already guards: @nova-123 must not
+ // read as a mention of nova-12.
+ assert.deepEqual(mentionedAgentIds('this is for @nova-123', MEMBERS), [])
+})
+
+test('an email address is not a mention', () => {
+ assert.deepEqual(mentionedAgentIds('mail nova@nova-12 about it', MEMBERS), [])
+})
+
+test('@all is not an individual mention', () => {
+ // @all is a broadcast, handled separately in the prompt; it must not read as
+ // naming any particular agent.
+ assert.deepEqual(mentionedAgentIds('@all standup in 5', MEMBERS), [])
+})
+
+test('an empty body mentions nobody', () => {
+ assert.deepEqual(mentionedAgentIds('', MEMBERS), [])
+})
+
+test('it agrees with the muted-delivery rule on the same inputs', () => {
+ // One definition of "addressed to me" across delivery and prompting — if
+ // these ever disagree, an agent could be told a message is theirs by one rule
+ // and denied it by the other.
+ for (const body of [
+ 'please check this @nova-12',
+ 'ping @NOVA-12, please',
+ 'this is for @nova-123',
+ 'mail nova@nova-12 about it',
+ 'ordinary room chatter',
+ ]) {
+ assert.equal(
+ mentionedAgentIds(body, ['nova-12']).length > 0,
+ shouldDeliverToMutedAgent({ agentId: 'nova-12', conversationKind: 'group', body, quotedAuthorId: null }),
+ `disagreement on: ${body}`,
+ )
+ }
+})
diff --git a/server/src/agents/scheduler.ts b/server/src/agents/scheduler.ts
index 9637b73..5c9dc31 100644
--- a/server/src/agents/scheduler.ts
+++ b/server/src/agents/scheduler.ts
@@ -635,6 +635,27 @@ export function shouldDeliverToMutedAgent(args: {
return mention.test(args.body)
}
+/** Which of `agentMemberIds` this message explicitly @-mentions.
+ *
+ * Same token rule as {@link shouldDeliverToMutedAgent} — an exact `@id`, not a
+ * prefix and not an email — so "addressed to" means one thing everywhere.
+ *
+ * This is a PROMPT signal, never a delivery decision. Narrowing the wake to
+ * the mentioned agents looks tempting and is not safe: a mentioned BYOA agent
+ * whose laptop is closed has its wake deferred, and the peers who would have
+ * covered were never woken — so the room stays silent. Worse, an excluded peer
+ * who posts anything else in that room auto-acks their read cursor to NOW
+ * (`cumora reply`), which puts the human's message permanently BEHIND the
+ * cursor: not deferred, gone. So every member still wakes, and the mention only
+ * tells them who it was for.
+ *
+ * Exported for tests. */
+export function mentionedAgentIds(body: string, agentMemberIds: readonly string[]): string[] {
+ if (!body) return []
+ return agentMemberIds.filter((id) =>
+ new RegExp(`(^|[^\\w@])@${escapeRegex(id)}(?![\\w-])`, 'i').test(body))
+}
+
/** Exported for tests — pulled out of `wake` so the wake-policy is
* easy to assert on. Fire-and-forget per recipient. */
function renderTriageNote(verdict: InboxTriageVerdict): string {
diff --git a/server/src/agents/turn.ts b/server/src/agents/turn.ts
index 3ff32d1..8913105 100644
--- a/server/src/agents/turn.ts
+++ b/server/src/agents/turn.ts
@@ -115,6 +115,7 @@ interface InboxRow {
}
import { classifyWake } from './turn-wake.js'
+import { mentionedAgentIds } from './scheduler.js'
export interface AgentTurnOptions {
/** Why this turn was started. Message-driven turns remain the default. */
@@ -632,6 +633,18 @@ function renderContext(
// quote target keep treating it as a general group message and
// chime in unprompted; this puts the address signal in the
// glance-zone of the wake prompt.
+ // An exact @-mention of the viewer is the strongest "this one is yours"
+ // signal a room carries, and it was only ever visible as raw text inside
+ // the body. Surface it in the same glance-zone as the quote tag below.
+ //
+ // Only the POSITIVE case is tagged. The mirror ("addressed to X — not
+ // you") would need the conversation's member list to tell a real
+ // participant from a stray @token, and the two errors are not
+ // symmetric: a wrong "not you" can silence the whole room, while a wrong
+ // "YOU" only costs one extra reply.
+ if (viewerAgentId && !m.is_self && mentionedAgentIds(m.body ?? '', [viewerAgentId]).length > 0) {
+ line += ` ↦ @-mentioned YOU`
+ }
if (m.quoted_message_id && m.quoted && !m.is_self) {
if (viewerAgentId && m.quoted.authorId === viewerAgentId) {
line += ` ↦ addressed to YOU (quote-reply)`