From c32596555c42dd50033a57dfc87f51f20c8c89b0 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 21 Nov 2026 16:07:25 +0530 Subject: [PATCH] crossday: pair chronotype nights, put sri on the lived clock --- lib/compute/crossday_pipeline.dart | 23 +++++++++------ test/crossday_pipeline_test.dart | 45 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/lib/compute/crossday_pipeline.dart b/lib/compute/crossday_pipeline.dart index cf2e8f30f..7025e84e9 100644 --- a/lib/compute/crossday_pipeline.dart +++ b/lib/compute/crossday_pipeline.dart @@ -199,6 +199,10 @@ Map buildCrossDayBundle( final freeMidH = []; final workMidH = []; final freeDurH = []; + // chronotype pairs each free night's mid-sleep with its duration, so a free + // night with a window but no TST (vendor-staged or manual) must sit out of + // BOTH lists, or one bad night leaves them unequal for the whole window. + final chronoMidH = []; final allDurH = []; for (final d in days) { final onset = (d['onset_sec'] as num?)?.toDouble(); @@ -218,7 +222,10 @@ Map buildCrossDayBundle( final free = _isFreeDay(d['date'] as String?); if (free) { freeMidH.add(midH); - if (dur != null) freeDurH.add(dur); + if (dur != null) { + freeDurH.add(dur); + chronoMidH.add(midH); + } } else { workMidH.add(midH); } @@ -227,7 +234,7 @@ Map buildCrossDayBundle( final socialJetlag = ana.socialJetlag(freeMidH, workMidH); final chronotype = ana.chronotype( - freeMidH, + chronoMidH, freeDurH, avgWeekSleepDurH: avgWeekDurH, totalDaysObserved: allDurH.length, @@ -1277,14 +1284,12 @@ bool _isNextDay(String a, String b) { final end = (seg['end'] as num?)?.toDouble(); final stage = seg['stage'] as String?; if (start == null || end == null) continue; - // Segment bounds are epoch SECONDS; map to clock-minute-of-day [0,1440). - // this was using `start % 86400` directly, which is the UTC - // time-of-day, not the user's wall clock - the exact mistake - // _localTodMin (right here in this same file, used correctly at - // line ~100) already exists to avoid. - final startMin = _localTodMin(start.round()).floor(); + // Segment bounds are epoch SECONDS; map to clock-minute-of-day [0,1440) + // on the clock the night was LIVED on (see _nightClockMin), so a trip + // does not shift every earlier night against the later ones. + final startMin = _nightClockMin(start.round(), d).floor(); // end is exclusive of the segment's trailing edge; cover [start,end). - final endMinRaw = _localTodMin(end.round()).ceil(); + final endMinRaw = _nightClockMin(end.round(), d).ceil(); // WRAP, don't clamp. Both bounds are clock-minutes-of-day in [0,1440), // so a segment crossing local midnight reads e.g. start=1430, // end=20 — and the old `for (m = startMin; m < endMin; m++)` simply diff --git a/test/crossday_pipeline_test.dart b/test/crossday_pipeline_test.dart index fcb0634b3..78b215d13 100644 --- a/test/crossday_pipeline_test.dart +++ b/test/crossday_pipeline_test.dart @@ -636,6 +636,51 @@ void _wiredFamilies() { }); }); + group('free night with a window but no TST', () { + test('does not make chronotype refuse for the whole window', () { + final days = _synthDays(30); + // 2024-01-06 is a Saturday: keep its window, drop its TST. + final sat = days.firstWhere((d) => d['date'] == '2024-01-06'); + sat.remove('tst_min'); + final chrono = (buildCrossDayBundle(days, const {})['chronotype'] as Map) + .cast(); + expect(chrono['value'], isA(), reason: '${chrono['note']}'); + }); + }); + + group('sleep regularity across a time-zone change', () { + test('nights lived on the same clock read as perfectly regular', () { + // Every night is light 23:00-07:00 on the clock it was lived on. The + // first week was in a zone 3 h ahead, so on the phone's current clock + // those instants read 20:00-04:00; clock_shift_sec puts them back. + int at(int day, int h) => + DateTime(2024, 3, 4 + day, h).millisecondsSinceEpoch ~/ 1000; + final days = [ + for (var i = 0; i < 14; i++) + { + 'date': '2024-03-${(5 + i).toString().padLeft(2, '0')}', + if (i < 7) 'clock_shift_sec': 3 * 3600, + 'hypnogram': [ + { + 'start': at(i, i < 7 ? 20 : 23), + 'end': at(i + 1, i < 7 ? 4 : 7), + 'stage': 'light', + }, + // awake the rest of the day, so every minute is observed + { + 'start': at(i + 1, i < 7 ? 4 : 7), + 'end': at(i + 1, i < 7 ? 20 : 23), + 'stage': 'wake', + }, + ], + }, + ]; + final reg = (buildCrossDayBundle(days, const {})['regularity'] as Map) + .cast(); + expect((reg['value'] as Map)['sri'], closeTo(100, 1e-9)); + }); + }); + group('sleep performance is last night only', () { test('no TST last night → absent, not an older night\'s TST', () { final days = _synthDays(30);