Skip to content

Address the #628 review: baseline, coverage, clock alignment, accounting, toggle, re-derive - #24

Open
kurexw wants to merge 16 commits into
BucciMobile:feat/sleep-stage-plausibilityfrom
kurexw:fix/628-review
Open

kurexw wants to merge 16 commits into
BucciMobile:feat/sleep-stage-plausibilityfrom
kurexw:fix/628-review

Conversation

@kurexw

@kurexw kurexw commented Nov 15, 2026

Copy link
Copy Markdown

Summary

Fixes for abdulsaheel's review of OpenStrap#628 (comment), ready to merge into feat/sleep-stage-plausibility. It also merges current main, which OpenStrap#628 needed.

# Review point What changed
1 No baseline With no plausible HR or RMSSD in the first 120 min, _classifySleepStagesHMM returns null and the shipped labels stay.
2 Mixed-source nights withHypnoStages recomputes the accounting from the final labels using segmentSleep's own definitions: observed seconds only, WASO between first and last sleep, efficiency_pct over observed in-bed time. The stager's interval and awakening fields, which edge doesn't recompute, are dropped instead of describing the old labels. The night carries sleep.stager_source: 'hmm'.
3 Toggle setSleepHmmStages moves the field only after setBool returns true, and returns whether it saved.
4 Sparse rows Observations are placed by clock second (label i is sleepOnsetSec + i). Before, a night with any gap in the substrate failed the length == check and was silently never re-staged. A night whose lengths happened to match would have been misaligned.
5 Coverage A 30 s epoch with no plausible HR or no RMSSD keeps its shipped label instead of taking the decoder's guess.
6 Cache After a saved flip, the same forced re-derive a sleep correction uses (_reanalyzeForOverride) runs over every day still on disk.
7 log(0) REM → deep is now double.negativeInfinity, commented as an intentional hard forbid. A decode with no finite path returns nothing, so the caller keeps the shipped labels.

With the switch off, nothing changes: stager_source is only written on a night the HMM actually staged, so default users' stored nights are byte-for-byte as before. No kAlgoVersion bump is needed for that path.

Testing

Six new tests in test/sleep_cycle_continuity_test.dart (PR #628 review group):

  • Missing baseline keeps the labels. Fails on the current branch head.
  • A 20-minute substrate gap is re-staged, is placed by clock, and its epochs abstain. Fails on the current branch head.
  • Accounting comes from the new labels: efficiency < 100 % and equal to tst / observed, tagged hmm. Fails on the current branch head.
  • A refused pref write leaves the switch off. Fails on the current branch head.
  • With the switch off the bundle is unchanged (guard).
  • No NaN or infinite scores, and no REM → deep (guard).

Also run:

  • The whole file (12 tests).
  • 433 related tests: whoop_freeze_golden_test, the pipeline, sleep and settings suites, settings_audit_guard_test, ui2_gallery_test. All pass.
  • flutter analyze: no issues in tracked files.

Risks and rollback

  • Point 6 only reaches days whose raw data is still on disk (~5 days). Older finalized nights keep whichever stager produced them. stager_source makes that visible, but it can't be undone without raw data. Stopping it would need the HMM to write to a separate series rather than the immutable day_result.
  • The flip re-derive sets reanalyzing while it runs, so the Re-analyze button shows its spinner during that pass.
  • Revert this PR to return the branch to its current head.

Checklist

  • Each review point covered, with a test where it's testable without a device
  • Switch-off output unchanged (goldens pass)
  • No metric fabricated: every abstention keeps the shipped stager's labels, never a guess

🤖 Generated with Claude Code

uvtqt and others added 16 commits November 14, 2026 23:28
…late in the evening after a DST change

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…urve can be reached

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…day when a step can't be read, read the day list beside the strain

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… even when it overtook a step still loading

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…at 0, so naps no longer read "Usual range 47m–25m"

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…history happens to be

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…closes the database

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
test: step fixture days by calendar day so two tests pass late in the evening after a DST change
test: wait for a screen's first load by time, not by poll count
…rdown

test: wait for a background reconnect's derive write before tearDown closes the database
Usual range: start a never-negative metric's band at 0, so naps no longer read "Usual range 47m–25m"
Day strain: add the day stepper, so an earlier day's strain-by-time curve can be reached
- No baseline: when the first 120 min hold no plausible HR or RMSSD the
  decoder abstains and the shipped labels stay, instead of every
  emission going neutral and a measured night decoding as wake.
- Coverage: a 30 s epoch with no plausible HR or no RMSSD keeps its
  shipped label rather than taking the decoder's guess.
- Sparse rows: observations are placed by clock second over the window
  (second i is sleepOnsetSec + i). Pairing rows by index skipped every
  night with a gap in the substrate, and would misalign one whose
  lengths happened to match.
- Mixed-source nights: the accounting is recomputed from the final
  labels by segmentSleep's own definitions (observed seconds only, WASO
  between first and last sleep, efficiency over observed in-bed time);
  figures edge does not recompute are dropped rather than left
  describing the old labels. The night is tagged stager_source: hmm.
  A shipped-stager night is stored exactly as before.
- Toggle: the field moves only once the pref write is accepted, then
  every day still on disk is re-derived through the same forced pass a
  sleep correction uses.
- REM -> deep is an explicit hard forbid (-infinity, commented), and a
  decode with no finite path returns nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 15, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants