#14098: Sanitize JSON tool-call payload text
agents
stale
Cluster:
Error Handling in Agent Tools
## Summary
- add stripJsonToolCallText helper to remove raw tool-call JSON payloads before they reach user surfaces
- call the helper from extractAssistantText, sanitizeTextContent, and the streaming pipeline so Ollama/local providers can no longer leak blobs when tool downgrades happen
- add regression tests covering the helper and ensuring extractAssistantText drops JSON payloads but retains normal JSON blobs
## Testing
- corepack pnpm vitest run src/agents/pi-embedded-utils.test.ts
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Adds defense against raw JSON tool-call payload leakage in text content. When local/Ollama providers downgrade tool calls to text (due to incompatibility or errors), the raw JSON can leak into user-facing surfaces. This PR introduces `stripJsonToolCallText()` helper that detects and removes JSON objects/arrays matching tool-call structure (having `name` + one of: `arguments`, `args`, `input`, `tool_input`, `parameters`, `payload`). The helper is integrated into the streaming pipeline (`pi-embedded-subscribe.ts`), text extraction (`extractAssistantText`), and message sanitization (`sanitizeTextContent`). Test coverage includes both removal of tool payloads and preservation of legitimate JSON blobs.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The implementation is defensive and well-tested. The helper function includes both strict JSON parsing validation and a fallback regex pattern. All integration points (streaming, extraction, sanitization) are covered. Test cases verify both positive (removing tool payloads) and negative (preserving legitimate JSON) scenarios. The change only affects text sanitization paths and cannot break tool call functionality.
- 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
#3647: fix: sanitize tool arguments in session history
by nhangen · 2026-01-29
80.1%
#19094: Fix empty tool_call_id and function names in provider transcript pa...
by yxshee · 2026-02-17
77.5%
#4964: fix: strip null-valued optional parameters from tool calls for prov...
by umut-polat · 2026-01-30
77.5%
#20534: fix: handle malformed tool call JSON parse errors gracefully
by echoVic · 2026-02-19
77.0%
#12487: fix(agents): strip orphaned tool_result when tool_use is sanitized ...
by skylarkoo7 · 2026-02-09
77.0%
#14328: fix: strip incomplete tool_use blocks from errored/aborted messages...
by Kropiunig · 2026-02-12
76.9%
#6687: fix(session-repair): strip malformed tool_use blocks to prevent per...
by NSEvent · 2026-02-01
76.6%
#16966: fix: strip tool_use blocks from aborted/errored assistant messages
by StressTestor · 2026-02-15
75.8%
#16733: fix(ui): avoid injected newlines when tool output is hidden
by jp117 · 2026-02-15
75.6%
#15649: fix: sanitize tool_use IDs on session write path
by aldoeliacim · 2026-02-13
75.4%