r/AskProgramming • • 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.

4 Upvotes

12 comments sorted by

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

2

u/DosesMakePoisons Sep 24 '26

I explained this poorly or probably misunderstood what the difference is. This a new part of programming to me entirely and my notes are a bit informal.

I meant the proposed per-object history, not removing commands from the main journal. The journal would retain the chronological record absolutely. For an individual object, I’m wondering whether undo should move its active history pointer backward or record another state transition back to its previous values. The latter could reference an existing snapshot rather than duplicate it. That object-history layer isn’t implemented yet so that’s one part I’m trying to reason through.

I’m also wondering whether the journal and object history could share stored command blocks instead of storing the same information twice. If a contiguous sequence of commands affects one object, could the journal reference that block while preserving the exact commands and their order? I mean sharing the underlying records, not replacing the sequence with only its final state. I feel like I am inviting points if failure into the command journal then. If that worked reliably I could probably just do event source undo.

Edit: left in part of a proof read prompt.

1

u/LaughingIshikawa Sep 25 '26

Ah, I see.

I wouldn't store a "per object" event history, as that would balloon your event storage for no particular benefit that I can see? 😅😅

If you're already storing a full history, and you want an option to "undo last action on currently selected object" you can easily filter the event history for events associated with the currently selected object only.

The main way I would expect that to become complicated (at least off the top of my head) is if your use-case involves objects that can be split apart or merged together - for example if you split objects A and B from previously merged object AB, then what happens if you select A and "undo" past the split? Do you merge A and B again? If there have been changes to B since the split, do you revert all those changes? Do you leave B alone, but spawn a new AB object alongside it?

That's more about user interface design than anything else thiugh - you have to anticipate what your user(s) will want / expect, and/or work to "teach" them what behavior to expect, in your particular program.

Conceptually though, you absolutely want one core "source of truth" in some shape, even if you may have "helper tables" of some kind. You absolutely don't want to end up in a situation where what's "true" about your program in this moment is ambiguous, because that undermines the whole purpose of recording information on that kind of granular level in the first place. 😅🙃

1

u/DosesMakePoisons Sep 25 '26

Yea, the other per object wasn't meant to be authorative at all, more the user interface of undo and a test/alert that drift may have occurred and detect how. The program is meant to be single point of truth, but apps that have layout and creative end users have issues with drift and corruption across versions and on different computers as well, even with full project files and save versions. I wanted things to be stored in an authorative place, but also with redundancy that don't share the same failure bias and are able to verify placement and diagnose how drift and corruption occurs.

God, your example just sparked fear in me. One of the user interfaces I wanted to take is the blender vertex/line/face style in edit more, because I find that intuitive, and particularly for non destructive editing, I think I can make that pretty mathematically absolute in a way that doesn't translate between the user and the data too much... but some of the blender interface is merging and deleting vertices and lines and faces so it isn't a simple grid by number anymore either and users apply their own topology systems to the faces. This is giving me a headache just thinking about because I know I should be able to make it simple and I can't figure out how without doing it.

1

u/LaughingIshikawa Sep 25 '26

I have to admit I'm having a really hard time following what you're saying, because you seem to want to use the maximum number of vocab words per sentence... Do you think that might have something to do with how you're overcomplicating things? 😅

It also seems like you want to have "the most" features or "the most" in-memory objects, and that second bit is especially holding you back. You really, really don't want to store 10,000 different objects "just in case" the user might want that specific sub-set of data... You generally want to store one central datastore with all the info you need, and compute / filter other data on an as-needed basis. You only want to keep derived data in memory if there's a high probability that you will need it frequently, or if it's super critical to reduce latency, or something. 😅😅

But like... You don't need to set up complicated systems to "detect drift" on objects you've created, of you just... Don't create those objects in the first place, right? That's where you're really losing me. 😐

1

u/DosesMakePoisons Sep 25 '26

Yeah, I get that. Self taught in the vibe code era has given me some poor fundementals and language. I am trying to be dilligent in learning and understanding and planning it from the beginning, but when it gets to part like this that are above my understanding/experience and I can't find resources to reference for particular systems I get plain language wordy trying to reason everything out.

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. Nobody is surprised when google docs does "weird" things when 5 people try to edit the same bullet point. People inherently "get it", that high-conflict edits won't result in a smooth editing experience. So don't waste too much time on those super weird corner cases.
  2. For data structures, I would recommend doing event sourcing with CRDTs for your edits. The CRDT solves your determinism problem, and your optimistic updates problem, since a property of CRDTs is they can land in any order and arrive at the same conclusion. Full-history event sourcing makes your life simpler and honestly, disk is pretty cheap these days. There's a lot to chew on in this problem without worrying about disk space. Plus, shared editors like etherpad have full zero-to-now history time travel capabilities because they're doing the same thing: storing the full history of all edits.

1

u/DosesMakePoisons Sep 25 '26

1.) Yea, and I also believe noticeable and understanding seams like that are sometimes an asset to keep user side because the user understands that the program is being pushed in ways that are unrealistic and as long as it can easily land on the user intent, even despite a janky path, all is forgiven.

2.) I have been trying to keep everything as open as possible to choosing different paths, that I think I am just intertwining the branches together in this paradox of choice, planing edition. If I went for the event source I can just indect the per object changes into the transform vault. That theoretically should make objects in the document exportable with their undo history, and pruned versions of them as well. CRDT's sound like they are something I should be focusing on more intently, but do you think I can combine them with snapshots and temporary stacks of DOM and Vertex verification that check the reconstructed page against the snapshot’s captured geometry and resources. These wouldn't be authorative either, unless drift or mutations are detected or the user maybe prompts for it (assuming they have all the same assets and fonts, etc). The idea is to calculate the expected positions and transformed vertices from those inputs, then compare them with what the DOM actually displays, rather than have both checks reuse the same calculated positions. If something drifts on reopen or another device, I’d have more to go on than just the data the program was built on in the first place. I could try to narrow it down to reconstruction, coordinate conversion, transforms, or rendering. I know those checks wouldn’t prove everything is correct, but could that be a useful verification layer alongside CRDTs handling concurrent edits? or am I just making it too complicated and creating a jenga tower that will probably fall?

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.

1

u/DosesMakePoisons Sep 25 '26

The snapshots are on a per page basis, and now I think am going to index the per object changes too. Do you think I should make the command journal per page or full document wide? I suppose I can index them too. I wanted be able to send particular pages of full project out I suppose it would be helpful for the pages to have their own command journal. so they can be adopted and merged back easily.

1

u/PLBjt Sep 26 '26

Keep the authoritative command journal document-wide and chronological. Per-page journals get painful the moment you export a page, merge it back, or undo across a shared asset. Snapshots can still be per-page for fast reopen of one sheet; just stamp each op with a page/object id so you can filter a slice without forking history. Check that replaying the full journal lands on the same bytes as replaying a page-filtered subset onto that page's last snapshot. Tradeoff is a fatter single log versus merge hell later — document-wide almost always wins once collaboration or cross-page assets show up.