feat: v42 personalized self-improving cardio stager - #25
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThe advanced sleep detector now preserves short overnight continuations. The cardio stager adds persistent user profiles, blended thresholds, autonomic REM features, revised deep detection, nightly observation capture, and associated tests. ChangesAdvanced sleep tail continuation
Profile-aware cardio staging
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant SleepInput
participant cardioStager
participant SleepUserProfile
participant ObservationBuffer
SleepInput->>cardioStager: epoch HR, RR, and motion data
cardioStager->>SleepUserProfile: read personalized thresholds
SleepUserProfile-->>cardioStager: blended baseline values
cardioStager->>cardioStager: classify REM and deep epochs
cardioStager->>ObservationBuffer: record qualifying nightly observation
cardioStager-->>SleepInput: processed sleep stages
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
lib/src/onehz/sleep/advanced_stager.dart (1)
305-358: 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy liftBound the night-tail exemption to the first post-overnight fragment.
chainFromOvernightstays true for everycontinuesChainhop once the chain starts from an overnight onset, so the new min-session bypass applies to each later sub-60-min fragment within 90 min of the previous accepted end. That can admit a long sequence of short daytime sessions, not just the first tail after sleep onset.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/src/onehz/sleep/advanced_stager.dart` around lines 305 - 358, Limit the night-tail min-session exemption in the session loop around chainFromOvernight and isNightTail to only the first post-overnight fragment. Track whether a qualifying night-tail has already been accepted, and require that state to be unset when computing the exemption; preserve normal chain tracking while preventing subsequent continuesChain hops from bypassing the minimum session floor.
🧹 Nitpick comments (1)
test/onehz/sleep_user_profile_test.dart (1)
108-113: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winMake the stability assertion discriminating.
Given both Wake percentages must already be below 20, allowing their difference to be below 50 adds no coverage. Assert that the profile result remains close to the unprofiled result.
expect( (withProfile.wakePct - noProfile.wakePct).abs(), - lessThan(50.0), + lessThan(5.0), reason: 'a consistent profile must not swing staging wildly', );🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/onehz/sleep_user_profile_test.dart` around lines 108 - 113, Update the stability assertion comparing withProfile.wakePct and noProfile.wakePct to use a meaningfully tighter tolerance that verifies the profile result remains close to the unprofiled result. Keep the existing absolute-difference comparison and reason, but replace the non-discriminating lessThan(50.0) threshold with the intended close-result bound.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/src/onehz/sleep/cardio_stager.dart`:
- Around line 168-172: Validate horizonNights at the start of
SleepUserProfile.fold before calculating a, rejecting values less than or equal
to zero with the established argument-validation behavior. Preserve the existing
EWMA calculation for positive horizons.
- Around line 514-517: Update the post-processing flow around _mergeShortDeep so
deepFlag is synchronized with the final consolidated stage: clear the
corresponding deep flag whenever consolidateSleepStages changes an epoch from
deep NREM to REM or Wake, rather than relying on _mergeShortDeep to skip it.
Preserve deep flags for epochs that remain eligible deep NREM stages.
- Around line 728-729: Update the loHz initialization in the cardio staging flow
to explicitly convert the clamped value returned by clamp() to double before
passing it to freqGrid. Keep the existing clamp bounds and lombScargle behavior
unchanged.
---
Outside diff comments:
In `@lib/src/onehz/sleep/advanced_stager.dart`:
- Around line 305-358: Limit the night-tail min-session exemption in the session
loop around chainFromOvernight and isNightTail to only the first post-overnight
fragment. Track whether a qualifying night-tail has already been accepted, and
require that state to be unset when computing the exemption; preserve normal
chain tracking while preventing subsequent continuesChain hops from bypassing
the minimum session floor.
---
Nitpick comments:
In `@test/onehz/sleep_user_profile_test.dart`:
- Around line 108-113: Update the stability assertion comparing
withProfile.wakePct and noProfile.wakePct to use a meaningfully tighter
tolerance that verifies the profile result remains close to the unprofiled
result. Keep the existing absolute-difference comparison and reason, but replace
the non-discriminating lessThan(50.0) threshold with the intended close-result
bound.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 77dbea46-21e3-461e-94a2-34598ed1e331
📒 Files selected for processing (3)
lib/src/onehz/sleep/advanced_stager.dartlib/src/onehz/sleep/cardio_stager.darttest/onehz/sleep_user_profile_test.dart
- bound the night-tail 60-min-floor exemption to the first SHORT tail per chain: a >60-min continuation is accepted on its own merit and no longer burns the one-shot, so a run of short fragments can't chain-extend the window while a genuine multi-fragment pre-dawn tail still reaches the wake - validate horizonNights > 0 in SleepUserProfile.fold before the EWMA alpha - sync deepFlag with the final consolidated stage (clear the deep flag on epochs reclassified to REM/Wake) rather than relying on _mergeShortDeep - cast the LF/HF loHz clamp() result to double before freqGrid - tighten the profile-stability assertion (50.0 -> 5.0)
This pull request introduces major improvements to the sleep staging algorithm, focusing on REM detection accuracy and user personalization. The changes add new autonomic features for REM detection, implement a rolling per-user sleep profile for personalized threshold blending, and enhance post-processing to reduce stage flicker. The updates are designed to improve sensitivity to REM without sacrificing specificity and to make the algorithm adapt over time to each user's sleep patterns.
Key changes include:
REM Detection Improvements
_windowRemFeaturesfunction to extract LF/HF and R(k) features from RR intervals using Lomb–Scargle PSD and instantaneous HR variability.Personalization (Per-User Sleep Profile)
SleepUserProfileclass to store and blend rolling per-user sleep baselines (e.g., HR, RMSSD, motion thresholds) with per-night-local values, improving adaptation to individual sleep patterns. [1] [2] [3] [4]SleepNightObservation), fold them into the user profile, and blend profile values into nightly thresholds, with a capped influence to preserve local adaptation. [1] [2] [3]Post-processing Enhancements
Other Algorithmic Refinements
AdvancedSleepStagerto exempt night-tail continuations from the 60-min minimum, ensuring genuine fragmented sleep is not truncated.These updates significantly improve both the accuracy and personalization of sleep staging, especially for REM and deep sleep detection.
Summary by CodeRabbit
New Features
Bug Fixes
Tests