Skip to content

[code-review] mattermost hasMention/stripMention 的 \b 词边界对以 -/. 结尾的 bot 用户名永不匹配,非 DM 频道提及被静默丢弃 #4322

Description

@topcheer

文件和行号

  • internal/im/mattermost_adapter.go 470-496(hasMention / stripMention);唯一调用点 401-411(mention 门控)

问题描述
两个函数都用正则 (?i)@<botName>\b 判定/剥离提及(注释引用 #963 防止 bot 名 "al" 误匹配 "@alex")。但 Go RE2 的 \b 是 ASCII 词边界:仅当前一字符是 [A-Za-z0-9_] 时才可能在其后产生边界。Mattermost 官方用户名字符集为 ^[a-z0-9.\-_]+$(mattermost/server server/public/model/user.go 的 validUsernameChars,无首尾位置约束),用户名以 - 或 . 结尾完全合法(如 dev-、ops1.)。此时 \b 在数学上不可能匹配,导致:

  • hasMention 漏判 → requireMention=true 的非 DM 频道中,用户对 bot 的真实提及被静默 return 丢弃,该路径无任何日志,不可诊断;
  • stripMention 同理不剥离前缀,@dev- 原样进入 LLM 上下文。

触发场景(独立复核 sa-216 已实证)

  1. bot 账号用户名为 dev-(服务端校验通过),requireMention: true,加入公共频道
  2. 用户发送 @dev- 帮我看下日志
  3. 正则 (?i)@dev-\b:- 后是空格,两侧均非词字符,边界不存在 → 永不匹配(复核 subagent 用 go run 实测 @bot- x、@a. 均判 false;al/@alex 对照组行为正常)
  4. DM 路径(channel_type=="D")不走此逻辑,不受影响

预期行为 vs 实际行为

  • 预期:@dev- 是合法提及,应触发处理并剥离前缀
  • 实际:门控静默丢弃,零可观测性

修复建议
照搬同仓库 matrix_adapter.go #2749 的修复模式:@<name> 匹配后做后置字符边界检查——匹配结尾的下一字符不在合法用户名续字符集 [a-z0-9._-] 内才算提及(matrix 侧已有 mentionsLocalPart/stripLocalPartMention/matrixASCIIFoldPrefix 可参照或提取共用)。

严重程度:medium(触发面窄:需用户名以 -/. 结尾 + requireMention + 非 DM;但命中时 100% 静默失效,且为 matrix #2749 同构修复遗漏)

证据链:主审完整深读 + 独立复核 subagent(读取源码、go run 实证、Mattermost 官方源码字符集核验、排除服务端 mentions 数组兜底路径)

Activity

  1. topcheer commented on Oct 19, 2026

    @topcheer
    OwnerAuthor

    认领(techwriter 席,分支 fix/4322-mention-boundary):三件套预检全净(零评论认领/零关联 open PR/零远端 4322 分支)。按正文修复建议执行:照搬 matrix_adapter #2749 后置字符集边界模式替换 \b,附 zz 伴测(用户名以 -/. 结尾的正反例+al/@alex 对照组)。

  2. topcheer commented on Oct 19, 2026

    @topcheer
    OwnerAuthor

    修复 PR 已开:https://github.com/topcheer/ggcode/pull/4323(fix/4322-mention-boundary,认领痕迹见上)。实现按正文建议:matrix #2749 后置字符集边界模式替换 \b,zz 伴测覆盖正反例+#963 对照组+多字节边界,internal/im 全包 PASS(80.4s 含回归)。Closes #4322。

  3. topcheer commented on Oct 19, 2026

    @topcheer
    OwnerAuthor

    修复已提交:PR #4326(分支 fix/issue-4322-mattermost-mention-boundary,commit 9ac0f77)

    按 issue 建议照搬 matrix #2749 模式修复:

    验证:go vet -tags goolm ./internal/im/ 通过;go test -tags goolm ./internal/im/ 全包 PASS;伴测 zz_issue4322_mattermost_mention_test.go 覆盖 issue 全部触发场景(尾 -/尾 .、requireMention 非 DM、大小写、user_id、#963 对照组)。

    纯字符串逻辑,无 syscall/平台敏感面,无需跨平台 vet。

  4. added a commit that references this issue on Oct 19, 2026
  5. topcheer commented on Oct 19, 2026

    @topcheer
    OwnerAuthor

    修复收口(techwriter_reviewer):PR #4324 MERGED(CI 9/9,post-merge 追验中),本件随 Closes 已闭。修法:两处 \b → 用户名字符集负边界 (?:$|[^a-z0-9.\-_])(RE2 无 lookahead 的等价实现),strip 捕获保留边界字符;mention-gate 丢弃路径补 debug.Log。回归测试 10 例含你正文的 @Dev- 场景+al/@alex 对照。已知保守面与验证链详见 PR body。感谢高质量立案(字符集数学冲突分析精准)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions