claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

Undo/Checkpoint Feature for Correcting AI-Generated Code

Open cyberpapiii opened this issue 8 months ago • 31 comments

Bug Description NEED AN UNDO OR CHECKPOINT FEATURE SO USERS CAN GO BACK AND REVERSE CODE OR MISTAKES THAT CLAUDE MAKES

Environment Info

  • Platform: macos
  • Terminal: ghostty
  • Version: 0.2.32
  • Feedback ID: 175ac8be-c9ab-4955-8051-086d22413551

cyberpapiii avatar Mar 05 '25 22:03 cyberpapiii

This is something we're thinking about! How would you imagine it working?

ashwin-ant avatar Mar 05 '25 22:03 ashwin-ant

This is something we're thinking about! How would you imagine it working?

Could maybe integrate it with a prompt edit feature, kinda like cursor. You'd have file state checkpoints created when the user sends a prompt, and then later the user can restore to that checkpoint, which would clear the chat context below it and allow you to edit and resend that prompt.

You could also have checkpoints created each time claude attempts to use the file edit tool, and if the user reverts back to it later (can maybe have some kind of /revert command) it could clear the context below that tool use and just show up as a user interruption to Claude

The file state snapshots could maybe work similarly to the local history feature in some IDEs

mmstroik avatar Mar 05 '25 23:03 mmstroik

You could have /prev and /next commands to navigate up and down the stack of changes maybe when you run /prev, it makes the last edit's output greyed out so you can still see it but scroll up to the one that is full bright that is currently selected. Maybe people who have the previous summary feature turned on would have the changeset that they just navigated away from with /prev get summarized to be smaller

If you run /prev and then do a different claude prompt, it could clear the rest of the output in front of it since you're overwriting

Personally i would prefer having the changesets not disappear entirely when you run /prev, but i realize that this would make it necessary for you to scroll a lot, and if you run /prev enough you could go up beyond your scrollback buffer

Kimeiga avatar Mar 13 '25 05:03 Kimeiga

I came here to propose something similar: fast state/context saving & restoration would be super valuable.

At the moment, I decently often have threads where I'm building up the model's context to a point for it to be able to tackle a harder issue.

Unfortunately, as things stand, there's no /fast/ way to then snapshot that state of affairs and come back to it later

This feels like it's a third branch amongst the options of /clear, /compact and now /save + /restore. The idea being that you could:

  1. Build up a bunch of context
  2. /save <name>
  3. Work on a problem
  4. Approach full context, or decide to go backwards and try something else
  5. /restore <name>

Ideally given a <name> to save to (or an LLM-generated name), you could /save in one session and then open another session (or sessions) and /restore <name> to pick up from the first.

Would love to see something like this!

tekacs avatar Mar 19 '25 19:03 tekacs

+1! I'd prefer something simple, just an /undo to restore any file changes and conversation history back to the last user instruction. And maybe a /revert to go back (restore files) to before claude code was run. I'd also think it's fine to override any user edits to files between interactions with claude where inverting the diff/merging isn't possible.

gsidsid avatar Mar 21 '25 21:03 gsidsid

Basically you could use a set of internal git stashes - this would do it I think.

aaronlippold avatar Mar 23 '25 00:03 aaronlippold

This is one of the reason I usually use VSCode as my 'terminal' given I git a bit of undo naturally as part of the file updates in vscode but I know the desired first class approach is the strait terminal so a secondary git repo with a history of stashes - which you could name with whatever you use as your internal api interaction index so claude could reference it would likely get you where you need to go.

Also this would help save the 'claude went made and just assume all its magical code would work with no testing or validation of that assumption what so ever' --- sorry my own experiences are showing. LOL

aaronlippold avatar Mar 23 '25 00:03 aaronlippold

Or maybe the other git worktree?

None of this should be taken as complaint. I have accomplished amazing I love this and will keep doing it but I have been basically doing a daily driver with this now for a solid month or so and like others and turning over a few rocks.

This features and a bit of focused work on active context management will take this to a level or two above its already leveled up status.

aaronlippold avatar Mar 23 '25 00:03 aaronlippold

