Skip to content

smart wake: late-window protection can actually veto - #826

Merged
abdulsaheel merged 1 commit into
mainfrom
fix/auto-smart-wake-late-window
Nov 20, 2026
Merged

abdulsaheel merged 1 commit into
mainfrom
fix/auto-smart-wake-late-window

Conversation

@abdulsaheel

@abdulsaheel abdulsaheel commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

User description

the late-window deep-sleep protection only vetoed when hr was within 2 bpm of baseline, but the arousal gate right after it already rejects anything under baseline + 4, so it never blocked a single buzz and the 5/10 min windows did nothing.

now the 2 bpm margin sits on top of the arousal bar: in the last few minutes a call that only just clears the bar is held back and the band's own alarm fires at window end. clear calls still buzz. updated the old test that pinned the broken behaviour and added one for the default profile + workout-day 10 min zone.

Summary by Sourcery

Make late-window deep-sleep protection veto marginal buzzes while preserving clear arousal alerts.

Bug Fixes:

  • Fix late-window deep-sleep protection so marginal arousal calls can veto an early buzz.

Enhancements:

  • Apply late-window protection relative to the configured arousal threshold, while allowing clearly elevated calls to buzz.

Tests:

  • Update existing late-window protection expectations and add coverage for default and workout-day protection windows.

PR Type

Bug fix, Tests


Description

  • Fix late-window deep-sleep protection so it can actually veto marginal buzzes

  • Protection threshold now sits 2 bpm above the arousal bar, not 2 bpm above bare baseline

  • arousalBpm parameter added to lateWindowProtectionActive and wired from caller

  • Updated existing test expectation and added new tests for default and workout-day profiles


Diagram Walkthrough

flowchart LR
  A["likelyLightSleep()"] -- "passes profile.hrArousalBpm" --> B["lateWindowProtectionActive()"]
  B -- "currentHr <= baselineHr + arousalBpm + 2.0" --> C["veto: return false (no buzz)"]
  B -- "currentHr > baselineHr + arousalBpm + 2.0" --> D["clear call: allow buzz"]
Loading

File Walkthrough

Relevant files
Bug fix
smart_wake.dart
Anchor late-window veto above arousal bar, not bare baseline

lib/state/smart_wake.dart

  • Added arousalBpm parameter (default 0.0) to lateWindowProtectionActive
  • Changed veto condition from currentHr - baselineHr <= 2.0 to currentHr
    - baselineHr <= arousalBpm + 2.0
  • Updated doc comment to explain margin sits on top of the arousal bar
  • likelyLightSleep now passes profile.hrArousalBpm into
    lateWindowProtectionActive
+13/-6   
Tests
smart_wake_v2_test.dart
Update and extend late-window protection tests                     

test/smart_wake_v2_test.dart

  • Updated existing test expectation from isTrue to isFalse for a +5 bpm
    marginal call in the late window
  • Updated test comment to reflect new veto semantics
  • Added new test 'vetoes a marginal buzz under the default arousal bar'
    covering mid-window pass, late-window veto, clear-call pass, and
    workout-day 10-min zone
+30/-4   

Summary by CodeRabbit

  • Improvements
    • Late-window smart wake protection now accounts for the profile’s heart-rate arousal threshold. It remains active until heart rate rises more than that threshold plus 2 bpm above baseline, instead of relying on a fixed 2 bpm margin. This makes the protection behavior responsive to the configured threshold.

@sourcery-ai

sourcery-ai Bot commented Nov 20, 2026

Copy link
Copy Markdown

Sorry @abdulsaheel, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 40 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The late-window protection check now accepts an optional arousal threshold. likelyLightSleep passes the profile’s heart-rate arousal threshold to the check.

Changes: Smart-wake late-window protection

Layer / File(s) Summary
Apply the arousal threshold
lib/state/smart_wake.dart
lateWindowProtectionActive keeps protection active when heart rate is at most the baseline plus the arousal threshold plus 2 bpm. likelyLightSleep passes the profile’s threshold.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

  • OpenStrap/edge#630: Introduces the smart-wake v2 detector and its late-window protection logic, which this change updates.

Suggested reviewers: mistral-vibe

Merge Risk: Minimal · up to 7ca4b

