Skip to content

Fix inverted time factors in mixed glucose/time unit conversions - #15

Merged
ps2 merged 1 commit into
LoopKit:mainfrom
sbalint:fix/loopunit-rate-conversion-factors
Oct 26, 2026
Merged

ps2 merged 1 commit into
LoopKit:mainfrom
sbalint:fix/loopunit-rate-conversion-factors

Conversation

@sbalint

@sbalint sbalint commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Summary

LoopUnit.conversionFactor applies a duration-style time factor (×1/60 for seconds → minutes) to the four conversions that change both the glucose unit and the time unit. Rates convert in the opposite direction, so each of these was off by 3600×:

Conversion Was Should be
mg/dL·s → mmol/L·min 1/UMM / 60 1/UMM * 60
mg/dL·min → mmol/L·s 1/UMM * 60 1/UMM / 60
mmol/L·s → mg/dL·min UMM / 60 UMM * 60
mmol/L·min → mg/dL·s UMM * 60 UMM / 60

The same-glucose-unit rate cases (mg/dL·s → mg/dL·min) were already correct, and the scalar cases (mg/dL → mmol/L) were already correct. Only the mixed conversions were wrong, so only mmol/L users were affected.

User-visible impact

Most visibly this broke the carb effect chart. Insulin counteraction effects are stored in mg/dL·s and CarbEffectChart requests them in <glucose unit>/min, so for mmol/L users the observed series rendered 3600× too small and collapsed onto zero — while the predicted carb effect series, which never uses a rate unit, rendered correctly. The chart looked like the algorithm had failed to observe any carb absorption at all.

Dosing is unaffected. The algorithm paths stay in mg/dL throughout and never hit a mixed conversion. This is a display-layer bug only.

Background

These four cases were previously grouped two-per-case, with each group pairing opposite glucose directions under one shared factor. fa581b3 ([LOOP-5162] unit conversion fix) split them so each pair gets its own glucose factor — correct — but the /60 and *60 time factors were carried over from the old groupings and ended up attached to the wrong pairs. The accompanying test updates encoded the new values, so the suite has passed against the inverted factors since.

Test plan

  • The existing tests asserted the same incorrect factors, so the suite passed against the bug. Those assertions are corrected.
  • testConversionFactorsMatchHealthKit now checks every unit pair against HealthKit's own dimensional analysis, so the table cannot silently drift again.
  • Full suite: 183 tests, 0 failures against current main (9628dfe).
  • Verified in the Loop app in mmol/L: the observed carb absorption series now tracks the predicted series instead of sitting at zero.

🤖 Generated with Claude Code

LoopUnit.conversionFactor applied a duration-style time factor (x1/60 for
seconds to minutes) to the four conversions that change both the glucose
unit and the time unit. Rates convert in the opposite direction, so each
of these was off by 3600x:

  mg/dL·s  -> mmol/L·min
  mg/dL·min -> mmol/L·s
  mmol/L·s  -> mg/dL·min
  mmol/L·min -> mg/dL·s

The same-glucose-unit rate cases were already correct, so only mmol/L
users were affected.

Most visibly this broke the carb effect chart. Insulin counteraction
effects are stored in mg/dL·s and CarbEffectChart requests them in
<glucose unit>/min, so for mmol/L users the observed series rendered
3600x too small and collapsed onto zero, while the predicted carb effect
series - which never uses a rate unit - rendered correctly. Dosing is
unaffected; the algorithm paths stay in mg/dL throughout and never hit a
mixed conversion.

The existing tests asserted the same incorrect factors, so the suite
passed. Those assertions are corrected, and testConversionFactorsMatchHealthKit
now checks every unit pair against HealthKit's own dimensional analysis
so the table cannot silently drift again.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

@ps2 ps2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for tracking this down! The fix is right, and checking the whole table against HealthKit is a great guard. I confirmed the new test fails on exactly the four bad pairs with the old code and passes with the fix.

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