diff --git a/app/lib/application/tab_locations.dart b/app/lib/application/tab_locations.dart index ee0357b5..50a3870e 100644 --- a/app/lib/application/tab_locations.dart +++ b/app/lib/application/tab_locations.dart @@ -67,18 +67,14 @@ class TabLocations { entry.key: durableTabRoute(entry.value), }, ); - _writes = _writes.then((_) async { - if (_disposed) return; - try { - final saved = await orderedLocalStorage(_storageKey, () async { - if (_disposed) return null; - return store.save(snapshot); - }); - if (saved != null) persistenceFailed = !saved; - } catch (_) { - persistenceFailed = true; - } - }); + // Queue the accepted snapshot immediately, before a replacement owner's + // load can enter the same key's order. Disposal prevents new records, but + // must not discard the last valid move already accepted from the user. + _writes = orderedLocalStorage(_storageKey, () => store.save(snapshot)) + .then( + (saved) => persistenceFailed = !saved, + onError: (Object _, StackTrace _) => persistenceFailed = true, + ); } void dispose() => _disposed = true; diff --git a/app/notes/persistent-tab-locations.md b/app/notes/persistent-tab-locations.md index 97f4281d..af74b2b9 100644 --- a/app/notes/persistent-tab-locations.md +++ b/app/notes/persistent-tab-locations.md @@ -40,14 +40,19 @@ publication are still required; this lane did not deploy or restart anything. ## Replacement-owner write ordering -A disposed owner skips writes that have not started, including operations queued -behind another storage owner. Already-started operations share a small per-key -read/write order with replacement owners; replacement reads wait for an old -in-flight write before merging and saving newer navigation. Idle operation keys -are removed, and a failure never poisons later operations. +Accepted navigation snapshots enter the shared per-key operation order +immediately. Disposal rejects new records and late read-back writes, but does +not discard a valid last move that was already queued. Replacement reads wait +for all previously accepted writes before merging and saving newer navigation. +This preserves the last move when an app is recreated without a new choice, +while newer replacement navigation still wins. Idle operation keys are removed, +and a failure never poisons later operations. -18 focused tab/controller/full-app navigation cases passed after this follow-up -(`/tmp/memwatch/out.hBbm4Y`), including a blocked old write plus a queued old write, -disposal, replacement navigation and delayed release. The newer state wins for -both an in-memory store and distinct SharedPreferences store instances sharing -a logical key. Two independent ordering-helper tests passed (`out.FffOAC`). +21 focused controller/full-app navigation/ordering cases passed after the final +follow-up (`/tmp/memwatch/out.ksAW8x`); analyzer clean (`JMtEk8`). Regressions +cover a blocked old write plus a queued accepted old write, disposal, replacement +navigation and delayed release. Older accepted history is written before the +replacement loads it; newer navigation wins afterward. Recreating without a new +choice restores the old owner's last accepted chat, not its previous Settings +location. Both same-instance fake stores and distinct SharedPreferences store +instances sharing one logical key are covered. diff --git a/app/test/application/tab_locations_test.dart b/app/test/application/tab_locations_test.dart index f6630b12..6c7e5933 100644 --- a/app/test/application/tab_locations_test.dart +++ b/app/test/application/tab_locations_test.dart @@ -261,7 +261,7 @@ void main() { }, ); test( - 'disposed queued writes cannot overwrite a replacement controller', + 'accepted old writes finish before a replacement load and newer move', () async { final gate = Completer(); final store = MemoryTabLocationsStore()..write = gate; @@ -288,10 +288,12 @@ void main() { expect(store.value.routes[AppDestination.pipelines], AppRoute.settings); expect( store.value.routes[AppDestination.chat], - isNull, - reason: 'the disposed owner never starts its queued Chat write', + AppRoute.chat, + reason: 'the accepted queued Chat move is durable before replacement', ); - expect(store.writes, hasLength(2)); + expect(store.writes, hasLength(3)); + expect(store.writes[1].active, AppDestination.chat); + expect(store.writes.last.active, AppDestination.runs); }, ); @@ -322,7 +324,46 @@ void main() { expect(saved.active, AppDestination.runs); expect(saved.routes[AppDestination.runs], AppRoute.run(18)); expect(saved.routes[AppDestination.pipelines], AppRoute.settings); - expect(saved.routes[AppDestination.chat], isNull); + expect(saved.routes[AppDestination.chat], AppRoute.chat); + }, + ); + test( + 'recreating without a new choice restores the last accepted queued move', + () async { + SharedPreferences.setMockInitialValues({}); + final oldStore = GatedPreferencesTabLocationsStore( + scope: 'ordinary-restart', + ); + final old = TabLocations(oldStore)..record(AppRoute.settings); + await old.restore(restoreActive: false); + await oldStore.started.future; + old.record(AppRoute.chatSession('last-open-chat')); + old.dispose(); + old.record(AppRoute.run(999)); // disposal rejects new, unaccepted moves + const newStore = SharedPreferencesTabLocationsStore( + scope: 'ordinary-restart', + ); + final replacement = TabLocations(newStore) + ..record(AppRoute.pipelines, visited: false); + var readFinished = false; + final restoring = replacement.restore(restoreActive: true).then((value) { + readFinished = true; + return value; + }); + await Future.delayed(Duration.zero); + expect(readFinished, isFalse); + oldStore.gate.complete(); + expect(await restoring, AppRoute.chatSession('last-open-chat')); + await old.settled; + await replacement.settled; + final saved = await newStore.load(); + expect(saved.active, AppDestination.chat); + expect( + saved.routes[AppDestination.chat], + AppRoute.chatSession('last-open-chat'), + ); + expect(saved.routes[AppDestination.runs], isNull); + expect(saved.routes[AppDestination.pipelines], AppRoute.settings); }, ); }