Late-window deep-sleep protection can now veto marginal arousal alerts, as the PR intends. The change is small and localized, and I found no merge-blocking risk in it.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: late-window protection in smart wake can now veto arousal calls. The raw summary confirms the signature change and the veto logic in lib/state/smart_wake.dart. The tit…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: late-window protection in smart wake can now veto arousal calls. The raw summary confirms the signature change and the veto logic in lib/state/smart_wake.dart. The title is specific and matches the changeset, so it passes.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
Fix failing CI checks

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 7a9f3ed and 7ca4b78.

Files ignored due to path filters: 1

  • test/smart_wake_v2_test.dart is excluded by !test/**
Files selected for processing: 1

  • lib/state/smart_wake.dart

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

@sourcery-ai

sourcery-ai Bot commented Nov 20, 2026

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Move the late-window veto margin above the arousal threshold and pass the configured threshold into the protection check so marginal calls can be held back.

  • lib/state/smart_wake.dart L295-301 (+4 small edits)

Tests

Update the existing late-window expectation and add coverage for default and extended protection windows, including clear calls that should still buzz.

  • test/smart_wake_v2_test.dart L476-477, L486, L669-695

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions

Copy link
Copy Markdown
Contributor

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Missing return value

The old lateWindowProtectionActive returned (currentHr - baselineHr) <= 2.0 after the three early-return guards. The new code adds return (currentHr - baselineHr) <= arousalBpm + 2.0; inside the function body, but the diff shows the closing brace immediately after the new return statement. If arousalBpm defaults to 0.0 and the caller omits it, the new threshold is <= 2.0, which is identical to the old bare-baseline check and would still never veto a buzz that cleared the arousal gate. The fix only works when arousalBpm is explicitly passed. The call site in likelyLightSleep does pass profile.hrArousalBpm, so the live path is correct — but any direct caller of lateWindowProtectionActive that omits arousalBpm silently gets the broken old behaviour. The default of 0.0 makes the parameter appear optional when it is semantically required for the fix to have any effect.

  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 unless HR clears the arousal bar by
  // more than 2 bpm.
  return (currentHr - baselineHr) <= arousalBpm + 2.0;
}
Stale comment

The comment block at lines 442–444 still says "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." This explanation described the old bug (margin measured from bare baseline). After the fix the margin is measured from baseHr + arousalBpm, so the comment now misdescribes the mechanism and will mislead future readers about why the two baselines matter.

// 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.

@github-actions

Copy link
Copy Markdown
Contributor

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
Clarify threshold arithmetic to match stated semantics

When arousalBpm defaults to 0.0, the protection threshold becomes baselineHr + 2.0,
which is the same as the old behaviour. But when a caller passes a positive
arousalBpm (e.g. 4.0 from profile.hrArousalBpm), the veto only fires when HR is
within 2 bpm above the arousal bar — meaning a reading that already cleared the
arousal gate by up to 2 bpm is still vetoed. However, the call site in
likelyLightSleep only reaches lateWindowProtectionActive after likelyLightSleep has
already decided the arousal bar is cleared; a reading that is exactly at baselineHr
+ arousalBpm would be vetoed here but would not have triggered a buzz anyway. Verify
that the intended semantics are "veto if HR has not cleared baselineHr + arousalBpm
by more than 2 bpm", i.e. (currentHr - (baselineHr + arousalBpm)) <= 2.0, which is
equivalent to the current expression — but the test at line ~495 asserts run(55,
late) is true (HR = 55, baseline = 48, arousalBpm = 4 by default profile, so 55 - 48
= 7 > 4 + 2 = 6). Confirm ConfounderProfile.identity exposes hrArousalBpm = 4.0 so
the boundary at +6 is correct; if identity sets hrArousalBpm = 0.0 the threshold
collapses to +2 and run(55, late) would be vetoed (returning false), breaking the
new test.

lib/state/smart_wake.dart [326]

-  return (currentHr - baselineHr) <= arousalBpm + 2.0;
+  // Within the protection zone: active unless HR clears the arousal bar by
+  // more than 2 bpm.
+  return (currentHr - baselineHr - arousalBpm) <= 2.0;
Suggestion importance[1-10]: 2

__

Why: The expressions (currentHr - baselineHr) <= arousalBpm + 2.0 and (currentHr - baselineHr - arousalBpm) <= 2.0 are mathematically identical, so the improved_code produces no runtime behavior change. The suggestion is essentially a style/readability refactor, which should not score highly per the evaluation criteria.

Low

@abdulsaheel

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor
Action performed

Full review finished.

@abdulsaheel
abdulsaheel merged commit c3d73a2 into main Nov 20, 2026
5 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant