Skip to content

fix(discord): reject mismatched reply context - #114716

Merged
steipete merged 2 commits into
openclaw:mainfrom
pfrederiksen:fix/discord-reply-context-fallback
Jul 28, 2026
Merged

steipete merged 2 commits into
openclaw:mainfrom
pfrederiksen:fix/discord-reply-context-fallback

Conversation

@pfrederiksen

@pfrederiksen pfrederiksen commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • verify that Discord's nested referenced_message.id matches message_reference.message_id
  • fetch the canonical reply target when the nested payload is missing or mismatched
  • clear mismatched reply context if canonical hydration fails, preventing unrelated message text from reaching the model

What Problem This Solves

Discord reply turns can arrive with stale or mismatched nested referenced-message data. Trusting that payload allows an unrelated older message to be assembled as the current reply context.

This hardens the existing reply hydration path associated with #112650.

Evidence

  • pnpm test:changed — 1 shard, 9/9 tests passed
  • pnpm check:changed — passed (format, policy guards, API contracts, extension and extension-test typechecks, lint, import cycles)
  • mandatory autoreview — clean, no actionable findings
  • TruffleHog preflight — clean
  • After-fix runtime proof on rebased head 21221e103f7d:
    • replaces a mismatched nested reply with the canonical referenced message passed through the real Message hydration and buildDiscordMessageProcessContext path.
    • The resulting context asserts ReplyToId === "m0", ReplyToBody === "the canonical reply target", and that ReplyToBody excludes "unrelated older context".
    • discards a mismatched nested reply when canonical hydration fails passed through the same downstream context builder and asserts both ReplyToId and ReplyToBody are absent.
    • Verbose run: 9/9 passed in the Discord extension shard.
  • Contract inspection:
    • The internal Message.referencedMessage getter reads rawData.referenced_message.
    • Hydration runs in message preflight before buildDiscordMessageProcessContext.
    • resolveReplyContext consumes only the hydrated message.referencedMessage, so clearing an invalid nested payload prevents it from reaching the agent envelope.
  • Redacted real-Discord setup proof:
    • Captured a real native reply and two real bot messages from the active Discord channel through OpenClaw's Discord connector.
    • Replayed the captured reply through the patched hydration/context path with the unrelated captured message injected as stale nested context and the real referenced message supplied as the canonical fetch result.
    • Output proved replyToIdMatchesCanonical: true, staleBodyExcluded: true, and a redacted canonical body digest.
    • Fault-injected canonical fetch failure proved replyToIdOmitted: true and replyToBodyOmitted: true.

Release note

Fixed Discord replies occasionally using unrelated stale message context when a referenced-message payload did not match the canonical reply target.

@openclaw-barnacle openclaw-barnacle Bot added channel: discord Channel integration: discord size: S triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. and removed triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Jul 27, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Jul 27, 2026
@clawsweeper

clawsweeper Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 27, 2026, 4:20 PM ET / 20:20 UTC.

ClawSweeper review

What this changes

The PR treats a Discord nested reply payload whose ID differs from the message reference as invalid, replaces it with the canonical REST-fetched message, and omits reply context if that fetch fails.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

This remains necessary: it addresses the open Discord native-reply regression by preventing stale nested reply text from becoming agent context. The proposed fail-closed behavior is bounded to mismatched payloads, has downstream context coverage, and the contributor supplied redacted real-Discord replay output; no discrete patch defect was established. Likely related person: pfrederiksen is the recent patch contributor, but historical ownership could not be established from the supplied review materials.

Priority: P1
Reviewed head: 21221e103f7d1e9a35b49e985be8b913db5016f5

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused repair with downstream regression coverage and credible redacted real-connector evidence; only the normal refreshed-head review remains.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The contributor supplied redacted live Discord connector capture/replay output through the patched hydration and agent-context path, showing canonical replacement, stale-body exclusion, and omission on fetch failure; private content and identifiers were redacted.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The contributor supplied redacted live Discord connector capture/replay output through the patched hydration and agent-context path, showing canonical replacement, stale-body exclusion, and omission on fetch failure; private content and identifiers were redacted.
Evidence reviewed 5 items Patch boundary: The PR head classifies reply payloads as complete, missing, or invalid; an invalid nested payload is refetched from the canonical message-reference target rather than trusted.
Fail-closed downstream behavior: When canonical reply hydration fails after an ID mismatch, the patch clears the nested reply payload so the existing context builder cannot emit an unrelated ReplyToId or ReplyToBody.
Regression coverage: Added tests exercise both canonical replacement and fetch-failure clearing through Message hydration and buildDiscordMessageProcessContext, asserting canonical reply data is used or omitted downstream.
Findings None None.
Security None None.

