opencode icon indicating copy to clipboard operation
opencode copied to clipboard

[Bug]: file deleted on Undo action

Open Isos9 opened this issue 1 year ago • 10 comments

Hi, when I undo my last prompt, it did not undo the one-line change I had, it actually deleted the whole file, showing /dev/null in the undo summary instead of the file name.

I think it actually happened because when I did the undo, I closed the opencode instance right away, I didn't wait for it to undo it.

I also had two opencodes opened on the same session at this time, but the other I just opened it to check something quickly in full screen, no interaction with it, so that might not be relevant.

The version was 0.15.0, on Alacritty, Arch linux.

Isos9 avatar Oct 13 '25 19:10 Isos9

This issue might be a duplicate of existing issues. Please check:

  • #2642: [bug] /undo removed the last message, but didn't revert the code edit - Similar undo functionality not working correctly
  • #2575: undo doesnt removes the context - Another undo-related issue where undo doesn't work as expected
  • #3073: Snapshot tracking does not initialize per-project git directory, prevents undo/redo and leaves no snapshot data - Related to underlying snapshot mechanism that undo depends on
  • #2379: [bug]: /undo is broken when you re-open opencode - Similar undo problems with snapshot handling

Feel free to ignore if none of these address your specific case.

github-actions[bot] avatar Oct 13 '25 19:10 github-actions[bot]

Same thing happens to me on opencode v0.15.7 on Windows in Wezterm. After undoing few changes in session, files that were only edited (not created) in current session get all deleted. 🤨 Good that I had files backed up with Git otherwise I would lose files... This happened to me multiple times, it's important to fix this.

ikodbapc avatar Oct 17 '25 11:10 ikodbapc

yeah i had it happen once or twice too we will look into this

rekram1-node avatar Oct 17 '25 14:10 rekram1-node

I was only able to reproduce one way: When you start opencode, it deletes the whole snapshots directory (code here). When you /undo in any opencode instance (new or already running), opencode detects that the file doesn't exist in any snapshot (effectively implying the file was created from scratch by the LLM), so it deletes it (code here).

(I believe this is the exact same scenario reported by https://github.com/sst/opencode/issues/2379)

@rekram1-node I propose to remove the init function that deletes the storage directory at startup. I imagine it was added as a crude sort of garbage collection mechanism.

Bit out of scope, but I would like to propose a different strategy for git-based protection of user data: Instead of creating snapshots, store the diffs introduced by LLM's edits. When undoing a message, OC can apply the diff on the file (with the --reverse flag). I believe this might help with issues of large snapshot sizes, which have been reported in the past. It has the added benefit of being more robust to changes made to the file by the user (i.e. reverting only the LLM's part while preserving user changes, whereas using snapshots reverts user changes as well).

veracioux avatar Oct 18 '25 19:10 veracioux

We are going to rework it entirely without git soon so this shouldn't be a problem anymore

But that's a good callout, ill take a look

rekram1-node avatar Oct 18 '25 19:10 rekram1-node

fixed in next release, good callout @veracioux

rekram1-node avatar Oct 19 '25 23:10 rekram1-node

undo command doesn't work on 0.15.10 either (with windows) (wsl, npm both doesn't work)

undo command is revert the message but code doesn't reverted.

JSCOP avatar Oct 20 '25 22:10 JSCOP

@JSCOP it only works in projects tracked by git currently

rekram1-node avatar Oct 20 '25 23:10 rekram1-node

@rekram1-node This is test video https://youtu.be/24unfQ8xv5Q

I thought the /redo command would sequentially undo each command in the same way as /undo, but /redo always goes all the way back to the last request before undoing. Is this intended behavior? Or is this a bug in the Windows OpenCode?

JSCOP avatar Oct 21 '25 19:10 JSCOP

hm looks like bug

rekram1-node avatar Oct 21 '25 19:10 rekram1-node