Why does Android Studio give me this message sometimes when I try to undo?:
Cannot undo. Unexpected document state
This is an old bug reported here.
I've just had the same error in 2023: my uncommitted changes just disappeared in the file for no reason (after running an npm build) and I could not undo it.
As @Steven commented, local history helped:
Go to file -> local history -> show history
and pick the latest.
Congrats, your 10h of work are saved!