How this fits together

Discord inbound messages are hydrated before OpenClaw builds the agent-visible message context. This change validates reply metadata at that hydration boundary, then either supplies the canonical referenced message or prevents unrelated reply text from reaching the agent envelope.

flowchart LR
  A[Discord inbound reply] --> B[Reply metadata check]
  B -->|Nested message matches| C[Use nested reply]
  B -->|Missing or mismatched| D[Fetch canonical reply]
  D -->|Fetch succeeds| E[Hydrated canonical message]
  D -->|Fetch fails after mismatch| F[Clear reply context]
  C --> G[Agent message context]
  E --> G
  F --> G
Loading

Before merge

  • Resolve merge risk (P1) - The deliberate fail-closed branch omits reply context when a mismatched payload cannot be canonically fetched; that avoids unrelated context but can leave the agent without a valid quoted message during a Discord REST failure. The branch is also behind current main, so final merge should use a refreshed head and checks.
  • Complete next step (P1) - No actionable patch defect remains for an automated repair; final action is normal owner review of the intentional session-context fail-closed behavior after the branch is refreshed.
Agent review details

Security

None.

PR surface

Source +22, Tests +102. Total +124 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 28 6 +22
Tests 1 103 1 +102
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 131 7 +124

Review metrics

None.

Merge-risk options

Maintainer options:

  1. Refresh and retain the safe fallback (recommended)
    Rebase or update the branch onto current main, rerun required checks, and retain omission of mismatched reply context when canonical hydration fails.
  2. Accept the fail-closed context tradeoff
    Merge with the explicit understanding that a Discord REST failure after an invalid nested reply will omit reply context rather than risk supplying unrelated text.

Technical review

Best possible solution:

Keep validation at the Discord hydration boundary, preserve the canonical-fetch-or-clear rule for mismatched reply data, and merge only after the branch is refreshed against current main and normal Discord-owner review confirms the fail-closed tradeoff.

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

Yes—source inspection from the supplied diff gives a high-confidence path: current behavior only distinguishes missing nested payloads, so a present mismatched payload can reach the downstream reply-context builder; the new regression tests directly cover that path.

Is this the best way to solve the issue?

Yes. Validating the identifier at the existing Discord hydration boundary is narrower and safer than adding defensive checks in later context projections, because all consumers then receive either canonical reply data or no reply data.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 762f04da5abc.

Labels

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient. The contributor supplied redacted live Discord connector capture/replay output through the patched hydration and agent-context path, showing canonical replacement, stale-body exclusion, and omission on fetch failure; private content and identifiers were redacted.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The contributor supplied redacted live Discord connector capture/replay output through the patched hydration and agent-context path, showing canonical replacement, stale-body exclusion, and omission on fetch failure; private content and identifiers were redacted.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: 📣 needs proof: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P1: A malformed Discord native reply can cause the agent to receive unrelated conversational context, affecting active channel workflows.
  • merge-risk: 🚨 session-state: The patch changes which referenced-message state is propagated into the agent context and intentionally clears that state on canonical-fetch failure.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The contributor supplied redacted live Discord connector capture/replay output through the patched hydration and agent-context path, showing canonical replacement, stale-body exclusion, and omission on fetch failure; private content and identifiers were redacted.
  • proof: sufficient: Contributor real behavior proof is sufficient. The contributor supplied redacted live Discord connector capture/replay output through the patched hydration and agent-context path, showing canonical replacement, stale-body exclusion, and omission on fetch failure; private content and identifiers were redacted.

Evidence

What I checked:

  • Patch boundary: The PR head classifies reply payloads as complete, missing, or invalid; an invalid nested payload is refetched from the canonical message-reference target rather than trusted. (extensions/discord/src/monitor/message-handler.hydration.ts:175, 21221e103f7d)
  • Fail-closed downstream behavior: When canonical reply hydration fails after an ID mismatch, the patch clears the nested reply payload so the existing context builder cannot emit an unrelated ReplyToId or ReplyToBody. (extensions/discord/src/monitor/message-handler.hydration.ts:231, 21221e103f7d)
  • Regression coverage: Added tests exercise both canonical replacement and fetch-failure clearing through Message hydration and buildDiscordMessageProcessContext, asserting canonical reply data is used or omitted downstream. (extensions/discord/src/monitor/message-handler.hydration.test.ts:219, 21221e103f7d)
  • Real behavior proof: The PR body and follow-up comment include redacted output from a real Discord connector capture replayed through the patched hydration/context path, reporting canonical target selection, stale-body exclusion, and omission on injected fetch failure; the PR checks also report successful Real behavior proof runs. (21221e103f7d)
  • Canonical regression relationship: The related open report documents a live Discord native-reply context failure and specifies the established expectation that a missing referenced-message payload is fetched and injected. This PR hardens that same hydration boundary for mismatched nested data. (extensions/discord/src/monitor/message-handler.hydration.ts:175, 21221e103f7d)

