#13318: fix(agents): prevent sanitizeUserFacingText from rewriting conversational context overflow mentions
agents
stale
## Fix
The function `sanitizeUserFacingText` was incorrectly rewriting valid assistant responses that happened to mention "context overflow" (e.g., explaining why a request failed previously).
This PR adds `isLikelyAssistantContent` heuristic to detect structured responses (markdown, paragraphs, conversational markers) and skip the rewrite unless the text is explicitly a raw error payload.
Fixes #13310.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR changes `sanitizeUserFacingText` to avoid rewriting normal assistant-facing prose that happens to mention “context overflow”, by introducing an `isLikelyAssistantContent` heuristic (length/paragraphs/markdown/conversational starts) and only applying the “raw error payload” formatting when the input still looks like an API/HTTP error.
It also adds a new Vitest file to cover the intended behavior: raw context-overflow errors should still be rewritten to the user-friendly message, while conversational/markdown explanations containing the phrase should pass through unchanged.
<h3>Confidence Score: 3/5</h3>
- This PR should not be merged until the new test import is fixed and the sanitizer’s `<final>`-stripping behavior is made consistent in the new heuristic branch.
- The core logic change is small and well-targeted, but the added test appears to import from a module path that doesn’t exist in-repo, and the new early-return path formats errors from the unstripped input, which can regress existing sanitization guarantees.
- src/agents/pi-embedded-helpers.context-overflow.test.ts, src/agents/pi-embedded-helpers/errors.ts
<!-- greptile_other_comments_section -->
<sub>(5/5) You can turn off certain types of comments like style [here](https://app.greptile.com/review/github)!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#10792: Agents: avoid context overflow false positives
by arunsanna · 2026-02-07
88.9%
#8661: fix: display rate limit errors correctly instead of as context over...
by dbottme · 2026-02-04
83.2%
#15109: fix: distinguish transient API errors from billing errors
by jwchmodx · 2026-02-13
80.7%
#12974: fix: intermittent (no output) reported by users
by vincentkoc · 2026-02-10
80.2%
#18997: fix: improve context overflow error messages and docs
by realhoratiobot · 2026-02-17
80.1%
#3880: fix: drop assistant messages with stopReason 'error' to avoid orpha...
by SalimBinYousuf1 · 2026-01-29
80.0%
#12325: fix: trim leading/trailing whitespace from outbound messages
by jordanstern · 2026-02-09
79.3%
#7085: test: skip flaky workspace-paths & safe-bins tests on non-Linux/CI ...
by ThinkIbrokeIt · 2026-02-02
79.3%
#19271: fix: remove false-positive billing text rewrite in sanitizeUserFaci...
by MisterGuy420 · 2026-02-17
79.1%
#15395: Auto-reply: strip leaked protocol transcript lines from inbound con...
by kiranjd · 2026-02-13
78.8%