Skip to content

Mi Body Composition Scale 2: pin the real 7-byte history count reply - #608

Merged
abdulsaheel merged 6 commits into
OpenStrap:mainfrom
Riedel-David:fix/miscale-composition-history
Nov 2, 2026
Merged

abdulsaheel merged 6 commits into
OpenStrap:mainfrom
Riedel-David:fix/miscale-composition-history

Conversation

@Riedel-David

@Riedel-David Riedel-David commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Needs OpenStrap/protocol#81, which is merged.

The XMTZC05HM answers the history request with 7 bytes, not 3. The composition history test now sends that reply (captured on a real scale), so it fails on a protocol that can't read it. Header comments mark the Composition Scale 2 as verified on hardware; the Mi Smart Scale 2 stays experimental.

The openstrap_protocol pin is bumped to b4fde6a, the #81 merge commit.

Summary by Sourcery

Support the verified Mi Body Composition Scale 2 protocol responses by updating the protocol pin and hardware-backed history test.

Bug Fixes:

  • Update Mi Body Composition Scale 2 history handling to validate the real seven-byte history count response captured from hardware.

Enhancements:

  • Mark the Mi Body Composition Scale 2 as hardware-verified while retaining experimental status for the Mi Smart Scale 2.

Build:

  • Pin the OpenStrap protocol dependency to the merge containing the scale history fix and required decoder hardening changes.

Tests:

  • Update composition history coverage to use the scale's real seven-byte count reply.

Pin range

89080bc to b4fde6a is 41 protocol commits, not only #81: the AGPL license/docs (#80) and #82 decoder-hardening, which #81 sits on top of, so it cannot be pinned alone. #82 gates the R10 decoders on packet type, changes hello/battery parsing, adds reassembler truncation semantics and drops deprecated gen5 APIs. No kAlgoVersion bump here, to keep this PR small. Full suite run against the new pin shows only 3 failures (low_readiness_notification_test x2, health_workout_export_delete_gate_test x1), which fail identically on main.

Not adopted yet: edge does not read the reassembler's new lostFrames signal on the commit-before-ACK path (follow-up).

Summary by CodeRabbit

  • New Features
    • Added support for retrieving stored history from the Mi Body Composition Scale 2.
  • Compatibility
    • The Mi Body Composition Scale 2 (XMTZC05HM) has been physically verified for pairing, stored history, and live weight readings with impedance measurements.
    • The Mi Smart Scale 2 remains experimental; it has not been verified on a physical unit.

The XMTZC05HM answers the history request with 7 bytes, not 3. The
composition history test now sends that reply, so it fails on a protocol
that cannot read it. Mark the composition scale as verified on hardware;
the Mi Smart Scale 2 stays experimental.

Needs the openstrap_protocol pin bumped to a commit containing
OpenStrap/protocol#81.
@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: OpenStrap/edge
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: abd50bb
  • Layers: Repository YAML
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: d03dc81a-07af-4ac5-bf2d-b10aed199160
Commits

Reviewing files that changed from the base of the PR and between 876d6b5 and b4c3a2f.

