diag(hrv): shadow-log a deduped R-R stream to validate the over-count fix (#1331/#1008/#1118) - #1352
Merged
Merged
Conversation
… fix (#1331/#1008/#1118) The WHOOP 4.0 R-R over-count (#1008 — each beat emitted 2-3x, an exact dup plus a two-optical-channel twin ~34ms off) corrupts HRV (#1008/#1118) AND, via #1127's RSA beat-accuracy gate, blanks respiratory rate on 4.0 since V10 (#1331). The fix is a de-dup, but it changes a physiological signal, so it must be validated against ground truth before it becomes the read path. Stage 1 (this PR) — instrument, do not change behaviour: - HRVAnalyzer/HrvAnalyzer.collapseOverCount: pure, byte-identical de-dup that collapses same-second beats within ~40ms of one already kept (the exact dups + the ~34ms channel twins). Returns the deduped (tsSec, rrMs). - On an over-count night the always-on hrv diag appends a `hrv dedup` line with the DEDUPED stream's rmssd/sdnn/meanNN/coverage/beatAccurate beside the raw. Shipped HRV/resp is UNCHANGED — this only logs a candidate. So a strap log now shows, per 4.0 over-count night, whether de-dup drives coverage→~1.0 and beatAccurate high (which would pass #1127's RSA gate → resp returns, the #1331 fix) and rmssd/sdnn toward physiological — validated against WHOOP's own numbers and @artemc's Polar H10 (#1118) before Stage 2 flips it live. Tests: HrvCollapseOverCount{Test,Tests} pin a synthetic 3x over-count collapsing 180→60 beats, coverage ~3x→~1.0, clean streams untouched. compileFullDebugKotlin + the Kotlin test green; StrandAnalytics via swift-packages; IntelligenceEngine shadow is app-target → app-build gate. Refs #1331, #1008, #1118, #1127.
…te (#1331) Re-review caught that the ~40ms same-second collapse can over-merge two REAL neighbouring beats whose R-R values sit within 40ms (normal at sleeping HR), so its coverage undershoots 1.0 even when de-dup is working — confounding the validation signal. Add the EXACT-duplicate collapse (rrTolMs=0, same ts AND value, provably no real-beat loss) as a safe reference line, so the shadow logs the honest spectrum: covExact (safe floor, the unambiguous exact-dup magnitude) alongside cov40/beatAcc40/rmssd40 (aggressive upper bound). The real de-dup sits between them. Test pins the exact-dup floor (180->120, coverage ~2x, twins kept).
simoncad7
pushed a commit
to simoncad7/noop
that referenced
this pull request
Aug 17, 2026
… fix (ryanbr#1331/ryanbr#1008/ryanbr#1118) (ryanbr#1352) * diag(hrv): shadow-log a deduped R-R stream to validate the over-count fix (ryanbr#1331/ryanbr#1008/ryanbr#1118) The WHOOP 4.0 R-R over-count (ryanbr#1008 — each beat emitted 2-3x, an exact dup plus a two-optical-channel twin ~34ms off) corrupts HRV (ryanbr#1008/ryanbr#1118) AND, via ryanbr#1127's RSA beat-accuracy gate, blanks respiratory rate on 4.0 since V10 (ryanbr#1331). The fix is a de-dup, but it changes a physiological signal, so it must be validated against ground truth before it becomes the read path. Stage 1 (this PR) — instrument, do not change behaviour: - HRVAnalyzer/HrvAnalyzer.collapseOverCount: pure, byte-identical de-dup that collapses same-second beats within ~40ms of one already kept (the exact dups + the ~34ms channel twins). Returns the deduped (tsSec, rrMs). - On an over-count night the always-on hrv diag appends a `hrv dedup` line with the DEDUPED stream's rmssd/sdnn/meanNN/coverage/beatAccurate beside the raw. Shipped HRV/resp is UNCHANGED — this only logs a candidate. So a strap log now shows, per 4.0 over-count night, whether de-dup drives coverage→~1.0 and beatAccurate high (which would pass ryanbr#1127's RSA gate → resp returns, the ryanbr#1331 fix) and rmssd/sdnn toward physiological — validated against WHOOP's own numbers and @artemc's Polar H10 (ryanbr#1118) before Stage 2 flips it live. Tests: HrvCollapseOverCount{Test,Tests} pin a synthetic 3x over-count collapsing 180→60 beats, coverage ~3x→~1.0, clean streams untouched. compileFullDebugKotlin + the Kotlin test green; StrandAnalytics via swift-packages; IntelligenceEngine shadow is app-target → app-build gate. Refs ryanbr#1331, ryanbr#1008, ryanbr#1118, ryanbr#1127. * diag(hrv): log an exact-dup reference beside the ~40ms de-dup candidate (ryanbr#1331) Re-review caught that the ~40ms same-second collapse can over-merge two REAL neighbouring beats whose R-R values sit within 40ms (normal at sleeping HR), so its coverage undershoots 1.0 even when de-dup is working — confounding the validation signal. Add the EXACT-duplicate collapse (rrTolMs=0, same ts AND value, provably no real-beat loss) as a safe reference line, so the shadow logs the honest spectrum: covExact (safe floor, the unambiguous exact-dup magnitude) alongside cov40/beatAcc40/rmssd40 (aggressive upper bound). The real de-dup sits between them. Test pins the exact-dup floor (180->120, coverage ~2x, twins kept). * fixup: reattach collapsedCoverage doc (collapseOverCount was inserted between doc + fn) doc_comment_lint flagged HrvAnalyzer.kt:503 — collapseOverCount landed between collapsedCoverage's ryanbr#550 KDoc and its declaration, detaching the doc. Move collapseOverCount above the ryanbr#550 doc so it reattaches. No code change.
ryanbr
added a commit
that referenced
this pull request
Aug 17, 2026
…w candidate (#1331/#1118) The #1331 4.0 logs read `crossSecondOverCount` every night: the same-second shadow collapses (#1352's exact + 40 ms candidates) can't reach the duplicate twins because they straddle the second boundary, so `cov40` stays ~1.7-2.0 on the heavy nights and RSA-respiration stays blanked by #1127's beat-accuracy gate. Add a THIRD shadow candidate, `xsec` — the 40 ms collapse widened to a 1-second window — logged beside the others so we can SIZE how much of a night is cross-second (does coverage fall to ~1.0, does beat-accuracy clear the 0.5 gate?). `collapseOverCount` gains a `windowSec` param (default 0 = same-second, byte- identical for every existing caller). Instrumentation ONLY — the shipped HRV/resp path is unchanged. The window is a strict UPPER BOUND, deliberately NOT a shippable de-dup: a steady real HR has near-identical intervals one second apart, so a cross-second collapse over-merges real beats (the test pins this: a 10-beat steady stream drops to 5 at window=1). The real fix is density/timeline-based and must be validated against ground truth (@artemc's H10) before it becomes the read path — this just quantifies the target. Both platforms (byte-identical `hrv dedup` line + twin tests).
ryanbr
added a commit
that referenced
this pull request
Aug 17, 2026
…w candidate (#1331/#1118) (#1399) The #1331 4.0 logs read `crossSecondOverCount` every night: the same-second shadow collapses (#1352's exact + 40 ms candidates) can't reach the duplicate twins because they straddle the second boundary, so `cov40` stays ~1.7-2.0 on the heavy nights and RSA-respiration stays blanked by #1127's beat-accuracy gate. Add a THIRD shadow candidate, `xsec` — the 40 ms collapse widened to a 1-second window — logged beside the others so we can SIZE how much of a night is cross-second (does coverage fall to ~1.0, does beat-accuracy clear the 0.5 gate?). `collapseOverCount` gains a `windowSec` param (default 0 = same-second, byte- identical for every existing caller). Instrumentation ONLY — the shipped HRV/resp path is unchanged. The window is a strict UPPER BOUND, deliberately NOT a shippable de-dup: a steady real HR has near-identical intervals one second apart, so a cross-second collapse over-merges real beats (the test pins this: a 10-beat steady stream drops to 5 at window=1). The real fix is density/timeline-based and must be validated against ground truth (@artemc's H10) before it becomes the read path — this just quantifies the target. Both platforms (byte-identical `hrv dedup` line + twin tests).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stage 1 of the WHOOP 4.0 over-count fix — instrumentation only, no behaviour change. Shadow-logs a candidate de-dup so it can be validated against ground truth before it ever becomes the read path.
Why
The WHOOP 4.0 R-R over-count (#1008 — each beat emitted 2–3×: an exact duplicate plus a two-optical-channel twin ~34 ms off) is behind three symptoms: HRV ≠ WHOOP (#1008), the HRV "unverified" caveat (#1118), and — via #1127's RSA beat-accuracy gate — the respiratory blank on 4.0 since V10 (#1331). The real fix is a de-dup, but it changes a physiological signal (HRV + respiration), so per the "validate against the artifact, not one match" rule it can't be shipped on a guess.
What (this PR)
HRVAnalyzer.collapseOverCount/HrvAnalyzer.collapseOverCount— a pure, byte-identical de-dup: a same-second beat within ~40 ms of one already kept (the exact dups + the ~34 ms channel twins) is dropped, keeping one representative. Returns the deduped(tsSec, rrMs).hrv diagappends ahrv dedup …line with the deduped stream'srmssd / sdnn / meanNN / coverage / beatAccurate, beside the raw values already logged. The shipped HRV/resp path is unchanged — this only logs a candidate.What it buys us
A strap log now shows, per 4.0 over-count night, whether de-dup:
That's the exact data to validate against WHOOP's own numbers and @artemc's Polar H10 dataset (#1118) — across his over-count and clean nights — before Stage 2 flips the read path to the deduped stream (which closes #1008 + #1118 + #1331 together).
Verification
HrvCollapseOverCount{Test,Tests}(both platforms): a synthetic 3× over-count collapses 180→60 beats, coverage ~3× → ~1.0, and a clean stream passes through untouched. Kotlin green locally.StrandAnalyticsviaswift-packages; theIntelligenceEngineshadow line is app-target → app-build gate.Refs #1331, #1008, #1118, #1127. Instrumentation-first, mirroring #1117/#1332/#1344.