Skip to content

fix: handle square brackets in agent names for mention parsing#1992

Open
kagura-agent wants to merge 2 commits intomultica-ai:mainfrom
kagura-agent:fix/mention-bracket-escape-1991
Open

fix: handle square brackets in agent names for mention parsing#1992
kagura-agent wants to merge 2 commits intomultica-ai:mainfrom
kagura-agent:fix/mention-bracket-escape-1991

Conversation

@kagura-agent
Copy link
Copy Markdown
Contributor

Problem

Agent mentions fail silently when the agent's display name contains square brackets (e.g. David[TF]).

The mention link [@David[TF]](mention://agent/<id>) conflicts with Markdown's own link syntax because the regex uses [^\]]* to match the label — this stops at the first ] inside the name, causing the entire mention to go unrecognized.

Root Cause

Both the backend Go regex (MentionRe in server/internal/util/mention.go) and the frontend Tiptap tokenizer (mention-extension.ts) used [^\]]+ / [^\]]* to match the label portion of a mention link. This character class excludes ], so any ] inside the agent name breaks the match.

Fix

Backend (mention.go):

  • Changed MentionRe to use .+? (non-greedy) instead of [^\]]*. The ](mention:// anchor is specific enough to prevent over-matching — mention:// will never appear in normal text.
  • Updated ParseMentions to use the correct capture group indices (the label is now captured as group 1).

Frontend (mention-extension.ts):

  • Same regex fix in markdownTokenizer.start() and tokenize().
  • renderMarkdown() now escapes [ and ] in the label before serializing, preventing ambiguous markdown from being created in the first place.
  • tokenize() unescapes \\[ and \\] when parsing, so the round-trip is clean.

Testing

  • Added mention_test.go with comprehensive test cases:
    • Simple agent/member/issue/all mentions
    • Agent names with square brackets (David[TF])
    • Nested brackets (Bot[v2][beta])
    • Multiple bracket-containing mentions in one string
    • Escaped brackets (David\[TF\])
    • Deduplication
    • No-mention content

All tests pass locally.

Closes #1991

…ca-ai#1991)

The mention regex used [^\]]* to match labels, which broke when agent
names contained square brackets (e.g. David[TF]). The ] inside the name
caused the regex to stop matching prematurely, silently dropping the
mention.

Changes:
- Backend (mention.go): Switch to .+? (non-greedy) anchored on
  ](mention:// to correctly match labels with brackets
- Frontend (mention-extension.ts): Same regex fix in tokenizer, plus
  escape [ and ] in renderMarkdown to prevent creating ambiguous
  markdown syntax
- Add comprehensive tests for ParseMentions covering bracket names

Fixes multica-ai#1991
@vercel
Copy link
Copy Markdown

vercel Bot commented May 2, 2026

@kagura-agent is attempting to deploy a commit to the IndexLabs Team on Vercel.

A member of the Team first needs to authorize it.

Fixes TS2532: Object is possibly 'undefined' on match[1] when calling
.replace() in the mention tokenizer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Agent mentions fail when agent name contains square brackets []

1 participant