From 7ca4b78cfa5bcf7689bc51187f33b4f93c6b29fc Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Fri, 20 Nov 2026 10:13:26 +0530 Subject: [PATCH] smart wake: late-window protection can actually veto now --- lib/state/smart_wake.dart | 19 +++++++++++++------ test/smart_wake_v2_test.dart | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/lib/state/smart_wake.dart b/lib/state/smart_wake.dart index 9bc864036..4fa7d6173 100644 --- a/lib/state/smart_wake.dart +++ b/lib/state/smart_wake.dart @@ -292,8 +292,13 @@ bool enmoTrendExceeded( // ── lateWindowProtectionActive (Q3 — deep-sleep protection near window end) ── /// True when the late-window deep-sleep protection is active: we are within -/// [protectionMinutes] of [windowEnd] AND the current HR is still within -/// 2 bpm of [baselineHr] (i.e., still clearly in deep sleep). +/// [protectionMinutes] of [windowEnd] AND the current HR is no more than +/// 2 bpm past the arousal bar ([baselineHr] + [arousalBpm]), i.e. the +/// light-sleep call is only marginal. +/// +/// The margin sits on top of [arousalBpm] on purpose: the arousal gate in +/// [likelyLightSleep] already refuses anything below baseline + arousal, so +/// a margin measured from the bare baseline could never veto a buzz. /// /// When active, the caller must NOT buzz early — the fallback SET_ALARM at /// [windowEnd] fires regardless (Q3: "der letzte Wecker muss funktionieren"). @@ -310,14 +315,15 @@ bool lateWindowProtectionActive({ required int protectionMinutes, required double currentHr, required double baselineHr, + double arousalBpm = 0.0, }) { if (protectionMinutes <= 0) return false; final msBeforeEnd = windowEnd.difference(now).inMilliseconds; if (msBeforeEnd <= 0) return false; // at/past window end — window check handles if (msBeforeEnd > protectionMinutes * 60 * 1000) return false; // not yet in zone - // Within the protection zone: active only if HR is still near baseline - // (within 2 bpm — still clearly deep sleep). - return (currentHr - baselineHr) <= 2.0; + // Within the protection zone: active unless HR clears the arousal bar by + // more than 2 bpm. + return (currentHr - baselineHr) <= arousalBpm + 2.0; } // ── percentileHr (10th-percentile nightly HR baseline) ─────────────────────── @@ -432,7 +438,7 @@ bool likelyLightSleep({ final recentMotion = _mean([for (final s in hrWindow) enmo(s)]); // Q3: late-window deep-sleep protection — if we're in the last few minutes - // AND HR is still near baseline, don't buzz. Fallback fires at window end. + // AND HR only just clears the arousal bar, don't buzz. Fallback fires at window end. // The veto compares against the NIGHTLY baseline (baseHr), not the // caller-passed [baselineHr] — with the live caller those used to be the same // value, which made protection unable to veto a positive detection. @@ -443,6 +449,7 @@ bool likelyLightSleep({ protectionMinutes: profile.lateWindowProtectionMinutes, currentHr: recentHr, baselineHr: baseHr, + arousalBpm: profile.hrArousalBpm, )) { return false; } diff --git a/test/smart_wake_v2_test.dart b/test/smart_wake_v2_test.dart index ef5aa6a6d..ad4cbb20f 100644 --- a/test/smart_wake_v2_test.dart +++ b/test/smart_wake_v2_test.dart @@ -473,9 +473,8 @@ void main() { likelyLightSleep(baseline: baseline, recent: recentElevated), isTrue, ); - // With late-window protection active (HR 53 vs baseline 48 = +5, but - // protection checks if HR is "near baseline" — within 2 bpm): - // 53 is NOT within 2 bpm of 48 → protection inactive → still true + // In the last 5 min a +5 call is only 1 bpm past the 4 bpm arousal bar, + // so protection holds it back. expect( likelyLightSleep( baseline: baseline, @@ -484,7 +483,7 @@ void main() { now: nowLate, baselineHr: 48, ), - isTrue, // HR too elevated for protection to apply + isFalse, // marginal call near window end ); // But if HR is truly near baseline (49 = +1 bpm): final recentNearBaseline = still(49, 60); @@ -667,6 +666,33 @@ void main() { expect(run(end.subtract(const Duration(minutes: 30))), isTrue); expect(run(end.subtract(const Duration(minutes: 2))), isFalse); }); + + test('vetoes a marginal buzz under the default arousal bar', () { + final end = DateTime(2026, 6, 10, 7, 30); + bool run(double hr, DateTime now, + [ConfounderProfile p = ConfounderProfile.identity]) => + likelyLightSleep( + baseline: still(48, 90), + recent: still(hr, 60), + profile: p, + windowEnd: end, + now: now, + ); + final mid = end.subtract(const Duration(minutes: 30)); + final late = end.subtract(const Duration(minutes: 2)); + // +5 bpm clears the 4 bpm bar: buzzes mid-window, held back late. + expect(run(53, mid), isTrue); + expect(run(53, late), isFalse); + // +7 bpm is a clear call and still buzzes late. + expect(run(55, late), isTrue); + // Workout day: 7 min before end is inside the 10-min zone only. + final seven = end.subtract(const Duration(minutes: 7)); + expect(run(53, seven), isTrue); + expect( + run(53, seven, + const ConfounderProfile(lateWindowProtectionMinutes: 10)), + isFalse); + }); }); group('a late meal does not loosen the movement veto', () {