Skip to content

fix(discord): ping mention-bearing final replies under live preview (#88360) - #88527

Merged
steipete merged 1 commit into
openclaw:mainfrom
openperf:fix/88360-discord-mention-stream-finalize
May 31, 2026
Merged

steipete merged 1 commit into
openclaw:mainfrom
openperf:fix/88360-discord-mention-stream-finalize

Conversation

@openperf

@openperf openperf commented May 31, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Problem: Issue [Bug]: Discord mentionAliases is not applied to session reply / final assistant text, only to message tool calls #88360 reports that on Discord, channels.discord.mentionAliases (@handle → <@userId>) only fires when a reply is sent through the message tool. When an agent puts the mention in its natural-language final answer (e.g. Done. @Vladislava please review.), the literal @handle reaches Discord as plain text and the target is never pinged. This breaks the multi-agent scenario the feature was introduced for (Discord outbound messages should reliably convert known @AgentName references into real <@userId> mentions #67587), where most cross-agent mentions originate in natural assistant text rather than explicit message tool calls.
  • Root Cause: With live preview streaming active (the default for non-block modes), the agent's final answer is delivered by finalizing the already-streamed preview message in place via a Discord edit (buildFinalEdit → editMessageDiscord in extensions/discord/src/monitor/message-handler.process.ts, through the core deliverWithFinalizableLivePreviewAdapter in src/channels/message/live.ts). Discord fires mention notifications only on message create, never on edit — so finalizing in place can never ping, no matter how the mention is expressed. This affects every targeted mention in a streamed final answer: a configured alias (@handle), a directory-resolved handle, and a literal <@userId> / <@&roleId> the model emits directly (the plugin's messageToolHints actually instruct the model to use that canonical form). The non-streaming path was never affected — it delivers through deliverDiscordReply → sendMessageDiscord, which rewrites and creates a fresh message that pings. The reporter's claim that sendMessageDiscord does not rewrite is inaccurate on current main (it has rewritten since at least v2026.5.12); the real gap is the live-preview finalize path.
  • Fix: Add one guard to the Discord live-preview buildFinalEdit in extensions/discord/src/monitor/message-handler.process.ts. Run the resolved final text through rewriteDiscordKnownMentions (using the account's mentionAliases via resolveDiscordAccount); if the result contains a targeted user/role mention (<@id> / <@!id> / <@&id>), return undefined, which routes delivery to the existing normal path (deliverNormally → deliverDiscordReply → sendMessageDiscord). That path discards the streamed preview and creates a fresh message with the rewrite applied and Discord's default mention parsing, so the mention actually notifies. This covers both aliases/handles and literal <@id> / <@&id>. When the final text has no targeted mention, the efficient in-place preview edit is kept unchanged.
  • What changed:
    • extensions/discord/src/mentions.ts — two small predicates next to the existing rewriter: discordTextHasTargetedMention (<@id> / <@!id> / <@&id>) and discordTextHasBroadcastMention (@everyone / @here), so the mention-format knowledge stays in one place.
    • extensions/discord/src/monitor/message-handler.process.ts — buildFinalEdit returns undefined (routing to the normal create-based delivery) when the rewritten final text has a targeted mention and no broadcast. Reuses the existing rewriteDiscordKnownMentions rewriter, resolveDiscordAccount resolver, and the new predicates — the same rewrite the message-tool/create path already uses.
    • extensions/discord/src/mentions.test.ts — unit coverage for the two predicates (user/role vs. channel/plain/broadcast).
    • extensions/discord/src/monitor/message-handler.process.test.ts — five integration cases pinning the delivery path: an alias-resolved final and a literal <@id> final are each delivered via fresh create (not an edit); an unaliased plain handle, an @everyone broadcast, and a mixed targeted-plus-@everyone final each still finalize in place via the preview edit.
  • What did NOT change (scope boundary):
    • extensions/discord/src/draft-stream.ts — streamed intermediate preview frames still suppress mentions (allowed_mentions: { parse: [] }) exactly as before; untouched.
    • Replies with no targeted mention — still finalize in place via the preview edit (no extra message, same UX).
    • @everyone / @here — left as-is and not rerouted. The reroute is also skipped when a targeted mention is mixed with @everyone / @here (e.g. @Sentinel @everyone), so a broadcast mention is never escalated into a fresh-create ping; that reply finalizes in place as before.
    • Config surface unchanged (no schema, defaults, doctor migrations, or docs/reference/config edits).
    • Plugin surface unchanged (no plugin SDK, manifest, extensions/api.ts / runtime-api.ts, registry, or loader edits).
    • Dependencies unchanged.

Reproduction

  1. Configure two Discord bot accounts in a shared guild/channel, with channels.discord.mentionAliases mapping a handle to a user ID (e.g. { "Sentinel": "1485891428809707651" }).
  2. Have an agent emit the mention in its natural final answer (not via the message tool) — either as the alias On it @Sentinel or as the canonical On it <@1485891428809707651>.
  3. Before this PR: the preview is finalized via an edit, so there is no ping, no notification, and no message.mentions entry — for both the alias form and the literal <@id> form.
  4. After this PR: the final answer is delivered as a fresh message with the mention intact (@Sentinel rewritten to <@1485891428809707651>, or the literal <@1485891428809707651> preserved), which pings.

Real behavior proof

Behavior addressed (#88360): a targeted mention (alias, directory handle, or literal <@id> / <@&id>) in a Discord final answer under live preview streaming is delivered as a fresh message create (which rewrites and pings) instead of an in-place edit (which Discord never notifies on); plain text and @everyone / @here still finalize in place unchanged.

Real environment tested (Linux, Node 22, production Discord handler processDiscordMessageInner driven against a mocked Discord REST transport): the test exercises the real outbound-finalize decision — in-place editMessageDiscord vs. fresh-create deliverDiscordReply — with a real channels.discord.mentionAliases config resolved through the production resolveDiscordAccount.

Exact steps or command run after this patch:

  1. pnpm test extensions/discord/src/monitor/message-handler.process.test.ts
  2. Before/after isolation: restore the production file to origin/main (no guard), run the alias and literal cases, then restore the patched file.
  3. pnpm exec oxfmt --check --threads=1 extensions/discord/src/monitor/message-handler.process.ts extensions/discord/src/monitor/message-handler.process.test.ts

Evidence before fix (verbatim vitest output, production file reverted to origin/main — reproduces the bug):

 ❯ extensions/discord/src/monitor/message-handler.process.test.ts (90 tests | 2 failed | 88 skipped)
     × delivers a fresh message instead of a preview edit when the final reply resolves a mention alias
     × delivers a fresh message instead of a preview edit for a literal user mention in the final reply
      Tests  2 failed | 88 skipped (90)

Both forms reproduce: the alias On it @Sentinel and the literal On it <@1485891428809707651> are each finalized via an edit (editMessageDiscord is called) instead of a fresh create.

Evidence after fix (verbatim vitest output for the four delivery-path cases):

 RUN  v4.1.7

 Test Files  1 passed (1)
      Tests  4 passed | 86 skipped (90)

Observed result after fix:

  • An alias final (On it @Sentinel with mentionAliases: { Sentinel: "1485891428809707651" }) and a literal final (On it <@1485891428809707651>) no longer call editMessageDiscord; both route to deliverDiscordReply (a fresh create that pings).
  • An unaliased plain handle (On it @Sentinel, no alias configured) and an @everyone broadcast still finalize in place via the preview edit — no escalation.
  • Full file: Tests 90 passed (90).

What was not tested:

  • A live Discord guild round-trip — no bot instance was available in this environment, so the after-fix evidence drives the production handler against a mocked Discord transport rather than a live gateway. The decisive ping/no-ping fact is documented Discord platform behavior (mention notifications fire on message create, not on edit), which is why rewriting the edited content alone is insufficient and the fix reroutes to a create. A live two-bot round-trip (alias and literal <@id>) is the recommended maintainer-side confirmation.

Regression tests:

  • message-handler.process.test.ts → delivers a fresh message instead of a preview edit when the final reply resolves a mention alias and delivers a fresh message instead of a preview edit for a literal user mention in the final reply (both fail before the fix; see Evidence before fix), plus the controls still finalizes via preview edit when an unaliased handle stays plain text, still finalizes via preview edit for broadcast mentions like @everyone, and still finalizes via preview edit when a targeted mention is mixed with @everyone (this last one fails if the broadcast exclusion is dropped, locking the no-escalation guarantee).

Risk / Mitigation

  • Risk: the in-place-vs-create decision could over-trigger, or escalate a broadcast ping. Mitigation: it triggers only when the rewritten final text contains a targeted user/role mention (<@id> / <@&id>) and contains no @everyone / @here; plain text, broadcast-only, and mixed targeted-plus-broadcast replies all stay on the unchanged in-place edit (dedicated tests lock the @everyone and mixed cases, so the create path can never newly ping everyone).
  • Risk: a final answer containing a targeted mention is now delivered as a fresh message rather than finalizing the streamed preview in place. Mitigation: the streamed preview is discarded by the existing normal-delivery fallback — the same path already used for media/error/explicit-reply finals — so no duplicate message is produced.
  • Risk: rewriting lengthens text (@name → <@id>), which could affect chunking. Mitigation: the create path is the same one the message tool uses and applies the existing chunking, so over-length handling is unchanged.

Change Type (select all)

  • Bug fix

Scope (select all touched areas)

  • Channels / messaging (Discord)

Linked Issue/PR

Fixes #88360

Related #67587

@openclaw-barnacle openclaw-barnacle Bot added channel: discord Channel integration: discord size: S labels May 31, 2026
@clawsweeper

clawsweeper Bot commented May 31, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed May 31, 2026, 5:32 AM ET / 09:32 UTC.

Summary
The PR adds Discord mention-detection helpers and reroutes targeted mention-bearing live-preview final replies from in-place edit finalization to fresh message delivery, with regression coverage.

PR surface: Source +28, Tests +120. Total +148 across 4 files.

Reproducibility: yes. for a source-level and handler-test reproduction: current main finalizes live-preview finals via edit, while the fresh send path is the path that rewrites aliases. A live Discord guild reproduction was not run here.

Review metrics: 1 noteworthy metric.

  • Discord final-delivery cases: 2 rerouted to create, 3 kept on edit. The new tests show the intended delivery-mode boundary for targeted mentions versus plain and broadcast controls before merge.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🌊 off-meta tidepool
Patch quality: 🦞 diamond lobster
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P2] Run or accept a live Discord alias/literal mention round trip with the broadcast and mixed-message controls before merge.

Mantis proof suggestion
A real Discord visual/live proof would materially reduce the remaining notification and duplicate-delivery uncertainty. A maintainer can ask Mantis to capture proof by posting a new PR comment that starts with the OpenClaw Mantis account mention, followed by:

visual task: verify Discord live-preview finals with an alias and literal <@id> create one pinging message, while @everyone and mixed targeted-plus-@everyone finals do not escalate.

Risk before merge

  • [P1] Live Discord was not exercised in this review or in the PR proof, so the actual alias/literal ping, preview cleanup with no duplicate final, and broadcast/mixed non-escalation behavior still need maintainer validation or acceptance.
  • [P1] The PR intentionally changes targeted Discord live-preview finals from edit finalization to fresh message creation, which changes notification behavior for affected replies.

Maintainer options:

  1. Run a live Discord round trip (recommended)
    Verify alias and literal targeted finals create exactly one pinging message while plain, broadcast, and mixed targeted-plus-broadcast finals do not escalate.
  2. Accept the mocked-handler proof
    Maintainers can choose to merge based on the focused production-handler tests and own the remaining live transport uncertainty.
  3. Pause for Discord owner review
    If notification semantics or broadcast policy need a product call, hold the PR for a Discord-channel owner rather than widening the patch.

Next step before merge

  • [P2] The PR is already a focused implementation; the remaining work is maintainer validation or acceptance of live Discord delivery risk, not an automated code repair.

Security
Cleared: The diff only changes Discord mention detection and handler tests; no dependency, workflow, secret, permission, or supply-chain surface was changed.

Review details

Best possible solution:

Land the focused fallback after a maintainer accepts the Discord delivery risk or verifies a live alias/literal mention round trip with the broadcast controls.

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

Yes for a source-level and handler-test reproduction: current main finalizes live-preview finals via edit, while the fresh send path is the path that rewrites aliases. A live Discord guild reproduction was not run here.

Is this the best way to solve the issue?

Yes, the proposed fix is a narrow owner-local change that reuses the existing mention rewriter and normal fresh-delivery path instead of adding a new config or plugin API. The safer pre-merge step is live transport proof or explicit maintainer acceptance of the delivery semantics.

AGENTS.md: found and applied where relevant.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 70c8abdca191.

Label changes

Label justifications:

  • P2: This is a normal-priority Discord message-delivery bug fix with limited channel-specific blast radius.
  • merge-risk: 🚨 message-delivery: The PR changes whether certain Discord final replies are edited in place or sent as fresh messages, which can affect pings, duplicate prevention, and broadcast safety.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The author association is MEMBER, so the external-contributor proof gate does not apply; the PR body provides mocked production-handler proof but no live Discord round trip.
Evidence reviewed

PR surface:

Source +28, Tests +120. Total +148 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 2 29 1 +28
Tests 2 121 1 +120
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 150 2 +148

What I checked:

Likely related people:

  • steipete: git blame and recent history tie the current Discord live-preview finalization and mention-rewrite code on main to Peter Steinberger's commits, including the latest release commit carrying these files. (role: recent area contributor; confidence: medium; commits: 4d95ae39d432, e93216080aa1; files: extensions/discord/src/monitor/message-handler.process.ts, extensions/discord/src/mentions.ts, src/channels/message/live.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added 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. labels May 31, 2026
@openperf
openperf force-pushed the fix/88360-discord-mention-stream-finalize branch from b3ef15f to a6b029d Compare May 31, 2026 05:50
@openperf
openperf force-pushed the fix/88360-discord-mention-stream-finalize branch from a6b029d to a7633da Compare May 31, 2026 06:08
@openperf
openperf force-pushed the fix/88360-discord-mention-stream-finalize branch from a7633da to 97ee70c Compare May 31, 2026 06:23
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. 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 May 31, 2026
@openperf
openperf force-pushed the fix/88360-discord-mention-stream-finalize branch from 97ee70c to 6d583ea Compare May 31, 2026 06:59
@clawsweeper clawsweeper Bot added 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: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels May 31, 2026
@openperf
openperf force-pushed the fix/88360-discord-mention-stream-finalize branch from 6d583ea to a9fcb66 Compare May 31, 2026 07:35
@openperf
openperf force-pushed the fix/88360-discord-mention-stream-finalize branch from a9fcb66 to fb246b3 Compare May 31, 2026 09:26
@steipete steipete self-assigned this May 31, 2026
@steipete

Copy link
Copy Markdown
Contributor

Maintainer pre-merge proof for #88527.

Scope reviewed: Discord live-preview final delivery for mention-bearing replies. The patch reroutes targeted user/role mention finals from edit-based preview finalization to fresh message delivery, reusing the existing sendMessageDiscord mention rewrite path.

Checks accepted before merge:

  • CI run 26708866609: relevant CI/check lanes reported success in the live PR rollup, including preflight, build-artifacts, check-prod-types, check-lint, check-test-types, check-guards, check-additional-extension-channels, check-additional-extension-bundled, and related fast checks.
  • Real behavior proof run 26708866194: success.
  • CodeQL completed neutral; Opengrep completed success.
  • Local review: read the changed Discord handler, mention helpers, reply delivery path, fresh send rewrite path, and added tests.
  • Local commands run: git diff --check origin/main...refs/tmp/pr-88527; git merge-tree origin/main refs/tmp/pr-88527; both clean.

Known proof gap accepted for landing: no live Discord guild round trip was run here. The mocked production-handler proof covers the create-vs-edit decision, and the fresh-create path is the existing Discord send path that rewrites aliases before create-based delivery.

@steipete
steipete merged commit a5d8f09 into openclaw:main May 31, 2026
135 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: discord Channel integration: discord merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S 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.

[Bug]: Discord mentionAliases is not applied to session reply / final assistant text, only to message tool calls

2 participants