From 98841f1c2bfb23d4ca7ce682b52a08858deb947c Mon Sep 17 00:00:00 2001 From: Strycher Date: Fri, 10 Jul 2026 14:23:08 -0400 Subject: [PATCH] fix(#193): clear stale slot history on channel reassignment Channel message history is keyed by the reusable slot index, not by the channel's PSK. Adding a channel to a slot that previously held a different channel surfaced the prior occupant's messages (e.g. #echo history appearing in a newly QR-added channel). setChannel now clears the slot's stored history unless the slot already holds the same channel (same PSK, e.g. a rename). Only deleteChannel cleared before. Build-B (bleed-stop) of epic #190. Build-A (PSK-keyed history + migration) to follow. Accepted edge: re-adding the exact same channel that was orphaned device-side loses its old history (messages carry no PSK to match on). --- lib/connector/meshcore_connector.dart | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 8f2ebdb..e30e4a1 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -3826,7 +3826,22 @@ class MeshCoreConnector extends ChangeNotifier { Future setChannel(int index, String name, Uint8List psk) async { if (!isConnected) return; + // Channel history is stored keyed by the reusable slot [index], not by the + // channel's PSK. If this slot is being (re)assigned to a different channel + // than the one whose history is stored here, clear the stale history so a + // previous occupant's messages (e.g. #echo) don't surface under the newly + // added channel. Preserve only when the slot already holds the SAME channel + // (same PSK) — e.g. a rename. (#193) + final existing = _findChannelByIndex(index); + final sameChannel = existing != null && listEquals(existing.psk, psk); + await sendFrame(buildSetChannelFrame(index, name, psk)); + + if (!sameChannel) { + await _channelMessageStore.clearChannelMessages(index); + _channelMessages.remove(index); + } + // Refresh channels after setting await getChannels(force: true); }