gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

Agent using Replace Tool (in WSL) fails repeatedly

Open s3m1ck opened this issue 1 year ago • 7 comments

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

s3m1ck avatar Aug 06 '25 20:08 s3m1ck

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.

DinoChiesa avatar Aug 06 '25 22:08 DinoChiesa

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

scidomino avatar Aug 07 '25 00:08 scidomino

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: #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.

s3m1ck avatar Aug 07 '25 19:08 s3m1ck

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.

Thanks @DinoChiesa I'll give that a try!

s3m1ck avatar Aug 07 '25 19:08 s3m1ck

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.

onairmax avatar Aug 08 '25 07:08 onairmax

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.

open6amer avatar Aug 11 '25 11:08 open6amer

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

open6amer avatar Aug 11 '25 12:08 open6amer