Repository navigation
Fix inverted time factors in mixed glucose/time unit conversions - #15
Merged
Merged
Conversation
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
approved these changes
Oct 26, 2026
ps2
left a comment
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
LoopUnit.conversionFactorapplies 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×:mg/dL·s→mmol/L·min1/UMM / 601/UMM * 60mg/dL·min→mmol/L·s1/UMM * 601/UMM / 60mmol/L·s→mg/dL·minUMM / 60UMM * 60mmol/L·min→mg/dL·sUMM * 60UMM / 60The 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·sandCarbEffectChartrequests 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/dLthroughout 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/60and*60time 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
testConversionFactorsMatchHealthKitnow checks every unit pair against HealthKit's own dimensional analysis, so the table cannot silently drift again.main(9628dfe).🤖 Generated with Claude Code