Repository navigation
sleep: defer a night still going at midnight to the day it ends #962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1330,6 +1330,9 @@ List<PhysioDay> calendarDays( | |
| // full set of sessions that can legitimately end today. | ||
| final searchStart = math.max(dataStart, dayStart - kNocturnalSearchLookbackSec); | ||
| final searchEnd = math.min(dataEnd, dayEnd); | ||
| // Data carries on past this day's midnight cut: a sleep still going at the | ||
| // cut belongs to the day that sees it end, not to this one. | ||
| final recordContinues = dataEnd > dayEnd; | ||
| final loS = _lowerBound(sub.tsSec, searchStart); | ||
| final hiS = _lowerBound(sub.tsSec, searchEnd); | ||
|
|
||
|
|
@@ -1426,6 +1429,7 @@ List<PhysioDay> calendarDays( | |
| // the auto night at the band's own last SLEEP (AUTO path only — | ||
| // never the override or the HR-led fallback below). | ||
| bandSleepState: sub.bandSleepStateSlice(loS, hiS), | ||
| recordContinues: recordContinues, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bump Both the merge base and reviewed revision use Increment the constant in Based on learnings, “Bump
|
||
| ); | ||
| } else { | ||
| // Not an error and not "no sleep" — just no accel evidence. Fall | ||
|
|
@@ -1437,13 +1441,18 @@ List<PhysioDay> calendarDays( | |
| s = ana.SleepSegmentation.absent; | ||
| } | ||
| src = 'auto'; | ||
| if (!s.present) { | ||
| // A deferred night is not a miss: the fallback would propose the same | ||
| // cut-off stretch. | ||
| if (!s.present && !s.unfinished) { | ||
| // Approach 2: accel-led detection found nothing → HR-led fallback. | ||
| // Propose the longest sustained nocturnal HR dip, then STAGE it via the | ||
| // forced-window path. Marked low-confidence for a "is this right?" prompt. | ||
| final tsSlice = [for (var i = loS; i < hiS; i++) sub.tsSec[i]]; | ||
| final cand = | ||
| ana.hrLedSleepWindow(hrSlice, tsSlice, hrBaseline: hrBaseline); | ||
| ana.hrLedSleepWindow(hrSlice, tsSlice, | ||
| hrBaseline: hrBaseline, | ||
| recordContinues: recordContinues, | ||
| tzOffsetResolver: tzOffset); | ||
| if (cand != null && | ||
| fallbackNightAcceptable(cand.onsetSec, cand.offsetSec, | ||
| tzOffsetSec: tzOffset(cand.onsetSec), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // An early bedtime (20:00) is still going at the day's midnight cut. The day | ||
| // that ends at midnight must not publish a truncated night ending at 00:00; | ||
| // only the next day, which sees the wake, owns it. | ||
| // | ||
| // Fixed local `DateTime(...)` literals, never `DateTime.now()`. | ||
|
|
||
| import 'dart:math' as math; | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:openstrap_edge/compute/substrate.dart'; | ||
|
|
||
| int _local(int y, int mo, int d, [int h = 0, int mi = 0]) => | ||
| DateTime(y, mo, d, h, mi).millisecondsSinceEpoch ~/ 1000; | ||
|
|
||
| Substrate _build(int start, int n, bool Function(int t) asleepAt) { | ||
| final ts = <int>[], hr = <int>[]; | ||
| final ax = <double>[], ay = <double>[], az = <double>[]; | ||
| for (var i = 0; i < n; i++) { | ||
| final t = start + i; | ||
| final asleep = asleepAt(t); | ||
| ts.add(t); | ||
| hr.add(asleep ? 50 : 72); | ||
| if (asleep) { | ||
| ax.add(0.0); | ||
| ay.add(0.0); | ||
| az.add(1.0); | ||
| } else { | ||
| final rad = (i % 9) * 10.0 * math.pi / 180.0; | ||
| ax.add(math.cos(rad)); | ||
| ay.add(0.0); | ||
| az.add(math.sin(rad)); | ||
| } | ||
| } | ||
| return Substrate( | ||
| tsSec: ts, | ||
| hr: hr, | ||
| rrTsMs: const [], | ||
| rrMs: const [], | ||
| ax: ax, | ||
| ay: ay, | ||
| az: az, | ||
| spo2Red: List<int>.filled(n, 0), | ||
| spo2Ir: List<int>.filled(n, 0), | ||
| skinTemp: List<int>.filled(n, 0), | ||
| skinContact: List<int>.filled(n, 0), | ||
| ); | ||
| } | ||
|
|
||
| void main() { | ||
| test('a night still going at midnight is owned only by the day it ends in', | ||
| () { | ||
| final start = _local(2025, 6, 15, 12); | ||
| final bed = _local(2025, 6, 15, 20); | ||
| final wake = _local(2025, 6, 16, 6); | ||
| final end = _local(2025, 6, 16, 11); | ||
| final days = calendarDays( | ||
| _build(start, end - start, (t) => t >= bed && t < wake)); | ||
|
|
||
| final slept = days.where((d) => d.sleep.present).toList(); | ||
| expect(slept, hasLength(1)); | ||
| final night = slept.single; | ||
| expect(night.date, '2025-06-16'); | ||
| final onSec = night.sleep.window!.onsetMs! ~/ 1000; | ||
| final offSec = night.sleep.window!.offsetMs! ~/ 1000; | ||
| expect((onSec - bed).abs(), lessThan(3600)); | ||
| expect((offSec - wake).abs(), lessThan(3600)); | ||
| }); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass the real record end into
calendarDayson the target-day path.The target-day search substrate ends at local midnight minus one second, so
calendarDayscan never seedataEnd > dayEndfor that day. The deferral added atlib/compute/substrate.dart:1335therefore never applies in ordinary derivation. A night still going at midnight is treated as finished at the cut. It can then be attributed to the day it is cut in, and again to the day it ends on.calendarDayscannot infer continuation from the clipped substrate. Carry the true record boundary, or an explicit continuation flag, from the caller throughprepareSleepSessionCandidateandprepareDerivationPayloadintocalendarDays. Passfalsewhen the actual record ends at that point.