Skip to content

fix(discord): protect mention aliases in code fences - #90648

Closed
rohitjavvadi wants to merge 1 commit into
openclaw:mainfrom
rohitjavvadi:fix-discord-mention-code-fences
Closed

rohitjavvadi wants to merge 1 commit into
openclaw:mainfrom
rohitjavvadi:fix-discord-mention-code-fences

Conversation

@rohitjavvadi

@rohitjavvadi rohitjavvadi commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #90643.

Discord mention alias rewriting now skips Markdown code spans with a local scanner instead of a non-greedy regex, so a triple-backtick literal inside a fenced code block no longer makes later in-fence @handle text eligible for outbound mention rewriting.

This keeps the existing Discord behavior where text after a closing fence marker can still be rewritten, while preventing the code body itself from producing an unintended <@USER_ID> mention.

Root Cause

rewriteDiscordKnownMentions previously used:

/```[\s\S]*?```|`[^`\n]*`/g

That regex closes the skipped code segment on the first downstream triple-backtick sequence anywhere. In a block like this, the string literal was treated as the closing fence:

```js
const fence = "```";
// do not ping @alice
```

The remaining code-body line was then passed through mention alias rewriting and could become a real Discord targeted mention.

Real behavior proof

  • Behavior addressed: Discord mention aliases no longer rewrite an @handle that is inside a backtick fenced code block when the code body contains a triple-backtick literal. Handles after the closing fence still rewrite.
  • Real environment tested: local OpenClaw checkout on this PR branch, exercising the same rewriteDiscordKnownMentions helper used by Discord normal sends, structured sends, webhooks, and final-preview create/edit gating.
  • Exact steps or command run after this patch: node --import tsx --input-type=module with the script below, importing rewriteDiscordKnownMentions and the Discord directory-cache helpers from this checkout.
  • Evidence after fix: terminal output copied from the command below: {"actual":"```js\nconst fence = \"```\";\n// do not ping @alice\n```\ntext <@123456789>"}.
  • Observed result after fix: the in-fence @alice remains literal, while the @alice after the closing fence rewrites to <@123456789>.
  • What was not tested: I did not run a live Discord send. The proof exercises the exact outbound rewriting helper used by the Discord send paths.

Command and terminal output:

node --import tsx --input-type=module - <<'NODE'
import assert from 'node:assert/strict';
import { rewriteDiscordKnownMentions } from './extensions/discord/src/mentions.ts';
import { resetDiscordDirectoryCacheForTest, rememberDiscordDirectoryUser } from './extensions/discord/src/directory-cache.ts';
resetDiscordDirectoryCacheForTest();
rememberDiscordDirectoryUser({ accountId: 'default', userId: '123456789', handles: ['alice'] });
const bug = '```js\nconst fence = "```";\n// do not ping @alice\n```\ntext @alice';
const actual = rewriteDiscordKnownMentions(bug, { accountId: 'default' });
assert.equal(actual, '```js\nconst fence = "```";\n// do not ping @alice\n```\ntext <@123456789>');
console.log(JSON.stringify({ actual }, null, 2));
NODE

Evidence after fix:

{
  "actual": "```js\nconst fence = \"```\";\n// do not ping @alice\n```\ntext <@123456789>"
}

Follow-up proof for the same-line inline triple-backtick case:

node --import tsx --input-type=module - <<'NODE'
import assert from 'node:assert/strict';
import { rewriteDiscordKnownMentions } from './extensions/discord/src/mentions.ts';
import { resetDiscordDirectoryCacheForTest, rememberDiscordDirectoryUser } from './extensions/discord/src/directory-cache.ts';
resetDiscordDirectoryCacheForTest();
rememberDiscordDirectoryUser({ accountId: 'default', userId: '123456789', handles: ['alice'] });
const inline = 'inline ```@alice```\ntext @alice';
const actual = rewriteDiscordKnownMentions(inline, { accountId: 'default' });
assert.equal(actual, 'inline ```@alice```\ntext <@123456789>');
console.log(JSON.stringify({ actual }, null, 2));
NODE

Follow-up evidence after fix:

{
  "actual": "inline ```@alice```\ntext <@123456789>"
}

Tests And Checks

