Skip to content

After RestoreFromState/LoadStateAsync, a never-saved state at position -1 reports no unsaved changes again (#76 regresses on load) #83

Description

@matt-edmondson

What's wrong

The #76 fix added _initialStateIsClean to SaveBoundaryManager, but the flag is not part of the saved state. UndoRedoService.RestoreFromState (Services/UndoRedoService.cs, ~lines 345-373) calls _saveBoundaryManager.Clear(), which resets the flag to true (Services/SaveBoundaryManager.cs, ~lines 516 and 604-608). Save boundaries are then recreated from the state, but when there are none the flag stays true.

So a loaded stack whose oldest commands were trimmed, and which has no save boundary, is dirty at position -1 but reports clean. That is exactly #76, and it comes back after every LoadStateAsync or RestoreFromState, including restoring a service from its own GetCurrentState().

Repro

MaxStackSize = 2. Execute 3 commands and Undo twice (position -1, which is after the first, trimmed command). Then either RestoreFromState(GetCurrentState()) into a new service, or round-trip through JsonUndoRedoSerializer.

before: pos=-1 v=1 HasUnsavedChanges=True
after restore: pos=-1 HasUnsavedChanges=False
after self-restore: pos=-1 HasUnsavedChanges=False
JSON round trip: loaded=True before=True after=False

Expected: HasUnsavedChanges stays True.

Reproduced against the current main build.

Why it matters

The app can close without prompting to save and lose the user's work. This is the scenario #76 fixed, and an app that persists its undo history across sessions hits it on every reload.

Suggested fix

Carry the "initial state is clean" flag through the saved state:

  • Add a field to UndoRedoStackState and to the serializer's SerializableStackState, defaulting to true so old data still loads.
  • Add a way on ISaveBoundaryManager to set the flag after Clear(), and call it from RestoreFromState.

If a format change is unwanted, a simpler fallback is to restore the flag as false whenever the saved state was dirty at -1.

Acceptance criteria

  • The repro reports HasUnsavedChanges == True after restore and after the JSON round trip.
  • Data saved before the change still loads.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions