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

+26 -3
View File
@@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:flutter/foundation.dart';
import '../models/app_settings.dart';
import '../models/custom_theme.dart';
import '../models/send_attempt_plan.dart';
import '../models/holiday_schedule.dart';
import '../models/image_codec_support.dart';
import '../models/translation_support.dart';
@@ -228,8 +229,22 @@ class AppSettingsService extends ChangeNotifier {
);
}
Future<void> setMaxMessageRetries(int value) async {
await updateSettings(_settings.copyWith(maxMessageRetries: value));
Future<void> setDirectRetries(int value) async {
await updateSettings(
_settings.copyWith(directRetries: SendAttemptPlan.clampDirect(value)),
);
}
Future<void> setFloodRetries(int value) async {
await updateSettings(
_settings.copyWith(floodRetries: SendAttemptPlan.clampFlood(value)),
);
}
Future<void> setLoginAttempts(int value) async {
await updateSettings(
_settings.copyWith(loginAttempts: SendAttemptPlan.clampLogin(value)),
);
}
Future<void> setChannelMinHopsEnabled(bool value) async {
@@ -714,13 +729,21 @@ class AppSettingsService extends ChangeNotifier {
Future<void> resetRoutingSettingsToDefault() async {
await updateSettings(
_settings.copyWith(
clearPathOnMaxRetry: false,
autoRouteRotationEnabled: true,
maxRouteWeight: AppSettings.defaultMaxRouteWeight,
initialRouteWeight: AppSettings.defaultInitialRouteWeight,
routeWeightSuccessIncrement:
AppSettings.defaultRouteWeightSuccessIncrement,
routeWeightFailureDecrement:
AppSettings.defaultRouteWeightFailureDecrement,
maxMessageRetries: AppSettings.defaultMaxMessageRetries,
directRetries: AppSettings.defaultDirectRetries,
floodRetries: AppSettings.defaultFloodRetries,
loginAttempts: AppSettings.defaultLoginAttempts,
channelMinHopsEnabled: false,
channelMinHops: 2,
channelMinHopsRetries: 1,
channelMinHopsCountRepeaters: false,
),
);
}
+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((
+7 -6
View File
@@ -259,18 +259,19 @@ class PathHistoryService extends ChangeNotifier {
/// Route for one send attempt of a message to [contactPubKeyHex].
///
/// The last attempt always floods. Otherwise a known route is drawn by
/// Thompson sampling over faded delivery stats, weighted by how well it
/// fits [context]; routes used for this message's recent attempts score
/// lower so retries usually try something different.
/// Attempts before [directRetries] draw a known route by Thompson sampling
/// over faded delivery stats, weighted by how well it fits [context];
/// routes used for this message's recent attempts score lower so retries
/// usually try something different. From [directRetries] on, the attempt
/// floods. [directRetries] of 0 floods immediately.
PathSelection selectPathForAttempt(
String contactPubKeyHex, {
required int attemptIndex,
required int maxRetries,
required int directRetries,
List<PathSelection> recentSelections = const [],
RouteContext context = RouteContext.unknown,
}) {
if (maxRetries <= 0 || attemptIndex >= maxRetries - 1) {
if (directRetries <= 0 || attemptIndex >= directRetries) {
return const PathSelection(pathBytes: [], hopCount: -1, useFlood: true);
}