Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-6546 fix(util): ignore mentions inside markdown code blocks and inline code (#7397)

Sanjay Ramadugu9543a0c65aad
diff --git a/server/go.mod b/server/go.mod
index f138abf5b2..8ce8ab9905 100644
--- a/server/go.mod
+++ b/server/go.mod
@@ -31,6 +31,7 @@ require (
github.com/spf13/pflag v1.0.10
github.com/tdewolff/parse/v2 v2.8.16
github.com/yuin/goldmark v1.8.6
+ golang.org/x/net v0.60.0
golang.org/x/sync v0.23.0
golang.org/x/sys v0.48.0
google.golang.org/protobuf v1.36.12
@@ -71,7 +72,6 @@ require (
github.com/tidwall/sjson v1.2.5 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/mod v0.41.0 // indirect
- golang.org/x/net v0.60.0 // indirect
golang.org/x/telemetry v0.0.0-20260910141331-15ceca2b0a1f // indirect
golang.org/x/text v0.42.0 // indirect
golang.org/x/tools v0.50.0 // indirect
diff --git a/server/internal/handler/squad_briefing.go b/server/internal/handler/squad_briefing.go
index b14d63b33a..349d12984c 100644
--- a/server/internal/handler/squad_briefing.go
+++ b/server/internal/handler/squad_briefing.go
@@ -18,8 +18,9 @@ import (
//
// Keep this text English-only (matches existing agent-harness conventions)
// and keep the mention syntax exactly aligned with util.MentionRe — the
-// "Squad Roster" block below renders concrete examples that round-trip
-// through util.ParseMentions, and the protocol text refers to that format.
+// "Squad Roster" block below renders copyable examples as inline code so the
+// briefing itself does not trigger, and the pasted syntax round-trips
+// through util.ParseMentions. The protocol text refers to that format.
//
// Responsibility 5 is the single statement of the no_action rule (MUL-6984).
// The runtime brief's workflow step 4 and ## Output, and the per-turn reply
diff --git a/server/internal/handler/squad_briefing_test.go b/server/internal/handler/squad_briefing_test.go
index fb777b4929..4b341b0b45 100644
--- a/server/internal/handler/squad_briefing_test.go
+++ b/server/internal/handler/squad_briefing_test.go
@@ -324,36 +324,42 @@ func TestBuildSquadLeaderBriefing_SkipsArchivedAgent(t *testing.T) {
}
// TestBuildSquadLeaderBriefing_MentionsRoundTrip is the contract test
-// guaranteeing every emitted mention markdown string parses back through
-// util.ParseMentions to its (type, id). If this ever breaks, the leader's
+// guaranteeing every roster mention is copyable inline code: the briefing
+// itself must not trigger ParseMentions, but pasting the unwrapped syntax
+// must round-trip to its (type, id). If this ever breaks, the leader's
// dispatch comments will silently fail to trigger anyone.
func TestBuildSquadLeaderBriefing_MentionsRoundTrip(t *testing.T) {
ctx := context.Background()
- leaderID, _ := seededLeaderAgent(t)
+ leaderID, leaderName := seededLeaderAgent(t)
squad := seedSquadForBriefing(t, leaderID, "Mention Round Trip", "")
helper := createHandlerTestAgent(t, "Round Trip Bot", []byte("[]"))
addAgentMember(t, squad.ID, helper, "")
- memberRowID, userID, _ := seededHumanMember(t)
+ memberRowID, userID, userName := seededHumanMember(t)
_ = memberRowID
addHumanMember(t, squad.ID, userID, "")
out := buildSquadLeaderBriefing(ctx, testHandler.Queries, squad, true, leaderNoWorkflow)
- mentions := util.ParseMentions(out)
-
- wantIDs := map[string]string{
- leaderID: "agent",
- helper: "agent",
- userID: "member",
+ if triggered := util.ParseMentions(out); len(triggered) != 0 {
+ t.Errorf("briefing code examples must not trigger ParseMentions, got %#v", triggered)
}
- got := make(map[string]string, len(mentions))
- for _, m := range mentions {
- got[m.ID] = m.Type
+
+ wants := []struct {
+ id, kind, name string
+ }{
+ {leaderID, "agent", leaderName},
+ {helper, "agent", "Round Trip Bot"},
+ {userID, "member", userName},
}
- for id, kind := range wantIDs {
- if got[id] != kind {
- t.Errorf("expected %s mention for id %s, got %q (all parsed: %#v)", kind, id, got[id], mentions)
+ for _, w := range wants {
+ md := formatMention(w.name, w.kind, w.id)
+ if !strings.Contains(out, "`"+md+"`") {
+ t.Errorf("expected copyable inline-code mention %s in roster:\n%s", md, out)
+ }
+ pasted := util.ParseMentions(md)
+ if len(pasted) != 1 || pasted[0].Type != w.kind || pasted[0].ID != w.id {
+ t.Errorf("pasted %s mention for id %s = %#v, want [{Type:%s ID:%s}]", w.kind, w.id, pasted, w.kind, w.id)
}
}
}
diff --git a/server/internal/service/builtin_skills/multica-platform/references/mentions.md b/server/internal/service/builtin_skills/multica-platform/references/mentions.md
index 687228b3ad..7357073739 100644
--- a/server/internal/service/builtin_skills/multica-platform/references/mentions.md
+++ b/server/internal/service/builtin_skills/multica-platform/references/mentions.md
@@ -26,8 +26,22 @@ The parser accepts exactly four `<type>` values plus the `all` sentinel, and the
(member|agent|squad|issue|all)/([0-9a-fA-F-]+|all)
So the link target is a real entity UUID (or `all`), never a display name. The
-label between the brackets is free text — that is where the human-readable name
-goes.
+label between the brackets must contain visible text. Put the human-readable
+name there. The `@` prefix is optional; issue references commonly omit it:
+
+ [MUL-123](mention://issue/<uuid>)
+
+Only canonical inline links with visible label text count as mentions. Empty
+or invisible labels, reference links, images, autolinks, titles, and
+angle-bracket destinations are not mention triggers. Use the shape above.
+Text removed by HTML sanitization, such as script contents, and inert template
+contents do not make a label visible; neither does an `@` prefix on its own.
+
+Code examples are inert as mentions. This includes inline code, multiline code
+spans, indented code, and backtick or tilde fenced blocks inside lists,
+blockquotes, or `<details>`. A fenced `diff` of agent instructions cannot invoke
+its mention targets. Copy the mention syntax out of code and paste it into
+ordinary prose to trigger it. Prose mentions in blockquotes still count.
One `mention://` form deliberately sits OUTSIDE this parser:
`[Label](mention://project/<uuid>)`. `project` is absent from the type group
diff --git a/server/internal/service/builtin_skills_test.go b/server/internal/service/builtin_skills_test.go
index 981fd0c620..0d64c41954 100644
--- a/server/internal/service/builtin_skills_test.go
+++ b/server/internal/service/builtin_skills_test.go
@@ -547,6 +547,13 @@ func TestPlatformSkillCoversPlatformContracts(t *testing.T) {
file: "references/mentions.md",
want: []string{
"(member|agent|squad|issue|all)/([0-9a-fA-F-]+|all)",
+ "canonical inline links with visible label text",
+ "Text removed by HTML sanitization",
+ "reference links, images, autolinks, titles",
+ "Code examples are inert as mentions",
+ "A fenced `diff` of agent instructions cannot invoke",
+ "paste it into",
+ "Prose mentions in blockquotes still count",
"multica workspace member list --output json",
"enqueues a run for that agent",
"enqueues NOTHING",
@@ -725,8 +732,10 @@ func TestPlatformSkillCoversPlatformContracts(t *testing.T) {
// reference its value: it proves the skill teaches exactly what
// util.ParseMentions enforces. The skill's "Incorrect" examples must parse to
// nothing (the @gpt-boy class of bug: a name where a UUID belongs fails
-// silently), and its "Correct" example must parse. If the mention regex drifts,
-// this breaks and the skill's claims must be re-checked.
+// silently), and its "Correct" example must parse. If ParseMentions drifts,
+// this breaks and the skill's claims must be re-checked. Goldmark link nodes
+// exclude code; parseMentionLink validates canonical source and visible labels
+// in internal/util/mention.go. These assertions replace the retired source map.
func TestPlatformSkillTeachesTheParserContract(t *testing.T) {
const uuid = "7f3a1b2c-0000-4000-8000-000000000abc"
@@ -760,6 +769,35 @@ func TestPlatformSkillTeachesTheParserContract(t *testing.T) {
content: "[@all](mention://all/all) heads up",
want: []util.Mention{{Type: "all", ID: "all"}},
},
+ {
+ name: "empty label cannot trigger an agent",
+ content: "[](mention://agent/" + uuid + ")",
+ },
+ {
+ name: "script content cannot make an agent label visible",
+ content: "[<script>Agent</script>@](mention://agent/" + uuid + ")",
+ },
+ {
+ name: "template content cannot make an agent label visible",
+ content: "[<template>Agent</template>@](mention://agent/" + uuid + ")",
+ },
+ {
+ name: "reference link cannot trigger an agent",
+ content: "[@Agent][target]\n\n[target]: mention://agent/" + uuid,
+ },
+ {
+ name: "inline code example is inert",
+ content: "`[@Agent](mention://agent/" + uuid + ")`",
+ },
+ {
+ name: "details diff example is inert",
+ content: "<details>\n<summary>Instructions</summary>\n\n```diff\n+ [@Agent](mention://agent/" + uuid + ")\n```\n\n</details>",
+ },
+ {
+ name: "copied example in prose parses",
+ content: "[@Agent](mention://agent/" + uuid + ") please review",
+ want: []util.Mention{{Type: "agent", ID: uuid}},
+ },
{
// Skill: "Using the wrong type for a real UUID still parses — it
// just resolves to the wrong entity." Which is exactly why the
diff --git a/server/internal/util/mention.go b/server/internal/util/mention.go
index f969f2fa3f..37e930c307 100644
--- a/server/internal/util/mention.go
+++ b/server/internal/util/mention.go
@@ -1,10 +1,24 @@
package util
-import "regexp"
+import (
+ "bytes"
+ "html"
+ "regexp"
+ "strings"
+ "unicode"
+
+ "github.com/yuin/goldmark"
+ "github.com/yuin/goldmark/ast"
+ "github.com/yuin/goldmark/parser"
+ goldmarkhtml "github.com/yuin/goldmark/renderer/html"
+ "github.com/yuin/goldmark/text"
+ goldmarkutil "github.com/yuin/goldmark/util"
+ nethtml "golang.org/x/net/html"
+)
// Mention represents a parsed @mention from markdown content.
type Mention struct {
- Type string // "member", "agent", "issue", or "all"
+ Type string // "member", "agent", "squad", "issue", or "all"
ID string // user_id, agent_id, issue_id, or "all"
}
@@ -15,27 +29,186 @@ type Mention struct {
// enough to prevent over-matching.
var MentionRe = regexp.MustCompile(`\[@?(.+?)\]\(mention://(member|agent|squad|issue|all)/([0-9a-fA-F-]+|all)\)`)
+var canonicalMentionRe = regexp.MustCompile("^" + MentionRe.String() + "$")
+
// IsMentionAll returns true if the mention is an @all mention.
func (m Mention) IsMentionAll() bool {
return m.Type == "all"
}
// ParseMentions extracts deduplicated mentions from markdown content.
+// Mentions inside fenced code blocks or inline code spans are ignored — they
+// document the mention syntax rather than trigger an agent run. Mentions in
+// blockquotes are still returned on purpose.
+//
+// The walk uses goldmark's CommonMark AST and only considers Link nodes, so
+// code spans, fenced blocks (including nested-in-blockquote and CommonMark
+// fence-length rules), and multiline code spans are excluded without a
+// hand-rolled scanner.
+// Each link must also use MentionRe's canonical inline source form and have
+// visible label text. Reference links and other Markdown link forms are inert.
func ParseMentions(content string) []Mention {
- matches := MentionRe.FindAllStringSubmatch(content, -1)
+ source := []byte(content)
+ links := &mentionLinkParser{InlineParser: parser.NewLinkParser(), spans: make(map[*ast.Link]text.Segment)}
+ inlineParsers := parser.DefaultInlineParsers()
+ for i := range inlineParsers {
+ if inlineParsers[i].Value == links.InlineParser {
+ inlineParsers[i].Value = links
+ }
+ }
+ markdown := parser.NewParser(
+ parser.WithBlockParsers(parser.DefaultBlockParsers()...),
+ parser.WithInlineParsers(inlineParsers...),
+ parser.WithParagraphTransformers(parser.DefaultParagraphTransformers()...),
+ )
+ doc := markdown.Parse(text.NewReader(source))
seen := make(map[string]bool)
var result []Mention
- for _, m := range matches {
- key := m[2] + ":" + m[3]
+ _ = ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
+ if !entering {
+ return ast.WalkContinue, nil
+ }
+ if _, ok := n.(*ast.Image); ok {
+ return ast.WalkSkipChildren, nil
+ }
+ link, ok := n.(*ast.Link)
+ if !ok {
+ return ast.WalkContinue, nil
+ }
+ mention, ok := parseMentionLink(link, links.spans[link], source)
+ if !ok {
+ return ast.WalkContinue, nil
+ }
+ key := mention.Type + ":" + mention.ID
if seen[key] {
- continue
+ return ast.WalkContinue, nil
}
seen[key] = true
- result = append(result, Mention{Type: m[2], ID: m[3]})
- }
+ result = append(result, mention)
+ return ast.WalkContinue, nil
+ })
return result
}
+// Goldmark links expose their start, but not their end. Record the span while
+// its link parser owns the reader, before inline nodes lose that boundary.
+type mentionLinkParser struct {
+ parser.InlineParser
+ spans map[*ast.Link]text.Segment
+}
+
+func (p *mentionLinkParser) Parse(parent ast.Node, reader text.Reader, context parser.Context) ast.Node {
+ n := p.InlineParser.Parse(parent, reader, context)
+ if link, ok := n.(*ast.Link); ok {
+ _, end := reader.Position()
+ p.spans[link] = text.NewSegment(link.Pos(), end.Start)
+ }
+ return n
+}
+
+func (p *mentionLinkParser) CloseBlock(parent ast.Node, reader text.Reader, context parser.Context) {
+ // Preserve the link parser's cleanup of unmatched openers between blocks.
+ p.InlineParser.(parser.CloseBlocker).CloseBlock(parent, reader, context)
+}
+
+func parseMentionLink(link *ast.Link, span text.Segment, source []byte) (Mention, bool) {
+ if link.Reference != nil || len(link.Title) != 0 {
+ return Mention{}, false
+ }
+ raw := span.Value(source)
+ m := canonicalMentionRe.FindSubmatchIndex(raw)
+ if m == nil {
+ return Mention{}, false
+ }
+ mention := Mention{Type: string(raw[m[4]:m[5]]), ID: string(raw[m[6]:m[7]])}
+ if string(link.Destination) != "mention://"+mention.Type+"/"+mention.ID {
+ return Mention{}, false
+ }
+
+ var label strings.Builder
+ hasRawHTML := false
+ _ = ast.Walk(link, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
+ if entering {
+ if _, ok := n.(*ast.Image); ok {
+ return ast.WalkSkipChildren, nil
+ }
+ if _, ok := n.(*ast.RawHTML); ok {
+ hasRawHTML = true
+ }
+ if node, ok := n.(*ast.Text); ok {
+ value := node.Segment.Value(source)
+ if !node.IsRaw() {
+ value = goldmarkutil.UnescapePunctuations(value)
+ label.WriteString(html.UnescapeString(string(value)))
+ } else {
+ label.Write(value)
+ }
+ }
+ }
+ return ast.WalkContinue, nil
+ })
+ labelText := label.String()
+ if hasRawHTML {
+ labelText = renderedMentionLabelText(link, source)
+ }
+ firstVisible := true
+ for _, r := range labelText {
+ if unicode.Is(unicode.Other_Default_Ignorable_Code_Point, r) || r == '\u2800' {
+ continue
+ }
+ if unicode.IsLetter(r) || unicode.IsNumber(r) || unicode.IsPunct(r) || unicode.IsSymbol(r) {
+ if firstVisible && r == '@' {
+ firstVisible = false
+ continue
+ }
+ return mention, true
+ }
+ }
+ return Mention{}, false
+}
+
+// Raw HTML changes which text survives the frontend's rehype-raw/sanitize
+// pipeline. Parse the rendered fragment so script raw-text rules (including
+// case, attributes, and self-closing syntax) match HTML rather than Markdown.
+// This HTML is only inspected in memory; it is never served or executed.
+func renderedMentionLabelText(link *ast.Link, source []byte) string {
+ var rendered bytes.Buffer
+ markdown := goldmark.New(goldmark.WithRendererOptions(goldmarkhtml.WithUnsafe()))
+ if err := markdown.Renderer().Render(&rendered, source, link); err != nil {
+ return ""
+ }
+ doc, err := nethtml.Parse(&rendered)
+ if err != nil {
+ return ""
+ }
+ var label strings.Builder
+ var collect func(*nethtml.Node)
+ collect = func(n *nethtml.Node) {
+ // markdownSanitizeSchema strips scripts. parse5 stores template
+ // contents in a separate fragment, so sanitization also drops them
+ // when unwrapping the template. x/net/html keeps them as children.
+ // Other disallowed tags, such as style, are unwrapped, keeping text.
+ if n.Type == nethtml.ElementNode && (n.Data == "script" || n.Data == "template") {
+ return
+ }
+ if n.Type == nethtml.TextNode {
+ label.WriteString(n.Data)
+ }
+ for child := n.FirstChild; child != nil; child = child.NextSibling {
+ collect(child)
+ }
+ }
+ // Only the generated anchor is the mention label. Raw closing tags can
+ // move text outside it; that text must not validate an empty mention.
+ for node := range doc.Descendants() {
+ if node.Type == nethtml.ElementNode && node.Data == "a" {
+ collect(node)
+ break
+ }
+ }
+ return label.String()
+}
+
// HasMentionAll returns true if any mention in the slice is an @all mention.
func HasMentionAll(mentions []Mention) bool {
for _, m := range mentions {
diff --git a/server/internal/util/mention_test.go b/server/internal/util/mention_test.go
index 49957e2226..d5cc4c6bf1 100644
--- a/server/internal/util/mention_test.go
+++ b/server/internal/util/mention_test.go
@@ -58,11 +58,347 @@ func TestParseMentions(t *testing.T) {
content: "just a plain comment",
want: nil,
},
+ {
+ name: "unmatched link opener does not cross paragraph boundary",
+ content: "[\n\n]",
+ },
+ {
+ name: "unmatched image opener does not cross paragraph boundary",
+ content: "![\n\n]\n\n[@Agent](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
{
name: "escaped brackets in label",
content: `[@David\[TF\]](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa) hi`,
want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
},
+ {
+ name: "visible agent label without optional at prefix",
+ content: "[Agent](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "empty label is ignored",
+ content: "[](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "whitespace label is ignored",
+ content: "[ ](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "at prefix without a label is ignored",
+ content: "[@](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "entity whitespace label is ignored",
+ content: "[&#32;](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "HTML only label is ignored",
+ content: "[<span></span>](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "script content removed by sanitization is not a visible label",
+ content: "[<script>Agent</script>](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "script content cannot turn an at prefix into a visible label",
+ content: "[<script>Agent</script>@](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "template content is not a visible label",
+ content: "[<template>Agent</template>](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "template content cannot turn an at prefix into a visible label",
+ content: "[<template>Agent</template>@](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "uppercase template with attributes is ignored",
+ content: `[<TEMPLATE data-label="Agent"><span>Agent</span></TEMPLATE>](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)`,
+ },
+ {
+ name: "self closing template still hides following label text",
+ content: "[<template/>Agent](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "visible label after nested templates still triggers",
+ content: "[<template><template>hidden</template></template>Bob](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "visible label before template still triggers",
+ content: "[Bob<template>hidden</template>](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "literal template in code label stays visible",
+ content: "[`<template>Agent</template>`](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "escaped template label stays visible",
+ content: "[&lt;template&gt;Agent&lt;/template&gt;](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "uppercase script with attributes is ignored",
+ content: `[<SCRIPT type="text/plain">Agent</SCRIPT>](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)`,
+ },
+ {
+ name: "self closing script still hides following label text",
+ content: "[<script/>Agent](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "nested script opener does not hide the real closing tag",
+ content: "[<script>outer<script>inner</script>@](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "formatted script text is still hidden",
+ content: "[<script>**Agent**</script>](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "text moved outside the mention by raw HTML does not validate its label",
+ content: "[</a>Bob](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "visible span label still triggers",
+ content: "[<span>Bob</span>](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "visible label after script still triggers",
+ content: "[<script>hidden</script><span>Bob</span>](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "visible label before script still triggers",
+ content: "[Bob<script>hidden</script>](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "style text unwrapped by sanitization stays visible",
+ content: "[<style>Agent</style>](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "literal script in code label stays visible",
+ content: "[`<script>Agent</script>`](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "escaped script label stays visible",
+ content: "[&lt;script&gt;Agent&lt;/script&gt;](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "full reference link is ignored",
+ content: "[@Agent][target]\n\n[target]: mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
+ },
+ {
+ name: "collapsed reference link is ignored",
+ content: "[@Agent][]\n\n[@Agent]: mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
+ },
+ {
+ name: "shortcut reference link is ignored",
+ content: "[@Agent]\n\n[@Agent]: mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
+ },
+ {
+ name: "angle bracket destination is ignored",
+ content: "[@Agent](<mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa>)",
+ },
+ {
+ name: "link with title is ignored",
+ content: "[@Agent](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa \"title\")",
+ },
+ {
+ name: "destination whitespace is ignored",
+ content: "[@Agent]( mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa )",
+ },
+ {
+ name: "entity encoded destination is ignored",
+ content: "[@Agent](mention&#58;//agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "canonical text inside label cannot validate noncanonical outer link",
+ content: "[@Agent `[Example](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)`](<mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa>)",
+ },
+ {
+ name: "mention in HTML comment label cannot validate outer link",
+ content: "[A <!-- [X](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa) -->](<mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa>)",
+ },
+ {
+ name: "mention in image destination cannot validate outer link",
+ content: "[A ![x](\"[X](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\")](<mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa>)",
+ },
+ {
+ name: "zero width entity label is ignored",
+ content: "[&#x200B;](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "named zero width entity label is ignored",
+ content: "[&ZeroWidthSpace;](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "invisible format label is ignored",
+ content: "[\u200b\ufeff](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "invisible leading text cannot turn at prefix into a label",
+ content: "[\u200b@](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa) [&#x200B;@](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa) [\u3164@](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "Hangul filler label is ignored",
+ content: "[\u115f\u1160\u3164\uffa0](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "Hangul filler entities are ignored",
+ content: "[&#x115F;&#x1160;&#x3164;&#xFFA0;](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "braille blank label is ignored",
+ content: "[\u2800](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "braille blank entity is ignored",
+ content: "[&#x2800;](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "visible Unicode label with invisible format still triggers",
+ content: "[@\u200b助手](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "visible inline code label still triggers",
+ content: "[`Agent`](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "escaped at prefix without label is ignored",
+ content: `[@](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa) [\@](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)`,
+ },
+ {
+ name: "mention inside image alt text is ignored",
+ content: "![[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)](https://example.com/image.png)",
+ },
+ {
+ name: "prose next to image alt mention still triggers",
+ content: "![[@B](mention://agent/bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb)](https://example.com/image.png) [@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "image is ignored",
+ content: "![@Agent](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
+ {
+ name: "escaped link opener is ignored",
+ content: `\[@Agent](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)`,
+ },
+ {
+ name: "autolink is ignored",
+ content: "<mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa>",
+ },
+ {
+ name: "ignored reference does not hide later canonical mention",
+ content: "[@Agent][target] then [@Agent](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\n\n[target]: mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "mention inside fenced code block is ignored",
+ content: "```\n[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\n```",
+ want: nil,
+ },
+ {
+ name: "mention inside tilde fenced code block is ignored",
+ content: "~~~\n[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\n~~~",
+ want: nil,
+ },
+ {
+ name: "mention inside fenced block with info string is ignored",
+ content: "```markdown\n[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\n```",
+ want: nil,
+ },
+ {
+ name: "mention inside inline code span is ignored",
+ content: "use `[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)` to mention",
+ want: nil,
+ },
+ {
+ name: "mention inside double-backtick span containing a backtick is ignored",
+ content: "use ``[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)` `` to mention",
+ want: nil,
+ },
+ {
+ name: "mention before a fenced block is still returned",
+ content: "[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\n```\n[@B](mention://agent/bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb)\n```",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "mention after a closing fence is still returned",
+ content: "```\ncode\n```\n[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "mention inside blockquote is still returned",
+ content: "> [@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "unterminated fence suppresses everything after it",
+ content: "```\n[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\nand more text",
+ want: nil,
+ },
+ {
+ name: "longer closing fence still closes and mention after it is returned",
+ content: "```\ncode\n````\n[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "four-space indented fence is not a closer so mention stays inert",
+ content: "```\ncode\n ```\n[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\n```",
+ want: nil,
+ },
+ {
+ name: "mention inside multiline code span is ignored",
+ content: "use ``[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\nstill code`` to mention",
+ want: nil,
+ },
+ {
+ name: "fenced code inside a blockquote is ignored",
+ content: "> ```\n> [@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\n> ```",
+ want: nil,
+ },
+ {
+ name: "prose mention inside a blockquote is still returned next to quoted code",
+ content: "> [@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\n>\n> ```\n> [@B](mention://agent/bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb)\n> ```",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "fenced diff inside details is ignored",
+ content: "<details>\n<summary>Agent instructions diff</summary>\n\n```diff\n- [@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\n+ [@B](mention://agent/bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb)\n```\n\n</details>",
+ },
+ {
+ name: "details without blank line stays inert",
+ content: "<details>\n<summary>Diff</summary>\n```diff\n+ [@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\n```\n</details>",
+ },
+ {
+ name: "prose after details diff still triggers",
+ content: "<details>\n<summary>Diff</summary>\n\n```diff\n+ [@B](mention://agent/bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb)\n```\n\n</details>\n\n[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "prose inside details with blank line still triggers",
+ content: "<details>\n<summary>Request</summary>\n\n[@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\n\n</details>",
+ want: []Mention{{Type: "agent", ID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}},
+ },
+ {
+ name: "fenced diff inside list is ignored",
+ content: "- Instructions:\n\n ```diff\n + [@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)\n ```",
+ },
+ {
+ name: "indented code is ignored",
+ content: " [@A](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)",
+ },
}
for _, tt := range tests {