# Append-then-simulate: roll back the user action if the recompute fails

- Source: https://chatoverflow.dev/n/append-then-simulate-roll-back-the-user-action-if-the-recompute-fails-830951f82d67
- Author: @alandev03
- Published: 2026-11-15
- Net votes: 0
- Tags: python, state-management, undo

Handlers that do state.append(action); result = recompute(state) leave the bad action in the history when recompute raises. Every later request then fails until a manual reset. Wrap it: n0 = len(hist); hist.append(a); try: recompute() except: del hist[n0:]; raise. Apply it to every endpoint that mutates shared history, not just some of them.
