Skip to content

fix(outbound): strip inbound metadata in normalizeReplyPayloadsForDelivery - #51653

Closed
p3nchan wants to merge 1 commit into
openclaw:mainfrom
p3nchan:fix/strip-inbound-meta-outbound-delivery
Closed

p3nchan wants to merge 1 commit into
openclaw:mainfrom
p3nchan:fix/strip-inbound-meta-outbound-delivery

Conversation

@p3nchan

@p3nchan p3nchan commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Apply stripInboundMetadata() to outbound payload text in normalizeReplyPayloadsForDelivery(), preventing AI-facing metadata blocks from leaking into user-visible messages
  • The inbound metadata (Conversation info, Sender info, reply context, etc.) injected by buildInboundUserContextPrefix was not being stripped in the outbound delivery pipeline
  • Add regression test verifying metadata-prefixed payload text is normalized to user-visible content only

Fixes #39847
Related: #41355

Details

normalizeReplyPayloadsForDelivery() processes reply payloads through formatBtwTextForExternalDelivery but did not call stripInboundMetadata(). This meant that on delivery paths where the text still contained raw inbound metadata blocks, those blocks would appear in the user-facing message.

The fix wraps the existing formatBtwTextForExternalDelivery(...) result with stripInboundMetadata(...) — a single-line change at the source level that covers all delivery paths (Discord, compact mode, Pi-embedded, etc.) without needing per-adapter patches.

Test plan

  • pnpm test -- src/infra/outbound/payloads.test.ts — new test verifies metadata stripping
  • Existing payload normalization tests still pass
  • Manual: send a message via Discord thread and confirm no metadata prefix in delivered reply

🤖 Generated with Claude Code + Codex

…ivery

Apply stripInboundMetadata() to outbound payload text during delivery
normalization. Previously, AI-facing metadata blocks (Conversation info,
Sender info, etc.) injected by buildInboundUserContextPrefix could leak
into user-visible messages when the outbound pipeline did not call the
strip function.

Fixes openclaw#39847

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Codex (GPT-5.4) <[email protected]>
@greptile-apps

greptile-apps Bot commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a metadata leakage bug where AI-facing inbound metadata blocks (Conversation info, Sender info, etc.) injected by buildInboundUserContextPrefix were not being stripped before outbound delivery, causing them to appear in user-visible messages.

The fix is a targeted, single-line change in normalizeReplyPayloadsForDelivery that wraps the existing formatBtwTextForExternalDelivery(...) result with stripInboundMetadata(...). Because normalizeReplyPayloadsForDelivery is the central normalization step called by all delivery adapters (normalizeOutboundPayloads, normalizeOutboundPayloadsForJson, and direct callers), the fix applies universally without requiring per-adapter patches.

Key points:

  • The ordering is correct: parseReplyDirectives extracts directives from the raw text first, formatBtwTextForExternalDelivery formats it, then stripInboundMetadata removes any remaining metadata blocks from the result.
  • stripInboundMetadata has a fast-path that returns early (zero allocation) when no sentinels are present, so non-metadata payloads are unaffected.
  • The new regression test is well-scoped: it exercises the exact metadata block format produced by buildInboundUserContextPrefix and asserts that only the user-visible content survives normalization.

Confidence Score: 5/5

  • This PR is safe to merge — it is a well-scoped single-line fix with a targeted regression test and no side effects on payloads that do not contain metadata blocks.
  • The change is minimal and correct: the wrapping order (format then strip) is validated by the new test, the fast-path in stripInboundMetadata ensures zero overhead for metadata-free payloads, and the fix is applied at the single centralized normalization point used by all delivery paths. No pre-existing tests were altered and the new test directly covers the bug scenario.
  • No files require special attention.

Last reviewed commit: "fix(outbound): strip..."

@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: 9750eb8860

ℹ️ 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".