Yeah, this feels like the main feature Claude Code is missing as compared to something like Cursor. Manually committing every time you ask Claude to make a new change seems pretty inefficient?

amackenzie1 avatar Apr 19 '25 22:04 amackenzie1

This is something we're thinking about! How would you imagine it working?

Just simply an /undo

I do also like the checkpoint idea as well though, where chat, code edits, etc reverts back.

nsanden avatar Apr 25 '25 19:04 nsanden

This please Sometimes Claude starts out well, but it's not quite good enough for me to commit. I request some modifications, but on the second try, it goes completely sideways. I really wish I had that original first checkpoint; even though it wasn't committable, it was a pretty good checkpoint.

sedghi avatar May 05 '25 19:05 sedghi

Agreed, a checkpoint feature would be extremely handy. I can think of two other IDE variants of this kind of project where they do checkpoints after changes have been made to a project, as well as one initially after the user starts a new task. Claude Code needs a checkpoint feature too.

Ixe1 avatar May 12 '25 08:05 Ixe1

This is something we're thinking about! How would you imagine it working?

Simple functionality to revert back to any prior prompt of user. Currently, if you are in a conversation and mess up a prompt, you encounter a dilemma, continue with the poisoned history burdening the context and clarity of Claude, or start a new chat and lose all your existing context. Neither options are appealing, but both are appalling. I fail to see any downside to implement what should be a simple and well needed feature. Please open my eyes.

Aron-Lomner avatar May 13 '25 17:05 Aron-Lomner

This is something we're thinking about! How would you imagine it working?

to keep claude relatively simple and avoid a whole new state management system, how about using git to manage the state?

  • when claude returns to the user, it runs sha=$(git stash create); git update-ref -m "claude $session_id $(date +%F_%H%M%S)" refs/claude-snapshot "$sha"; echo "$sha"
  • ...which saves the current state of the working tree as a ref, returning the hash of the ref. claude would be responsible for storing a map of conversation turns to git hashes
  • claude could offer a similar interface to the existing esc esc (which currently restores the conversation state), to restore the repo state to a past conversation turn
  • on restoring, it runs git restore -SW --source=<sha> ., the repo is restored to that state
    • maybe it runs the same command as above on restoring, so someone can always undo the restore on their own if they're in a real bind

because it writes to refs/claude-snapshot, git will clean up the old snapshots along with its usual cleanups, there's no infinitely accumulating storage. and no need to build an opaque state management system in sqlite etc.

one downside is that it wouldn't handle untracked files. it could add these with --intent-to-add, but that does then make a small change to someone's git index. I'm not sure of a simple way to get around that beyond a workaround to 'add, save, undo add"

max-sixty avatar May 13 '25 19:05 max-sixty

checkpoints are essential. This has to be a top priority for Claude Code? especially with Sonnet 3.7 going off the rails at times and creating something you never asked for - overwriting entire app pages.

DystopianDisco avatar May 14 '25 21:05 DystopianDisco

Agree that this is a must, maybe the only thing keeping me from moving off cursor. Maybe check out Codebuff for inspiration

Crazytieguy avatar May 26 '25 04:05 Crazytieguy

per step checkpoint is necessary. one bad edit would ruin many good changes between commits if git is the only tool at the moment.

lukaemon avatar May 29 '25 18:05 lukaemon

My current work around is putting this in the CLAUDE.md (wip but already works pretty well):

## Git Workflow

- **Commit after each user request**: When completing what the user asked for, immediately commit: `git add -A && git commit -m "[action]: [what was accomplished]"`
- Commits should happen WITHOUT asking - they're for checkpoints, not cleanliness (will be squashed later)
- Commits are restore points - if user says something like "let's go back to before X" or "Lets undo that", find the appropriate commit and run `git reset --hard [commit-hash]` to restore the state. Always verify the commit hash via `git log` or `git reflog` first.
- If you've reset to a previous commit and need to go forward again, use `git reflog` to see all recent commits (including those "lost" by reset), then `git reset --hard [commit-hash]` to jump forward to any commit shown in the reflog.
- **ALWAYS update claude-notes.md and include it in EVERY commit** - this preserves context so future Claude Code sessions can continue from any restore point. Maintain a list of the commit messages made during the session/feature.
- When feature complete and user approves or asks to push perform a squash: run `pnpm run lint` first, then find the first commit for the session/feature, then `git reset --soft [starting-commit]` then CLEAR claude-notes.md and commit with `"feat: [complete feature description]"`
- Before major feature work: Tell user "Starting [feature], will make frequent commits as checkpoints then squash when complete"
- Claude Code notes file should include:
  - Current feature being worked on
  - List of commits made during the session/feature
  - Progress status and next steps
  - Important context or decisions made
  - Relevant file locations or dependencies