Likely related people:

  • pfrederiksen: Authored the two commits on this focused Discord hydration repair; this is a routing signal only, not an assertion of historical ownership. (role: recent patch contributor; confidence: low; commits: 151f0683fb2a, 21221e103f7d; files: extensions/discord/src/monitor/message-handler.hydration.ts, extensions/discord/src/monitor/message-handler.hydration.test.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Refresh the branch against current main and rerun the required checks before final merge.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
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.

Workflow

  • 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.

History

Review history (2 earlier review cycles)
  • reviewed 2026-07-27T19:47:58.794Z sha 94668caa212db2ba2136e20aaf7f82e15eea0a40 :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-27T20:06:07.316Z sha 21221e1 :: needs real behavior proof before merge. :: none

@pfrederiksen
pfrederiksen force-pushed the fix/discord-reply-context-fallback branch from 94668ca to 21221e1 Compare July 27, 2026 20:00
@pfrederiksen

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Addressed the proof and downstream-contract concerns on rebased head 21221e103f7d:

  • Added end-to-end context assertions through buildDiscordMessageProcessContext.
  • Canonical replacement now proves ReplyToId=m0, ReplyToBody is the canonical target, and the unrelated stale body is absent.
  • Fetch-failure fallback now proves both ReplyToId and ReplyToBody are absent downstream.
  • Re-ran the verbose Discord extension shard: 9/9 passed.
  • Updated the PR Evidence section with the runtime and contract inspection details.

@clawsweeper

clawsweeper Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Command router queued. I will update this comment with the next step.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jul 27, 2026
@pfrederiksen

Copy link
Copy Markdown
Contributor Author

Added the requested redacted proof from a real Discord setup.

Method: captured a real native reply plus canonical and unrelated messages from the active Discord channel through OpenClaw’s Discord connector, injected the unrelated captured message as stale nested context, and ran that capture through the patched hydration and agent-context path. IDs and content were redacted/hashes only.

{
  "source": "redacted live Discord connector capture + patched hydration/context path",
  "canonicalFetchCalls": ["/channels/<redacted-id>/messages/<redacted-id>"],
  "success": {
    "replyToIdMatchesCanonical": true,
    "replyToBodyDigest": "8f9b1c031dbf",
    "staleBodyExcluded": true
  },
  "fetchFailure": {
    "replyToIdOmitted": true,
    "replyToBodyOmitted": true
  }
}

The live capture was not committed and contained no credentials. The PR Evidence section now records this proof.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Command router queued. I will update this comment with the next step.

@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: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 27, 2026
@steipete
steipete merged commit 28ecf95 into openclaw:main Jul 28, 2026
111 of 122 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 28, 2026
* fix(discord): reject mismatched reply context

* test(discord): prove safe reply context propagation
vincentkoc added a commit to christian-lallo/openclaw that referenced this pull request Jul 28, 2026
* origin/main: (561 commits)
  refactor(plugins): reuse prepared channel discovery (openclaw#114942)
  fix(discord): reject mismatched reply context (openclaw#114716)
  fix(auto-reply): keep fast approval result private
  fix(auto-reply): satisfy approval boundary checks
  fix(imessage): unblock text approval fallback
  fix(imessage): keep approval test resets internal
  fix(gateway): narrow channel approval runtime types
  fix(imessage): resolve approval controls safely
  fix(imessage): harden native approval polls
  feat(imessage): add native poll approval controls
  fix(mac): bound node worker crash retries (openclaw#114974)
  fix(channels): keep running-channel connectivity tri-state (openclaw#114970)
  feat(ui): expand the Lobsterdex with 14 palettes, lore tooltips, and a collection page (openclaw#114959)
  fix(agents): honor models.providers auth mode pin in credential eligibility (openclaw#114944)
  fix(apps): fence stale mobile delivery state
  test(pr): provide ripgrep surface in review harness (openclaw#114958)
  fix(code-mode): bound and simplify worker lifecycles (openclaw#114945)
  fix(mattermost): prevent hook-controlled preview leaks
  fix(tool-display): avoid recursive search summaries (openclaw#114933)
  perf(catalog): memoize probes and single-flight identical list requests (openclaw#114961)
  ...
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: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. 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.

2 participants