Skip to content

feat: Support insulin concentrations (U-10 through U-500) in Trio#31

Draft
Sjoerd-Bo3 with Copilot wants to merge 4 commits into
devfrom
copilot/support-insulin-strengths-u50-to-u500
Draft

feat: Support insulin concentrations (U-10 through U-500) in Trio#31
Sjoerd-Bo3 with Copilot wants to merge 4 commits into
devfrom
copilot/support-insulin-strengths-u50-to-u500

Conversation

Copilot AI commented Apr 7, 2026

Copy link
Copy Markdown

Summary

Implements insulin concentration support for non-standard insulin strengths (U-10 through U-500) in Trio. This allows users of concentrated (U-200, U-500) or diluted (U-10, U-25, U-50) insulin to use their real clinical values for all therapy settings, while Trio automatically translates commands at the pump boundary.

Architecture: "Concentration Translator" Pattern

  • Everything above the translator thinks in real insulin units (IU)
  • Everything below (PumpManager/LoopKit) thinks in pump-native (volume-equivalent) units
  • For U-100 (default), all translations are identity — zero behavioral change for existing users
User/Algorithm/Storage (real units)
        │
  ConcentrationService  ← translates at boundary
        │
  PumpManager (pump-native units, unchanged)

Concentration Factor

factor = concentration / 100
To pump:   pumpUnits = realUnits / factor
From pump: realUnits = pumpUnits × factor

Changes

New Files

File Description
InsulinConcentration.swift Enum model (U-10 through U-500) with factor computation
ConcentrationService.swift Protocol and implementation for translating between real and pump units
ConcentrationServiceTests.swift Comprehensive unit tests (25+ test cases)

Modified Files

File Description
APSManager.swift Wire outbound translation (enactBolus, enactTempBasal, performBolus, performBasal, roundBolus, fetchCurrentTempBasal)
PumpHistoryStorage.swift Wire inbound translation (storePumpEvents for bolus and tempBasal)
DeviceDataManager.swift Translate reservoir readings to real units
TrioSettings.swift Update insulinConcentration from raw Decimal to InsulinConcentration enum with legacy migration
ServiceAssembly.swift Register ConcentrationService in DI container
UnitsLimitsSettingsRootView.swift Add concentration UI section with toggle, picker, and safety notice
UnitsLimitsSettingsStateModel.swift Add concentration state bindings
SettingItems.swift Add concentration search terms

Key Design Decisions

  • Zero algorithm changes — determine-basal.js, IOB, COB, TDD all untouched
  • Zero LoopKit/pump driver changes — works with all pumps without forking
  • Safety limits preserved — maxIOB, maxBolus stay in real clinical units
  • Backward compatible — defaults to U-100 (identity function = zero behavioral change)
  • Gated behind toggleallowDilution must be enabled before concentration can be changed
  • Legacy migration — handles existing Decimal format for insulinConcentration gracefully

Testing

  • 25+ unit tests covering all concentrations, round-trips, edge cases, Codable, and mock service
  • Tests cover: U-10, U-25, U-50, U-100, U-200, U-300, U-500
  • Verifies identity behavior for U-100 (no change from current behavior)
  • Verifies outbound/inbound translation accuracy
  • Verifies reservoir conversion

What's Untouched (by design)

  • determine-basal.js — algorithm sees real units, no changes needed
  • IOB/COB/TDD calculations — all operate on stored real units
  • Nightscout/Tidepool uploads — already store real units from CoreData
  • LoopKit pump drivers — no fork needed
  • Bolus calculator — works in real units

Copilot AI and others added 4 commits April 6, 2026 22:36
…translation layer

- Create InsulinConcentration enum (U-10 through U-500) with factor computation
- Create ConcentrationService protocol and BaseConcentrationService implementation
- Register ConcentrationService in ServiceAssembly DI container
- Update TrioSettings to use InsulinConcentration enum with legacy Decimal migration
- Wire outbound translation in APSManager (enactBolus, enactTempBasal, performBolus, performBasal, roundBolus)
- Wire inbound translation in PumpHistoryStorage (storePumpEvents for bolus and tempBasal)
- Wire inbound translation in APSManager.fetchCurrentTempBasal
- Wire reservoir translation in DeviceDataManager.didReadReservoirValue

Agent-Logs-Url: https://github.com/Sjoerd-Bo3/Trio/sessions/f24a45f7-0802-4ac1-8f87-316116313265

Co-authored-by: Sjoerd-Bo3 <2100083+Sjoerd-Bo3@users.noreply.github.com>
…prehensive unit tests

- Add concentration toggle and picker to UnitsLimitsSettings view
- Add safety notice for non-standard concentrations
- Wire allowDilution and insulinConcentration state bindings
- Add concentration-related search terms to settings items
- Add InsulinConcentrationTests and ConcentrationServiceTests
- Add MockConcentrationService for testing

Agent-Logs-Url: https://github.com/Sjoerd-Bo3/Trio/sessions/f24a45f7-0802-4ac1-8f87-316116313265

Co-authored-by: Sjoerd-Bo3 <2100083+Sjoerd-Bo3@users.noreply.github.com>
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