I think a possible improvement would be to tell claude to use branches instead of reset --hard, but I haven't tried it yet

Crazytieguy avatar Jun 09 '25 21:06 Crazytieguy

it's a must indeed, considering moving exclusively to Cursor because of this...

davlasry avatar Jun 11 '25 21:06 davlasry

A must!

Slavezax avatar Jun 12 '25 20:06 Slavezax

The product is not usable without it even. Models make mistakes. That is fine. If you can undo. My way of working:

  • fire a prompt
  • let the model execute and watch
  • if you see he is taking the wrong direction
  • stop it and add a line to the prompt to make sure the model won't go into that wrong direction
  • iterate on this and eventually zero-shotting will ALWAYS work

MichielMAnalytics avatar Jun 13 '25 12:06 MichielMAnalytics

Agreed here - I would prefer being able to use Shift+Up/Down-arrows to navigate through each user-submitted message - once at the message, allows you to revert both the conversation and the state of the code to that state.

ghodss avatar Jun 15 '25 19:06 ghodss

This in CLAUDE.md rule works pretty well: https://gist.github.com/sohei1l/f19cc1d91320db9db3a6cb7c63b312e6

sohei1l avatar Jun 16 '25 23:06 sohei1l

I've built a checkpoint feature where you can undo any change. It's a VS Code extension that provides a chat interface for Claude Code as well as the ability to restore checkpoints, try it out! VS Code Extension: https://marketplace.visualstudio.com/items?itemName=AndrePimenta.claude-code-chat Github: https://github.com/andrepimenta/claude-code-chat

andrepimenta avatar Jun 17 '25 20:06 andrepimenta

its really a must . I wont pollute my commit history with random "better save now because its risky " commit . Thats just a crutch in my oppion . Cursor does it perfectly with its restore checkpoints. Once this is added I will be onboard .

reactsaas avatar Jun 20 '25 20:06 reactsaas

Lack of checkpoints is frustrating. This is probably the time to introduce a GUI where adding buttons makes sense.

thinknirmal avatar Jun 24 '25 07:06 thinknirmal

It looks like spamming the esc key lets you go back in the conversation now. This is already nice even without the file system checkpointing

Crazytieguy avatar Jun 24 '25 08:06 Crazytieguy

Something similar to git rebase -i would be cool, where you can unselect any recent messages (and replies?) you don't want to keep.

nickjanssen avatar Jun 24 '25 10:06 nickjanssen

Worktrees allow for Parallelization now. amazing! see https://docs.anthropic.com/en/docs/claude-code/common-workflows

MichielMAnalytics avatar Jun 27 '25 07:06 MichielMAnalytics

I’m glad they appreciated my suggestion


Aaron Lippold

@.***

260-255-4779

twitter/aim/yahoo,etc. 'aaronlippold'

On Fri, Jun 27, 2025 at 03:11 MichielMAnalytics @.***> wrote:

MichielMAnalytics left a comment (anthropics/claude-code#353) https://github.com/anthropics/claude-code/issues/353#issuecomment-3011949687

Worktrees allow for Parallelization now. amazing! see https://docs.anthropic.com/en/docs/claude-code/common-workflows

— Reply to this email directly, view it on GitHub https://github.com/anthropics/claude-code/issues/353#issuecomment-3011949687, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALK42DOYPPW5LJPNAECUGL3FTVAXAVCNFSM6AAAAABYNFGVHKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTAMJRHE2DSNRYG4 . You are receiving this because you commented.Message ID: @.***>

aaronlippold avatar Jun 27 '25 12:06 aaronlippold