#21166: fix(agents): sanitize tool names in session transcript repair (#8595)
agents
size: S
Cluster:
Error Handling in Agent Tools
Fixes #8595. Supersedes #8654.
## Description
This PR addresses the issue where hallucinated whitespace in tool names breaks strict providers like OpenAI Codex. It now uses the standardized `normalizeToolName` helper for consistency and includes a new unit test.
## Changes
- Trims and lowercases tool names in `extractToolCallsFromAssistant` using `normalizeToolName`.
- Ensures `session-transcript-repair` logic persists sanitized names.
- Adds `src/agents/tool-call-id.test.ts` to verify sanitization logic.
- Type safety improvements (removed `as any` casts).
## Verification
- Added new unit test `src/agents/tool-call-id.test.ts` covering valid, whitespace-padded, and missing-name scenarios.
- Verified with `pnpm test src/agents/tool-call-id.test.ts`.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR fixes tool name sanitization in session transcript repair by applying `normalizeToolName` to trim and lowercase tool names, preventing hallucinated whitespace from breaking strict providers like OpenAI Codex. The fix is applied in `extractToolCallsFromAssistant`, which is used by `repairToolUseResultPairing` to ensure consistent tool names throughout the transcript repair pipeline. Includes comprehensive test coverage and a separate logging improvement in compaction.ts.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The changes are focused, well-tested, and follow existing patterns. The fix correctly uses the standardized `normalizeToolName` helper which is already used throughout the codebase. The new tests provide good coverage of edge cases. The compaction.ts logging change aligns with the repo's logging standards.
- No files require special attention
<sub>Last reviewed commit: 4ce3310</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#12487: fix(agents): strip orphaned tool_result when tool_use is sanitized ...
by skylarkoo7 · 2026-02-09
86.1%
#8345: fix: prevent synthetic error repair from creating tool_result for d...
by vishaltandale00 · 2026-02-03
84.8%
#3647: fix: sanitize tool arguments in session history
by nhangen · 2026-01-29
84.8%
#15143: fix(transcript-repair): validate tool call id and name to prevent G...
by GreyC · 2026-02-13
84.7%
#19094: Fix empty tool_call_id and function names in provider transcript pa...
by yxshee · 2026-02-17
84.5%
#15509: fix(session): drop tool_use blocks with empty or missing name
by aldoeliacim · 2026-02-13
83.6%
#4844: fix(agents): skip error/aborted assistant messages in transcript re...
by lailoo · 2026-01-30
83.5%
#6687: fix(session-repair): strip malformed tool_use blocks to prevent per...
by NSEvent · 2026-02-01
83.3%
#9861: fix(agents): re-run tool_use/tool_result repair after limitHistoryT...
by CyberSinister · 2026-02-05
83.2%
#13831: fix(agents): include Anthropic in tool call ID sanitization
by lailoo · 2026-02-11
83.1%