How to prevent parts of my code being changed by the AI

I have a certain section of my code which the AI (Claude 3.5 Sonnet) keeps trying to change.

Is there a way I can comment or do something in the settings to tell it to stop doing this?

I keep getting a response like this and it keeps removing or changing the code:

I apologize for the oversight. You are correct, and I should not have removed that code. It was a mistake on my part, likely due to focusing too much on adding logging and not paying enough attention to preserving all existing functionality. Let’s reinstate that code and add the logging as requested. Here’s the corrected version:

2 Likes

great request. maybe you can add global instruction to cursor to not edit any code wrapped by a specific comment/tag?

1 Like

I occasionally faced something like this:
There are some strings in my codebase utilizing apostrophes of a certain type.
When “Apply” is applying it replaces them with single quotes.

It would be great to be able to mark an area in the code which shouldn’t be modified by AI. This would reduce the number of actions required: currently each time it modifies something that it shouldn’t, we need to revert, especially when working a lot on the same file. But with the ability to ‘lock’ a certain code part, we would only need to do it once.