diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index c12dca7c..c41a9e7c 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -334,12 +334,6 @@ class MeshCoreConnector extends ChangeNotifier { ReceivedImageStore? _receivedImageStore; void Function(ImageChunkOutcome outcome)? _onImageChunk; void Function(int senderPrefix)? _onImageSenderPrefix; - // Intentionally global (not per-contact): tracks overall network activity. - // Frequent RX from any source indicates a busy network with more collisions. - DateTime _lastRxTime = DateTime.now(); - // Snapshot of _lastRxTime taken before the ACK frame updates it, so that - // onDeliveryObserved records the pre-ACK elapsed time (matching prediction). - DateTime _lastRxBeforeFrame = DateTime.fromMillisecondsSinceEpoch(0); DateTime _lastRadioRxTime = DateTime.fromMillisecondsSinceEpoch(0); DateTime _lastContactMsgRxTime = DateTime.fromMillisecondsSinceEpoch(0); DateTime _lastChannelMsgRxTime = DateTime.fromMillisecondsSinceEpoch(0); @@ -1810,15 +1804,11 @@ class MeshCoreConnector extends ChangeNotifier { recentSelections: recentSelections, ), onDeliveryObserved: (contactKey, pathLength, messageBytes, tripTimeMs) { - final secSinceRx = DateTime.now() - .difference(_lastRxBeforeFrame) - .inSeconds; _timeoutPredictionService?.recordObservation( contactKey: contactKey, pathLength: pathLength, messageBytes: messageBytes, tripTimeMs: tripTimeMs, - secondsSinceLastRx: secSinceRx, ); }, ), @@ -6073,8 +6063,6 @@ class MeshCoreConnector extends ChangeNotifier { void _handleFrame(List data) { if (data.isEmpty) return; - _lastRxBeforeFrame = _lastRxTime; - _lastRxTime = DateTime.now(); final frame = Uint8List.fromList(data); _receivedFramesController.add(frame); @@ -6834,12 +6822,10 @@ class MeshCoreConnector extends ChangeNotifier { final physicsMax = _physicsMaxTimeout(pathLength, airtime); // Try ML-based prediction - final secSinceRx = DateTime.now().difference(_lastRxTime).inSeconds; final mlTimeout = _timeoutPredictionService?.predictTimeout( contactKey: contactKey, pathLength: pathLength, messageBytes: messageBytes, - secondsSinceLastRx: secSinceRx, ); if (mlTimeout != null) { // Use device est_timeout as a baseline floor when available — diff --git a/lib/models/delivery_observation.dart b/lib/models/delivery_observation.dart index a4bd63bb..c063fae3 100644 --- a/lib/models/delivery_observation.dart +++ b/lib/models/delivery_observation.dart @@ -2,7 +2,6 @@ class DeliveryObservation { final String contactKey; final int pathLength; final int messageBytes; - final int secondsSinceLastRx; final bool isFlood; final int deliveryMs; final DateTime timestamp; @@ -11,7 +10,6 @@ class DeliveryObservation { required this.contactKey, required this.pathLength, required this.messageBytes, - required this.secondsSinceLastRx, required this.isFlood, required this.deliveryMs, required this.timestamp, @@ -22,7 +20,6 @@ class DeliveryObservation { 'contact_key': contactKey, 'path_length': pathLength, 'message_bytes': messageBytes, - 'seconds_since_last_rx': secondsSinceLastRx, 'is_flood': isFlood, 'delivery_ms': deliveryMs, 'timestamp': timestamp.toIso8601String(), @@ -34,7 +31,6 @@ class DeliveryObservation { contactKey: json['contact_key'] as String, pathLength: json['path_length'] as int, messageBytes: json['message_bytes'] as int, - secondsSinceLastRx: json['seconds_since_last_rx'] as int? ?? 0, isFlood: json['is_flood'] as bool? ?? false, deliveryMs: json['delivery_ms'] as int, timestamp: DateTime.parse(json['timestamp'] as String), diff --git a/lib/services/message_retry_service.dart b/lib/services/message_retry_service.dart index 85b85c9f..b0da4e1e 100644 --- a/lib/services/message_retry_service.dart +++ b/lib/services/message_retry_service.dart @@ -843,9 +843,12 @@ class MessageRetryService extends ChangeNotifier { tripTimeMs, ); } + // Learn from the attempt this ACK belongs to, not the latest one: a + // late ACK for a direct attempt can land after a retry went flood. if (config?.onDeliveryObserved != null && tripTimeMs > 0 && - message.pathLength != null) { + selection != null && + !ambiguousPath) { final outboundTextForObserved = config!.prepareContactOutboundText?.call(contact, message.text) ?? message.text; @@ -854,7 +857,7 @@ class MessageRetryService extends ChangeNotifier { .length; config.onDeliveryObserved!( contact.publicKeyHex, - message.pathLength!, + selection.useFlood ? -1 : selection.hopCount, messageBytesForObserved, tripTimeMs, ); diff --git a/lib/services/path_history_service.dart b/lib/services/path_history_service.dart index d476a750..f1a51d39 100644 --- a/lib/services/path_history_service.dart +++ b/lib/services/path_history_service.dart @@ -357,8 +357,9 @@ class PathHistoryService extends ChangeNotifier { updatedPaths.removeWhere((p) => _pathsEqual(p.pathBytes, pathBytes)); + // Make room by dropping the weakest path so new discoveries still land. if (existing == null && updatedPaths.length >= _maxHistoryEntries) { - return; + updatedPaths.remove(_lowestScoredPath(updatedPaths)); } updatedPaths.insert(0, newRecord); @@ -540,6 +541,17 @@ class PathHistoryService extends ChangeNotifier { return ranked; } + PathRecord _lowestScoredPath(List paths) { + final fastestTripMs = _getFastestKnownTripMs(paths); + final highestRouteWeight = _getHighestKnownRouteWeight(paths); + double score(PathRecord p) => _scorePathRecord( + p, + fastestTripMs: fastestTripMs, + highestRouteWeight: highestRouteWeight, + ); + return paths.reduce((a, b) => score(b) < score(a) ? b : a); + } + int? _getFastestKnownTripMs(List paths) { final knownTrips = paths .where((path) => path.tripTimeMs > 0) diff --git a/lib/services/timeout_prediction_service.dart b/lib/services/timeout_prediction_service.dart index a9b8f48f..ce34c62f 100644 --- a/lib/services/timeout_prediction_service.dart +++ b/lib/services/timeout_prediction_service.dart @@ -65,7 +65,6 @@ class TimeoutPredictionService extends ChangeNotifier { required int pathLength, required int messageBytes, required int tripTimeMs, - int secondsSinceLastRx = 0, }) { if (recordingSuspended) return; final isFlood = pathLength < 0; @@ -75,7 +74,6 @@ class TimeoutPredictionService extends ChangeNotifier { // only; isFlood carries the flood signal as a separate feature. pathLength: isFlood ? 0 : pathLength, messageBytes: messageBytes, - secondsSinceLastRx: secondsSinceLastRx, isFlood: isFlood, deliveryMs: tripTimeMs, timestamp: DateTime.now(), @@ -110,7 +108,6 @@ class TimeoutPredictionService extends ChangeNotifier { String? contactKey, required int pathLength, required int messageBytes, - int secondsSinceLastRx = 0, }) { if (_model == null) return null; @@ -123,7 +120,6 @@ class TimeoutPredictionService extends ChangeNotifier { // prediction see the same pathLength values; isFlood carries the signal. 'pathLength': flood ? 0.0 : pathLength.toDouble(), 'messageBytes': messageBytes.toDouble(), - 'secSinceRx': secondsSinceLastRx.toDouble(), 'isFlood': flood ? 1.0 : 0.0, }; final row = _activeFeatures.map((f) => allFeatures[f]!).toList(); @@ -174,13 +170,12 @@ class TimeoutPredictionService extends ChangeNotifier { try { // Build feature columns, then exclude any with zero variance // (ml_algo's OLS produces all-zero coefficients for singular matrices) - final allNames = ['pathLength', 'messageBytes', 'secSinceRx', 'isFlood']; + final allNames = ['pathLength', 'messageBytes', 'isFlood']; final allExtractors = [ // pathLength is already clamped to >=0 in recordObservation, but guard // here as well for any observations loaded from older persisted data. (o) => o.pathLength < 0 ? 0.0 : o.pathLength.toDouble(), (o) => o.messageBytes.toDouble(), - (o) => o.secondsSinceLastRx.toDouble(), (o) => o.isFlood ? 1.0 : 0.0, ]; diff --git a/test/services/message_retry_watchdog_test.dart b/test/services/message_retry_watchdog_test.dart index d111182c..9b0f29ec 100644 --- a/test/services/message_retry_watchdog_test.dart +++ b/test/services/message_retry_watchdog_test.dart @@ -5,12 +5,12 @@ import 'package:meshcore_open/models/contact.dart'; import 'package:meshcore_open/models/message.dart'; import 'package:meshcore_open/services/message_retry_service.dart'; -Contact _contact() => Contact( +Contact _contact({int pathLength = -1, List path = const []}) => Contact( publicKey: Uint8List.fromList(List.generate(32, (i) => 0xAA + i)), name: 'Test', type: 1, - pathLength: -1, - path: Uint8List(0), + pathLength: pathLength, + path: Uint8List.fromList(path), lastSeen: DateTime.now(), ); @@ -19,6 +19,7 @@ void main() { late Map latest; late List<({int attempt, int timestamp})> sends; late int timeoutMs; + late List<({int pathLength, int tripTimeMs})> observed; final selfKey = Uint8List(32); setUp(() { @@ -28,6 +29,7 @@ void main() { latest = {}; sends = []; timeoutMs = 20; + observed = []; service.initialize( RetryServiceConfig( sendMessage: (_, _, attempt, timestamp) async { @@ -38,6 +40,8 @@ void main() { getSelfPublicKey: () => selfKey, calculateTimeout: (_, _, {String? contactKey, int? deviceTimeoutMs}) => timeoutMs, + onDeliveryObserved: (_, pathLength, _, tripTimeMs) => + observed.add((pathLength: pathLength, tripTimeMs: tripTimeMs)), ), ); }); @@ -92,4 +96,21 @@ void main() { await Future.delayed(const Duration(milliseconds: 1100)); expect(sends, hasLength(1)); }); + + test('late ACK is observed with the path of the attempt it acks', () async { + final direct = _contact(pathLength: 2, path: [0x11, 0x22]); + await service.sendMessageWithRetry(contact: direct, text: 'hello'); + await pumpEventQueue(); + + // Attempt 0 times out; the retry then goes out as a flood. + await Future.delayed(const Duration(milliseconds: 100)); + service.updatePendingContact(_contact()); + await Future.delayed(const Duration(milliseconds: 1100)); + expect(sends.map((s) => s.attempt), [0, 1]); + + service.handleAckReceived(ackHashFor(0), 700); + + expect(latest.values.single.status, MessageStatus.delivered); + expect(observed, [(pathLength: 2, tripTimeMs: 700)]); + }); } diff --git a/test/services/path_history_service_test.dart b/test/services/path_history_service_test.dart index d8e3f077..1e16c53a 100644 --- a/test/services/path_history_service_test.dart +++ b/test/services/path_history_service_test.dart @@ -602,6 +602,33 @@ void main() { // Group 4: handlePathUpdated // ------------------------------------------------------------------------- group('handlePathUpdated', () { + test('full history evicts the weakest path for a new one', () async { + final pubKey = _hex('full'); + for (int i = 0; i < 100; i++) { + svc.handlePathUpdated( + _makeContact(publicKeyHex: pubKey, pathLength: 2, path: [i, 0xEE]), + ); + await _flush(); + } + const weak = PathSelection( + pathBytes: [7, 0xEE], + hopCount: 2, + useFlood: false, + ); + svc.recordPathResult(pubKey, weak, success: false); + svc.recordPathResult(pubKey, weak, success: false); + + svc.handlePathUpdated( + _makeContact(publicKeyHex: pubKey, pathLength: 2, path: [0xFF, 0xEE]), + ); + await _flush(); + + final paths = svc.getRecentPaths(pubKey).map((p) => p.pathBytes); + expect(paths, hasLength(100)); + expect(paths, contains(equals([0xFF, 0xEE]))); + expect(paths, isNot(contains(equals([7, 0xEE])))); + }); + test( 'pathLength >= 0 with path bytes → records path using pathLength', () async { diff --git a/test/services/timeout_prediction_service_test.dart b/test/services/timeout_prediction_service_test.dart index dbd852d8..58880c0e 100644 --- a/test/services/timeout_prediction_service_test.dart +++ b/test/services/timeout_prediction_service_test.dart @@ -156,7 +156,6 @@ DeliveryObservation _obs({ contactKey: contactKey, pathLength: pathLength, messageBytes: messageBytes, - secondsSinceLastRx: 5, isFlood: pathLength < 0, deliveryMs: deliveryMs, timestamp: DateTime.now(),