From afbacbc7ebe9a3373fe2e2b6afee6a9a4ca2e949 Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Mon, 2 Nov 2026 19:34:26 -0500 Subject: [PATCH] fix(#1062): fail-closed channel sync, ingress barrier, slot identity diff Epic A of #1061, fixing the #1060 class: the client's slot-to-channel map could silently diverge from the device and misroute notifications and conversations. - A slot the device never answers for is UNKNOWN: tracked, surfaced via a persistent channels-screen banner with Retry, and never backfilled from the previous list. Lookups for it fail closed, including notification evaluation, which now suppresses instead of defaulting to noisy. - Ingress barrier: live channel traffic holds until this connection's list is confirmed, then flushes. Pull-based message frames are never dropped (the radio already popped them); lossy raw sniffs stay bounded. Raw-RX no longer matches the offline cache. - Per-slot unread counts survive a sync only when the slot keeps the same PSK; identity changes are listed in a dismissible notice and logged. - A sync with unconfirmed slots does not overwrite the last fully-confirmed offline cache; a sync that fails on a live connection retries itself. New strings English-only per the #1021-era ruling; regenerated l10n + untranslated.json committed. Co-Authored-By: Claude Fable 5 --- lib/connector/meshcore_connector.dart | 257 +++++++++++++++++--- lib/l10n/app_en.arb | 18 ++ lib/l10n/app_localizations.dart | 24 ++ lib/l10n/app_localizations_bg.dart | 16 ++ lib/l10n/app_localizations_de.dart | 16 ++ lib/l10n/app_localizations_en.dart | 16 ++ lib/l10n/app_localizations_es.dart | 16 ++ lib/l10n/app_localizations_fr.dart | 16 ++ lib/l10n/app_localizations_hu.dart | 16 ++ lib/l10n/app_localizations_it.dart | 16 ++ lib/l10n/app_localizations_ja.dart | 16 ++ lib/l10n/app_localizations_ko.dart | 16 ++ lib/l10n/app_localizations_nl.dart | 16 ++ lib/l10n/app_localizations_pl.dart | 16 ++ lib/l10n/app_localizations_pt.dart | 16 ++ lib/l10n/app_localizations_ru.dart | 16 ++ lib/l10n/app_localizations_sk.dart | 16 ++ lib/l10n/app_localizations_sl.dart | 16 ++ lib/l10n/app_localizations_sv.dart | 16 ++ lib/l10n/app_localizations_uk.dart | 16 ++ lib/l10n/app_localizations_zh.dart | 16 ++ lib/screens/channels_screen.dart | 35 +++ test/connector/channel_sync_truth_test.dart | 203 ++++++++++++++++ untranslated.json | 68 ++++++ 24 files changed, 857 insertions(+), 36 deletions(-) create mode 100644 test/connector/channel_sync_truth_test.dart diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 454f00a6..0341a480 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -579,6 +579,38 @@ class MeshCoreConnector extends ChangeNotifier { int _totalChannelsToRequest = 0; List _previousChannelsCache = []; static const int _maxChannelSyncRetries = 3; + + // #1062: fail-closed channel sync. A slot the device never answered for is + // UNKNOWN: tracked here, surfaced to the UI, and never backfilled from a + // previous list (that backfill is how a stale slot map misrouted + // notifications and conversations, #1060). + final Set _unconfirmedChannelSlots = {}; + + /// Device slots this connection's sync could not confirm. Non-empty means + /// the channel list has holes the UI must surface (#1062). + Set get unconfirmedChannelSlots => + Set.unmodifiable(_unconfirmedChannelSlots); + + // #1062: ingress barrier. Live channel traffic is held while this + // connection's channel list is not yet synced, then flushed, so a message + // is never filed against a guessed slot map. + bool _channelListSynced = false; + final List _heldChannelMsgFrames = []; + final List _heldRawChannelFrames = []; + static const int _maxHeldChannelFrames = 64; + + /// Human-readable per-slot identity changes found by the latest completed + /// sync (old name -> new name). Surfaced once by the channels screen (#1062). + final List _slotIdentityChanges = []; + List get slotIdentityChanges => + List.unmodifiable(_slotIdentityChanges); + + /// User dismissed the slot-identity-change notice (#1062). + void acknowledgeSlotIdentityChanges() { + _slotIdentityChanges.clear(); + notifyListeners(); + } + static const int _channelSyncTimeoutMs = 2000; // 2 second timeout per channel static const Duration _batteryPollInterval = Duration(seconds: 120); @@ -4039,6 +4071,20 @@ class MeshCoreConnector extends ChangeNotifier { @visibleForTesting void setConnectedForTest() => _state = MeshCoreConnectionState.connected; + // #1062 test hooks: drive the sync timeout path without real timers, seed + // the offline cache, and observe the fail-closed slot lookup. + @visibleForTesting + void handleChannelSyncTimeoutForTest(int channelIndex) => + _handleChannelSyncTimeout(channelIndex); + + @visibleForTesting + void seedCachedChannelsForTest(List channels) { + _cachedChannels = List.from(channels); + } + + @visibleForTesting + Channel? findChannelByIndexForTest(int index) => _findChannelByIndex(index); + @visibleForTesting void setAppDebugLogServiceForTest(AppDebugLogService service) => _appDebugLogService = service; @@ -4126,6 +4172,14 @@ class MeshCoreConnector extends ChangeNotifier { _nextChannelIndexToRequest = 0; _totalChannelsToRequest = 0; _previousChannelsCache.clear(); + // #1062: a new connection starts with an unconfirmed list. Held MESSAGE + // frames are kept: the radio already popped them from its queue, so they + // exist nowhere else, and the next confirmed sync files them. Raw sniff + // frames are lossy observations; a dead connection's are stale. + _channelListSynced = false; + _unconfirmedChannelSlots.clear(); + _slotIdentityChanges.clear(); + _heldRawChannelFrames.clear(); } bool get _shouldAutoReconnect => @@ -6233,6 +6287,11 @@ class MeshCoreConnector extends ChangeNotifier { _isLoadingChannels = true; _isSyncingChannels = true; _hasLoadedChannels = false; + // Re-engage the ingress barrier for the duration of the sync: filing + // against a list that is mid-replacement is the same hazard as filing + // against a stale one (#1062). + _channelListSynced = false; + _unconfirmedChannelSlots.clear(); _previousChannelsCache = List.from(_channels); _channels.clear(); _nextChannelIndexToRequest = 0; @@ -6254,6 +6313,11 @@ class MeshCoreConnector extends ChangeNotifier { return; } + // Queued continuations from retries can land after the sync already + // finished; without this guard they re-enter _completeChannelSync against + // the reset counters (#1062). + if (!_isSyncingChannels) return; + if (_channelSyncInFlight) return; // Check if we've requested all channels @@ -6298,26 +6362,18 @@ class MeshCoreConnector extends ChangeNotifier { _channelSyncInFlight = false; unawaited(_requestNextChannel()); } else { - // Max retries reached for this channel, restore from cache and move to next - debugPrint( - '[ChannelSync] Max retries reached for channel $channelIndex, attempting cache restore', + // Max retries reached: the slot's contents are UNKNOWN. It used to be + // restored from the previous list here, which silently pinned a + // pre-reconfiguration channel at this index and misrouted notifications + // and conversations (#1060). Fail closed instead: leave the slot empty, + // record it, surface it (#1062). + _unconfirmedChannelSlots.add(channelIndex); + _appDebugLogService?.error( + 'Channel sync: device never answered for slot $channelIndex; ' + 'slot left unknown, not restored from cache', + tag: 'ChannelSync', ); - // Try to restore this channel from cache - try { - final cachedChannel = _previousChannelsCache.firstWhere( - (c) => c.index == channelIndex, - ); - if (!cachedChannel.isEmpty) { - _channels.add(cachedChannel); - debugPrint( - '[ChannelSync] Restored channel $channelIndex (${cachedChannel.name}) from cache', - ); - } - } catch (e) { - // No cached channel found, that's okay - } - // Move to next channel _nextChannelIndexToRequest++; _channelSyncRetries = 0; @@ -6331,25 +6387,66 @@ class MeshCoreConnector extends ChangeNotifier { _channelSyncTimeout?.cancel(); debugPrint( - '[ChannelSync] Sync complete: received ${_channels.length}/$_totalChannelsToRequest channels', + '[ChannelSync] Sync complete: received ${_channels.length}/$_totalChannelsToRequest channels' + '${_unconfirmedChannelSlots.isEmpty ? '' : ', unconfirmed slots: $_unconfirmedChannelSlots'}', ); _cleanupChannelSync(completed: true); - // Cache channels for offline use - _cachedChannels = List.from(_channels); - unawaited( - _channelStore.saveChannels(_channels).catchError((Object e) { - _appDebugLogService?.error( - 'Failed to persist channels (channel sync): $e', - tag: 'ChannelStore', + // #1062: per-slot identity diff against the last persisted truth. A slot + // whose PSK changed got a different channel; say so once instead of + // letting per-slot state ride across identities. + _slotIdentityChanges.clear(); + for (final fresh in _channels) { + final prior = _cachedChannels.cast().firstWhere( + (c) => c?.index == fresh.index, + orElse: () => null, + ); + if (prior != null && + !prior.isEmpty && + !fresh.isEmpty && + prior.pskHex != fresh.pskHex) { + _slotIdentityChanges.add( + 'slot ${fresh.index}: ${prior.name} -> ${fresh.name}', ); - }), - ); + _appDebugLogService?.warn( + 'Channel slot ${fresh.index} changed identity on the radio: ' + '"${prior.name}" -> "${fresh.name}"', + tag: 'ChannelSync', + ); + } + } + + if (_unconfirmedChannelSlots.isEmpty) { + // Full confirmation: the fresh list is the truth, replace the cache. + _cachedChannels = List.from(_channels); + unawaited( + _channelStore.saveChannels(_channels).catchError((Object e) { + _appDebugLogService?.error( + 'Failed to persist channels (channel sync): $e', + tag: 'ChannelStore', + ); + }), + ); + } else { + // Holes: keep the last fully-confirmed persisted list for offline use + // rather than overwriting it with a partial one. The holes are surfaced + // via [unconfirmedChannelSlots] and lookups for those slots fail closed. + _appDebugLogService?.error( + 'Channel sync finished with unconfirmed slots ' + '$_unconfirmedChannelSlots; cached list NOT overwritten. ' + 'Retry the sync to confirm them.', + tag: 'ChannelSync', + ); + } _recalculateCachedChannelsUnreadTotal(); // Apply ordering and notify UI _applyChannelOrder(); + + // #1062: open the ingress barrier and file what arrived mid-sync. + _channelListSynced = true; + _flushHeldChannelFrames(); notifyListeners(); // #839: one-time migration of the pre-per-radio notify state into THIS @@ -6380,6 +6477,50 @@ class MeshCoreConnector extends ChangeNotifier { ); } + /// Replays channel traffic held while the list was unconfirmed (#1062). + /// Runs after [_channelListSynced] flips true, so the handlers file against + /// the fresh list; a frame whose slot is still unconfirmed re-holds. + void _flushHeldChannelFrames() { + if (_heldChannelMsgFrames.isEmpty && _heldRawChannelFrames.isEmpty) return; + final msgFrames = List.from(_heldChannelMsgFrames); + final rawFrames = List.from(_heldRawChannelFrames); + _heldChannelMsgFrames.clear(); + _heldRawChannelFrames.clear(); + _appDebugLogService?.info( + 'Channel list synced; filing ${msgFrames.length} held message frame(s) ' + 'and ${rawFrames.length} held raw frame(s)', + tag: 'ChannelSync', + ); + for (final f in msgFrames) { + _handleIncomingChannelMessage(f); + } + for (final f in rawFrames) { + _handleLogRxData(f); + } + } + + /// Holds a frame behind the ingress barrier. Message frames are pull-based + /// replies the radio has already popped from its queue, so they are NEVER + /// dropped (the hold only lives for the seconds a sync takes). Raw sniff + /// frames are lossy by nature and stay bounded: overflow drops the oldest + /// and says so loudly (#1062). + void _holdChannelFrame( + List queue, + Uint8List frame, + String kind, { + required bool bounded, + }) { + if (bounded && queue.length >= _maxHeldChannelFrames) { + queue.removeAt(0); + _appDebugLogService?.error( + 'Held-$kind queue overflow while the channel list is unconfirmed; ' + 'dropped the oldest held frame', + tag: 'ChannelSync', + ); + } + queue.add(Uint8List.fromList(frame)); + } + void _cleanupChannelSync({required bool completed}) { _isSyncingChannels = false; _channelSyncInFlight = false; @@ -6406,6 +6547,16 @@ class MeshCoreConnector extends ChangeNotifier { // Keep cache on failure/disconnection for future attempts if (!completed) { + // #1062: a failed sync on a live connection leaves the ingress barrier + // closed with nothing scheduled to open it; retry rather than letting + // held messages starve. (A dead connection re-syncs on reconnect.) + if (isConnected) { + _channelsChangedDebounce?.cancel(); + _channelsChangedDebounce = Timer( + const Duration(seconds: 3), + () => unawaited(getChannels(force: true)), + ); + } notifyListeners(); } } @@ -9229,9 +9380,21 @@ class MeshCoreConnector extends ChangeNotifier { // Per-channel notification level, keyed by the channel's PSK identity so // the setting survives a rename and does not follow a reused slot (#259). // Evaluated against resolvedText so a translated mention still matches. + // #1062: an unknown slot means an unknown PSK means an unknowable mode; + // fail closed (no notification) rather than defaulting to noisy. + final slotChannel = _findChannelByIndex(channelIndex); + if (slotChannel == null && + _unconfirmedChannelSlots.contains(channelIndex)) { + _appDebugLogService?.warn( + 'Suppressing notification for channel slot $channelIndex: slot is ' + 'unconfirmed, its notify mode cannot be resolved', + tag: 'ChannelSync', + ); + return; + } final mode = _appSettingsService!.channelNotifyMode( devicePrefix: selfPublicKeyHex, - pskHex: _findChannelByIndex(channelIndex)?.pskHex, + pskHex: slotChannel?.pskHex, ); switch (mode) { case ChannelNotifyMode.off: @@ -9266,6 +9429,17 @@ class MeshCoreConnector extends ChangeNotifier { if (_isSyncingQueuedMessages) { _handleQueuedMessageReceived(); } + // #1062 ingress barrier: never file a message against a channel list that + // this connection has not confirmed; hold it and flush post-sync. + if (isConnected && !_channelListSynced) { + _holdChannelFrame( + _heldChannelMsgFrames, + frame, + 'message', + bounded: false, + ); + return; + } final parsed = ChannelMessage.fromFrame(frame); if (parsed != null && parsed.channelIndex != null) { // Device-composed (outgoing) messages legitimately carry our own name and @@ -9338,11 +9512,16 @@ class MeshCoreConnector extends ChangeNotifier { final channelHash = payload.readByte(); final encrypted = Uint8List.fromList(payload.readRemainingBytes()); - // Use cached channels as fallback if live channels not yet loaded - final channelsToSearch = _channels.isNotEmpty - ? _channels - : _cachedChannels; - for (final channel in channelsToSearch) { + // #1062 ingress barrier: a raw frame is filed by matching its channel + // hash against the CLIENT's list and reusing that object's index, so a + // stale or mid-sync list files it into the wrong conversation. Hold + // until this connection's list is confirmed; never match the offline + // cache (its indexes may describe a layout the radio no longer has). + if (!_channelListSynced) { + _holdChannelFrame(_heldRawChannelFrames, frame, 'raw', bounded: true); + return; + } + for (final channel in _channels) { if (channel.isEmpty) continue; final hash = _computeChannelHash(channel.psk); if (hash != channelHash) continue; @@ -9645,12 +9824,14 @@ class MeshCoreConnector extends ChangeNotifier { '[ChannelSync] Received channel ${channel.index}: ${channel.isEmpty ? "empty" : channel.name}', ); - // Preserve unread count from cached channel + // Preserve unread count from the cached channel, but only when the slot + // still holds the SAME channel. An unread count following the slot across + // an identity change is another channel's count (#1062). final cachedChannel = _cachedChannels.cast().firstWhere( (c) => c?.index == channel.index, orElse: () => null, ); - if (cachedChannel != null) { + if (cachedChannel != null && cachedChannel.pskHex == channel.pskHex) { channel.unreadCount = cachedChannel.unreadCount; } @@ -9750,6 +9931,10 @@ class MeshCoreConnector extends ChangeNotifier { } Channel? _findChannelByIndex(int index) { + // #1062: an unconfirmed slot's contents are unknown. Returning the cached + // occupant here is how a stale identity (wrong PSK, wrong name) attached + // itself to live traffic; fail closed instead. + if (_unconfirmedChannelSlots.contains(index)) return null; return _channels.cast().firstWhere( (c) => c?.index == index, orElse: () => null, diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 8960c537..317b1ee5 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -836,6 +836,24 @@ "channels_noChannelsConfigured": "No channels configured", "channels_addPublicChannel": "Add Public Channel", "channels_searchChannels": "Search channels...", + "channels_unconfirmedSlots": "The radio did not confirm channel slot(s) {slots}. Those channels are unknown until a sync succeeds.", + "@channels_unconfirmedSlots": { + "placeholders": { + "slots": { + "type": "String" + } + } + }, + "channels_retrySync": "Retry sync", + "channels_slotIdentityChanged": "Channel layout changed on the radio: {changes}", + "@channels_slotIdentityChanged": { + "placeholders": { + "changes": { + "type": "String" + } + } + }, + "common_dismiss": "Dismiss", "channels_noChannelsFound": "No channels found", "channels_channelIndex": "Channel {index}", "@channels_channelIndex": { diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart index c8b31e00..3044aeca 100644 --- a/lib/l10n/app_localizations.dart +++ b/lib/l10n/app_localizations.dart @@ -2818,6 +2818,30 @@ abstract class AppLocalizations { /// **'Search channels...'** String get channels_searchChannels; + /// No description provided for @channels_unconfirmedSlots. + /// + /// In en, this message translates to: + /// **'The radio did not confirm channel slot(s) {slots}. Those channels are unknown until a sync succeeds.'** + String channels_unconfirmedSlots(String slots); + + /// No description provided for @channels_retrySync. + /// + /// In en, this message translates to: + /// **'Retry sync'** + String get channels_retrySync; + + /// No description provided for @channels_slotIdentityChanged. + /// + /// In en, this message translates to: + /// **'Channel layout changed on the radio: {changes}'** + String channels_slotIdentityChanged(String changes); + + /// No description provided for @common_dismiss. + /// + /// In en, this message translates to: + /// **'Dismiss'** + String get common_dismiss; + /// No description provided for @channels_noChannelsFound. /// /// In en, this message translates to: diff --git a/lib/l10n/app_localizations_bg.dart b/lib/l10n/app_localizations_bg.dart index 4aef2fb4..b1b6f5ae 100644 --- a/lib/l10n/app_localizations_bg.dart +++ b/lib/l10n/app_localizations_bg.dart @@ -1579,6 +1579,22 @@ class AppLocalizationsBg extends AppLocalizations { @override String get channels_searchChannels => 'Търсене на канали...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Няма намерени канали'; diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart index 2b8d59ac..77afe12f 100644 --- a/lib/l10n/app_localizations_de.dart +++ b/lib/l10n/app_localizations_de.dart @@ -1573,6 +1573,22 @@ class AppLocalizationsDe extends AppLocalizations { @override String get channels_searchChannels => 'Suche Kanäle...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Keine Kanäle gefunden'; diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart index 05136266..39478abc 100644 --- a/lib/l10n/app_localizations_en.dart +++ b/lib/l10n/app_localizations_en.dart @@ -1555,6 +1555,22 @@ class AppLocalizationsEn extends AppLocalizations { @override String get channels_searchChannels => 'Search channels...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'No channels found'; diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart index be9d4a3e..2183176d 100644 --- a/lib/l10n/app_localizations_es.dart +++ b/lib/l10n/app_localizations_es.dart @@ -1574,6 +1574,22 @@ class AppLocalizationsEs extends AppLocalizations { @override String get channels_searchChannels => 'Buscar canales...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'No se encontraron canales'; diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart index 6738f5f9..7f729f47 100644 --- a/lib/l10n/app_localizations_fr.dart +++ b/lib/l10n/app_localizations_fr.dart @@ -1579,6 +1579,22 @@ class AppLocalizationsFr extends AppLocalizations { @override String get channels_searchChannels => 'Rechercher des canaux...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Aucun canal trouvé'; diff --git a/lib/l10n/app_localizations_hu.dart b/lib/l10n/app_localizations_hu.dart index e5bd2129..a6c3b9a4 100644 --- a/lib/l10n/app_localizations_hu.dart +++ b/lib/l10n/app_localizations_hu.dart @@ -1580,6 +1580,22 @@ class AppLocalizationsHu extends AppLocalizations { @override String get channels_searchChannels => 'Keresési opciók...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Nincs megtalálható csatorna'; diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart index b1978d2b..5b4b9d0f 100644 --- a/lib/l10n/app_localizations_it.dart +++ b/lib/l10n/app_localizations_it.dart @@ -1575,6 +1575,22 @@ class AppLocalizationsIt extends AppLocalizations { @override String get channels_searchChannels => 'Cerca canali...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Nessun canale trovato'; diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart index c5638629..74a35229 100644 --- a/lib/l10n/app_localizations_ja.dart +++ b/lib/l10n/app_localizations_ja.dart @@ -1523,6 +1523,22 @@ class AppLocalizationsJa extends AppLocalizations { @override String get channels_searchChannels => '検索オプション...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'チャンネルが見つかりませんでした'; diff --git a/lib/l10n/app_localizations_ko.dart b/lib/l10n/app_localizations_ko.dart index eef52521..cc8b2ad4 100644 --- a/lib/l10n/app_localizations_ko.dart +++ b/lib/l10n/app_localizations_ko.dart @@ -1518,6 +1518,22 @@ class AppLocalizationsKo extends AppLocalizations { @override String get channels_searchChannels => '검색 채널...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => '채널을 찾을 수 없습니다.'; diff --git a/lib/l10n/app_localizations_nl.dart b/lib/l10n/app_localizations_nl.dart index b3f00605..9a206eed 100644 --- a/lib/l10n/app_localizations_nl.dart +++ b/lib/l10n/app_localizations_nl.dart @@ -1566,6 +1566,22 @@ class AppLocalizationsNl extends AppLocalizations { @override String get channels_searchChannels => 'Zoek kanalen...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Geen kanalen gevonden'; diff --git a/lib/l10n/app_localizations_pl.dart b/lib/l10n/app_localizations_pl.dart index b1962d3b..846c6b3f 100644 --- a/lib/l10n/app_localizations_pl.dart +++ b/lib/l10n/app_localizations_pl.dart @@ -1585,6 +1585,22 @@ class AppLocalizationsPl extends AppLocalizations { @override String get channels_searchChannels => 'Wyszukaj kanały...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Brak znalezionych kanałów'; diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart index b3b57b10..111aecc5 100644 --- a/lib/l10n/app_localizations_pt.dart +++ b/lib/l10n/app_localizations_pt.dart @@ -1575,6 +1575,22 @@ class AppLocalizationsPt extends AppLocalizations { @override String get channels_searchChannels => 'Pesquisar canais...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Nenhum canal encontrado'; diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart index 6748532e..d2724854 100644 --- a/lib/l10n/app_localizations_ru.dart +++ b/lib/l10n/app_localizations_ru.dart @@ -1576,6 +1576,22 @@ class AppLocalizationsRu extends AppLocalizations { @override String get channels_searchChannels => 'Поиск каналов...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Каналы не найдены'; diff --git a/lib/l10n/app_localizations_sk.dart b/lib/l10n/app_localizations_sk.dart index 0d7e01ce..de469971 100644 --- a/lib/l10n/app_localizations_sk.dart +++ b/lib/l10n/app_localizations_sk.dart @@ -1566,6 +1566,22 @@ class AppLocalizationsSk extends AppLocalizations { @override String get channels_searchChannels => 'Vyhľadávajte kanály...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Neobsiahlo sa žiadnych kanálov.'; diff --git a/lib/l10n/app_localizations_sl.dart b/lib/l10n/app_localizations_sl.dart index e90ee0f6..77cfd12c 100644 --- a/lib/l10n/app_localizations_sl.dart +++ b/lib/l10n/app_localizations_sl.dart @@ -1563,6 +1563,22 @@ class AppLocalizationsSl extends AppLocalizations { @override String get channels_searchChannels => 'Poišči kanale...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Ne najdem kanalov.'; diff --git a/lib/l10n/app_localizations_sv.dart b/lib/l10n/app_localizations_sv.dart index f395a517..b8fec2a9 100644 --- a/lib/l10n/app_localizations_sv.dart +++ b/lib/l10n/app_localizations_sv.dart @@ -1557,6 +1557,22 @@ class AppLocalizationsSv extends AppLocalizations { @override String get channels_searchChannels => 'Sök kanaler...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Inga kanaler hittades'; diff --git a/lib/l10n/app_localizations_uk.dart b/lib/l10n/app_localizations_uk.dart index c9ec29de..b1728c8f 100644 --- a/lib/l10n/app_localizations_uk.dart +++ b/lib/l10n/app_localizations_uk.dart @@ -1571,6 +1571,22 @@ class AppLocalizationsUk extends AppLocalizations { @override String get channels_searchChannels => 'Пошук каналів...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => 'Каналів не знайдено'; diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart index a397f517..64f10bc4 100644 --- a/lib/l10n/app_localizations_zh.dart +++ b/lib/l10n/app_localizations_zh.dart @@ -1504,6 +1504,22 @@ class AppLocalizationsZh extends AppLocalizations { @override String get channels_searchChannels => '搜索频道...'; + @override + String channels_unconfirmedSlots(String slots) { + return 'The radio did not confirm channel slot(s) $slots. Those channels are unknown until a sync succeeds.'; + } + + @override + String get channels_retrySync => 'Retry sync'; + + @override + String channels_slotIdentityChanged(String changes) { + return 'Channel layout changed on the radio: $changes'; + } + + @override + String get common_dismiss => 'Dismiss'; + @override String get channels_noChannelsFound => '未找到任何频道'; diff --git a/lib/screens/channels_screen.dart b/lib/screens/channels_screen.dart index 1b2ec301..167a14ea 100644 --- a/lib/screens/channels_screen.dart +++ b/lib/screens/channels_screen.dart @@ -187,6 +187,41 @@ class _ChannelsScreenState extends State return Column( children: [ + // #1062: persistent, undismissable while true. A slot the + // radio never confirmed means its channel is unknown; a + // 4-second toast is not an error surface (SAFELANE 6). + if (connector.unconfirmedChannelSlots.isNotEmpty) + MaterialBanner( + backgroundColor: Theme.of( + context, + ).colorScheme.errorContainer, + content: Text( + context.l10n.channels_unconfirmedSlots( + connector.unconfirmedChannelSlots.toList().join(', '), + ), + ), + actions: [ + TextButton( + onPressed: () => + unawaited(connector.getChannels(force: true)), + child: Text(context.l10n.channels_retrySync), + ), + ], + ), + if (connector.slotIdentityChanges.isNotEmpty) + MaterialBanner( + content: Text( + context.l10n.channels_slotIdentityChanged( + connector.slotIdentityChanges.join('; '), + ), + ), + actions: [ + TextButton( + onPressed: connector.acknowledgeSlotIdentityChanges, + child: Text(context.l10n.common_dismiss), + ), + ], + ), Padding( padding: const EdgeInsets.all(8.0), child: TextField( diff --git a/test/connector/channel_sync_truth_test.dart b/test/connector/channel_sync_truth_test.dart new file mode 100644 index 00000000..83371cee --- /dev/null +++ b/test/connector/channel_sync_truth_test.dart @@ -0,0 +1,203 @@ +// #1062 (Epic A of #1061, fixing #1060): the channel sync is fail-closed and +// atomic, live filing waits behind an ingress barrier, and per-slot state +// never follows a slot across an identity change. +// +// The #1060 field case: a wipe + config re-import rearranged the device's +// channel slots; the old sync backfilled unanswered slots from the previous +// list, so notifications fired for the wrong channel and messages rendered +// inside the wrong conversation. + +import 'dart:typed_data'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/connector/meshcore_connector.dart'; +import 'package:meshcore_open/connector/meshcore_protocol.dart'; +import 'package:meshcore_open/models/channel.dart'; +import 'package:meshcore_open/storage/prefs_manager.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +Uint8List _channelInfoFrame({ + required int index, + required String name, + required int pskSeed, +}) { + final frame = Uint8List(50); + frame[0] = respCodeChannelInfo; + frame[1] = index; + final nameBytes = name.codeUnits; + for (var i = 0; i < nameBytes.length && i < 31; i++) { + frame[2 + i] = nameBytes[i]; + } + for (var i = 0; i < 16; i++) { + frame[34 + i] = (pskSeed + i) & 0xFF; + } + return frame; +} + +Uint8List _v3ChannelMsgFrame({required int channelIdx, String text = 'A: hi'}) { + return Uint8List.fromList([ + respCodeChannelMsgRecvV3, + 0, 0, 0, + channelIdx, + 1, // path_len: 1 hop, width 1 + txtTypePlain, + 0x40, 0xE2, 0x01, 0x00, + ...text.codeUnits, + ]); +} + +Channel _channel(int index, String name, int pskSeed) { + final psk = Uint8List.fromList( + List.generate(16, (i) => (pskSeed + i) & 0xFF), + ); + return Channel(index: index, name: name, psk: psk); +} + +MeshCoreConnector _connector() { + final c = MeshCoreConnector(); + c.sendFrameOverrideForTest = (_) {}; + c.setConnectedForTest(); + c.setSelfPublicKeyForTest( + Uint8List.fromList(List.generate(32, (i) => i)), + ); + return c; +} + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + setUp(() async { + SharedPreferences.setMockInitialValues({}); + PrefsManager.reset(); + await PrefsManager.initialize(); + }); + + test( + 'an unanswered slot stays UNKNOWN, never backfilled from cache (#1060)', + () async { + final c = _connector(); + c.seedCachedChannelsForTest([_channel(1, 'StaleOccupant', 70)]); + + await c.getChannels(maxChannels: 2); + c.handleFrameForTest( + _channelInfoFrame(index: 0, name: 'Fresh0', pskSeed: 10), + ); + // Slot 1 never answers: exhaust the retries. + for (var i = 0; i <= 3; i++) { + c.handleChannelSyncTimeoutForTest(1); + await Future.delayed(Duration.zero); + } + + expect(c.unconfirmedChannelSlots, {1}); + expect(c.channels.map((ch) => ch.name), ['Fresh0']); + expect( + c.channels.any((ch) => ch.name == 'StaleOccupant'), + isFalse, + reason: 'the stale cache occupant must not be restored into the slot', + ); + expect( + c.findChannelByIndexForTest(1), + isNull, + reason: 'lookups for an unconfirmed slot fail closed', + ); + }, + ); + + test( + 'messages arriving mid-sync are held, then filed after the sync', + () async { + final c = _connector(); + + await c.getChannels(maxChannels: 2); + // Barrier is up: a pushed message must not file yet. + c.handleFrameForTest(_v3ChannelMsgFrame(channelIdx: 0)); + expect(c.channelMessagesForTest[0], isNull); + + c.handleFrameForTest( + _channelInfoFrame(index: 0, name: 'Alpha', pskSeed: 10), + ); + await Future.delayed(Duration.zero); + c.handleFrameForTest( + _channelInfoFrame(index: 1, name: 'Beta', pskSeed: 30), + ); + await Future.delayed(Duration.zero); + + expect(c.unconfirmedChannelSlots, isEmpty); + expect( + c.channelMessagesForTest[0], + isNotNull, + reason: 'the held frame files once the list is confirmed', + ); + expect(c.channelMessagesForTest[0]!.single.text, 'hi'); + }, + ); + + test( + 'unread count does not follow a slot across an identity change', + () async { + final c = _connector(); + final stale = _channel(0, 'OldChannel', 70)..unreadCount = 5; + c.seedCachedChannelsForTest([stale]); + + await c.getChannels(maxChannels: 1); + // Same slot, different PSK: a different channel lives here now. + c.handleFrameForTest( + _channelInfoFrame(index: 0, name: 'NewChannel', pskSeed: 10), + ); + await Future.delayed(Duration.zero); + + expect(c.channels.single.name, 'NewChannel'); + expect( + c.channels.single.unreadCount, + 0, + reason: 'another identity\'s unread count must not ride the slot', + ); + expect(c.slotIdentityChanges, isNotEmpty); + }, + ); + + test( + 'unread count IS preserved when the slot keeps the same channel', + () async { + final c = _connector(); + final same = _channel(0, 'Keep', 10)..unreadCount = 7; + c.seedCachedChannelsForTest([same]); + + await c.getChannels(maxChannels: 1); + c.handleFrameForTest( + _channelInfoFrame(index: 0, name: 'Keep', pskSeed: 10), + ); + await Future.delayed(Duration.zero); + + expect(c.channels.single.unreadCount, 7); + expect(c.slotIdentityChanges, isEmpty); + }, + ); + + test( + 'a sync with unconfirmed slots does not overwrite the offline cache', + () async { + final c = _connector(); + c.seedCachedChannelsForTest([ + _channel(0, 'GoodCache0', 10), + _channel(1, 'GoodCache1', 30), + ]); + + await c.getChannels(maxChannels: 2); + c.handleFrameForTest( + _channelInfoFrame(index: 0, name: 'Fresh0', pskSeed: 10), + ); + for (var i = 0; i <= 3; i++) { + c.handleChannelSyncTimeoutForTest(1); + await Future.delayed(Duration.zero); + } + + expect(c.unconfirmedChannelSlots, {1}); + // The confirmed slot is live; the unconfirmed one is a hole, and the last + // fully-confirmed cache survives for the next attempt (slot 0 lookup still + // resolves, slot 1 fails closed). + expect(c.findChannelByIndexForTest(0)?.name, 'Fresh0'); + expect(c.findChannelByIndexForTest(1), isNull); + }, + ); +} diff --git a/untranslated.json b/untranslated.json index eb4bc5a4..ea2bfb1d 100644 --- a/untranslated.json +++ b/untranslated.json @@ -126,6 +126,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -465,6 +469,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -804,6 +812,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -1143,6 +1155,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -1482,6 +1498,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -1821,6 +1841,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -2160,6 +2184,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -2499,6 +2527,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -2838,6 +2870,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -3177,6 +3213,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -3516,6 +3556,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -3855,6 +3899,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -4194,6 +4242,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -4533,6 +4585,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -4872,6 +4928,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -5211,6 +5271,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly", @@ -5550,6 +5614,10 @@ "contact_remoteCli", "contact_remoteCliSubtitle", "contact_cliConsole", + "channels_unconfirmedSlots", + "channels_retrySync", + "channels_slotIdentityChanged", + "common_dismiss", "channels_notifications", "channels_notifyAll", "channels_notifyMentionsOnly",