#16015: fix(gateway): truncate oversized message content in chat.history response
app: web-ui
gateway
stale
size: S
## Summary
- Add `truncateMessagesForChatHistory()` that caps each message's content fields to 50K chars before the response is byte-capped
- A single message with huge thinking output or tool response can dominate the entire `chat.history` payload, causing the Control UI to freeze
- Runs in the existing pipeline: `stripEnvelopeFromMessages → truncateMessagesForChatHistory → capArrayByJsonBytes`
- Truncated content ends with `… [content truncated for display]` so users know data was trimmed
## Test plan
- [x] 5 new tests in `chat-sanitize.test.ts` covering string content, content arrays, text field, no-op passthrough, and empty array
- [x] Build + lint clean
Ref #15992
lobster-biscuit
> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Added `truncateMessagesForChatHistory()` to cap message content fields at 50K characters before the response is byte-capped, preventing the Control UI from freezing when a single message with huge thinking output or tool response dominates the entire `chat.history` payload. The function runs in the existing sanitization pipeline: `stripEnvelopeFromMessages → truncateMessagesForChatHistory → capArrayByJsonBytes`. Truncated content includes `… [content truncated for display]` suffix to inform users of trimming.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The implementation follows existing patterns in the codebase, has comprehensive test coverage with 5 new tests covering various edge cases (string content, content arrays, text field, no-op passthrough, empty array), and the changes are well-isolated to a specific utility function. The truncation logic is straightforward and defensive, handling null/undefined cases properly. The function integrates seamlessly into the existing pipeline without disrupting other functionality.
- No files require special attention
<sub>Last reviewed commit: 185379a</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#16006: fix(gateway): reduce chat.history byte cap from 6 MB to 2 MB
by fagemx · 2026-02-14
84.9%
#8617: fix: truncate large console payloads before writing to file log
by dbottme · 2026-02-04
77.7%
#8360: Gateway: cap oversized transcript entries
by halbot2010 · 2026-02-03
76.7%
#16894: Fix text truncation splitting surrogate pairs in web-fetch, subagen...
by Clawborn · 2026-02-15
75.4%
#16261: feat(agents): add two-tier tool output truncation and excludeFromCo...
by ProgramCaiCai · 2026-02-14
74.8%
#23271: fix(chat): strip untrusted metadata blocks from Control UI messages
by lbo728 · 2026-02-22
74.6%
#12325: fix: trim leading/trailing whitespace from outbound messages
by jordanstern · 2026-02-09
74.2%
#12064: fix: prevent chunker from truncating messages that fit within limit
by joetomasone · 2026-02-08
73.7%
#16733: fix(ui): avoid injected newlines when tool output is hidden
by jp117 · 2026-02-15
73.5%
#19135: fix(agents): prune excess images from conversation history (#19099)
by pierreeurope · 2026-02-17
73.5%