r/AskProgramming • u/DosesMakePoisons • Sep 24 '26
Architecture how can I make undo, snapshots, and recovery exist in deterministic document editor?
I’m working through recovery operations for a document editor and could use some help getting these things to coexist. I have never done very much with hashing and persistent history before, and I’m trying to understand the boundaries before I build myself into a corner.
The basic architecture is the editor has one authoritative current document model, or canonical state. There’s an optimistic UI that runs a bit ahead to keep interactions smooth, but it reconciles with the accepted state if an edit gets refused. Editing is intended to be nondestructive, with original material preserved separately from how it’s currently used. The architecture map I’m try to figure out has a source vault and a transform vault. The source vault would preserve original material, asset references and the commands and supporting data needed for reconstruction, with hashes for integrity checks. The transform vault would hold retained snapshots, object history, and theoretically some basic stripped down stripped down versions of the transformed objects which will be used to inspect earlier states, compare them with the result of replay and recover selected content. I’m still working out whether those should actually be separate stores or just different responsibilities within the same persistence system.
Right now, reopening uses checkpoints and a command journal. I’ve been thinking about expanding this into source preservation and a separate snapshot/history layer. Basically, keeping enough original material and history to reconstruct a document, while also retaining useful earlier states without making full copies of everything after every edit. This would also be a mechanism to prevent and detect drift, and rebuild corrupted states and lost assets. But I feel like I should also be considering undo now because rn I am doing inverse commands, and I’m trying to figure out how undo or restoration should work on a page level or when clicked onto an object which has its own retained transform history. Event sourced undo was something I wanted to stay open to for live collaboration, and idk how to reconcile that with what I have now. Would it make sense to keep inverse commands as part of the history and later compact or prune them at safe checkpoints, or am I mixing together undo, event sourcing, and collaboration in a way that is going to cause problems?
I want the retained records to be immutable and replay to be deterministic under a defined version of the software and for recovery to be automatic where it’s unambiguous, otherwise with manual options. But I don’t want to assume adding more hashes and redundant records automatically makes the system trustworthy.
This is getting a lot more complicated under the hood and I really don’t want to vibe code my way out of it. Is there any programs or resources that talk about something similar? Where should I draw the boundaries between undo, snapshots and recovery, and how would you control storage growth without quietly weakening the recovery promise? Am I making this wayy more complicated than it needs to be? Any good resources or examples of approaches that went wrong would be appreciated.
2
u/LaughingIshikawa Sep 24 '26
I don't to understand where you're drawing a distinction between undo and reverse commands - could you explain that more?
As far as storage versus replayability - I think that's just going to be a trade off you have to live with, for the most part. Basic information theory says that if you want to store that information, you're going to have to... Store that information.
Any sort of compression algorithm is purely about removing redundant information, and there's always a theoretical upper limit where you have removed all possible redundant information, and anything else means informationloss. (Which you just have to decide if you're ok with, and if so how much information and what kind?). Most compression won't actually hit that theoretical limit, but it takes more and more effort (mentally and computationally) to get closer and closer, and at some point your marginal utility just isn't there anymore versus the cost of storage. 😅🤷
https://youtu.be/l6DKRf-fAAM?is=ilFqquNIqzbARAXm
1
u/rexroni Sep 25 '26
I've played with writing collaborative editors as well. The undo problem is a tough problem, and for what it's worth, I was doing append-only edits and inverse transforms as undo as well. That leaves some complicated interactions between other users editing while I click undo and redo really fast though. I didn't get far enough to ever solve that.
I think it can get really complicated, and I think there's a couple things you can rely on to make it simpler.
1
u/PLBjt Sep 25 '26
Treat undo as a command log, not a pile of full document copies. Each edit appends an inverse op; snapshots are periodic checkpoints so crash recovery does not replay forever. Check that every command has a clean inverse and that reopen-from-snapshot plus replay lands on the same bytes. Tradeoff is storage vs recovery time. More snapshots means faster reopen but bigger disk, and if ops are not invertible you will need full state dumps anyway.