Skip to content

oura: extract the history-drain/resume-cursor decisions into a pure, tested core (#291 follow-up) - #306

Merged
ryanbr merged 1 commit into
mainfrom
oura-drain-extract-test
Jul 12, 2026
Merged

ryanbr merged 1 commit into
mainfrom
oura-drain-extract-test

Conversation

@ryanbr

@ryanbr ryanbr commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Follow-up to #291. Regression-proofs the #91 fix.

Why

The #91 drain guards + resume-cursor logic lived untested, inline in OuraLiveSource (app-target BLE) — which is exactly why it silently regressed when the Oura BLE stack was refactored (#291: "lost when refactoring"). This moves the decisions into a pure, unit-tested type so it can't happen a third time.

What

New OuraHistoryDrain (in OuraProtocol, pure) owns the decisions:

  • drain-to-zero + stall guard (bytes_left must shrink; stop after 3 flat summaries) + 300s deadline, keeping banked progress on a forced stop
  • stored-ring-time high-water mark + reboot flag (a real sample older than the seek)
  • resume-cursor commit (advance only if forward AND resolves under the current anchor; reboot → 0 full pull)
  • loaded-cursor plausibility-ceiling sanitize

OuraLiveSource keeps all I/O — anchor resolution, persistence, logging, the historyCursorAdvanced emit — and now delegates every decision to drain. Behaviour-preserving: each of the ~7 sites maps 1:1 to the extracted logic. Also de-versions the [cursor-fix v1] marker → [cursor-fix].

Verification

  • 14 new OuraHistoryDrainTests + the existing 91 pass — I ran swift test locally on Linux (OuraProtocol is pure): 105/105. (Writing the tests already caught one flawed test premise — a 'not forward' case that was actually the reboot path — which is precisely the value here.)
  • OuraLiveSource compile: app-build (app-target Swift). No Kotlin twin — Oura is Swift-only/experimental.

…tested core (#291 follow-up)

The #91 drain guards + resume-cursor logic lived untested inline in OuraLiveSource
(app-target BLE) — which is exactly why it silently regressed when the Oura BLE
stack was refactored (#291: "lost when refactoring"). Move the DECISIONS into a
pure OuraHistoryDrain in OuraProtocol so they're pinned by unit tests, not just
on-device observation:

- drain-to-zero + stall guard (bytes_left must shrink; stop after 3 flat summaries)
  + 300s deadline guard, keeping banked progress on a forced stop
- stored-ring-time high-water mark + reboot flag (a real sample older than the seek)
- resume-cursor commit (advance only if forward AND resolves; reboot -> 0 full pull)
- loaded-cursor plausibility-ceiling sanitize

OuraLiveSource keeps ALL I/O — anchor resolution, persistence, logging, the
historyCursorAdvanced emit — and now delegates every decision to the drain core.
Behaviour-preserving: each site maps 1:1 to the extracted logic (verified by diff).
Also de-versions the [cursor-fix v1] log marker to [cursor-fix].

14 new OuraHistoryDrainTests + the existing 91 pass under swift test (105/105, run
on Linux — OuraProtocol is pure). OuraLiveSource compile: app-build.
@ryanbr
ryanbr merged commit 266c8fa into main Jul 12, 2026
12 checks passed
ryanbr added a commit that referenced this pull request Jul 12, 2026
…latforms CI-test the parity (#286 follow-up) (#307)

dataRangeNewestUnix decodes strapNewestTs, which gates automatic sync
(isFutureDatedNewest -> BackfillPolicy), so it must stay byte-identical Swift/
Kotlin. #286 left it inline in app-target BLEManager/WhoopBleClient with a Kotlin
test only — a Swift-side edit could silently drift the parity (the same gap the
Oura #306 follow-up closed).

Move the pure scan into WhoopProtocol.DataRange (Swift, builds+tests on Linux) and
com.noop.protocol.DataRange (Kotlin), byte-identical twins. BLEManager and
WhoopBleClient keep their signatures as thin wrappers that pass their own
futureSkew constant, so all call sites + the existing DataRangeScanTest are
unchanged. Adds DataRangeTests (Swift) mirroring the Kotlin fixtures — the real
WHOOP 4.0 offset-8 frames + the future/skew cases.

WhoopProtocol swift test 259/259 incl. 4 new DataRangeTests (run on Linux);
Android compileFullDebugKotlin + DataRangeScanTest green. BLEManager compile: app-build.
DX23876 pushed a commit to DX23876/noop that referenced this pull request Jul 22, 2026
…tested core (ryanbr#291 follow-up) (ryanbr#306)

The ryanbr#91 drain guards + resume-cursor logic lived untested inline in OuraLiveSource
(app-target BLE) — which is exactly why it silently regressed when the Oura BLE
stack was refactored (ryanbr#291: "lost when refactoring"). Move the DECISIONS into a
pure OuraHistoryDrain in OuraProtocol so they're pinned by unit tests, not just
on-device observation:

- drain-to-zero + stall guard (bytes_left must shrink; stop after 3 flat summaries)
  + 300s deadline guard, keeping banked progress on a forced stop
- stored-ring-time high-water mark + reboot flag (a real sample older than the seek)
- resume-cursor commit (advance only if forward AND resolves; reboot -> 0 full pull)
- loaded-cursor plausibility-ceiling sanitize

OuraLiveSource keeps ALL I/O — anchor resolution, persistence, logging, the
historyCursorAdvanced emit — and now delegates every decision to the drain core.
Behaviour-preserving: each site maps 1:1 to the extracted logic (verified by diff).
Also de-versions the [cursor-fix v1] log marker to [cursor-fix].

14 new OuraHistoryDrainTests + the existing 91 pass under swift test (105/105, run
on Linux — OuraProtocol is pure). OuraLiveSource compile: app-build.
DX23876 pushed a commit to DX23876/noop that referenced this pull request Jul 22, 2026
…latforms CI-test the parity (ryanbr#286 follow-up) (ryanbr#307)

dataRangeNewestUnix decodes strapNewestTs, which gates automatic sync
(isFutureDatedNewest -> BackfillPolicy), so it must stay byte-identical Swift/
Kotlin. ryanbr#286 left it inline in app-target BLEManager/WhoopBleClient with a Kotlin
test only — a Swift-side edit could silently drift the parity (the same gap the
Oura ryanbr#306 follow-up closed).

Move the pure scan into WhoopProtocol.DataRange (Swift, builds+tests on Linux) and
com.noop.protocol.DataRange (Kotlin), byte-identical twins. BLEManager and
WhoopBleClient keep their signatures as thin wrappers that pass their own
futureSkew constant, so all call sites + the existing DataRangeScanTest are
unchanged. Adds DataRangeTests (Swift) mirroring the Kotlin fixtures — the real
WHOOP 4.0 offset-8 frames + the future/skew cases.

WhoopProtocol swift test 259/259 incl. 4 new DataRangeTests (run on Linux);
Android compileFullDebugKotlin + DataRangeScanTest green. BLEManager compile: app-build.
@ryanbr
ryanbr deleted the oura-drain-extract-test branch August 2, 2026 07:41
simoncad7 pushed a commit to simoncad7/noop that referenced this pull request Aug 17, 2026
…tested core (ryanbr#291 follow-up) (ryanbr#306)

The ryanbr#91 drain guards + resume-cursor logic lived untested inline in OuraLiveSource
(app-target BLE) — which is exactly why it silently regressed when the Oura BLE
stack was refactored (ryanbr#291: "lost when refactoring"). Move the DECISIONS into a
pure OuraHistoryDrain in OuraProtocol so they're pinned by unit tests, not just
on-device observation:

- drain-to-zero + stall guard (bytes_left must shrink; stop after 3 flat summaries)
  + 300s deadline guard, keeping banked progress on a forced stop
- stored-ring-time high-water mark + reboot flag (a real sample older than the seek)
- resume-cursor commit (advance only if forward AND resolves; reboot -> 0 full pull)
- loaded-cursor plausibility-ceiling sanitize

OuraLiveSource keeps ALL I/O — anchor resolution, persistence, logging, the
historyCursorAdvanced emit — and now delegates every decision to the drain core.
Behaviour-preserving: each site maps 1:1 to the extracted logic (verified by diff).
Also de-versions the [cursor-fix v1] log marker to [cursor-fix].

14 new OuraHistoryDrainTests + the existing 91 pass under swift test (105/105, run
on Linux — OuraProtocol is pure). OuraLiveSource compile: app-build.
simoncad7 pushed a commit to simoncad7/noop that referenced this pull request Aug 17, 2026
…latforms CI-test the parity (ryanbr#286 follow-up) (ryanbr#307)

dataRangeNewestUnix decodes strapNewestTs, which gates automatic sync
(isFutureDatedNewest -> BackfillPolicy), so it must stay byte-identical Swift/
Kotlin. ryanbr#286 left it inline in app-target BLEManager/WhoopBleClient with a Kotlin
test only — a Swift-side edit could silently drift the parity (the same gap the
Oura ryanbr#306 follow-up closed).

Move the pure scan into WhoopProtocol.DataRange (Swift, builds+tests on Linux) and
com.noop.protocol.DataRange (Kotlin), byte-identical twins. BLEManager and
WhoopBleClient keep their signatures as thin wrappers that pass their own
futureSkew constant, so all call sites + the existing DataRangeScanTest are
unchanged. Adds DataRangeTests (Swift) mirroring the Kotlin fixtures — the real
WHOOP 4.0 offset-8 frames + the future/skew cases.

WhoopProtocol swift test 259/259 incl. 4 new DataRangeTests (run on Linux);
Android compileFullDebugKotlin + DataRangeScanTest green. BLEManager compile: app-build.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant