diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 17f60520..070fbf8a 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -476,7 +476,10 @@ class MeshCoreConnector extends ChangeNotifier { String? _recoveryRadioKey; final Set _recoveryDelivered = {}; final Set _recoveryGone = {}; + int? _recoveryDeclared; + DateTime? _recoveryWaitingSince; bool _recoveryCheckingKeys = false; + static const Duration contactRecoveryMaxWait = Duration(minutes: 2); Timer? _recoverySettleTimer; DateTime _lastRadioFrameAt = DateTime.fromMillisecondsSinceEpoch(0); ContactSyncShortfall? _contactSyncShortfall; @@ -3193,6 +3196,8 @@ class MeshCoreConnector extends ChangeNotifier { _recoveryRadioKey = null; _recoveryDelivered.clear(); _recoveryGone.clear(); + _recoveryDeclared = null; + _recoveryWaitingSince = null; _recoveryCheckingKeys = false; _recoverySettleTimer?.cancel(); _recoverySettleTimer = null; @@ -3934,6 +3939,7 @@ class MeshCoreConnector extends ChangeNotifier { _recoveryGone.clear(); } _recoveryDelivered.addAll(_contactSyncDeliveredKeys); + if (declared != null) _recoveryDeclared = declared; if (isContactSyncComplete(declared: declared, received: received)) { final before = _contacts.length; _contacts.removeWhere( @@ -3970,11 +3976,12 @@ class MeshCoreConnector extends ChangeNotifier { _scheduleRecoveryStream(_recoveryRun); return; } - unawaited(_checkMissingContactsByKey(_recoveryRun, declared)); + unawaited(_checkMissingContactsByKey(_recoveryRun)); } void _scheduleRecoveryStream(int run) { _recoverySettleTimer?.cancel(); + _recoveryWaitingSince = contactSyncClock(); _recoverySettleTimer = Timer( contactRecoverySettle, () => _runRecoveryStreamWhenSettled(run), @@ -3992,6 +3999,20 @@ class MeshCoreConnector extends ChangeNotifier { isSyncingQueuedMessages; final quietFor = contactSyncClock().difference(_lastRadioFrameAt); if (busy || quietFor < contactRecoverySettle) { + final waited = contactSyncClock().difference( + _recoveryWaitingSince ?? contactSyncClock(), + ); + if (waited >= contactRecoveryMaxWait) { + // Never let a busy link hide a short sync: stop waiting and let the + // owner decide, without sending anything more. (Gemini review) + _appDebugLogService?.warn( + 'Contact recovery gave up waiting for a quiet link after ' + '${waited.inSeconds}s', + tag: 'ContactSync', + ); + unawaited(_checkMissingContactsByKey(run, checkKeys: false)); + return; + } final wait = busy ? contactRecoverySettle : contactRecoverySettle - quietFor; @@ -4020,11 +4041,16 @@ class MeshCoreConnector extends ChangeNotifier { /// apart, about each saved contact no sync delivered. Removes only the ones /// it answers "not found" for, and only when every declared contact has /// been accounted for; otherwise the owner decides. - Future _checkMissingContactsByKey(int run, int? declared) async { - final missing = _contacts - .where((c) => !_recoveryDelivered.contains(c.publicKeyHex)) - .map((c) => c.publicKey) - .toList(); + Future _checkMissingContactsByKey( + int run, { + bool checkKeys = true, + }) async { + final missing = !checkKeys + ? const [] + : _contacts + .where((c) => !_recoveryDelivered.contains(c.publicKeyHex)) + .map((c) => c.publicKey) + .toList(); _recoveryCheckingKeys = true; for (final key in missing) { await Future.delayed(contactKeyCheckGap); @@ -4040,6 +4066,8 @@ class MeshCoreConnector extends ChangeNotifier { } _recoveryCheckingKeys = false; + // The latest total: a refresh during the checks may have changed it. + final declared = _recoveryDeclared; final accounted = _recoveryDelivered.length; if (declared != null && accounted >= declared) { final gone = Set.of(_recoveryGone); @@ -4100,6 +4128,7 @@ class MeshCoreConnector extends ChangeNotifier { } void dismissContactSyncShortfall() { + if (_contactSyncDecisionPending) return; _contactSyncShortfall = null; notifyListeners(); } @@ -6851,6 +6880,12 @@ class MeshCoreConnector extends ChangeNotifier { _contactSyncDeliveredKeys.add(contactTmp.publicKeyHex); _contactRequestLastActivity = contactSyncClock(); } + // Any frame for a key proves the radio holds it now, so recovery must + // not remove it even if an earlier check said "not found". + if (_recoveryRadioKey != null) { + _recoveryGone.remove(contactTmp.publicKeyHex); + _recoveryDelivered.add(contactTmp.publicKeyHex); + } if (listEquals(contactTmp.publicKey, _selfPublicKey)) { appLogger.info( 'Ignoring contact with self public key: ${contactTmp.name}', diff --git a/test/connector/contact_sync_test.dart b/test/connector/contact_sync_test.dart index 6833b1e0..867230cf 100644 --- a/test/connector/contact_sync_test.dart +++ b/test/connector/contact_sync_test.dart @@ -357,6 +357,81 @@ void main() { ); }); + test('a contact the radio holds again after "not found" is kept', () async { + seedSaved(); + connector.contactsForTest + ..add(_saved(0x44, 'Delta')) + ..add(_saved(0x55, 'Echo')); + await connector.getContacts(); + await answerStream(3, [ + _contactFrame(0x11, 'Alpha'), + _contactFrame(0x22, 'Bravo'), + ]); + await answerStream(3, [_contactFrame(0x33, 'Charlie')]); + connector.handleFrameForTest( + Uint8List.fromList([respCodeErr, errCodeNotFound]), + ); + await settle(); + connector.handleFrameForTest( + _contactFrame(0x44, 'Delta', code: pushCodeNewAdvert), + ); + await Future.delayed(const Duration(milliseconds: 100)); + + expect(names(), contains('Delta')); + }); + + test('a busy link cannot hide a short sync', () async { + var now = DateTime.utc(2026, 10, 2, 6); + connector.contactSyncClock = () => now; + connector.contactRecoverySettle = const Duration(milliseconds: 10); + seedSaved(); + await fullSync(declared: 350, streamed: [_contactFrame(0x11, 'Alpha')]); + now = now.add(MeshCoreConnector.contactRecoveryMaxWait); + connector.handleFrameForTest( + Uint8List.fromList([pushCodeAdvert, ...List.filled(32, 0x11)]), + ); + await Future.delayed(const Duration(milliseconds: 1100)); + + expect(contactRequests(), 1, reason: 'no sync onto a busy link'); + expect(byKeyRequests(), 0); + expect(connector.contactSyncDecisionPending, isTrue); + expect(names(), {'Alpha', 'Bravo', 'Charlie'}); + }); + + test('the latest declared total decides the result', () async { + connector.contactKeyCheckTimeout = const Duration(milliseconds: 100); + seedSaved(); + final alpha = [_contactFrame(0x11, 'Alpha')]; + await connector.getContacts(); + await answerStream(350, alpha); + await answerStream(350, alpha); + await answerStream(350, alpha); + // By-key checks are running; a refresh reports a new total of 2, and + // Charlie turns up while the checks are still going. + await connector.getContacts(); + await answerStream(2, alpha); + connector.handleFrameForTest(_contactFrame(0x33, 'Charlie')); + await Future.delayed(const Duration(milliseconds: 400)); + + expect(connector.contactSyncShortfall, isNull); + expect(connector.contactSyncDecisionPending, isFalse); + expect(names(), {'Alpha', 'Bravo', 'Charlie'}); + }); + + test('the banner cannot be dismissed while a decision is open', () async { + await threeShortSyncs(); + connector.dismissContactSyncShortfall(); + + expect(connector.contactSyncShortfall, isNotNull); + }); + + test('the give-up wait is pinned', () { + expect( + MeshCoreConnector.contactRecoveryMaxWait, + const Duration(minutes: 2), + ); + }); + test('recovery waits for the radio to go quiet', () async { var now = DateTime.utc(2026, 10, 2, 6); connector.contactSyncClock = () => now;