Skip to content

Commit db8b6ca

Browse files
authored
fix(mentions): match derived names with Unicode boundaries
1 parent 00affca commit db8b6ca

2 files changed

Lines changed: 86 additions & 49 deletions

File tree

‎src/auto-reply/reply/mentions.test.ts‎

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
// Tests mention detection and command trigger matching.
21
import { describe, expect, it } from "vitest";
3-
import { buildMentionRegexes, matchesMentionPatterns, stripStructuralPrefixes } from "./mentions.js";
2+
// Tests mention detection and command trigger matching.
3+
import type { MsgContext } from "../templating.js";
4+
import {
5+
buildMentionRegexes,
6+
matchesMentionPatterns,
7+
stripMentions,
8+
stripStructuralPrefixes,
9+
} from "./mentions.js";
410

511
describe("stripStructuralPrefixes", () => {
612
it("returns empty string for undefined input at runtime", () => {
@@ -51,42 +57,69 @@ describe("stripStructuralPrefixes", () => {
5157
});
5258
});
5359

54-
describe("CJK single-char mention matching (regression #87303)", () => {
55-
const cfgWithCjkName = {
56-
agents: {
57-
list: [{ id: "cjk-agent", identity: { name: "包" } }],
60+
describe("derived Unicode mention matching", () => {
61+
function configForName(name: string) {
62+
return {
63+
agents: {
64+
list: [{ id: "unicode-agent", identity: { name } }],
65+
},
66+
} as Parameters<typeof buildMentionRegexes>[0];
67+
}
68+
69+
it.each(["包", "苏苏", "あ", "김", "Jörg", "Б", "ع", "क"])(
70+
"matches standalone %s and rejects Unicode substrings",
71+
(name) => {
72+
const regexes = buildMentionRegexes(configForName(name), "unicode-agent");
73+
74+
expect(matchesMentionPatterns(`@${name} 你好`, regexes)).toBe(true);
75+
expect(matchesMentionPatterns(`${name} 你好`, regexes)).toBe(true);
76+
expect(matchesMentionPatterns(`前${name}後`, regexes)).toBe(false);
5877
},
59-
} as Parameters<typeof buildMentionRegexes>[0];
78+
);
6079

61-
const cfgWithCjkTwoChar = {
62-
agents: {
63-
list: [{ id: "cjk-two", identity: { name: "苏苏" } }],
64-
},
65-
} as Parameters<typeof buildMentionRegexes>[0];
80+
it("does not match a Han name inside mixed Han/kana words", () => {
81+
const regexes = buildMentionRegexes(configForName("包"), "unicode-agent");
6682

67-
it("matches single-char CJK name with @ prefix in Chinese text", () => {
68-
const regexes = buildMentionRegexes(cfgWithCjkName, "cjk-agent");
69-
expect(regexes.length).toBeGreaterThan(0);
70-
expect(matchesMentionPatterns("@包 你好", regexes)).toBe(true);
83+
expect(matchesMentionPatterns("包みを開ける", regexes)).toBe(false);
84+
expect(matchesMentionPatterns("面包好吃", regexes)).toBe(false);
7185
});
7286

73-
it("matches single-char CJK name without @ prefix", () => {
74-
const regexes = buildMentionRegexes(cfgWithCjkName, "cjk-agent");
75-
expect(matchesMentionPatterns("包 你好", regexes)).toBe(true);
87+
it("does not match a name inside a grapheme with combining marks", () => {
88+
expect(
89+
matchesMentionPatterns("कि", buildMentionRegexes(configForName("क"), "unicode-agent")),
90+
).toBe(false);
91+
expect(
92+
matchesMentionPatterns("e\u0301", buildMentionRegexes(configForName("e"), "unicode-agent")),
93+
).toBe(false);
7694
});
7795

78-
it("does not match single-char CJK name inside a longer CJK word", () => {
79-
const regexes = buildMentionRegexes(cfgWithCjkName, "cjk-agent");
80-
expect(matchesMentionPatterns("面包好吃", regexes)).toBe(false);
96+
it("uses the same Unicode boundaries when stripping derived mentions", () => {
97+
const cfg = configForName("包");
98+
99+
expect(stripMentions("@包 你好", {} as MsgContext, cfg, "unicode-agent")).toBe("你好");
100+
expect(stripMentions("包みを開ける", {} as MsgContext, cfg, "unicode-agent")).toBe(
101+
"包みを開ける",
102+
);
81103
});
82104

83-
it("matches two-char CJK name with @ prefix", () => {
84-
const regexes = buildMentionRegexes(cfgWithCjkTwoChar, "cjk-two");
85-
expect(matchesMentionPatterns("@苏苏 你好", regexes)).toBe(true);
105+
it("keeps explicit configured patterns on their existing regex flags", () => {
106+
const regexes = buildMentionRegexes({
107+
messages: { groupChat: { mentionPatterns: [String.raw`\bopenclaw\b`] } },
108+
});
109+
110+
expect(regexes[0]?.flags).toBe("i");
86111
});
112+
});
87113

88-
it("does not match two-char CJK name as substring", () => {
89-
const regexes = buildMentionRegexes(cfgWithCjkTwoChar, "cjk-two");
90-
expect(matchesMentionPatterns("紫苏苏叶", regexes)).toBe(false);
114+
describe("CJK single-char mention matching (regression #87303)", () => {
115+
const cfgWithCjkName = {
116+
agents: {
117+
list: [{ id: "cjk-agent", identity: { name: "包" } }],
118+
},
119+
} as Parameters<typeof buildMentionRegexes>[0];
120+
121+
it("matches the reported standalone Han identity", () => {
122+
const regexes = buildMentionRegexes(cfgWithCjkName, "cjk-agent");
123+
expect(matchesMentionPatterns("@包 你好", regexes)).toBe(true);
91124
});
92125
});

‎src/auto-reply/reply/mentions.ts‎

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ import type { MsgContext } from "../templating.js";
1818
import type { BuildMentionRegexesOptions, ExplicitMentionSignal } from "./mentions.types.js";
1919
export type { BuildMentionRegexesOptions } from "./mentions.types.js";
2020

21-
const CJK_PATTERN = /[一-鿿㐀-䶿]/;
21+
type ResolvedMentionPatterns = {
22+
patterns: string[];
23+
unicode: boolean;
24+
};
2225

23-
function wrapWithBoundaries(pattern: string): string {
24-
if (!CJK_PATTERN.test(pattern)) {
25-
return String.raw`\b@?${pattern}\b`;
26-
}
27-
// \b does not recognise CJK characters as word characters, so single-char
28-
// CJK mention names (e.g. @包) fail to match. Use explicit lookaround
29-
// assertions that treat CJK characters as boundary-worthy.
30-
const lead = String.raw`(?:(?<=^)|(?<![\w一-鿿㐀-䶿]))`;
31-
const trail = String.raw`(?![\w一-鿿㐀-䶿])`;
32-
return `${lead}@?${pattern}${trail}`;
26+
const UNICODE_WORD_CHAR = String.raw`[\p{L}\p{M}\p{N}\p{Pc}\u200C\u200D]`;
27+
28+
function wrapDerivedMentionPattern(pattern: string): string {
29+
// JavaScript \b is ASCII-oriented. Derived identity names need Unicode word
30+
// boundaries so a name is neither missed nor matched inside another word.
31+
return `(?:@|(?<!${UNICODE_WORD_CHAR}))${pattern}(?!${UNICODE_WORD_CHAR})`;
3332
}
3433

3534
function deriveMentionPatterns(identity?: { name?: string; emoji?: string }) {
@@ -38,7 +37,7 @@ function deriveMentionPatterns(identity?: { name?: string; emoji?: string }) {
3837
if (name) {
3938
const parts = name.split(/\s+/).filter(Boolean).map(escapeRegExp);
4039
const re = parts.length ? parts.join(String.raw`\s+`) : escapeRegExp(name);
41-
patterns.push(wrapWithBoundaries(re));
40+
patterns.push(wrapDerivedMentionPattern(re));
4241
}
4342
const emoji = normalizeOptionalString(identity?.emoji);
4443
if (emoji) {
@@ -126,21 +125,24 @@ function compileMentionPatternsCached(params: {
126125
return cacheMentionRegexes(params.cache, cacheKey, compiled.regexes);
127126
}
128127

129-
function resolveMentionPatterns(cfg: OpenClawConfig | undefined, agentId?: string): string[] {
128+
function resolveMentionPatterns(
129+
cfg: OpenClawConfig | undefined,
130+
agentId?: string,
131+
): ResolvedMentionPatterns {
130132
if (!cfg) {
131-
return [];
133+
return { patterns: [], unicode: false };
132134
}
133135
const agentConfig = agentId ? resolveAgentConfig(cfg, agentId) : undefined;
134136
const agentGroupChat = agentConfig?.groupChat;
135137
if (agentGroupChat && Object.hasOwn(agentGroupChat, "mentionPatterns")) {
136-
return agentGroupChat.mentionPatterns ?? [];
138+
return { patterns: agentGroupChat.mentionPatterns ?? [], unicode: false };
137139
}
138140
const globalGroupChat = cfg.messages?.groupChat;
139141
if (globalGroupChat && Object.hasOwn(globalGroupChat, "mentionPatterns")) {
140-
return globalGroupChat.mentionPatterns ?? [];
142+
return { patterns: globalGroupChat.mentionPatterns ?? [], unicode: false };
141143
}
142144
const derived = deriveMentionPatterns(agentConfig?.identity);
143-
return derived.length > 0 ? derived : [];
145+
return { patterns: derived, unicode: derived.length > 0 };
144146
}
145147

146148
/** Builds mention regexes from config, agent identity, and channel policy. */
@@ -152,10 +154,11 @@ export function buildMentionRegexes(
152154
if (!resolveMentionPatternPolicy({ ...options, cfg, agentId }).enabled) {
153155
return [];
154156
}
155-
const patterns = normalizeMentionPatterns(resolveMentionPatterns(cfg, agentId));
157+
const resolved = resolveMentionPatterns(cfg, agentId);
158+
const patterns = normalizeMentionPatterns(resolved.patterns);
156159
return compileMentionPatternsCached({
157160
patterns,
158-
flags: "i",
161+
flags: resolved.unicode ? "iu" : "i",
159162
cache: mentionMatchRegexCompileCache,
160163
warnRejected: true,
161164
});
@@ -232,9 +235,10 @@ export function stripMentions(
232235
const providerMentions = providerId
233236
? (getLoadedChannelPluginById(providerId) as ChannelPlugin | undefined)?.mentions
234237
: undefined;
238+
const resolvedPatterns = resolveMentionPatterns(cfg, agentId);
235239
const configRegexes = compileMentionPatternsCached({
236-
patterns: normalizeMentionPatterns(resolveMentionPatterns(cfg, agentId)),
237-
flags: "gi",
240+
patterns: normalizeMentionPatterns(resolvedPatterns.patterns),
241+
flags: resolvedPatterns.unicode ? "giu" : "gi",
238242
cache: mentionStripRegexCompileCache,
239243
warnRejected: true,
240244
});

0 commit comments

Comments
 (0)