Files ignored due to path filters: 2
  • pubspec.lock is excluded by !**/*.lock
  • test/adapters/miscale_test.dart is excluded by !test/**
Files selected for processing: 1
  • lib/compute/derivation_engine.dart

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 1 remain after this review.

Walkthrough

The protocol dependency pin changed. Documentation records physical verification of the Mi Body Composition Scale 2 for pairing, stored history, and live weight with impedance. The Mi Smart Scale 2 remains experimental.

Changes: Mi Scale support

Layer / File(s) Summary
Protocol pin and scale verification notes
pubspec.yaml, lib/compute/derivation_engine.dart, lib/ble/adapters/miscale.dart, lib/ble/adapters/_registry.dart
The protocol pin changed. Its comments note Mi Body Composition Scale 2 history-count support and decoder hardening, and state that no algorithm-version bump is required. The adapter documentation records physical verification for the Mi Body Composition Scale 2 and retains the experimental status for the Mi Smart Scale 2.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Possibly related PRs

  • OpenStrap/edge#612: Adds foreground discovery and syncing for paired Mi Scale devices. This change adds the protocol support for decoding the seven-byte history-count reply used during that sync.

Suggested reviewers: abdulsaheel

Merge Risk: Minimal · up to b4c3a

The protocol pin and scale verification notes introduce no established runtime, data, security, or availability regression.

Pre-merge checks: Passed 4
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and specifically describes the main change: pinning the protocol version that supports the Mi Body Composition Scale 2's 7-byte history-count reply.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@sourcery-ai

sourcery-ai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Align Mi scale support with the verified XMTZC05HM hardware by testing its real seven-byte history-count response, pinning the compatible OpenStrap protocol revision, and updating device verification documentation while keeping the Mi Smart Scale 2 experimental.

Sequence diagram for Mi Body Composition Scale 2 history retrieval

sequenceDiagram
    participant Test as CompositionHistoryTest
    participant Scale as XMTZC05HM
    participant Protocol as OpenStrapProtocol

    Test->>Scale: history request
    Scale-->>Test: 7-byte history-count reply
    Test->>Protocol: decode history-count reply
    Protocol-->>Test: decoded history count
Loading

File-Level Changes

Change Details Files
Update the composition-scale history fixture to match the real hardware response format.
  • Change the simulated history-count reply from three bytes to the captured seven-byte payload.
  • Keep subsequent history records synthetic while exercising the protocol’s ability to parse the real count response.
test/adapters/miscale_test.dart
Pin the application to the protocol revision that supports the seven-byte history reply and associated decoder changes. pubspec.yaml
pubspec.lock
Clarify hardware verification status for the supported Mi scale variants.
  • Mark the Mi Body Composition Scale 2 (XMTZC05HM) as verified for pairing, stored history, and live impedance measurements.
  • Retain experimental status for the Mi Smart Scale 2.
lib/ble/adapters/_registry.dart
lib/ble/adapters/miscale.dart

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Riedel-David
Riedel-David marked this pull request as ready for review November 1, 2026 11:50

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="pubspec.yaml" line_range="271" />
<code_context>
+      # (R10 gated on packet type, hello/battery parsing, reassembler
+      # truncation semantics, removal of deprecated gen5 APIs). #81 sits on top
+      # of #82, so it cannot be pinned alone.
+      ref: b4fde6ab5589e53154b808f8b2bbb4d01d9c15b5
   openstrap_analytics:
     git:
</code_context>
<issue_to_address>
**Lost history records are discarded**

When the reassembler reports lost frames during historical sync, including a burst that passes the count gate, `BleEngine._onChunk` ignores `lostFrames`, so the history drain can commit and ACK an incomplete burst; the band then trims records that were never stored, leaving the user with missing history.

Have `_onChunk` account for `lostFrames` and prevent an incomplete burst from reaching commit or ACK.
</issue_to_address>

### Comment 2
<location path="pubspec.yaml" line_range="271" />
<code_context>
+      # (R10 gated on packet type, hello/battery parsing, reassembler
+      # truncation semantics, removal of deprecated gen5 APIs). #81 sits on top
+      # of #82, so it cannot be pinned alone.
+      ref: b4fde6ab5589e53154b808f8b2bbb4d01d9c15b5
   openstrap_analytics:
     git:
</code_context>
<issue_to_address>
**Pin check fails after repin**

When the pin-consistency test runs with the updated protocol dependency ref, `kProtocolPin` still contains the old SHA, so `db_serve_version_and_reads_test` compares it with the new dependency ref and fails, making the test suite red.

Update `kProtocolPin` to match the protocol ref selected by `pubspec.yaml` and `pubspec.lock`.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and if the updated protocol decoder mishandles the 7-byte reply, it could request or decode the wrong scale-history entries and leave incorrect weight measurements in the app. Those records are bounded and can be corrected or re-imported; reverting restores the previous decoder behavior but would not undo any records already written.

Blocking findings: pubspec.yaml:271, pubspec.yaml:271


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread pubspec.yaml Resolved
Comment thread pubspec.yaml Resolved
@abdulsaheel
abdulsaheel merged commit 69cef42 into OpenStrap:main Nov 2, 2026
3 of 4 checks passed
@Riedel-David
Riedel-David deleted the fix/miscale-composition-history branch November 2, 2026 15:59
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.

2 participants