Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions lib/compute/derivation_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,10 @@ import 'vendor_sleep.dart';
// disk (rows are immutable per version), only nights whose hypnogram segments
// (sleep or wake) cross a DST change get a different SRI value. In the
// repeated autumn hour each clock-minute keeps its first occurrence only.
const int kAlgoVersion = 138;
// 138 → 139: resp_day inside the sleep window now matches the nightly
// estimator, a 10-minute window with no stillness gate. Daytime windows are
// unchanged.
const int kAlgoVersion = 139;
/// The sibling SHAs this version was derived against, asserted against
/// pubspec.yaml in test/db_serve_version_and_reads_test.dart.
///
Expand Down Expand Up @@ -11141,7 +11144,10 @@ class DerivationEngine {
static int debugRespGateRejects = 0;

@visibleForTesting
static List<Map<String, num>> dayRespCurve(Substrate s) {
static List<Map<String, num>> dayRespCurve(Substrate s,
{int sleepOnsetSec = 0,
int sleepOffsetSec = 0,
int tonightSleepOnsetSec = 0}) {
// RESP-05 — the gate is per-FAMILY, and an unknown strap gets no cut rather
// than gen4's (device.dart contract). Refusing the whole curve is the
// correct output for every pre-schema-41 day and every import: we cannot
Expand All @@ -11167,33 +11173,49 @@ class DerivationEngine {
}
if (rr.length < 60) return const [];
const winMs = 180000.0; // 3 min
const sleepWinMs = 600000.0;
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
final sleepLoMs = sleepOnsetSec * 1000.0;
final sleepHiMs = sleepOffsetSec * 1000.0;
final tonightMs = tonightSleepOnsetSec * 1000.0;
final out = <Map<String, num>>[];
var lo = 0;
var dayLo = 0, sleepLo = 0;
var lastEmit = -1e18;
// Cursors into s.tsSec for the window bounds. Both `ts[lo]` and `ts[i]` are
// non-decreasing across the loop, so these only ever move forward.
var qLo = 0, qHi = 0;
for (var i = 0; i < rr.length; i++) {
while (ts[i] - ts[lo] > winMs) {
lo++;
}
while (ts[i] - ts[dayLo] > winMs) {
dayLo++;
}
final inMain =
sleepLoMs < sleepHiMs && ts[i] >= sleepLoMs && ts[i] < sleepHiMs;
final inTonight = tonightMs > 0 && ts[i] >= tonightMs;
final asleep = inMain || inTonight;
final sleepStartMs = inTonight ? tonightMs : sleepLoMs;
while (sleepLo < i &&
(ts[i] - ts[sleepLo] > sleepWinMs ||
(asleep && ts[sleepLo] < sleepStartMs))) {
sleepLo++;
}
final lo = asleep ? sleepLo : dayLo;
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
if (i - lo >= 30 && ts[i] - lastEmit > 300000) {
// 5-min cadence
// STILLNESS GATE, BEFORE the estimator. Rejecting here is also where
// the perf win lives: the triple Lomb-Scargle never runs on a window
// that could not have produced a resting rate anyway.
final loSec = (ts[lo] / 1000).floor();
final hiSec = (ts[i] / 1000).ceil();
while (qLo < s.length && s.tsSec[qLo] < loSec) {
while (!asleep && qLo < s.length && s.tsSec[qLo] < loSec) {
qLo++;
}
if (qHi < qLo) qHi = qLo;
while (qHi < s.length && s.tsSec[qHi] < hiSec) {
while (!asleep && qHi < s.length && s.tsSec[qHi] < hiSec) {
qHi++;
}
final spanSec = hiSec - loSec;
final stillSec = quietPrefix[qHi] - quietPrefix[qLo];
if (spanSec <= 0 || stillSec < _respQuietFraction * spanSec) {
if (!asleep &&
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
(spanSec <= 0 || stillSec < _respQuietFraction * spanSec)) {
// Advance the cadence cursor on a rejection too — otherwise a moving
// stretch re-tests (and re-scans) once per beat, which is the same
// shape as the v60 bug this loop already carries a fix for.
Expand Down Expand Up @@ -12134,7 +12156,10 @@ class DerivationEngine {
bundlePatch['daytime_hrv'] = _daytimeHrv(daySub, onset, offset,
tonightSleepOnsetSec: inp.tonightSleepOnsetSec);
seriesPatch['hrv_day'] = dayHrvCurve(daySub);
seriesPatch['resp_day'] = dayRespCurve(daySub);
seriesPatch['resp_day'] = dayRespCurve(daySub,
sleepOnsetSec: onset,
sleepOffsetSec: offset,
tonightSleepOnsetSec: inp.tonightSleepOnsetSec);
seriesPatch['skin_temp_day'] = _daySkinTempCurve(daySub);
bundlePatch['restlessness'] = _restlessness(sleepSub);
// napSub extends a few hours past this day's calendar end so a nap/
Expand Down
75 changes: 75 additions & 0 deletions test/absence_and_offload_guards_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,81 @@ void _respGroups() {
expect(DerivationEngine.debugRespAttempts, 0);
});

test('inside the sleep window a restless stretch is still attempted', () {
const t0 = 1780000000;
final sub = _rrSub(n: 3600, moving: {for (var i = 0; i < 3600; i += 4) i});
final spans = <double>[];
DerivationEngine.debugRespGateRejects = 0;
DerivationEngine.debugRespAttempts = 0;
DerivationEngine.debugRespEstimator = (nn, nnt) {
spans.add(nnt.last - nnt.first);
return 15.0;
};
addTearDown(() => DerivationEngine.debugRespEstimator = null);

final out = DerivationEngine.dayRespCurve(sub,
sleepOnsetSec: t0 + 1800, sleepOffsetSec: t0 + 3600);

expect(DerivationEngine.debugRespGateRejects, greaterThan(0));
expect(out, isNotEmpty);
for (final p in out) {
expect(p['t']!, greaterThanOrEqualTo(t0 + 1800));
}
for (var k = 1; k < out.length; k++) {
expect(out[k]['t']! - out[k - 1]['t']!, greaterThanOrEqualTo(300));
}
expect(spans.length, out.length);
for (var k = 0; k < out.length; k++) {
expect(out[k]['t']! * 1000 - spans[k],
greaterThanOrEqualTo((t0 + 1800) * 1000 - 1000));
}
expect(spans.last, greaterThan(590000));
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
});

test('the sleep window ends at wake, exclusive', () {
const t0 = 1780000000;
final sub = _rrSub(n: 3600, moving: {for (var i = 0; i < 3600; i += 4) i});
DerivationEngine.debugRespEstimator = (nn, nnt) => 15.0;
addTearDown(() => DerivationEngine.debugRespEstimator = null);

final out = DerivationEngine.dayRespCurve(sub,
sleepOnsetSec: t0, sleepOffsetSec: t0 + 1800);

expect(out, isNotEmpty);
for (final p in out) {
expect(p['t']!, lessThan(t0 + 1800));
}
});

test('tonight\'s sleep before midnight is treated as sleep', () {
const t0 = 1780000000;
final sub = _rrSub(n: 3600, moving: {for (var i = 0; i < 3600; i += 4) i});
final spans = <double>[];
DerivationEngine.debugRespEstimator = (nn, nnt) {
spans.add(nnt.last - nnt.first);
return 15.0;
};
addTearDown(() => DerivationEngine.debugRespEstimator = null);

final out = DerivationEngine.dayRespCurve(sub,
tonightSleepOnsetSec: t0 + 1800);

expect(out, isNotEmpty);
for (var k = 0; k < out.length; k++) {
expect(out[k]['t']!, greaterThanOrEqualTo(t0 + 1800));
expect(out[k]['t']! * 1000 - spans[k],
greaterThanOrEqualTo((t0 + 1800) * 1000 - 1000));
}
});

test('a sleep window with no length changes nothing', () {
final sub = _rrSub(n: 1800, moving: {for (var i = 0; i < 1800; i += 4) i});
DerivationEngine.debugRespAttempts = 0;
DerivationEngine.dayRespCurve(sub,
sleepOnsetSec: 1780000000 + 900, sleepOffsetSec: 1780000000 + 900);
expect(DerivationEngine.debugRespAttempts, 0);
});

test('an unstamped strap gets no curve, not gen4\'s cut', () {
DerivationEngine.debugRespAttempts = 0;
expect(DerivationEngine.dayRespCurve(_rrSub(n: 1800, family: null)),
Expand Down
2 changes: 1 addition & 1 deletion test/ed2a_substrate_transfer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void main() {
markTestSkipped('committed golden is UTC; run this test with TZ=UTC');
return;
}
expect(kAlgoVersion, 138, reason: 'ED-2b repins analytics; transfer remains exact');
expect(kAlgoVersion, 139, reason: 'ED-2b repins analytics; transfer remains exact');
final fixture = _fixture();
final baseline = substrateFromDecodedPage(fixture.frames, fixture.rr);
final transferred = Substrate.fromTransfer(baseline.toJson());
Expand Down
2 changes: 1 addition & 1 deletion test/ed2b_algo_version_114_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:openstrap_edge/compute/derivation_engine.dart';
void main() {
// WHY: finalized days need a new version when the analytics pin changes outputs.
test('ED-2b adoption and workout rest repin name their output changes', () {
expect(kAlgoVersion, 138);
expect(kAlgoVersion, 139);
// #113 moved code between repos with no output change, so the version holds.
expect(kAnalyticsPin, 'b92686a20e4a9a1852d5e43c52d3e3b42f360e7b');
final src = File('lib/compute/derivation_engine.dart').readAsStringSync();
Expand Down
Loading