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
61 changes: 54 additions & 7 deletions lib/compute/derivation_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,13 @@ import 'vendor_sleep.dart';
// next midnight belongs to the next day only. Band days move through
// active minutes when their floor was first frozen out of order; stored day
// output only.
const int kAlgoVersion = 141;
// 141 → 142: strain and the quiet level are priced on the median of the prior
// nights' resting HR instead of this morning's (analytics steadyRestingHr). The
// level is stored against each earlier day's own night, so a higher night
// shrank every minute of an ordinary day against it and strain read 0.0; the
// day after a hard session is the usual case. Strain, trimp_net, quiet_hrr and
// the strain curve move; stored history re-scores on the recompute.
const int kAlgoVersion = 142;
/// 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 @@ -2196,7 +2202,7 @@ const int kAlgoVersion = 141;
// REPIN @ b92686a: analytics main, #113 merge (hmm stager and lutealFlags moved
// in from edge, code unchanged), no kAlgoVersion bump: WHOOP output is the
// same.
const String kAnalyticsPin = 'b92686a20e4a9a1852d5e43c52d3e3b42f360e7b';
const String kAnalyticsPin = 'f29472e64719670fe74e31521f4975c34dc113cd';
// Repinned to analytics main's tip, which carries BOTH PR #72 (hrv_freq
// Welch gap guard) and PR #73 (overreachingConjunction rhr quantum guard) —
// the two independent kAlgoVersion bumps above (93 and 94). Verified both
Expand Down Expand Up @@ -3130,6 +3136,10 @@ class _BaselineHistoryCache {
'v$kAlgoVersion|n${rhr.length}|rhr${fmt(_median(rhr))}|rmssd${fmt(_median(rmssd))}'
'|temp${fmt(_median(temp))}|resp${fmt(_median(resp))}'
'${hasDev ? '|dev${_datedFingerprint('ln_rmssd_dev')}' : ''}'
// Strain reads the trailing nights' resting HR itself now, so a moved
// night with the same count and median still moves a day's reference.
// Before `|q`: a quiet-only change is told apart by cutting there.
'|r${_datedFingerprint('rhr')}'
'|q${_quietFingerprint()}';
return {
'algo_version': kAlgoVersion,
Expand Down Expand Up @@ -6628,8 +6638,25 @@ class DerivationEngine {
await LocalDb.setCursor(_rescanRetriesCursor, '0');
// Store the new signature so the next tick is a cheap no-op until it
// moves. Quiet snapshot before the signature, for the reason above.
await _storeQuietEntries();
await LocalDb.setCursor('baseline_sig', await _baselineSignature());
// The levels this pass wrote can differ from the ones its later days
// were priced on (they follow the resting-HR nights now). Keep the
// pre-pass pair so the next tick narrows to the days after them; the
// levels no longer depend on each other, so it settles in one more.
// Same for the nights themselves: a night this pass revised was read
// stale by the days after it.
final post = await _BaselineHistoryCache.load();
final moved = changedQuietDates(
history.quietWindowEntries(), post.quietWindowEntries());
final nightsMoved =
history._datedFingerprint('rhr') != post._datedFingerprint('rhr');
if ((moved != null && moved.isNotEmpty) || nightsMoved) {
await LocalDb.setCursor(_baselineQuietCursor,
jsonEncode(history.quietWindowEntries()));
await LocalDb.setCursor('baseline_sig', sig);
} else {
await _storeQuietEntries();
await LocalDb.setCursor('baseline_sig', await _baselineSignature());
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}
await LocalDb.refreshComputeFreshness();
return done;
Expand Down Expand Up @@ -7672,6 +7699,12 @@ class DerivationEngine {
? null
: rhrFallbackFromHistory(history.valuesBefore('rhr', day.date,
family: _baselineFamily('rhr', daySub.deviceFamily))),
// Only a night's own value is smoothed; a typed or fallback one is
// already steady.
rhrHistory: (scMap?['rhr_nocturnal'] as num?) == null
? const []
: history.valuesBefore('rhr', day.date,
family: _baselineFamily('rhr', daySub.deviceFamily)),
Comment thread
abdulsaheel marked this conversation as resolved.
Resolved
maxHrUsed: (bundle['max_hr_used'] as num?)?.round(),
liveStepsReal: liveSteps.total,
liveStepsFromStrap: liveSteps.strap,
Expand Down Expand Up @@ -9827,6 +9860,7 @@ class DerivationEngine {
required int dayCalendarEndSec,
required int dataNowSec,
double? restingHr,
List<double> rhrHistory = const [],
double? dynFloorG,
int liveStepsReal = 0,
int liveStepsFromStrap = 0,
Expand Down Expand Up @@ -9858,6 +9892,7 @@ class DerivationEngine {
dayCalendarEndSec: dayCalendarEndSec,
dataNowSec: dataNowSec,
restingHr: restingHr,
rhrHistory: rhrHistory,
dynFloorG: dynFloorG,
stepSpans: stepSpans,
quietHrrHistory: quietHrrHistory,
Expand Down Expand Up @@ -10605,6 +10640,7 @@ class DerivationEngine {
required int dayCalendarEndSec,
required int dataNowSec,
double? restingHr,
List<double> rhrHistory = const [],
double? dynFloorG,
List<List<int>> stepSpans = const [],
int tonightSleepOnsetSec = 0,
Expand Down Expand Up @@ -10678,10 +10714,14 @@ class DerivationEngine {
// never scores today (that would subtract today's own living from itself).
// Below `quietHrrTraitMinMinutes` wake minutes the day measured that
// window, not ordinary living, and contributes nothing.
final quietToday = (rhrForTrimp == null || hrMax == null)
// The level and the day are priced on the SAME slow resting HR, not this
// morning's: a raised night would otherwise shrink every minute against a
// level stored on calmer ones and score an ordinary day 0 (#471).
final rhrStrain = ana.steadyRestingHr(rhrForTrimp, rhrHistory);
Comment thread
abdulsaheel marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
final quietToday = (rhrStrain == null || hrMax == null)
? null
: ana.dailyQuietWakingHrr(perMin,
restingHr: rhrForTrimp,
restingHr: rhrStrain,
maxHr: hrMax,
minMinutes: ana.quietHrrTraitMinMinutes);
double? strain;
Expand Down Expand Up @@ -10772,7 +10812,7 @@ class DerivationEngine {
// so a quiet afternoon cannot cancel a morning run.
net = ana.netTrimpAboveQuiet(
perMin,
restingHr: rhrForTrimp,
restingHr: rhrStrain,
maxHr: hrMax,
quietHrr: quiet.value!.hrr,
sex: sexEnum,
Expand Down Expand Up @@ -12315,6 +12355,7 @@ class DerivationEngine {
dayCalendarEndSec: localNextMidnightSecForDayLabel(inp.date),
dataNowSec: inp.dataNowSec,
restingHr: inp.rhr ?? inp.rhrFallback,
rhrHistory: inp.rhrHistory,
dynFloorG: inp.dynFloorG,
liveStepsReal: inp.liveStepsReal,
liveStepsFromStrap: inp.liveStepsFromStrap,
Expand Down Expand Up @@ -13078,6 +13119,11 @@ class _DayBlocksInput {
/// night of its own ([rhrFallbackFromHistory]). Never used for workout
/// detection, which keeps needing a real night.
final double? rhrFallback;

/// Prior nights' resting HR, so strain is scored against the same slow
/// reference the quiet level was measured on ([ana.steadyRestingHr]). Empty
/// when the user typed their own resting HR.
final List<double> rhrHistory;
final int? maxHrUsed;
final int liveStepsReal;
/// Step-counter calibration read on the main isolate; null = uncalibrated.
Expand Down Expand Up @@ -13165,6 +13211,7 @@ class _DayBlocksInput {
this.napExcludeEndSec,
required this.rhr,
this.rhrFallback,
this.rhrHistory = const [],
required this.maxHrUsed,
required this.liveStepsReal,
this.counterProfile,
Expand Down
9 changes: 7 additions & 2 deletions lib/compute/onehz_pipeline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,11 @@ Map<String, dynamic> _deriveDayBundle(Map<String, dynamic> inputJson,
// abstains with the baseline grammar rather than pricing being awake at a
// population constant.
final quiet = personalQuietWakingHrr(d.quietHrrHistory);
// Strain and its curve are priced on the same slow resting HR the quiet level
// was measured on, not this morning's (#471). Only a night's own value is
// smoothed; a typed or fallback one is already steady.
final rhrStrain =
steadyRestingHr(rhrForTrimp, rhrToday == null ? const [] : d.rhrHistory);
// ── WHY the activity family is absent, named AT THE GATE THAT CAUSED IT ────
//
// These four figures — strain/TRIMP, the zone minutes, the ceiling they were
Expand Down Expand Up @@ -1459,7 +1464,7 @@ Map<String, dynamic> _deriveDayBundle(Map<String, dynamic> inputJson,
)
: strainScoreFromSeries(
perMin,
restingHr: rhrForTrimp,
restingHr: rhrStrain,
maxHr: hrMax,
quietHrr: quiet.value!.hrr,
sex: sexEnum,
Expand Down Expand Up @@ -1499,7 +1504,7 @@ Map<String, dynamic> _deriveDayBundle(Map<String, dynamic> inputJson,
final nightShape = nightHrvShape(nn, nnTimes, coverage: rrCov);
final strainCurve = _strainCurve(
wakeHr,
restingHr: rhrForTrimp,
restingHr: rhrStrain,
maxHr: hrMax,
sex: sex,
quietHrr: quiet.value?.hrr,
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -961,8 +961,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: b92686a20e4a9a1852d5e43c52d3e3b42f360e7b
resolved-ref: b92686a20e4a9a1852d5e43c52d3e3b42f360e7b
ref: f29472e64719670fe74e31521f4975c34dc113cd
resolved-ref: f29472e64719670fe74e31521f4975c34dc113cd
url: "https://github.com/OpenStrap/analytics.git"
source: git
version: "1.0.0"
Expand Down
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,9 @@ dependencies:
# and cosinor policies require kAlgoVersion 124.
# analytics main @ #113 merge: hmm stager and lutealFlags moved in from edge.
# Logic unchanged, so WHOOP output is unchanged.
ref: b92686a20e4a9a1852d5e43c52d3e3b42f360e7b
# analytics main @ #116 merge (f29472e): steadyRestingHr, strain is priced
# on the median of prior nights' resting HR (#471).
ref: f29472e64719670fe74e31521f4975c34dc113cd

# BLE — flutter_blue_plus is the maintained cross-platform GATT client.
flutter_blue_plus: ^1.36.8
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, 141, reason: 'ED-2b repins analytics; transfer remains exact');
expect(kAlgoVersion, 142, 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
6 changes: 3 additions & 3 deletions test/ed2b_algo_version_114_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ 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, 141);
// #113 moved code between repos with no output change, so the version holds.
expect(kAnalyticsPin, 'b92686a20e4a9a1852d5e43c52d3e3b42f360e7b');
expect(kAlgoVersion, 142);
// #116 steadyRestingHr moves strain, so 142.
expect(kAnalyticsPin, 'f29472e64719670fe74e31521f4975c34dc113cd');
final src = File('lib/compute/derivation_engine.dart').readAsStringSync();
final at = src.indexOf('// 121 → 122');
expect(at, isNonNegative);
Expand Down
Loading
Loading