From 1ec41ff7611bc32ef74b49bc86e2d03379e0fd3b Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Thu, 3 Dec 2026 22:32:06 +0530 Subject: [PATCH] counter regression: track per record type, v26 and 1hz runs were tripping each other --- lib/ble/ble_engine.dart | 2 +- lib/ble/ble_state.dart | 16 ++++++++++------ test/ble_state_test.dart | 13 +++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/ble/ble_engine.dart b/lib/ble/ble_engine.dart index 49908bf458..67fc7c04fe 100644 --- a/lib/ble/ble_engine.dart +++ b/lib/ble/ble_engine.dart @@ -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}. ' diff --git a/lib/ble/ble_state.dart b/lib/ble/ble_state.dart index 8257f8ec6b..9ce6a76997 100644 --- a/lib/ble/ble_state.dart +++ b/lib/ble/ble_state.dart @@ -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 _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. @@ -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(); } } diff --git a/test/ble_state_test.dart b/test/ble_state_test.dart index 035775370e..6fc36f97ba 100644 --- a/test/ble_state_test.dart +++ b/test/ble_state_test.dart @@ -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);