FA-63981 / Medication dosing calculations / Open access
Meal and correction insulin bolus with insulin on board: insulin on board scope · case 01
Insulin on board keeps eating into the meal dose after the correction reaches zero.
ROOT CAUSE
IOB is subtracted from the correction unconditionally and the result is not floored, so it reduces carbohydrate coverage.
VERIFIED REPAIR
Subtract IOB only from a positive correction and floor that correction at zero; leave negative corrections untouched.
Unsuccessful approach: Flooring every correction at zero also discards the stipulated meal reduction when glucose is below target.
Case contract
Input {bg, target, isf, carbs_g, icr, iob, max_units}. Toy rule: bg < 70 returns HOLD with 0 units. correction = (bg-target)/isf; when positive, insulin on board is subtracted from it and the result floored at 0; a negative correction (bg below target) is kept and reduces the meal dose. meal = carbs_g/icr. total = max(0, meal + correction), capped at max_units, then rounded DOWN to 0.5 units. Return {action GIVE|HOLD, units}.
Why this case matters
Bolus calculators combine several unit ratios and safety rules; insulin-on-board, hypoglycaemia holds and pen resolution are separate decision points.
1 / The failure
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
bg, tgt = x['bg'], x['target']
if bg < 70:
return {'action': 'HOLD', 'units': 0.0}
corr = Fraction(bg - tgt, x['isf'])
corr = corr - Fraction(str(x['iob']))
meal = Fraction(x['carbs_g'], x['icr'])
total = max(Fraction(0), meal + corr)
total = min(total, Fraction(str(x['max_units'])))
units = Fraction(math.floor(total * 2), 2)
return {'action': 'GIVE', 'units': float(units)}
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('low glucose with meal is held',
{'bg': 61, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 65, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 8.5}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 40, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 59, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 5.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.8'},
{'action': 'GIVE', 'units': 7.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 11},
{'action': 'GIVE', 'units': 2.0})],
[('low glucose with meal is held',
{'bg': 62, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 5.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 9.0}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 2, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 5.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 49, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 4.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.6'},
{'action': 'GIVE', 'units': 7.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 12},
{'action': 'GIVE', 'units': 2.0})],
[('low glucose with meal is held',
{'bg': 63, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 6.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 75, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 9.5}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 6.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 38, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 3.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '4.7'},
{'action': 'GIVE', 'units': 4.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 13},
{'action': 'GIVE', 'units': 2.0})],
[('low glucose with meal is held',
{'bg': 64, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 7.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 10.0}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 4, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 7.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 69, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 6.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '6.9'},
{'action': 'GIVE', 'units': 6.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 14},
{'action': 'GIVE', 'units': 2.0})],
[('low glucose with meal is held',
{'bg': 65, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 90, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 8.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 85, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 10.5}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 5, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 8.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 29, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 2.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '10.6'},
{'action': 'GIVE', 'units': 10.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 2.0})]]
for label, args, expected in fixtures[N-1]:
check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| low glucose with meal is held | {'action': 'HOLD', 'units': 0.0} | {'action': 'HOLD', 'units': 0.0} | Passed |
| glucose exactly 70 is dosed | {'action': 'GIVE', 'units': 4.0} | {'action': 'GIVE', 'units': 4.0} | Passed |
| high glucose with insulin on board | {'action': 'GIVE', 'units': 8.5} | {'action': 'GIVE', 'units': 8.5} | Passed |
| below target with insulin on board | {'action': 'GIVE', 'units': 3.0} | {'action': 'GIVE', 'units': 4.0} | Failed |
| insulin on board exceeds correction | {'action': 'GIVE', 'units': 2.0} | {'action': 'GIVE', 'units': 4.0} | Failed |
| pen resolution rounds down | {'action': 'GIVE', 'units': 5.5} | {'action': 'GIVE', 'units': 5.5} | Passed |
| maximum bolus cap not on half unit | {'action': 'GIVE', 'units': 7.5} | {'action': 'GIVE', 'units': 7.5} | Passed |
| correction only control | {'action': 'GIVE', 'units': 2.0} | {'action': 'GIVE', 'units': 2.0} | Passed |
SHA-256 / 2a0cfa1d46883da13fb4077f40661e054de4b981884d14ce8165511acf2ea8d2
2 / The unsuccessful fix
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
bg, tgt = x['bg'], x['target']
if bg < 70:
return {'action': 'HOLD', 'units': 0.0}
corr = Fraction(bg - tgt, x['isf'])
corr = max(Fraction(0), corr - Fraction(str(x['iob'])))
meal = Fraction(x['carbs_g'], x['icr'])
total = max(Fraction(0), meal + corr)
total = min(total, Fraction(str(x['max_units'])))
units = Fraction(math.floor(total * 2), 2)
return {'action': 'GIVE', 'units': float(units)}
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('low glucose with meal is held',
{'bg': 61, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 65, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 8.5}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 40, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 59, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 5.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.8'},
{'action': 'GIVE', 'units': 7.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 11},
{'action': 'GIVE', 'units': 2.0})],
[('low glucose with meal is held',
{'bg': 62, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 5.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 9.0}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 2, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 5.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 49, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 4.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.6'},
{'action': 'GIVE', 'units': 7.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 12},
{'action': 'GIVE', 'units': 2.0})],
[('low glucose with meal is held',
{'bg': 63, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 6.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 75, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 9.5}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 6.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 38, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 3.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '4.7'},
{'action': 'GIVE', 'units': 4.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 13},
{'action': 'GIVE', 'units': 2.0})],
[('low glucose with meal is held',
{'bg': 64, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 7.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 10.0}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 4, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 7.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 69, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 6.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '6.9'},
{'action': 'GIVE', 'units': 6.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 14},
{'action': 'GIVE', 'units': 2.0})],
[('low glucose with meal is held',
{'bg': 65, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 90, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 8.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 85, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 10.5}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 5, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 8.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 29, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 2.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '10.6'},
{'action': 'GIVE', 'units': 10.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 2.0})]]
for label, args, expected in fixtures[N-1]:
check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| low glucose with meal is held | {'action': 'HOLD', 'units': 0.0} | {'action': 'HOLD', 'units': 0.0} | Passed |
| glucose exactly 70 is dosed | {'action': 'GIVE', 'units': 5.0} | {'action': 'GIVE', 'units': 4.0} | Failed |
| high glucose with insulin on board | {'action': 'GIVE', 'units': 8.5} | {'action': 'GIVE', 'units': 8.5} | Passed |
| below target with insulin on board | {'action': 'GIVE', 'units': 5.0} | {'action': 'GIVE', 'units': 4.0} | Failed |
| insulin on board exceeds correction | {'action': 'GIVE', 'units': 4.0} | {'action': 'GIVE', 'units': 4.0} | Passed |
| pen resolution rounds down | {'action': 'GIVE', 'units': 5.5} | {'action': 'GIVE', 'units': 5.5} | Passed |
| maximum bolus cap not on half unit | {'action': 'GIVE', 'units': 7.5} | {'action': 'GIVE', 'units': 7.5} | Passed |
| correction only control | {'action': 'GIVE', 'units': 2.0} | {'action': 'GIVE', 'units': 2.0} | Passed |
SHA-256 / d1144020b39da7a57e787ea5e0bba9e5a54bc19071d530508cffe84ae1ba245d
3 / The verified repair
Exit 0"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
bg, tgt = x['bg'], x['target']
if bg < 70:
return {'action': 'HOLD', 'units': 0.0}
corr = Fraction(bg - tgt, x['isf'])
if corr > 0:
corr = max(Fraction(0), corr - Fraction(str(x['iob'])))
meal = Fraction(x['carbs_g'], x['icr'])
total = max(Fraction(0), meal + corr)
total = min(total, Fraction(str(x['max_units'])))
units = Fraction(math.floor(total * 2), 2)
return {'action': 'GIVE', 'units': float(units)}
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('low glucose with meal is held',
{'bg': 61, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 65, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 8.5}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 40, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 59, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 5.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.8'},
{'action': 'GIVE', 'units': 7.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 11},
{'action': 'GIVE', 'units': 2.0})],
[('low glucose with meal is held',
{'bg': 62, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 5.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 9.0}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 2, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 5.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 49, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 4.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.6'},
{'action': 'GIVE', 'units': 7.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 12},
{'action': 'GIVE', 'units': 2.0})],
[('low glucose with meal is held',
{'bg': 63, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 6.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 75, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 9.5}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 6.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 38, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 3.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '4.7'},
{'action': 'GIVE', 'units': 4.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 13},
{'action': 'GIVE', 'units': 2.0})],
[('low glucose with meal is held',
{'bg': 64, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 7.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 10.0}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 4, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 7.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 69, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 6.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '6.9'},
{'action': 'GIVE', 'units': 6.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 14},
{'action': 'GIVE', 'units': 2.0})],
[('low glucose with meal is held',
{'bg': 65, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'HOLD', 'units': 0.0}),
('glucose exactly 70 is dosed',
{'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 90, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 8.0}),
('high glucose with insulin on board',
{'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 85, 'icr': 10, 'iob': 1, 'max_units': 15},
{'action': 'GIVE', 'units': 10.5}),
('below target with insulin on board',
{'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 5, 'max_units': 15},
{'action': 'GIVE', 'units': 4.0}),
('insulin on board exceeds correction',
{'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 3, 'max_units': 15},
{'action': 'GIVE', 'units': 8.0}),
('pen resolution rounds down',
{'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 29, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 2.5}),
('maximum bolus cap not on half unit',
{'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '10.6'},
{'action': 'GIVE', 'units': 10.5}),
('correction only control',
{'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 15},
{'action': 'GIVE', 'units': 2.0})]]
for label, args, expected in fixtures[N-1]:
check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| low glucose with meal is held | {'action': 'HOLD', 'units': 0.0} | {'action': 'HOLD', 'units': 0.0} | Passed |
| glucose exactly 70 is dosed | {'action': 'GIVE', 'units': 4.0} | {'action': 'GIVE', 'units': 4.0} | Passed |
| high glucose with insulin on board | {'action': 'GIVE', 'units': 8.5} | {'action': 'GIVE', 'units': 8.5} | Passed |
| below target with insulin on board | {'action': 'GIVE', 'units': 4.0} | {'action': 'GIVE', 'units': 4.0} | Passed |
| insulin on board exceeds correction | {'action': 'GIVE', 'units': 4.0} | {'action': 'GIVE', 'units': 4.0} | Passed |
| pen resolution rounds down | {'action': 'GIVE', 'units': 5.5} | {'action': 'GIVE', 'units': 5.5} | Passed |
| maximum bolus cap not on half unit | {'action': 'GIVE', 'units': 7.5} | {'action': 'GIVE', 'units': 7.5} | Passed |
| correction only control | {'action': 'GIVE', 'units': 2.0} | {'action': 'GIVE', 'units': 2.0} | Passed |
SHA-256 / 83a53204570077d92ce524dde2f25d1630a6f0850a027abf8e070f15326367b8
Verification & scope
A deterministic toy software model with explicitly stipulated thresholds and formulas; it is not clinical guidance, not a validated medical calculator, and makes no claim of conformance to any published protocol. Numbered variants vary patient and order inputs. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.
Observations recorded using Python 3.12.14 at 2026-09-29T14:47:19.843250+00:00.
Case digest / 36c77608ddc65f746585ad28275fc6e9dfa3410effc2ec4b5ae8efc8fa79b7b5