FAILURE MAP
← Case archive

FA-63996 / Medication dosing calculations / Open access

Meal and correction insulin bolus with insulin on board: cap before pen rounding · case 01

A 7.3 unit maximum produces a 7.3 unit dose that the pen cannot deliver.

Verified by executionVariant 1 · 8 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

The maximum is applied after rounding, reintroducing a non-half-unit value.

VERIFIED REPAIR

Cap the total first and then round down to 0.5 units.

Unsuccessful approach: Capping at the whole-unit floor of the maximum loses a deliverable half unit (7.8 maximum gives 7 instead of 7.5).

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'])
    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)
    units = min(Fraction(math.floor(total * 2), 2), Fraction(str(x['max_units'])))
    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 fixtureActualExpectedOutcome
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.8}{'action': 'GIVE', 'units': 7.5}Failed
correction only control{'action': 'GIVE', 'units': 2.0}{'action': 'GIVE', 'units': 2.0}Passed

SHA-256 / e24e1600d83ade44b215066163ed22fb01ab436fd5f2b24d31e4137515241b24

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'])
    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)
    units = Fraction(math.floor(total * 2), 2)
    units = min(units, Fraction(math.floor(Fraction(str(x['max_units']))), 1))
    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 fixtureActualExpectedOutcome
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.0}{'action': 'GIVE', 'units': 7.5}Failed
correction only control{'action': 'GIVE', 'units': 2.0}{'action': 'GIVE', 'units': 2.0}Passed

SHA-256 / 1c86da66daa40139daf1cd546098dddca47dc88c2d695652898dfa6af62005c3

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 fixtureActualExpectedOutcome
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.975699+00:00.

Case digest / 2510db236d0848888958beeaaab8dc19cc08f7ce3aebad14a177484a8b8acf49