fix(#763): close three gaps the pre-PR review found in the forced resync

- A contact the radio answered "not found" for, then held again (an
  advert or contact frame mid-recovery), was still removed. Any frame for
  a key now clears it from the gone set and counts it as delivered.
- A link that never went quiet postponed recovery forever, so a short
  sync never reached the banner: the silent failure #668 exists to
  prevent. After 2 minutes of waiting, recovery stops sending and goes
  straight to the owner's decision.
- The by-key checks judged completeness against the declared total from
  when they started; a refresh during them can change it. They now read
  the latest one.

Also: dismissing the banner is a no-op while the decision is open (the
modal already covers it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
feat/703-forced-resync
diffwireauto 2 months ago
parent a252adb62d
commit b4ae00c79a

@ -476,7 +476,10 @@ class MeshCoreConnector extends ChangeNotifier {
String? _recoveryRadioKey;
final Set<String> _recoveryDelivered = {};
final Set<String> _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<void> _checkMissingContactsByKey(int run, int? declared) async {
final missing = _contacts
.where((c) => !_recoveryDelivered.contains(c.publicKeyHex))
.map((c) => c.publicKey)
.toList();
Future<void> _checkMissingContactsByKey(
int run, {
bool checkKeys = true,
}) async {
final missing = !checkKeys
? const <Uint8List>[]
: _contacts
.where((c) => !_recoveryDelivered.contains(c.publicKeyHex))
.map((c) => c.publicKey)
.toList();
_recoveryCheckingKeys = true;
for (final key in missing) {
await Future<void>.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<String>.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}',

@ -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<void>.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<int>.filled(32, 0x11)]),
);
await Future<void>.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<void>.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;

Loading…
Cancel
Save

Powered by TurnKey Linux.