#5982: fix: sanitize model reasoning blocks from Discord output
channel: discord
size: S
Cluster:
Model Reasoning Fixes
**Description:**
## Problem
Models like Gemini 3 Pro, Kimi K2.5, and Grok emit internal reasoning blocks (`<think>...</think>`, `Thought:...`) which were being sent to Discord verbatim, leaking chain-of-thought and causing stalled outputs during tool calls.
## Solution
Sanitizes assistant output at the Discord provider boundary (`send.shared.ts`), removing reasoning-only blocks while preserving user-facing responses.
## Testing
- Added comprehensive test suite with 9 test cases
- All tests passing
- Handles multiple reasoning formats (Gemini, Grok, DeepSeek, Kimi)
Fixes #5946
<h2>**Comment not updated to current version**</h2>
<!-- greptile_comment -->
<h6>Greptile Overview</h6>
<h6>Greptile Summary</h6>
This PR adds a Discord-boundary sanitizer (`sanitizeModelOutput`) in `src/discord/send.shared.ts` to strip common “reasoning” formats (e.g., `<think>…</think>`, `Thought:` blocks, and bracket/paren markers) before chunking/sending messages, aiming to prevent chain-of-thought leakage and tool-call stalls. It also adds a standalone `test-sanitizer.js` script with several example cases.
Main concerns are around edge-case behavior: `sendDiscordText` now throws when sanitization removes everything (which can turn “reasoning-only” outputs into hard send failures), and the `Thought:` regex doesn’t behave consistently at the beginning of the string due to missing multiline anchoring. Additionally, the provided tests are not integrated into the repo’s Vitest suite, so they won’t run in CI by default.
<h3>Confidence Score: 3/5</h3>
- This PR is likely safe to merge after addressing a couple edge-case behavior issues.
- Core change is localized to Discord send boundary and is conceptually straightforward, but there are a few behavior gaps: (1) sending can now hard-fail if output is fully sanitized away, and (2) the `Thought:` removal regex doesn’t match start-of-string as intended. The added tests are not wired into CI, so correctness relies on manual execution unless converted to Vitest.
- src/discord/send.shared.ts; test-sanitizer.js
<!-- greptile_other_comments_section -->
<!-- /greptile_comment —>
Most Similar PRs
#17304: feat(gemini): robust handling for non-XML reasoning headers (`Think...
by YoshiaKefasu · 2026-02-15
80.6%
#5011: fix(gemini): strip JSON thoughtSignature from tool calls for Gemini 3+
by shayan919293 · 2026-01-30
79.3%
#17455: fix: strip content before orphan closing think tags
by jwt625 · 2026-02-15
77.7%
#10097: fix: add empty thinking blocks to tool call messages when thinking is…
by cyxer000 · 2026-02-06
77.5%
#16733: fix(ui): avoid injected newlines when tool output is hidden
by jp117 · 2026-02-15
77.2%
#13318: fix(agents): prevent sanitizeUserFacingText from rewriting conversa...
by hleliofficiel · 2026-02-10
76.1%
#22321: fix: sanitize tool schemas for all Cloud Code Assist providers
by Sr-0w · 2026-02-21
76.1%
#16464: fix: harden OpenAI reasoning replay sanitization
by Swader · 2026-02-14
75.8%
#18187: fix: tool summaries silently dropped when reasoningLevel is stream
by ayanesakura · 2026-02-16
75.6%
#20050: fix: Telegram polling regression and thinking blocks corruption (AI...
by Vaibhavee89 · 2026-02-18
75.6%