/// Poll-based hysteresis and bounded retry delay, independent of processes/UI. class UsbRecoveryPolicy { int missing = 0, attempts = 0, stable = 0; DateTime retryAt = DateTime.fromMillisecondsSinceEpoch(0); bool due( DateTime now, { required bool ready, bool unauthorized = false, bool broken = false, }) { if (unauthorized) { missing = 0; stable = 0; return false; } if (ready && !broken) { missing = 0; if (++stable >= 10) { attempts = 0; } return false; } stable = 0; missing++; return (broken || missing >= 3) && !now.isBefore(retryAt); } void attempted(DateTime now) { final seconds = [2, 4, 8, 16, 30][attempts.clamp(0, 4)]; attempts++; missing = 0; retryAt = now.add(Duration(seconds: seconds)); } }