Chat agent: prepareToolInvocation crashes with "Cannot read properties of undefined (reading 'replace')" when the model emits nested/malformed tool arguments
- Dominant language
- TypeScript
- Stars
- 193k
- Forks
- 42.4k
- PR merge metrics
- PR metrics pending
Description
- Copilot Chat Extension Version: 0.64.1
- VS Code Version: 1.136.1 (Windows 11, user setup)
- OS Version: Windows 11 x64
- Feature (e.g. agent/edit/ask mode): agent mode (tool invocation, run_in_terminal)
- Selected model (e.g. GPT 4.1, Claude 3.7 Sonnet): DeepSeek-V4-Flash-Vision-Exp via a BYOK/proposed `chatProvider` extension
- Logs: see stack trace below
## Steps to Reproduce
1. Open an agent-mode chat with a BYOK model that occasionally emits malformed tool-call arguments (a quantized small model reproduces this reliably enough).
2. Ask it to run terminal commands.
3. The model emits a tool call whose `arguments` field is **nested twice**: `{"arguments":{"arguments":{"command":"git ...","explanation":"..."}}}` instead of `{"command":"git ...","explanation":"..."}`.
4. The tool invocation fails with a core crash instead of a handled error.
## Actual behavior
The whole tool invocation throws from core code:
```
2026-09-07 03:45:10.345 [error] Error from tool run_in_terminal with args {"arguments":{"arguments":{"command":"git -C \"...\" log --oneline -3; ...","explanation":"...","goal":"...","mode":"sync"}}}: Cannot read properties of undefined (reading 'replace'): TypeError: Cannot read properties of undefined (reading 'replace')
at hee (vscode-file://vscode-app/.../out/vs/workbench/workbench.desktop.main.js:4457:766)
at up.prepareToolInvocation (vscode-file://vscode-app/.../workbench.desktop.main.js:4638:3141)
at async CW.prepareToolInvocation (...:3916:2135)
at async CW.invokeTool (...:3911:10471)
at async gjt.$invokeTool (...:1077:48089)
```
Downstream effects observed:
- The agent turn's tool call is dropped; the model gets a raw crash message and starts retrying, producing repeated failed invocations.
- The rapid retry loop then trips the chat request limiter: "The extension has been temporarily blocked due to making too many requests. Please try again later."
## Expected behavior
`prepareToolInvocation` should validate/handle malformed tool arguments gracefully — i.e. surface a normal "tool arguments were invalid" error back to the model (so it can self-correct) instead of an uncaught TypeError from `command.replace(...)` on an undefined value. A thrown TypeError in core also breaks the UX (turn interrupted, rate limiter triggered by retries).
## Notes
- The malformed double-nested `arguments` come from the model itself (confirmed: the provider extension's request log shows a healthy streamed response with `finishReason=tool_calls`; the args were already malformed when VS Code received them). So the root model behavior is on the model side — but core should still not crash on it.
- Reproduced on 2026-09-07 ~03:45 local time; the same signature appeared repeatedly in the session (also with `git`, `code` and other commands).
- I can provide the full session logs if needed.
Contributor guide
Assessment
This issue has not been assessed yet.