Unexpected code deletion when applying AI-generated code suggestions

In chat I asked him to change the code for me, and the code suggestion given was ok, but when I clicked apply and did a diff apply in the editor, I realized that he was going to delete the code that I had previously added myself. This is not right and not what I expected to want. Is it possible that the context that cursor doesn’t have is still old? He doesn’t know about the code I added myself before, and is making code changes based on the old code, and all the diffs come up showing that he’s deleting the code I added manually.

Could you clarify what you mean by the context being old? Did you previously apply some ai changes, edited your code manually afterwards and asked for additional ai suggestions in the same chat? In that case it seems to be a common occurrence, happens all the time to me. So I frequently have to start a new chat. If the ai just deleted the code by itself, without having previously seen the files without that code, that is something that occasionally happens to me also, albeit more rarely than the former case. Probably something to do with llm hallucinating unnecessary changes. I agree that this process should be improved. The ai should become aware of the changes the user made manually. And I’m not sure how the latter case could be fixed.

I mean, it may be stuck in the last context and not updating the AI with my changes in the latest context available.

Yes, that’s probably what’s happening: the AI suggests code based on the context before your manual changes.

Ideally, it should be fed the difference if the user has edited the code after the previous reply in the current chat.

I suppose a workaround could be:

You commit changes suggested by ai, add your changes, copy git diff to the chat, commit your added changes and then ask the ai for new suggestions, mentioning that you made some corrections since its last reply