From 9e422a1e15085e66faef11cb092fbdad99cbd4ec Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:09:04 -0500 Subject: [PATCH] fix(#1223): re-establish a pinned route once per send (owner ruling) The flood fallback clears the stored path on the radio, so without this a hand-picked route stopped being honoured after one bad send while the UI still claimed it. That is the silent failure that got "Direct" removed, so leaving it was not an option. Owner ruled option (a): re-push the override once at the start of each send. This is not the defect #1198 removed. That was a client-side snapshot of a path the RADIO had learned, re-asserted before every attempt, able to overwrite a fresher route the radio had just picked up off the air. This is the user's own explicit choice, written once, at the start of a send, and it has to be written because the previous send's final attempt may have cleared it. One write per send, never one per attempt. `RetryServiceConfig.setContactPath` now carries `hashWidth`. The callback dropped it, so a 2-byte route would have been written at width 1 and re-read as twice as many 1-byte hops, routing to the wrong nodes (#279). That trap was latent while no path write existed on this path; adding one back makes it reachable, so the signature closes it. Tests: a pinned route is written exactly once per send and at the width it was captured at, and the retry after it writes nothing. --- lib/services/message_retry_service.dart | 43 +++++++++---- test/services/retry_and_protocol_test.dart | 73 +++++++++++++++++++--- 2 files changed, 96 insertions(+), 20 deletions(-) diff --git a/lib/services/message_retry_service.dart b/lib/services/message_retry_service.dart index f3161e17..d061409f 100644 --- a/lib/services/message_retry_service.dart +++ b/lib/services/message_retry_service.dart @@ -38,7 +38,11 @@ class RetryServiceConfig { final void Function(String, Message) addMessage; final void Function(Message) updateMessage; final Function(Contact)? clearContactPath; - final Function(Contact, Uint8List, int)? setContactPath; + + /// Carries `hashWidth` because a route's bytes are meaningless without the + /// width they were captured at: a 2-byte route written at width 1 reads as + /// twice as many 1-byte hops and routes to the wrong nodes (#279). + final Function(Contact, Uint8List, int, {int hashWidth})? setContactPath; final int Function(int pathLength, int messageBytes, {String? contactKey})? calculateTimeout; final Uint8List? Function()? getSelfPublicKey; @@ -309,22 +313,39 @@ class MessageRetryService extends ChangeNotifier { // every attempt overwrote whatever the radio had, including a fresher // route it had just learned, and guaranteed the clear below never ran. // - // A user's path override is still pushed to the radio, but at the moment - // they choose it: `setPathOverride()` calls `setContactPath()` itself. - // Re-sending it on every attempt added nothing. - // // Clearing is kept: it is a per-send routing decision the radio cannot // make for us, because the wire protocol has no per-send routing field. // Clearing a dead path is how the mesh re-learns one. (#1198/#1223) - if (config.clearContactPath != null) { - final bool useFlood = currentSelection != null - ? currentSelection.useFlood - : (effectiveMessage.pathLength != null && - effectiveMessage.pathLength! < 0); + final bool useFlood = currentSelection != null + ? currentSelection.useFlood + : (effectiveMessage.pathLength != null && + effectiveMessage.pathLength! < 0); - if (useFlood) { + if (useFlood) { + if (config.clearContactPath != null) { await config.clearContactPath!(contact); } + } else if (message.retryCount == 0 && (contact.pathOverride ?? 0) > 0) { + // One write per send, and only for a route the user chose by hand. + // + // This is not the defect above. That was a client-side snapshot of a + // path the RADIO had learned, re-asserted before every attempt, which + // could overwrite a fresher route the radio had just picked up off the + // air. This is the user's own explicit choice, written once, at the + // start of a send, and it has to be written because the previous send's + // final attempt may have cleared it: the fallback floods, and flooding + // means clearing the stored path. Without this a pinned route stops + // being honoured after one bad send while the UI still claims it, which + // is the silent failure that got "Direct" removed. (#1223 R4) + final resolved = resolvePathSelection(contact); + if (config.setContactPath != null && resolved.pathBytes.isNotEmpty) { + await config.setContactPath!( + contact, + Uint8List.fromList(resolved.pathBytes), + resolved.hopCount, + hashWidth: resolved.hashWidth, + ); + } } // Re-validate after async gap, a timer or ACK could have resolved/retried diff --git a/test/services/retry_and_protocol_test.dart b/test/services/retry_and_protocol_test.dart index f2c3f923..b4fc41ee 100644 --- a/test/services/retry_and_protocol_test.dart +++ b/test/services/retry_and_protocol_test.dart @@ -577,7 +577,7 @@ void main() { addMessage: (_, message) => addedMessage = message, updateMessage: (_) {}, clearContactPath: (_) {}, - setContactPath: (_, _, _) {}, + setContactPath: (_, _, _, {hashWidth = 1}) {}, selectRetryPath: (_, _, _, _) => null, ), ); @@ -611,7 +611,7 @@ void main() { addMessage: (_, message) => addedMessage = message, updateMessage: (_) {}, clearContactPath: (_) {}, - setContactPath: (_, _, _) {}, + setContactPath: (_, _, _, {hashWidth = 1}) {}, ), ); @@ -682,7 +682,7 @@ void main() { addMessage: (_, _) {}, updateMessage: updates.add, clearContactPath: (_) {}, - setContactPath: (_, _, _) {}, + setContactPath: (_, _, _, {hashWidth = 1}) {}, ), ); @@ -970,7 +970,8 @@ void main() { sendMessage: (_, _, _, _) async {}, addMessage: (_, _) {}, updateMessage: (_) {}, - setContactPath: (_, bytes, hops) => setCalls.add(hops), + setContactPath: (_, bytes, hops, {hashWidth = 1}) => + setCalls.add(hops), clearContactPath: (c) => clearCalls.add(c.publicKeyHex), ), ); @@ -1007,7 +1008,8 @@ void main() { sendMessage: (_, _, _, _) async {}, addMessage: (_, _) {}, updateMessage: (_) {}, - setContactPath: (_, bytes, hops) => setCalls.add(hops), + setContactPath: (_, bytes, hops, {hashWidth = 1}) => + setCalls.add(hops), clearContactPath: (c) => clearCalls.add(c.publicKeyHex), ), ); @@ -1124,7 +1126,7 @@ void main() { sendMessage: (_, _, _, _) async {}, addMessage: (_, _) {}, updateMessage: (_) {}, - setContactPath: (_, _, hops) => setCalls.add(hops), + setContactPath: (_, _, hops, {hashWidth = 1}) => setCalls.add(hops), clearContactPath: (c) => clearCalls.add(c.publicKeyHex), ), ); @@ -1178,7 +1180,7 @@ void main() { sendMessage: (_, _, _, _) async {}, addMessage: (_, _) {}, updateMessage: (_) {}, - setContactPath: (_, _, hops) => setCalls.add(hops), + setContactPath: (_, _, hops, {hashWidth = 1}) => setCalls.add(hops), clearContactPath: (c) => clearCalls.add(c.publicKeyHex), ), ); @@ -1191,6 +1193,13 @@ void main() { isEmpty, reason: 'the chosen route gets the ordinary attempts, untouched', ); + expect( + setCalls, + equals([2]), + reason: + 'the pinned route is written once at the start of the send, ' + 'because a previous send may have cleared it (#1223 R4)', + ); // Radio confirms the send and arms a 1 ms delivery timeout, so the // retry runs without a real ACK. maxRetries of 2 makes attempt 1 final. @@ -1207,8 +1216,54 @@ void main() { ); expect( setCalls, - isEmpty, - reason: 'and it still never writes a path to the radio (#1198)', + equals([2]), + reason: + 'and the retry writes nothing: one write per send, never one ' + 'per attempt (#1198)', + ); + + retryService.dispose(); + }); + + test('a pinned route is re-established once per send, at the width it ' + 'was captured at', () async { + final retryService = MessageRetryService(); + // A 2-byte-wide route. Written at width 1 it would read as four + // 1-byte hops and route to the wrong nodes (#279). + final contact = Contact( + publicKey: recipientKey, + receivedName: 'Test', + type: 1, + pathLength: 2, + pathHashWidth: 2, + path: Uint8List.fromList(const [0x10, 0x11, 0x20, 0x21]), + pathOverride: 2, + pathOverrideBytes: Uint8List.fromList(const [0x10, 0x11, 0x20, 0x21]), + lastSeen: DateTime.now(), + ); + final writes = <({int hops, int width, int bytes})>[]; + + retryService.initialize( + RetryServiceConfig( + sendMessage: (_, _, _, _) async {}, + addMessage: (_, _) {}, + updateMessage: (_) {}, + setContactPath: (_, bytes, hops, {hashWidth = 1}) => + writes.add((hops: hops, width: hashWidth, bytes: bytes.length)), + clearContactPath: (_) {}, + ), + ); + + await retryService.sendMessageWithRetry(contact: contact, text: 'one'); + await Future.delayed(const Duration(milliseconds: 20)); + + expect(writes.length, equals(1), reason: 'exactly one write per send'); + expect( + writes.single, + equals((hops: 2, width: 2, bytes: 4)), + reason: + 'two hops of two bytes each. Dropping the width is how a ' + 'multi-byte route gets re-read as twice as many hops (#279)', ); retryService.dispose();