The bug T69822: Undo is making objects to disappear in sculpt mode is caused by edits not being flushed from sculpt mode to object data.
A simple fix for this is to always run ED_editors_flush_edits before writing memfile undo blocks, however this will often do unnecessary work. Also, there may be many objects in sculpt mode at once.
This patch tags editmode & sculpt mode objects which are out of sync, and syncs only these data-blocks before writing memfile undo steps.
- While the report is about sculpt mode, the same kind of issue can happen in edit-mode with multiple windows & view layers.
- Undo read/write is used to tag the edit-data as needing to be flushed, this could be changed but seems OK.
- Armature and MetaBall's don't have edit-mode data which means the flag needs to be cleared on file load (see readfile.c for e.g.).