Skip to content

fix: generate the subfield-gated components on event.data - #92

Merged
shaonianche merged 1 commit into
mainfrom
fix/subfield-gated-components
Nov 20, 2026
Merged

shaonianche merged 1 commit into
mainfrom
fix/subfield-gated-components

Conversation

@shaonianche

Copy link
Copy Markdown
Owner

What was wrong

The bundled Profile (21.222.0) declares three component sets that apply only under an event subfield value, all packing into event.data:

Subfield (gate) Components
gear_change_data (event = 42/43) rear_gear_num, rear_gear, front_gear_num, front_gear (8 bits each)
radar_threat_alert (event = 75) radar_threat_level_max, radar_threat_count, radar_threat_avg_approach_speed, radar_threat_max_approach_speed (8 bits; speeds at scale 1/10)
sport_point (event = 33) score, opponent_score (16 bits each)

The generator emitted none of them, in two places:

  1. fit_tool/gen/profile.py had the components / bits / accumulate columns commented out, so no FieldComponent was ever attached to the SubField objects it built.
  2. fit_tool/gen/templates/template_message.jinja emitted SubField(...) without a components= argument.

fit_tool/gen/component_registry.py only ever produced main-field sources, and that is correct for it: its key is the packing field, which cannot express a gate. The runtime was never the problem — components_for_field() already prefers components on the active subfield. With nothing attached it expanded nothing, so a Di2/eTap gear-change or Varia radar-threat event decoded event.data as a single packed uint32 and the gear and radar fields read as empty.

What changed

  • gen/profile.py parses the subfield Components rows (per-component scale, offset, bit width, accumulate) and attaches them to the SubField. Destination ids come from a pre-pass over the Messages sheet because a destination field (front_gear) is declared after the data row that packs it.
  • gen/gen_profile.py sets a per-message has_subfield_components flag; the template emits the component list and imports FieldComponent only in messages that need it.
  • Regenerated with uv run gen-profile. Generated output diff is exactly two files: profile/messages/event_message.py gains the three component sets above (no other message module changes), and profile/component_registry.py changes only its header comment. A second generation run is byte-identical.
  • Docstrings that said subfield components were omitted now describe where they live, and the fixture gap inventory row points at the new tests.

Coverage is constructed, not real-device

No committed fixture carries event 33/42/43/75 — the fixtures' event values are 0, 1, 3, 4, 9 and 41. Every gear-change, radar-threat and sport-point case here is a constructed message or constructed wire bytes, so a reviewer should not read this as real-device coverage. A Di2/eTap or Varia file would be a valuable fixture to add later.

Tests and results

  • uv run pytest: 493 passed, 1 skipped (the skip is the live Garmin JS interop test, unchanged).
  • uv run ruff check fit_tool: clean. uv run mypy: clean.
  • All 23 committed .fit fixtures still decode and re-encode byte-identically.
  • New in test_components.TestProfileSubfieldGatedComponents: front and rear gear change, radar threat (raw 100/250 → 10.0/25.0 m/s, proving the per-component scale), sport point 16+16, an inactive event gate that must not expand, and a wire decode of a constructed gear-change record that asserts expansion plus a byte-identical re-encode. Five of these fail against the pre-change generated file.
  • New in test_profile.TestProfileSubfieldComponents: the generator attaches the three sets with the Profile's destination ids, bit widths and scales.

Caller-visible notes

  • event.data keeps its packed value; the gear/radar/score fields are added alongside and marked as expanded.
  • A destination already on the wire with a real value wins over the packed value; one present only as the invalid sentinel is filled from the packed data (same rule as existing main-field expansion).
  • Expansion is decode-side only. Authoring a gear-change event still means setting the packed data value; there is no pack-on-encode path.

News fragment: news/+subfield-gated-components.bugfix.

The Profile declares three component sets that apply only under an event
subfield: gear change (rear_gear_num, rear_gear, front_gear_num, front_gear),
Varia radar threat (level, count, average and maximum approach speed) and sport
point (score, opponent_score). The generator dropped all three: gen/profile.py
had the components, bits and accumulate columns commented out and never attached
FieldComponent entries to the SubField objects it built, and the message template
emitted SubField without components=. The runtime already prefers components on
the active subfield, so with nothing attached event.data decoded as one packed
uint32 and the gear and radar fields stayed empty.

Parse the subfield Components rows and emit them from the template. Destination
ids are resolved from a pre-pass over the Messages sheet because a destination
field (front_gear) is declared after the data row that packs it. Regenerated
output adds the components to event_message.py and leaves the main-field
component registry unchanged, since its key is the packing field and cannot
express a gate.

Coverage is constructive: no committed fixture carries event 33/42/43/75, so the
tests build messages and wire bytes for front and rear gear change, radar threat
(approach speeds at scale 1/10), sport point and an inactive event gate.

Tests: new expansion cases fail against the pre-change generated file; a decoded
gear-change record still re-encodes byte-for-byte, all 23 committed .fit fixtures
round-trip identically, and the full suite, ruff and mypy pass.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1f7e0-479c-740e-8086-4ee691ef3352
@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.43%. Comparing base (b3d8c2b) to head (81b2fd9).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #92      +/-   ##
==========================================
+ Coverage   93.35%   93.43%   +0.07%     
==========================================
  Files          46       46              
  Lines        2771     2771              
  Branches      440      440              
==========================================
+ Hits         2587     2589       +2     
+ Misses        105      104       -1     
+ Partials       79       78       -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.

@shaonianche
shaonianche merged commit 18b0e49 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