Skip to content

fix: check WIRE record size against the active definition - #91

Merged
shaonianche merged 2 commits into
mainfrom
fix/strict-wire-record-size
Nov 20, 2026
Merged

shaonianche merged 2 commits into
mainfrom
fix/strict-wire-record-size

Conversation

@shaonianche

Copy link
Copy Markdown
Owner

What this fixes

WIRE validation compared DataMessage.size — which counts every valid field, including fields that decode-time projection adds — with definition.defined_data_size. Component expansion (enhanced_speed from speed, enhanced_altitude from altitude) and the field 253 timestamp reconstructed for a compressed-timestamp record were therefore counted as wire payload, so the default validation path rejected files that round-trip byte-for-byte.

validate_data_message now sums the payload from the active definition: a listed field counts at its own size, or at its definition size when it was cleared in memory (encode pads it with protocol-invalid bytes). A genuine size mismatch is still reported.

Caller-visible changes

  1. WIRE size errors are gone for component-expanded fields and reconstructed compressed timestamps. fit.validate() no longer reports false ERRORs on standard Garmin activities, and to_bytes(strict=True) / FitFileBuilder(strict=True) no longer refuse files that round-trip byte-for-byte.
  2. An Activity file_id no longer requires serial_number. Garmin's FIT documentation marks the field optional, and Garmin's own activity files (Edge 1030 Plus, Forerunner 945 and Fenix 3 samples) omit it, carrying the device serial in device_info instead. type, manufacturer and time_created are still required.

Also: clearing a field after decode (for example record.heart_rate = None) no longer trips the WIRE size check, because encode writes the protocol-invalid value at the definition's size.

Before / after

Check Before After
sdk/Activity.fit — validate() findings 9,144 ERRORs (9,143 WIRE + 1 FILE_TYPE) 0
WIRE findings across all 22 committed fixtures 33,667 0
Total findings across all 22 fixtures 33,708 35
Activity.fit — to_bytes(strict=True) FitValidationError: record encodes 24 bytes but its definition requires 16 succeeds, byte-identical to the input
FitFileBuilder(strict=True) over a decoded activity raises builds
All 22 fixtures — preserve re-encode byte-identical byte-identical

The 35 remaining findings are the separate, documented gaps (developer-field metadata, unimplemented file types, workout_step ambiguity, message_index requirements), not WIRE size errors.

Tests

  • uv run pytest: 484 passed, 1 skipped (the skip is the live Garmin JS interop test, unchanged).
  • uv run ruff check fit_tool: clean. uv run mypy: clean.
  • New regression tests: a standard Garmin activity validates clean under strict mode and re-encodes byte-identically; component-expanded fields are not counted as payload; a corrupted definition size is still caught; a constructed compressed-timestamp record is not flagged while a corrupted size on it is.
  • Each change was reverted separately to confirm it is load-bearing.

News fragment: news/+strict-wire-record-size.bugfix.

Component expansion and compressed-timestamp reconstruction add fields to a
decoded message that its definition never writes: enhanced_speed derived from
speed, enhanced_altitude from altitude, and field 253 rebuilt from the 5-bit
offset. DataMessage.size counted them, so WIRE compared a projected size with a
wire size. 9,143 of the 9,194 data records in Garmin's Activity.fit were
reported oversized, and to_bytes(strict=True) / FitFileBuilder(strict=True)
refused files that round-trip byte-for-byte.

validate_data_message now sums the payload from the active definition, counting
a listed field at its own size or, when it was cleared in memory, at its
definition size because encode pads it with protocol-invalid bytes. A genuine
size mismatch is still reported.

An Activity file_id no longer requires serial_number: Garmin's FIT documentation
marks the field optional and Garmin's own activity files (Edge 1030 Plus,
Forerunner 945 and Fenix 3 samples) omit it, carrying the device serial in
device_info instead.

Tests: a standard Garmin activity validates with no findings and re-encodes
byte-identically under strict mode; component-expanded and compressed-timestamp
records are not counted as payload; a corrupted definition size is still caught.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1f763-9923-75c7-b1ef-d1817be4902d
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.35%. Comparing base (af9e1d3) to head (67aaa43).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #91      +/-   ##
==========================================
+ Coverage   93.21%   93.35%   +0.14%     
==========================================
  Files          46       46              
  Lines        2755     2771      +16     
  Branches      436      440       +4     
==========================================
+ Hits         2568     2587      +19     
+ Misses        107      105       -2     
+ Partials       80       79       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

The size check is driven by the active definition, so both directions need a
guard: fields the definition does not list are not payload, and a field the
definition lists still counts when the projected message no longer carries it
(native and developer fields). The second direction is what stops a broken
projection from passing as definition-sized.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1f763-9923-75c7-b1ef-d1817be4902d
@shaonianche
shaonianche merged commit b3d8c2b into main Nov 20, 2026
10 checks passed
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