You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A loaded state with currentIndex: -1 but non-empty items is accepted, and the next NavigateTo silently deletes the whole restored history #69
The NavigationState<T> constructor allows currentIndex == -1 for any number of items (Navigation/Models/NavigationState.cs ~L33). The stack itself only produces -1 when it is empty (FromNavigationStack).
Navigation<T>.LoadStateAsync passes the loaded state straight into RestoreState (Navigation/Services/NavigationStack.cs ~L160, ~L169-177), and RestoreState does no validation. After such a load, the stack has items but no current page:
Current == null
CanGoBack == false
CanGoForward == true
The next NavigateTo treats every restored item as forward history and removes it (RemoveRange(_currentIndex + 1, ...) with _currentIndex == -1).
LoadStateAsync is the single entry point for every IPersistenceProvider<T>, including custom ones (Architecture.md presents a DatabasePersistenceProvider). Yet the only checks on a loaded state, the null-item check added for #60 and the index range check, live in NavigationState's constructor. A state that bypasses them reaches RestoreState unchecked.
Reproduction
Write this state file: {"items":[{"id":"a","displayName":"A"},{"id":"b","displayName":"B"}],"currentIndex":-1}
Call LoadStateAsync(). It returns true, with count=2, Current=null, CanGoBack=false, CanGoForward=true.
Call NavigateTo(new NavigationItem("x","X")). The history is now just X; A and B are gone.
Once the stack is in this state, SaveStateAsync persists currentIndex: -1 with items again, so the corrupt state round-trips.
Suggested fix / acceptance criteria
In the NavigationState constructor, reject currentIndex == -1 when items is non-empty. JsonFilePersistenceProvider already maps ArgumentException to a null load.
Also validate in LoadStateAsync, so every provider gets the same checks:
no null items
-1 <= CurrentIndex < Items.Count
-1 only when Items is empty
On an invalid state, return false and leave the stack unchanged.
Tests:
The JSON above loads as false and leaves the existing stack untouched.
A custom provider returning an out-of-range index or a null item makes LoadStateAsync return false without changing the stack.
Priority: Medium. A malformed or custom-provider state loads successfully, the next NavigateTo then deletes the whole restored history, and the corrupt state persists again on save. It needs a bad state file to trigger, but the loss is silent and survives a save.
Area / suggested assignment:Navigation/Models/NavigationState constructor and NavigationStack.LoadStateAsync/RestoreState.
Suggested next step: move all state validation into LoadStateAsync, so custom providers go through the same checks. Keep the constructor check as a second guard.
Activity
matt-edmondson commentedon Sep 28, 2026
Triage
NavigateTothen deletes the whole restored history, and the corrupt state persists again on save. It needs a bad state file to trigger, but the loss is silent and survives a save.Navigation/Models/NavigationStateconstructor andNavigationStack.LoadStateAsync/RestoreState.SimpleUndoRedoProviderwith UndoRedo) does not touch state loading.Suggested next step: move all state validation into
LoadStateAsync, so custom providers go through the same checks. Keep the constructor check as a second guard.Generated by Claude Code