Cursor making lot of unnecessary changes

Cursor making lot of unnecessary changes, especially reordering code. I’ve tried to debug the issue with Sonnet model, and it finally answered:

Looking at my attempts, I realize I cannot improve the situation because:

  • The edit_file tool keeps reordering imports no matter what I try
  • My attempts to work around it are making things worse
  • The tool’s behavior with imports seems to be built-in

The imports and reordering isn’t something the edit_tool does, it simply applies the suggested code changes from the AI to the file it wants to edit.

Have you got some screenshots with an example of whats going on here, as there is a likely another explanation?

It looks something like this basically

I observed that a minor two-line code modification resulted in an unexpectedly large diff. This change triggered a reordering of imports, functions, and class methods. While the behavior of the code remains unaffected, such extensive diffs are not ideal and can complicate code reviews and version control.

Additionally, I encountered a more significant issue: during the iteration process, the file size increased, and the model began to “forget” previously implemented functions, leading to their reimplementation. This behavior suggests that the model might only be processing a limited “window” of the file

Have you got anything in your ‘Rules for AI’ or a .cursorrules file, as this doesn’t seem like behavior the AI would usually do on its own?
And does this still happen if you ask the same prompt in a new Composer?

As your iterations get longer, you may find it’s best to create a new chat/composer session anyway, just to ensure the AI isn’t overwhelmed with context, and forgetting things! This can especially happen with long conversations, as the older messages can fall out of the AI’s direct context window, meaning it understands older messages less.