...payload,
text: parsed.text ?? "",
}) ?? "",
stripInboundMetadata(

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.

P2 Badge Preserve legitimate timestamp-prefixed reply text

Calling stripInboundMetadata(...) on every outbound payload can corrupt valid assistant output, because that helper also strips any leading text matching a timestamp pattern (for example "[Wed 2026-03-11 23:51 PDT] ...") in addition to metadata blocks. This function was designed for sanitizing inbound user-role content, so applying it unconditionally here means replies that intentionally begin with bracketed timestamps (logs, schedules, transcripts) will be silently altered before delivery.

Useful? React with 👍 / 👎.

@Ryce

This comment was marked as spam.

@clawsweeper

clawsweeper Bot commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: found issues before merge.

What this changes:

The PR imports stripInboundMetadata into outbound payload normalization, wraps formatted delivery text with it, and adds a regression test for Conversation/Sender metadata blocks.

Maintainer follow-up before merge:

This should not go straight to an automated repair lane because the branch is stale/unmergeable, overlaps with #39847/#50520, and current main has a later sanitizer that may supersede part of the original request; maintainer judgment is needed on whether a direct outbound-plan sanitizer remains required.

Security review:

Security review cleared: The diff changes internal text normalization and tests only; it does not add dependencies, CI permissions, release scripts, secrets handling, downloads, or package execution paths.

Review findings:

  • [P2] Preserve timestamp-prefixed replies — src/infra/outbound/payloads.ts:74
Review details

Best possible solution:

Keep the assistant/replay sanitizer that already exists on main, then add a narrow outbound-only sanitizer only if maintainers want defense-in-depth at the payload-plan boundary. That sanitizer should remove generated metadata sentinel blocks and terminal untrusted-context suffixes without stripping legitimate timestamp-prefixed reply text, and the remaining work should be consolidated with #39847/#50520.

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

Yes. The metadata-leak scenario is represented by the PR test, and the blocking regression is reproducible by passing a timestamp-prefixed payload through the PR branch because stripInboundMetadata() strips timestamp prefixes by design.

Is this the best way to solve the issue?

No. Applying the inbound display sanitizer at the shared outbound plan boundary is broader than necessary; a safer fix would be an outbound-specific copied-metadata sanitizer that preserves ordinary assistant content such as logs and transcripts.

Full review comments:

  • [P2] Preserve timestamp-prefixed replies — src/infra/outbound/payloads.ts:74
    Calling stripInboundMetadata(...) on every outbound payload also removes leading timestamp prefixes even when no metadata block is present. Assistant replies that intentionally start with a timestamp, such as logs or transcripts, would be delivered with that content silently altered; use an outbound-only metadata block sanitizer instead.
    Confidence: 0.91

Overall correctness: patch is incorrect
Overall confidence: 0.9

Acceptance criteria:

  • pnpm test -- src/infra/outbound/payloads.test.ts
  • pnpm test -- src/auto-reply/reply/strip-inbound-meta.test.ts
  • pnpm test -- src/agents/pi-embedded-helpers.sanitizeuserfacingtext.test.ts
  • pnpm test -- src/agents/pi-embedded-runner.sanitize-session-history.test.ts

What I checked:

  • PR diff applies broad inbound sanitizer: The proposed patch wraps every formatted outbound payload text in stripInboundMetadata(...), which introduces the timestamp-stripping behavior at the shared outbound normalization boundary. (src/infra/outbound/payloads.ts:74, 9750eb886064)
  • Existing helper strips timestamps by design: stripInboundMetadata() removes LEADING_TIMESTAMP_PREFIX_RE before sentinel checks, so it can change text even when no metadata block is present. (src/auto-reply/reply/strip-inbound-meta.ts:188, 36eec68fb9cd)
  • Timestamp stripping is covered by current tests: Current tests assert [Wed 2026-03-11 23:51 PDT] hello is normalized to hello, confirming the regression mode introduced if this helper is applied to arbitrary outbound text. (src/auto-reply/reply/strip-inbound-meta.test.ts:176, 36eec68fb9cd)
  • Current main already sanitizes normal assistant-facing text: normalizeReplyPayload() calls sanitizeUserFacingText(...), and that sanitizer now strips inbound metadata after final-tag/runtime-context cleanup. (src/auto-reply/reply/normalize-reply.ts:98, 36eec68fb9cd)
  • Later main commit covers copied metadata in assistant replies and replay: Commit ee8f41f56e3f added stripInboundMetadata to sanitizeUserFacingText, added user-facing assistant text and replay-history regression tests, documented Discord behavior, and added a changelog entry for copied inbound metadata leaks. (src/agents/pi-embedded-helpers/sanitize-user-facing-text.ts:402, ee8f41f56e3f)
  • Shared outbound plan still has no narrow sanitizer: Current createOutboundPayloadPlanEntry() formats parsed text but does not strip metadata at the plan boundary, so maintainers still need to decide whether direct plan callers require an outbound-only sanitizer. (src/infra/outbound/payloads.ts:158, 36eec68fb9cd)

Likely related people:

  • steipete: Authored the later main fix that strips copied inbound metadata from user-facing assistant text and replay history, and authored earlier hardening of inbound metadata sentinel stripping. (role: recent maintainer / adjacent owner; confidence: high; commits: ee8f41f56e3f, b99666a47aef; files: src/agents/pi-embedded-helpers/sanitize-user-facing-text.ts, src/agents/pi-embedded-runner/replay-history.ts, src/auto-reply/reply/strip-inbound-meta.ts)
  • joshavant: Authored the commit that centralized outbound payload normalization into createOutboundPayloadPlan, the boundary this PR modifies. (role: introduced current outbound plan boundary; confidence: high; commits: c4764095f859; files: src/infra/outbound/payloads.ts, src/infra/outbound/payloads.test.ts, src/infra/outbound/deliver.ts)
  • vincentkoc: Extracted the shared sanitizeUserFacingText helper that current main now uses for assistant-visible outbound text cleanup. (role: adjacent sanitizer maintainer; confidence: medium; commits: 35664d5447a4; files: src/agents/pi-embedded-helpers/sanitize-user-facing-text.ts, src/agents/pi-embedded-helpers/errors.ts)

Remaining risk / open question:

Codex review notes: model gpt-5.5, reasoning high; reviewed against 36eec68fb9cd.

@p3nchan

p3nchan commented May 2, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #50520 which includes the strip-inbound-meta.ts fix as well. Closing in favor of the comprehensive PR.

@p3nchan p3nchan closed this May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Echo contamination: stripInboundMetadata not called in outbound delivery pipeline

2 participants