node scripts/run-vitest.mjs extensions/discord/src/mentions.test.ts extensions/discord/src/send.sends-basic-channel-messages.test.ts extensions/discord/src/send.webhook-activity.test.ts --reporter dot
node scripts/run-vitest.mjs extensions/discord/src/mentions.test.ts --reporter dot
pnpm exec oxfmt --check extensions/discord/src/mentions.ts extensions/discord/src/mentions.test.ts
node scripts/run-oxlint.mjs extensions/discord/src/mentions.ts extensions/discord/src/mentions.test.ts
pnpm tsgo:extensions
git diff --check

Results:

  • focused Vitest passed: 3 files, 63 tests
  • focused mention Vitest passed after the follow-up fix: 1 file, 20 tests
  • oxfmt check passed
  • oxlint passed
  • pnpm tsgo:extensions passed
  • git diff --check passed

Compatibility And Risk

The change stays local to Discord mention rewriting and does not add plugin dependencies. It preserves existing simple inline-code/fence behavior and adds coverage for longer fence delimiters, inline spans with shorter backtick runs inside, and the fact that tilde runs are not Discord code fences.

Overlap: #90646 opened against the same issue while this local fix was being prepared.

@openclaw-barnacle openclaw-barnacle Bot added channel: discord Channel integration: discord size: S triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels Jun 5, 2026
@clawsweeper

clawsweeper Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Close: this source PR is superseded by the merged maintainer replacement that carried forward the same Discord mention-alias code-fence fix, credited the contributor, and is now present on current main and in the latest release.

Root-cause cluster
Relationship: superseded
Canonical: #93662
Summary: The current PR, the linked bug report, the closed duplicate attempt, and the merged replacement all target the same Discord mention-alias code-fence bug; the merged replacement is the canonical landed path.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Canonical path: Close the superseded source branch, keep the merged replacement implementation, and handle any desired extra edge-case coverage as a new narrow follow-up.

So I’m closing this here because the remaining work is already tracked in the canonical issue.

Review details

Best possible solution:

Close the superseded source branch, keep the merged replacement implementation, and handle any desired extra edge-case coverage as a new narrow follow-up.

Do we have a high-confidence way to reproduce the issue?

Yes. The original bug is source-reproducible from the old non-greedy regex and documented caller paths, and the PR body includes terminal proof against the helper; current main now contains the scanner fix.

Is this the best way to solve the issue?

Yes. The merged replacement is the best current solution because it fixes the owner-local Discord helper used by all affected send paths and avoids keeping a conflicting source branch open.

Security review:

Security review cleared: Security review cleared: the source PR changes only Discord mention parsing and focused tests, with no dependency, workflow, secret, package, or code-execution surface changes.

AGENTS.md: found and applied where relevant.

What I checked:

  • Source PR diff: The open source PR changes only the Discord mention helper and its tests, replacing the regex skip with scanner helpers and adding backtick regression coverage. (extensions/discord/src/mentions.ts:130, 8585709cfa59)
  • Merged replacement PR: Live GitHub data shows the replacement PR merged on 2026-06-16, its body says it repairs and finalizes this source PR, and it preserves credit for this source contribution. (6656c71c7a12)
  • Current main implementation: Current main has findNextMarkdownCodeSegment and uses it in rewriteDiscordKnownMentions so only text outside Markdown backtick code segments is rewritten. (extensions/discord/src/mentions.ts:168, d1b917120a47)
  • Current regression coverage: Current main includes regression coverage for a longer fenced code block whose body contains a triple-backtick literal, preserving in-fence @alice and rewriting the post-fence alias. (extensions/discord/src/mentions.test.ts:106, d1b917120a47)
  • Affected caller path: Normal Discord sends, structured sends, webhooks, and preview-final mention gating all call rewriteDiscordKnownMentions, so the merged owner-local helper fix covers the affected send paths. (extensions/discord/src/send.outbound.ts:180, d1b917120a47)
  • Release provenance: The v2026.6.10 release is published at release SHA aa69b12, and inspecting that tag shows the same scanner implementation and regression test are shipped. (extensions/discord/src/mentions.ts:128, aa69b12d0086)

