← Back to PRs

#14470: fix(agents): unescape newlines in write and edit tool content

by VintLin open 2026-02-12 06:01 View on GitHub →
agents size: XS
# Summary Unescape literal \n, \r\n, and \r in write/edit tool arguments to handle models that double-escape tool call content. # Problem Some models (like Grok) or providers/proxies occasionally send tool calls where newlines are escaped as literal strings (e.g., "Line1\nLine2"). The write tool writes these characters as-is, corrupting code and documentation files. # Changes - Extended `RequiredParamGroup` to support an `unescape` flag. - Enabled `unescape` for `write.content`, `edit.oldText`, and `edit.newText`. - Implemented robust newline unescaping in `wrapToolParamNormalization` to convert \r\n, \r, and \n to real newlines. # Validation - Verified with a local reproduction unit test using both real newlines and literal \n sequences. - Confirmed existing agent tests pass. # Risk & Rollback - Low risk. Only affects tools wrapped with param normalization and specific keys. - Revert by undoing the unescape logic in `src/agents/pi-tools.read.ts`. Fixes #14452 <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR updates the agent tool parameter normalization layer (`src/agents/pi-tools.read.ts`) to handle providers/models that double-escape tool call strings. It extends `RequiredParamGroup` with an `unescape` flag and enables it for `write.content` and `edit.oldText`/`edit.newText`, then performs newline normalization in `wrapToolParamNormalization` before required-param validation and tool execution. This ensures `\\n` sequences don’t get written literally into files when the model escapes newlines in tool arguments. <h3>Confidence Score: 4/5</h3> - Safe to merge after fixing the CR unescape gap for double-escaped CRLF/CR. - Change is localized to tool param normalization for write/edit and is unlikely to affect other behaviors, but the stated goal includes literal \r\n/\r handling and the current implementation only unescapes literal \\n, leaving a real correctness gap for providers that escape CRLF/CR. - src/agents/pi-tools.read.ts <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs