Agent using Replace Tool (in WSL) fails repeatedly
What happened?
✦ I apologize again. It seems I am still having trouble with the replace tool. I will try to read the file again, and then I will try to apply the changes one more time. I will be very careful to make sure I am using the tool correctly this time.
What did you expect to happen?
I expect the agent to be able to edit and save files.
Client information
- CLI Version: 0.1.18
- Git Commit: cd7e60e0
- Operating System: linux v20.19.1
- Sandbox Environment: no sandbox
- Model Version: gemini-2.5-flash <-- was not flash
- Memory Usage: 184.6 MB
Login information
Google account
Anything else we need to know?
The agent repeated the same actions until context and usage for the day ran out
As a workaround you may try adding this to your Gemini.md
- **`replace` Tool Failures and Stale State**: The `replace` tool will fail if
the `old_string` provided does not exactly match the current content of the
file. This can happen if the file has been modified since it was last read
into the agent's memory. If a `replace` operation fails, do not simply retry
the same command. Instead, you **MUST** re-read the file to get the latest
content and then formulate a new `replace` command with the correct
`old_string` based on the fresh data. This prevents getting stuck in a
failure loop.
I don't understand what you mean by:
Model Version: gemini-2.5-flash <-- was not flash
Was it flash or wasn't it? If it was, this is unsurprising: Flash is pretty dumb and this is a known issue: https://github.com/google-gemini/gemini-cli/issues/5254
I don't understand what you mean by:
Model Version: gemini-2.5-flash <-- was not flashWas it flash or wasn't it? If it was, this is unsurprising: Flash is pretty dumb and this is a known issue: #5254
@scidomino The agent was operating in 2.5 non-flash in an apparent doom loop of the messages I shared, until it ran out of the allowed capacity and switched to gemini-2.5-flash for the remainder of the session, which is when I created the bug report. It reported it as flash as that was the model it stepped down to, however the bug happened with the non-flash model. FWIW, I don't believe the model has much to do with the problem, it's the tool usage that is an issue. WSL might come into play here.
replaceTool Failures and Stale State: Thereplacetool will fail if theold_stringprovided does not exactly match the current content of the file. This can happen if the file has been modified since it was last read into the agent's memory. If areplaceoperation fails, do not simply retry the same command. Instead, you MUST re-read the file to get the latest content and then formulate a newreplacecommand with the correctold_stringbased on the fresh data. This prevents getting stuck in a failure loop.
Thanks @DinoChiesa I'll give that a try!
We've encountered an issue where the replace operation behaves unpredictably, modifying unrelated parts of the file in unexpected ways—often leading to compile errors. This results in a failed modification attempt and triggers a loop of repeated fixes. Upon inspecting the file, we found that the affected code blocks are duplicated multiple times, causing redundant growth in the content.
As a temporary workaround, we're forcing updates in gemini.md to use the write file tool instead of replace. This approach avoids the duplication issue, but comes with the trade-off of significantly longer execution times.
It seems like there are 1.5 people working on the project. These problems are very old and stretch from version to version, in which nothing is fixed at all.
As a workaround you may try adding this to your Gemini.md
- **`replace` Tool Failures and Stale State**: The `replace` tool will fail if the `old_string` provided does not exactly match the current content of the file. This can happen if the file has been modified since it was last read into the agent's memory. If a `replace` operation fails, do not simply retry the same command. Instead, you **MUST** re-read the file to get the latest content and then formulate a new `replace` command with the correct `old_string` based on the fresh data. This prevents getting stuck in a failure loop.
This is not working