From 6a8df764bb7ad4ab2679d740a6f0cd4e8ab2e11a Mon Sep 17 00:00:00 2001 From: zjs81 Date: Sun, 11 Oct 2026 18:04:53 -0700 Subject: [PATCH] Stamp legacy images inside the migration; hold early channel frames - Image entries are stamped with their channel ID before the migration is marked done, so an interrupted run repeats it instead of leaving them unstamped (and invisible) forever. - Channel message/data frames that arrive before this session's slot map is confirmed are held and replayed once it is. Current firmware only sends them in reply to SYNC_NEXT_MESSAGE, which already waits; this guards against a stale saved map filing them under the wrong channel. --- lib/connector/meshcore_connector.dart | 35 +++++++++++++++++--- lib/storage/channel_storage_migration.dart | 10 +++++- test/connector/channel_slot_map_test.dart | 26 +++++++++++++++ test/storage/channel_storage_by_id_test.dart | 21 ++++++++++++ 4 files changed, 87 insertions(+), 5 deletions(-) diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index bf4d7005..e62e60e9 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -379,6 +379,12 @@ class MeshCoreConnector extends ChangeNotifier { bool _isProcessingDeferredQueuedContactMessages = false; bool _queuedMessageSyncInFlight = false; final List _deferredQueuedContactMessageFrames = []; + + /// Channel frames that arrived before this session's slot map was + /// confirmed. The firmware only sends them in reply to SYNC_NEXT_MESSAGE, + /// which waits for the map, so this is a guard: their slot index cannot be + /// resolved yet, and they are replayed once it can. + final List _channelFramesAwaitingMap = []; bool _pendingQueueSync = false; // CMD_APP_START stops the firmware's contact iterator (END_OF_CONTACTS is // then never sent), so APP_START requested during a contact sync is held @@ -3431,6 +3437,7 @@ class MeshCoreConnector extends ChangeNotifier { _isProcessingDeferredQueuedContactMessages = false; _queuedMessageSyncInFlight = false; _deferredQueuedContactMessageFrames.clear(); + _channelFramesAwaitingMap.clear(); _pendingQueueSync = false; _resetSyncFlowState(); _queueSyncTimeout?.cancel(); @@ -5682,6 +5689,19 @@ class MeshCoreConnector extends ChangeNotifier { _finishChannelSync(); } + void _replayChannelFramesAwaitingMap() { + if (_channelFramesAwaitingMap.isEmpty) return; + final frames = List.of(_channelFramesAwaitingMap); + _channelFramesAwaitingMap.clear(); + for (final frame in frames) { + if (frame[0] != respCodeChannelDataRecv) { + _handleIncomingChannelMessage(frame); + } else if (_appSettingsService?.settings.imageMessagesEnabled ?? false) { + _handleIncomingChannelData(frame); + } + } + } + /// A channel whose key changed in place keeps its position in the custom /// order: [after] takes [before]'s place when no slot holds [before] any /// more. Returns true when the order changed. @@ -5714,6 +5734,7 @@ class MeshCoreConnector extends ChangeNotifier { } void _finishChannelSync() { + if (_channelMapConfirmed) _replayChannelFramesAwaitingMap(); final completer = _channelSyncCompleter; _channelSyncCompleter = null; if (completer != null && !completer.isCompleted) completer.complete(); @@ -5745,12 +5766,11 @@ class MeshCoreConnector extends ChangeNotifier { // First: the channel sync is already running and carries unread counts // over from these. The migration never touches the channel cache. await loadCachedChannels(); - final migrated = await ChannelStorageMigration.migrate( + await ChannelStorageMigration.migrate( _channelStore.publicKeyHex, + beforeDone: (slotIds) async => + _receivedImageStore?.stampLegacyChannelIds(slotIds), ); - if (migrated.isNotEmpty) { - await _receivedImageStore?.stampLegacyChannelIds(migrated); - } await _loadChannelOrder(); final saved = await _channelStore.loadSlotChannelIds(); _slotChannelIds @@ -5967,6 +5987,13 @@ class MeshCoreConnector extends ChangeNotifier { unawaited(_handleIncomingMessage(frame)); } break; + case respCodeChannelMsgRecv || + respCodeChannelMsgRecvV3 || + respCodeChannelDataRecv + when !_channelMapConfirmed: + if (_isSyncingQueuedMessages) _handleQueuedMessageReceived(); + _channelFramesAwaitingMap.add(frame); + break; case respCodeChannelMsgRecv: case respCodeChannelMsgRecvV3: _handleIncomingChannelMessage(frame); diff --git a/lib/storage/channel_storage_migration.dart b/lib/storage/channel_storage_migration.dart index 80fb6187..ec4afbbb 100644 --- a/lib/storage/channel_storage_migration.dart +++ b/lib/storage/channel_storage_migration.dart @@ -25,7 +25,14 @@ class ChannelStorageMigration { /// Migrates [scope] (first 10 hex chars of the radio's public key) and /// returns the slot -> channel ID mapping it used. Returns an empty map when /// the scope was already migrated or must not adopt legacy data. - static Future> migrate(String scope) async { + /// + /// [beforeDone] runs with that mapping before the migration is marked done, + /// so data kept elsewhere (images) is moved on the same one-shot terms: an + /// interrupted run repeats it on the next launch. + static Future> migrate( + String scope, { + Future Function(Map slotIds)? beforeDone, + }) async { if (scope.isEmpty || !canMigrateLegacyKeys(scope)) return {}; final prefs = PrefsManager.instance; final doneKey = '$_doneKeyPrefix$scope'; @@ -61,6 +68,7 @@ class ChannelStorageMigration { ); } await _migrateOrder(scope, slotIds); + if (slotIds.isNotEmpty) await beforeDone?.call(slotIds); await prefs.setBool(doneKey, true); await channelStore.saveSlotChannelIds(slotIds); diff --git a/test/connector/channel_slot_map_test.dart b/test/connector/channel_slot_map_test.dart index acd0d19e..65097f67 100644 --- a/test/connector/channel_slot_map_test.dart +++ b/test/connector/channel_slot_map_test.dart @@ -243,6 +243,32 @@ void main() { expect(_storedTexts(hikingId), isEmpty); }); + test('a channel frame that arrives before the slot map is held, then filed ' + 'by the confirmed map', () async { + final radio = _TestRadio( + afterHandle: (radio, frame) { + if (frame[0] == cmdAppStart) { + radio.emitForTest( + ReviewFrames.channelMessage( + channelIndex: 1, + sender: 'Sam', + message: 'before the map', + timestampSecs: DateTime.now().millisecondsSinceEpoch ~/ 1000, + ), + ); + } + }, + ); + await _connect( + radio, + // A stale map from last session would file it under the ghost channel. + seed: () => _seedSlotMap({0: _id(_psk(0)), 1: ghostId, 2: _id(_psk(2))}), + ); + await _waitFor(() => _texts(1).contains('before the map')); + expect(_storedTexts(hikingId), contains('before the map')); + expect(_storedTexts(ghostId), isNot(contains('before the map'))); + }); + test('MSG_WAITING before the channel sync waits for the slot map', () async { // DEVICE_INFO is held back so MSG_WAITING lands between SELF_INFO and the // start of the channel sync. diff --git a/test/storage/channel_storage_by_id_test.dart b/test/storage/channel_storage_by_id_test.dart index ae04855f..ec7a376c 100644 --- a/test/storage/channel_storage_by_id_test.dart +++ b/test/storage/channel_storage_by_id_test.dart @@ -254,6 +254,27 @@ void main() { ); }); + test('images are stamped before the migration is marked done', () async { + await _init(legacyValues()); + await expectLater( + ChannelStorageMigration.migrate( + _scopeA, + beforeDone: (_) async => throw StateError('killed'), + ), + throwsStateError, + ); + // Not marked done, so the next launch stamps again. + Map? stamped; + await ChannelStorageMigration.migrate( + _scopeA, + beforeDone: (slotIds) async => stamped = slotIds, + ); + expect(stamped, {0: _idOne, 2: _idTwo}); + // Data moved by the first run is still there. + final messages = ChannelMessageStore()..setPublicKeyHex = _deviceA; + expect((await messages.loadChannelMessages(_idOne)).single.text, 'hello'); + }); + test('skips a scope that must not adopt legacy data', () async { await _init(legacyValues()); scopesWithoutLegacyMigration.add(_scopeA);