Skip to content
Merged
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
19 changes: 13 additions & 6 deletions lib/state/smart_wake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand All @@ -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) ───────────────────────
Expand Down Expand Up @@ -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.
Expand All @@ -443,6 +449,7 @@ bool likelyLightSleep({
protectionMinutes: profile.lateWindowProtectionMinutes,
currentHr: recentHr,
baselineHr: baseHr,
arousalBpm: profile.hrArousalBpm,
)) {
return false;
}
Expand Down
34 changes: 30 additions & 4 deletions test/smart_wake_v2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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', () {
Expand Down
Loading