Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ble/ble_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5897,7 +5897,7 @@ class BleEngine {
// Explicit, observable band-reboot signal — see CounterRegressionDetector.
// 0 is _counterFromInner's fallback for a too-short frame, not a real
// counter value, so it's excluded to avoid a false regression report.
if (counter > 0 && _counterRegression.feed(counter)) {
if (counter > 0 && _counterRegression.feed(counter, kind: recType)) {
_log(
'[SYNC] Record counter regressed (band likely rebooted): '
'counter=$counter, regressions_total=${_counterRegression.regressions}. '
Expand Down
16 changes: 10 additions & 6 deletions lib/ble/ble_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -643,21 +643,24 @@ class RecordGate {
/// regression is caught even across the reconnect that a reboot itself
/// usually causes — the two events are correlated, not sequential.
class CounterRegressionDetector {
int? _lastCounter;
int? _seed;
// Last counter per record type: v26 PPG and the 1 Hz records interleave in
// one burst with their own runs, so one shared "last" flags every record.
final Map<int, int> _last = {};

/// Regressions observed since construction (never reset by [reset] — reset
/// only clears the last-seen counter for reseeding at a fresh connect).
int regressions = 0;

CounterRegressionDetector({int? seedCounter}) : _lastCounter = seedCounter;
CounterRegressionDetector({int? seedCounter}) : _seed = seedCounter;

/// Feed the next record's raw hardware counter (u32, may wrap on a
/// sufficiently long-running band). Returns true exactly when this counter
/// is a genuine regression against the previous one (not benign u32
/// wraparound near the top of the range).
bool feed(int counter) {
final prev = _lastCounter;
_lastCounter = counter;
bool feed(int counter, {int kind = 0}) {
final prev = _last[kind] ?? _seed;
_last[kind] = counter;
if (prev == null || counter >= prev) return false;
// Wraparound guard: prev near the top of u32, counter near 0 is normal
// roll-over on an extremely long-running band, not a reboot.
Expand All @@ -670,7 +673,8 @@ class CounterRegressionDetector {
/// Re-seed for a fresh connection (does not clear the lifetime [regressions]
/// count — that's diagnostics across the engine's lifetime).
void reseed(int? seedCounter) {
_lastCounter = seedCounter;
_seed = seedCounter;
_last.clear();
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/ble_state_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,19 @@ void main() {
expect(d.regressions, 1);
});

test('interleaved record types each keep their own run (no false reboot)',
() {
final d = CounterRegressionDetector();
// 1 Hz run (type 24) at 20725849.. interleaved with a v26 run (type 26)
// that sits lower; one shared "last" would flag every record.
for (var i = 0; i < 5; i++) {
expect(d.feed(20725900 + i, kind: 24), isFalse);
expect(d.feed(20725849 + i, kind: 26), isFalse);
}
expect(d.regressions, 0);
expect(d.feed(3, kind: 26), isTrue); // a real drop still trips
});

test('first feed after construction with no seed never trips', () {
final d = CounterRegressionDetector();
expect(d.feed(0), isFalse);
Expand Down
Loading