#12325: fix: trim leading/trailing whitespace from outbound messages
channel: imessage
stale
size: XS
## Problem
`sanitizeUserFacingText` computes a trimmed version of outbound text for validation checks, but returns the untrimmed result from `collapseConsecutiveDuplicateBlocks(stripped)`. This causes leading/trailing newlines to appear in delivered messages.
This is especially visible on platforms like iMessage that don't collapse whitespace — messages arrive with blank lines before or after the actual content.
## Fix
Added `.trim()` to the final return of `sanitizeUserFacingText` so outbound text is always clean.
## Test
Added test cases for leading, trailing, and surrounding whitespace in the existing test suite.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This change fixes `sanitizeUserFacingText` so it returns trimmed outbound text after collapsing consecutive duplicate blocks, preventing delivered messages from having leading/trailing blank lines or spaces (notably on clients like iMessage that preserve whitespace). The PR also adds unit tests to cover leading, trailing, and surrounding whitespace cases.
The updated logic lives in `src/agents/pi-embedded-helpers/errors.ts` and is exercised by the colocated Vitest suite in `src/agents/pi-embedded-helpers.sanitizeuserfacingtext.test.ts`.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- The change is narrowly scoped (a final `.trim()` on sanitized outbound text) and is covered by new unit tests for the regressions described in the PR. No API surface changes or complex logic modifications were introduced.
- No files require special attention
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#10612: fix: trim leading blank lines on first emitted chunk only (#5530)
by 1kuna · 2026-02-06
83.5%
#8052: fix(whatsapp): strip leading whitespace from outbound messages
by FelixFoster · 2026-02-03
80.6%
#13318: fix(agents): prevent sanitizeUserFacingText from rewriting conversa...
by hleliofficiel · 2026-02-10
79.3%
#20301: Security: scrub untrusted metadata from user-facing replies
by ashishc2503 · 2026-02-18
78.8%
#19271: fix: remove false-positive billing text rewrite in sanitizeUserFaci...
by MisterGuy420 · 2026-02-17
78.0%
#16894: Fix text truncation splitting surrogate pairs in web-fetch, subagen...
by Clawborn · 2026-02-15
77.2%
#16733: fix(ui): avoid injected newlines when tool output is hidden
by jp117 · 2026-02-15
76.7%
#7395: fix(whatsapp): strip markdown bold/italic from URLs before sending
by lailoo · 2026-02-02
76.5%
#15395: Auto-reply: strip leaked protocol transcript lines from inbound con...
by kiranjd · 2026-02-13
75.9%
#17244: fix: strip TTS tags from agent replies before delivery (#14652)
by robbyczgw-cla · 2026-02-15
75.7%