Move send controls into Routing and split retry limits

Direct and flood attempts are separate settings, available with auto
route rotation off. Saved single retry limits migrate to that split,
and reset restores the whole Routing section.

Co-authored-by: zjs81 <zjs81@users.noreply.github.com>
This commit is contained in:
Cursor Agentandzjs81 committed 2026-11-21 00:50:53 +00:00
1 parent 34ea0cb7ca
commit 1b804bd1eb
69 files changed
+2199 -784

No files matched your search

+54 -13
View File
@@ -7,6 +7,7 @@ import '../connector/meshcore_protocol.dart';
import '../models/contact.dart';
import '../models/message.dart';
import '../models/path_selection.dart';
import '../models/send_attempt_plan.dart';
import 'app_settings_service.dart';
import 'app_debug_log_service.dart';
@@ -58,7 +59,7 @@ class RetryServiceConfig {
final PathSelection? Function(
String contactKey,
int attemptIndex,
int maxRetries,
int directRetries,
List<PathSelection> recentSelections,
)?
selectRetryPath;
@@ -88,8 +89,21 @@ class MessageRetryService extends ChangeNotifier {
/// shared globally; cap at 6 to leave two slots of headroom.
static const int _maxGlobalInFlight = 6;
int _maxRetries = 5;
int get maxRetries => _maxRetries;
int? _directOverride;
int? _floodOverride;
bool? _rotateOverride;
/// Pins both limits and turns rotation off. Tests use this to send a fixed
/// number of attempts on the contact's current path.
void setMaxRetries(int value) {
final attempts = value < 1 ? 1 : value;
_directOverride = attempts;
_floodOverride = attempts.clamp(
SendAttemptPlan.minFloodRetries,
SendAttemptPlan.maxFloodRetries,
);
_rotateOverride = false;
}
final Map<String, Timer> _timeoutTimers = {};
final Map<String, Message> _pendingMessages = {};
@@ -110,6 +124,7 @@ class MessageRetryService extends ChangeNotifier {
final Set<String> _activeMessages = {};
final Set<String> _resolvedMessages = {};
final Map<String, String> _expectedHashToMessageId = {};
final Map<String, int> _attemptLimits = {};
RetryServiceConfig? _config;
@@ -123,8 +138,26 @@ class MessageRetryService extends ChangeNotifier {
_config = config;
}
void setMaxRetries(int value) {
_maxRetries = value.clamp(2, 10);
SendAttemptPlan _planFor(Contact contact) {
final settings = _config?.appSettingsService?.settings;
final direct =
_directOverride ??
settings?.directRetries ??
SendAttemptPlan.defaultDirectRetries;
final flood =
_floodOverride ??
settings?.floodRetries ??
SendAttemptPlan.defaultFloodRetries;
final rotateFlag =
_rotateOverride ?? settings?.autoRouteRotationEnabled ?? true;
return SendAttemptPlan(
directRetries: direct,
floodRetries: flood,
rotateRoutes:
rotateFlag &&
contact.pathOverride == null &&
_config?.selectRetryPath != null,
);
}
/// Compute expected ACK hash using same algorithm as firmware:
@@ -176,6 +209,9 @@ class MessageRetryService extends ChangeNotifier {
pathBytes ?? Uint8List.fromList(resolved.pathBytes);
final messagePathLength =
pathLength ?? (resolved.useFlood ? -1 : resolved.hopCount);
_attemptLimits[messageId] = _planFor(
contact,
).attemptLimit(floodPath: messagePathLength < 0);
final message = Message(
senderKey: contact.publicKey,
text: text,
@@ -289,11 +325,8 @@ class MessageRetryService extends ChangeNotifier {
PathSelection? _selectPathForAttempt(Message message, Contact contact) {
final config = _config;
if (config == null) return null;
final autoRotationEnabled =
config.appSettingsService?.settings.autoRouteRotationEnabled == true;
if (!autoRotationEnabled ||
contact.pathOverride != null ||
config.selectRetryPath == null) {
final plan = _planFor(contact);
if (!plan.rotateRoutes || config.selectRetryPath == null) {
return null;
}
@@ -303,7 +336,7 @@ class MessageRetryService extends ChangeNotifier {
return config.selectRetryPath!(
contact.publicKeyHex,
message.retryCount,
maxRetries,
plan.floodFromAttempt ?? (1 << 30),
recentSelections,
);
}
@@ -626,6 +659,7 @@ class MessageRetryService extends ChangeNotifier {
_pendingMessages.remove(messageId);
_pendingContacts.remove(messageId);
_attemptPathHistory.remove(messageId);
_attemptLimits.remove(messageId);
_deferredRouteFailures.remove(messageId);
_timeoutTimers.remove(messageId);
_resolvedMessages.remove(messageId);
@@ -657,9 +691,16 @@ class MessageRetryService extends ChangeNotifier {
tag: 'AckHash',
);
if (message.retryCount < maxRetries - 1 &&
final limit =
_attemptLimits[messageId] ??
_planFor(
contact,
).attemptLimit(floodPath: resolvePathSelection(contact).useFlood);
if (message.retryCount < limit - 1 &&
_textFitsAttempt(message, contact, message.retryCount + 1)) {
final backoffMs = 1000 * (1 << message.retryCount);
// Cap the shift so a high retry limit does not wait for minutes.
final shift = message.retryCount.clamp(0, 5);
final backoffMs = 1000 * (1 << shift);
if (selection != null) {
_deferredRouteFailures.putIfAbsent(messageId, () => []).add((