Likely related people:

  • vincentkoc: Opened the merged replacement PR, which explicitly carried this source PR forward, and the latest release tag contains the same scanner behavior. (role: replacement PR author and release-area contributor; confidence: high; commits: 6656c71c7a12, aa69b12d0086; files: extensions/discord/src/mentions.ts, extensions/discord/src/mentions.test.ts)
  • rohitjavvadi: Authored this source PR and is credited as a co-author on the replacement head commit that carried the scanner approach forward. (role: source fix contributor; confidence: high; commits: 8585709cfa59, 3204295ff399; files: extensions/discord/src/mentions.ts, extensions/discord/src/mentions.test.ts)
  • scoootscooob: The Discord extension move commit introduced the current extension mention helper path and adjacent tests containing the older regex-based code-region matcher. (role: feature-history contributor; confidence: medium; commits: 5682ec37fada; files: extensions/discord/src/mentions.ts, extensions/discord/src/mentions.test.ts)
  • Tak Hoffman: Recent adjacent history changed Discord outbound mention account handling and tests around the send path that consumes the helper fixed here. (role: adjacent mention-path contributor; confidence: medium; commits: 58d6c16d12b1; files: extensions/discord/src/send.outbound.ts, extensions/discord/src/send.sends-basic-channel-messages.test.ts)

Codex review notes: model internal, reasoning high; reviewed against d1b917120a47; fix evidence: release v2026.6.10, commit 6656c71c7a12.

@rohitjavvadi
rohitjavvadi force-pushed the fix-discord-mention-code-fences branch from 0d663a8 to 1064165 Compare June 5, 2026 11:19
@rohitjavvadi
rohitjavvadi marked this pull request as ready for review June 5, 2026 11:19
@rohitjavvadi
rohitjavvadi force-pushed the fix-discord-mention-code-fences branch from 1064165 to dc1a57d Compare June 5, 2026 11:20
@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels Jun 5, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 10641656e9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread extensions/discord/src/mentions.ts Resolved
@rohitjavvadi
rohitjavvadi force-pushed the fix-discord-mention-code-fences branch from dc1a57d to 8912312 Compare June 5, 2026 11:28
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Jun 5, 2026
@clawsweeper clawsweeper Bot added the status: 🛠️ actively grinding The PR author has acted after the latest ClawSweeper review and work remains. label Jun 5, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label Jun 5, 2026
@rohitjavvadi

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Addressed the same-line triple-backtick scanner finding in 8912312. The scanner now advances to the next line for same-line rejected fence candidates, and the PR includes regression coverage plus follow-up real behavior proof for inline ```@alice```\ntext @alice.

@clawsweeper

clawsweeper Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 🛠️ actively grinding The PR author has acted after the latest ClawSweeper review and work remains. labels Jun 5, 2026
@rohitjavvadi
rohitjavvadi force-pushed the fix-discord-mention-code-fences branch from 8912312 to 8585709 Compare June 11, 2026 17:37
@rohitjavvadi

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Rebased this branch onto current main and resolved the previously addressed same-line triple-backtick review thread. The diff remains limited to the Discord mention scanner and regression tests.

Local checks after the rebase:

  • node scripts/run-vitest.mjs extensions/discord/src/mentions.test.ts --reporter dot
  • ./node_modules/.bin/oxfmt --check extensions/discord/src/mentions.ts extensions/discord/src/mentions.test.ts
  • node scripts/run-oxlint.mjs --tsconfig config/tsconfig/oxlint.extensions.json extensions/discord/src/mentions.ts extensions/discord/src/mentions.test.ts
  • git diff --check

All passed.

@clawsweeper

clawsweeper Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label Jun 11, 2026
@clawsweeper clawsweeper Bot added rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Jun 11, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. labels Jun 15, 2026
@vincentkoc

Copy link
Copy Markdown
Member

Clownfish 🐠 reef update

Thanks for the contribution. The source branch was not safely writable by Clownfish, so it opened a replacement PR and kept the credit trail visible.

Replacement PR: #93662
Source PR: #90648
This PR stays open for now, with the replacement linked as the current fix path.
The original contribution stays credited in the replacement PR context.

fish notes: model gpt-5.5, reasoning medium; reviewed against 3204295.

@clawsweeper clawsweeper Bot removed the merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. label Jun 19, 2026
@clawsweeper clawsweeper Bot closed this Jun 28, 2026
@clawsweeper

clawsweeper Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

🦞✅
ClawSweeper autoclose is complete.

Reason: structured ClawSweeper close marker: close-required (sha=8585709cfa599086ce1208b799227dbcb98e0853)

Closed:

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

Labels

channel: discord Channel integration: discord P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discord mention aliases rewrite @handle inside fenced code when body contains triple-backtick literal

2 participants