FA-97261 / Recipe scaling and nutrition / Open access
Sub-recipe allergen roll-up: nested precautionary labels · case 01
A "may contain sesame" note on a sub-recipe never reaches the menu item.
ROOT CAUSE
Only the root recipe contributes precautionary allergens.
VERIFIED REPAIR
Collect may-contain from every visited recipe.
Unsuccessful approach: Adding only leaf recipes still drops intermediate sub-recipes.
Case contract
recipes maps name -> {allergens, may_contain, uses}. Walk root and every used sub-recipe (depth first, in order). Names canonicalize: trim, lower, synonyms dairy->milk, peanuts->peanut, tree nuts->tree nut, eggs->egg. Result {contains, may_contain (minus contains), error: None}, both sorted. A shared sub-recipe reached twice (diamond) is fine; a cycle returns error "cycle: name" and an unknown name "unknown: name", with empty lists, stopping at the first error.
Why this case matters
Menu allergen matrices must propagate through nested sub-recipes without false cycle errors.
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(recipes, root):
SYN = {'dairy': 'milk', 'peanuts': 'peanut', 'tree nuts': 'tree nut', 'eggs': 'egg'}
def canon(a):
a = a.strip().lower()
return SYN.get(a, a)
contains, may = set(), set()
state = {}
def visit(n):
if n not in recipes:
return 'unknown: ' + n
if state.get(n) == 1:
return 'cycle: ' + n
if state.get(n) == 2:
return None
state[n] = 1
r = recipes[n]
contains.update(canon(a) for a in r.get('allergens', []))
if n == root:
may.update(canon(a) for a in r.get('may_contain', []))
for s in r.get('uses', []):
e = visit(s)
if e:
return e
state[n] = 2
return None
err = visit(root)
if err:
return {'contains': [], 'may_contain': [], 'error': err}
return {'contains': sorted(contains), 'may_contain': sorted(may - contains), 'error': None}
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['flat recipe', [{'base': {'allergens': ['milk'], 'may_contain': ['soy'], 'uses': []}}, 'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['soy']}],
['diamond sharing',
[{'a': {'allergens': ['wheat'], 'uses': ['c']},
'b': {'allergens': [], 'uses': ['c']},
'base': {'allergens': [], 'uses': ['a', 'b']},
'c': {'allergens': ['Dairy'], 'uses': []}},
'base'],
{'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []}],
['cycle', [{'a': {'uses': ['base']}, 'base': {'uses': ['a']}}, 'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['regression: nested precautionary labels',
[{'base': {'allergens': ['soy'], 'may_contain': [' sesame ', 'soy'], 'uses': ['sauce', 'crust']},
'crust': {'allergens': ['soy', ' sesame '], 'may_contain': [], 'uses': ['sauce']},
'filling': {'allergens': ['tree nuts'], 'may_contain': ['soy'], 'uses': ['topping']},
'sauce': {'allergens': ['peanuts'], 'may_contain': ['Egg', 'wheat'], 'uses': []},
'topping': {'allergens': [], 'may_contain': ['soy'], 'uses': []}},
'base'],
{'contains': ['peanut', 'sesame', 'soy'], 'error': None, 'may_contain': ['egg', 'wheat']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': ['wheat'], 'may_contain': [], 'uses': ['sauce', 'filling']},
'crust': {'allergens': ['eggs'], 'may_contain': ['eggs'], 'uses': []},
'filling': {'allergens': ['tree nuts', 'soy'], 'may_contain': ['soy', 'tree nuts'], 'uses': []},
'sauce': {'allergens': ['soy'], 'may_contain': ['peanuts', 'tree nuts'], 'uses': ['crust']},
'topping': {'allergens': [], 'may_contain': ['wheat', 'Egg'], 'uses': []}},
'base'],
{'contains': ['egg', 'soy', 'tree nut', 'wheat'], 'error': None, 'may_contain': ['peanut']}],
['generated control 1',
[{'base': {'allergens': ['eggs'], 'may_contain': [], 'uses': ['topping']},
'crust': {'allergens': ['eggs'], 'may_contain': ['dairy', 'Egg'], 'uses': ['filling', 'topping']},
'filling': {'allergens': ['wheat', 'soy'], 'may_contain': [], 'uses': ['topping']},
'sauce': {'allergens': ['tree nuts', ' sesame '],
'may_contain': ['wheat', 'soy'],
'uses': ['topping', 'base']},
'topping': {'allergens': [], 'may_contain': ['peanuts'], 'uses': ['mystery']}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}],
['generated control 2',
[{'base': {'allergens': ['milk', 'dairy'], 'may_contain': ['dairy', 'tree nuts'], 'uses': ['topping']},
'crust': {'allergens': [' sesame ', 'Egg'], 'may_contain': ['peanuts', 'tree nuts'], 'uses': []},
'filling': {'allergens': [' sesame ', 'peanuts'], 'may_contain': [], 'uses': []},
'sauce': {'allergens': ['tree nuts'], 'may_contain': ['Egg', 'eggs'], 'uses': ['filling']},
'topping': {'allergens': [], 'may_contain': [], 'uses': []}},
'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['tree nut']}],
['generated control 3',
[{'base': {'allergens': ['milk'], 'may_contain': [], 'uses': ['crust', 'topping']},
'crust': {'allergens': ['soy'], 'may_contain': ['peanuts'], 'uses': ['topping', 'base']},
'filling': {'allergens': ['wheat'], 'may_contain': ['tree nuts'], 'uses': ['topping']},
'sauce': {'allergens': [' sesame '], 'may_contain': [], 'uses': ['topping']},
'topping': {'allergens': [' sesame ', 'milk'], 'may_contain': [], 'uses': []}},
'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}]],
[['diamond sharing',
[{'a': {'allergens': ['wheat'], 'uses': ['c']},
'b': {'allergens': [], 'uses': ['c']},
'base': {'allergens': [], 'uses': ['a', 'b']},
'c': {'allergens': ['Dairy'], 'uses': []}},
'base'],
{'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []}],
['cycle', [{'a': {'uses': ['base']}, 'base': {'uses': ['a']}}, 'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['unknown sub', [{'base': {'uses': ['ghost']}}, 'base'],
{'contains': [], 'error': 'unknown: ghost', 'may_contain': []}],
['regression: nested precautionary labels',
[{'base': {'allergens': ['dairy'], 'may_contain': ['soy'], 'uses': ['crust', 'topping']},
'crust': {'allergens': [], 'may_contain': [' sesame ', 'Egg'], 'uses': ['filling', 'sauce']},
'filling': {'allergens': [], 'may_contain': ['milk', 'eggs'], 'uses': []},
'sauce': {'allergens': ['Egg'], 'may_contain': [], 'uses': ['filling']},
'topping': {'allergens': [], 'may_contain': ['peanuts', 'milk'], 'uses': []}},
'base'],
{'contains': ['egg', 'milk'], 'error': None, 'may_contain': ['peanut', 'sesame', 'soy']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': ['dairy'],
'may_contain': ['milk', 'eggs'],
'uses': ['sauce', 'filling', 'topping']},
'crust': {'allergens': ['eggs'], 'may_contain': ['tree nuts'], 'uses': ['topping']},
'filling': {'allergens': [], 'may_contain': ['soy'], 'uses': ['topping']},
'sauce': {'allergens': [' sesame '], 'may_contain': ['Egg'], 'uses': ['crust']},
'topping': {'allergens': [], 'may_contain': [], 'uses': []}},
'base'],
{'contains': ['egg', 'milk', 'sesame'], 'error': None, 'may_contain': ['soy', 'tree nut']}],
['generated control 1',
[{'base': {'allergens': ['wheat', 'peanuts'], 'may_contain': [], 'uses': ['sauce', 'crust']},
'crust': {'allergens': [], 'may_contain': ['wheat'], 'uses': ['topping']},
'filling': {'allergens': [], 'may_contain': ['wheat', 'milk'], 'uses': []},
'sauce': {'allergens': ['dairy'], 'may_contain': ['dairy'], 'uses': ['crust', 'topping']},
'topping': {'allergens': [], 'may_contain': [], 'uses': []}},
'base'],
{'contains': ['milk', 'peanut', 'wheat'], 'error': None, 'may_contain': []}],
['generated control 2',
[{'base': {'allergens': [], 'may_contain': [], 'uses': ['sauce', 'topping', 'mystery']},
'crust': {'allergens': [], 'may_contain': ['wheat'], 'uses': ['filling', 'topping']},
'filling': {'allergens': ['tree nuts', 'Egg'], 'may_contain': ['Egg', 'milk'], 'uses': []},
'sauce': {'allergens': [], 'may_contain': [], 'uses': ['crust', 'filling']},
'topping': {'allergens': ['wheat', 'eggs'], 'may_contain': [' sesame '], 'uses': ['sauce']}},
'base'],
{'contains': [], 'error': 'cycle: sauce', 'may_contain': []}],
['generated control 3',
[{'base': {'allergens': ['eggs', 'tree nuts'],
'may_contain': [],
'uses': ['sauce', 'crust', 'filling', 'topping']},
'crust': {'allergens': ['dairy'], 'may_contain': ['tree nuts', 'eggs'], 'uses': ['topping', 'base']},
'filling': {'allergens': ['eggs', 'peanuts'], 'may_contain': ['soy', 'eggs'], 'uses': ['topping']},
'sauce': {'allergens': [], 'may_contain': ['tree nuts', 'wheat'], 'uses': ['crust', 'filling']},
'topping': {'allergens': ['wheat'], 'may_contain': [], 'uses': []}},
'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}]],
[['cycle', [{'a': {'uses': ['base']}, 'base': {'uses': ['a']}}, 'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['unknown sub', [{'base': {'uses': ['ghost']}}, 'base'],
{'contains': [], 'error': 'unknown: ghost', 'may_contain': []}],
['flat recipe', [{'base': {'allergens': ['milk'], 'may_contain': ['soy'], 'uses': []}}, 'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['soy']}],
['regression: nested precautionary labels',
[{'base': {'allergens': [' sesame '], 'may_contain': ['wheat'], 'uses': ['filling']},
'crust': {'allergens': ['wheat', 'milk'], 'may_contain': ['eggs', 'tree nuts'], 'uses': ['filling']},
'filling': {'allergens': ['peanuts'], 'may_contain': ['soy', 'peanuts'], 'uses': []},
'sauce': {'allergens': [], 'may_contain': ['eggs'], 'uses': ['filling']},
'topping': {'allergens': ['soy'], 'may_contain': ['dairy', 'peanuts'], 'uses': []}},
'base'],
{'contains': ['peanut', 'sesame'], 'error': None, 'may_contain': ['soy', 'wheat']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': [], 'may_contain': ['soy'], 'uses': ['sauce', 'crust']},
'crust': {'allergens': [], 'may_contain': ['eggs', 'milk'], 'uses': ['filling', 'topping']},
'filling': {'allergens': [], 'may_contain': [' sesame '], 'uses': []},
'sauce': {'allergens': ['tree nuts'], 'may_contain': ['Egg', 'tree nuts'], 'uses': ['topping']},
'topping': {'allergens': ['dairy', 'milk'], 'may_contain': ['soy'], 'uses': ['filling']}},
'base'],
{'contains': ['milk', 'tree nut'], 'error': None, 'may_contain': ['egg', 'sesame', 'soy']}],
['generated control 1',
[{'base': {'allergens': ['wheat'],
'may_contain': ['peanuts', 'Egg'],
'uses': ['crust', 'filling', 'topping']},
'crust': {'allergens': ['eggs'], 'may_contain': ['soy', 'wheat'], 'uses': ['topping', 'base']},
'filling': {'allergens': ['Egg'], 'may_contain': ['Egg', ' sesame '], 'uses': []},
'sauce': {'allergens': [], 'may_contain': [' sesame ', 'tree nuts'], 'uses': ['crust', 'topping']},
'topping': {'allergens': ['Egg', 'eggs'], 'may_contain': ['dairy'], 'uses': []}},
'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['generated control 2',
[{'base': {'allergens': [], 'may_contain': ['wheat'], 'uses': ['sauce', 'topping']},
'crust': {'allergens': ['milk', 'dairy'], 'may_contain': [' sesame '], 'uses': ['topping']},
'filling': {'allergens': [], 'may_contain': ['peanuts', 'eggs'], 'uses': []},
'sauce': {'allergens': ['milk', 'peanuts'],
'may_contain': ['eggs', 'dairy'],
'uses': ['filling', 'topping']},
'topping': {'allergens': [' sesame ', 'milk'], 'may_contain': [], 'uses': ['mystery']}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}],
['generated control 3',
[{'base': {'allergens': ['Egg'], 'may_contain': ['tree nuts'], 'uses': ['crust', 'topping']},
'crust': {'allergens': [], 'may_contain': [], 'uses': ['mystery']},
'filling': {'allergens': [], 'may_contain': [], 'uses': ['topping']},
'sauce': {'allergens': ['Egg'], 'may_contain': ['Egg', ' sesame '], 'uses': ['base']},
'topping': {'allergens': ['eggs', 'milk'], 'may_contain': [], 'uses': ['mystery']}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}]],
[['unknown sub', [{'base': {'uses': ['ghost']}}, 'base'],
{'contains': [], 'error': 'unknown: ghost', 'may_contain': []}],
['flat recipe', [{'base': {'allergens': ['milk'], 'may_contain': ['soy'], 'uses': []}}, 'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['soy']}],
['diamond sharing',
[{'a': {'allergens': ['wheat'], 'uses': ['c']},
'b': {'allergens': [], 'uses': ['c']},
'base': {'allergens': [], 'uses': ['a', 'b']},
'c': {'allergens': ['Dairy'], 'uses': []}},
'base'],
{'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []}],
['regression: nested precautionary labels',
[{'base': {'allergens': ['peanuts'], 'may_contain': ['peanuts', ' sesame '], 'uses': ['filling']},
'crust': {'allergens': [], 'may_contain': [], 'uses': ['topping']},
'filling': {'allergens': [], 'may_contain': ['dairy', 'milk'], 'uses': ['topping']},
'sauce': {'allergens': ['Egg', 'soy'], 'may_contain': [], 'uses': []},
'topping': {'allergens': [], 'may_contain': ['eggs'], 'uses': []}},
'base'],
{'contains': ['peanut'], 'error': None, 'may_contain': ['egg', 'milk', 'sesame']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': ['wheat', 'milk'], 'may_contain': [' sesame '], 'uses': ['crust', 'topping']},
'crust': {'allergens': [' sesame '], 'may_contain': ['eggs', 'Egg'], 'uses': ['filling']},
'filling': {'allergens': [' sesame ', 'wheat'], 'may_contain': [], 'uses': []},
'sauce': {'allergens': [], 'may_contain': [], 'uses': ['filling']},
'topping': {'allergens': ['dairy'], 'may_contain': [], 'uses': ['sauce']}},
'base'],
{'contains': ['milk', 'sesame', 'wheat'], 'error': None, 'may_contain': ['egg']}],
['generated control 1',
[{'base': {'allergens': ['tree nuts'], 'may_contain': ['tree nuts'], 'uses': []},
'crust': {'allergens': [], 'may_contain': ['dairy', 'peanuts'], 'uses': ['filling', 'topping']},
'filling': {'allergens': ['milk'], 'may_contain': [], 'uses': []},
'sauce': {'allergens': ['peanuts', 'soy'], 'may_contain': [], 'uses': ['filling', 'base']},
'topping': {'allergens': [], 'may_contain': [' sesame '], 'uses': []}},
'base'],
{'contains': ['tree nut'], 'error': None, 'may_contain': []}],
['generated control 2',
[{'base': {'allergens': [], 'may_contain': [], 'uses': ['sauce', 'crust', 'topping']},
'crust': {'allergens': ['tree nuts'],
'may_contain': ['eggs', 'tree nuts'],
'uses': ['filling', 'topping']},
'filling': {'allergens': [], 'may_contain': [], 'uses': ['mystery']},
'sauce': {'allergens': ['eggs'], 'may_contain': ['tree nuts', 'wheat'], 'uses': ['crust', 'base']},
'topping': {'allergens': ['tree nuts', 'wheat'], 'may_contain': ['dairy'], 'uses': []}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}],
['generated control 3',
[{'base': {'allergens': [' sesame ', 'wheat'], 'may_contain': [], 'uses': ['sauce', 'filling']},
'crust': {'allergens': ['wheat'], 'may_contain': [], 'uses': []},
'filling': {'allergens': ['soy'], 'may_contain': [], 'uses': ['mystery']},
'sauce': {'allergens': ['eggs'],
'may_contain': ['tree nuts', 'dairy'],
'uses': ['crust', 'filling', 'topping']},
'topping': {'allergens': [], 'may_contain': ['eggs'], 'uses': []}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}]],
[['flat recipe', [{'base': {'allergens': ['milk'], 'may_contain': ['soy'], 'uses': []}}, 'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['soy']}],
['diamond sharing',
[{'a': {'allergens': ['wheat'], 'uses': ['c']},
'b': {'allergens': [], 'uses': ['c']},
'base': {'allergens': [], 'uses': ['a', 'b']},
'c': {'allergens': ['Dairy'], 'uses': []}},
'base'],
{'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []}],
['cycle', [{'a': {'uses': ['base']}, 'base': {'uses': ['a']}}, 'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['regression: nested precautionary labels',
[{'base': {'allergens': [], 'may_contain': ['milk', 'peanuts'], 'uses': ['sauce']},
'crust': {'allergens': ['milk'], 'may_contain': ['peanuts', 'wheat'], 'uses': ['filling']},
'filling': {'allergens': ['dairy'], 'may_contain': ['dairy'], 'uses': []},
'sauce': {'allergens': ['eggs', 'Egg'],
'may_contain': ['eggs', 'soy'],
'uses': ['crust', 'filling', 'topping']},
'topping': {'allergens': [' sesame ', 'soy'], 'may_contain': ['soy', ' sesame '], 'uses': []}},
'base'],
{'contains': ['egg', 'milk', 'sesame', 'soy'], 'error': None, 'may_contain': ['peanut', 'wheat']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': ['dairy'], 'may_contain': [], 'uses': ['sauce', 'filling']},
'crust': {'allergens': ['soy', 'milk'], 'may_contain': [], 'uses': []},
'filling': {'allergens': ['wheat'], 'may_contain': ['wheat'], 'uses': []},
'sauce': {'allergens': ['soy'], 'may_contain': ['Egg'], 'uses': ['crust', 'filling']},
'topping': {'allergens': ['milk'], 'may_contain': ['tree nuts', 'dairy'], 'uses': ['mystery']}},
'base'],
{'contains': ['milk', 'soy', 'wheat'], 'error': None, 'may_contain': ['egg']}],
['generated control 1',
[{'base': {'allergens': ['peanuts'],
'may_contain': ['dairy', 'eggs'],
'uses': ['sauce', 'crust', 'filling']},
'crust': {'allergens': [' sesame ', 'wheat'], 'may_contain': [], 'uses': []},
'filling': {'allergens': ['dairy'], 'may_contain': ['dairy'], 'uses': []},
'sauce': {'allergens': [], 'may_contain': ['milk'], 'uses': []},
'topping': {'allergens': ['tree nuts'], 'may_contain': ['tree nuts', 'soy'], 'uses': ['filling']}},
'base'],
{'contains': ['milk', 'peanut', 'sesame', 'wheat'], 'error': None, 'may_contain': ['egg']}],
['generated control 2',
[{'base': {'allergens': ['peanuts'], 'may_contain': ['eggs', 'dairy'], 'uses': ['sauce']},
'crust': {'allergens': ['peanuts'], 'may_contain': [], 'uses': ['filling', 'sauce']},
'filling': {'allergens': [], 'may_contain': [], 'uses': ['topping', 'mystery']},
'sauce': {'allergens': ['Egg', 'dairy'], 'may_contain': [], 'uses': ['topping', 'base']},
'topping': {'allergens': ['tree nuts', 'soy'], 'may_contain': [' sesame ', 'milk'], 'uses': []}},
'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['generated control 3',
[{'base': {'allergens': [], 'may_contain': ['eggs', ' sesame '], 'uses': []},
'crust': {'allergens': ['eggs', 'milk'], 'may_contain': ['eggs'], 'uses': []},
'filling': {'allergens': ['milk'], 'may_contain': [], 'uses': []},
'sauce': {'allergens': ['tree nuts', 'Egg'], 'may_contain': [], 'uses': ['filling']},
'topping': {'allergens': [], 'may_contain': ['soy'], 'uses': []}},
'base'],
{'contains': [], 'error': None, 'may_contain': ['egg', 'sesame']}]]]
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 |
|---|---|---|---|
| flat recipe | {'contains': ['milk'], 'error': None, 'may_contain': ['soy']} | {'contains': ['milk'], 'error': None, 'may_contain': ['soy']} | Passed |
| diamond sharing | {'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []} | {'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []} | Passed |
| cycle | {'contains': [], 'error': 'cycle: base', 'may_contain': []} | {'contains': [], 'error': 'cycle: base', 'may_contain': []} | Passed |
| regression: nested precautionary labels | {'contains': ['peanut', 'sesame', 'soy'], 'error': None, 'may_contain': []} | {'contains': ['peanut', 'sesame', 'soy'], 'error': None, 'may_contain': ['egg', 'wheat']} | Failed |
| repair check: nested precautionary labels | {'contains': ['egg', 'soy', 'tree nut', 'wheat'], 'error': None, 'may_contain': []} | {'contains': ['egg', 'soy', 'tree nut', 'wheat'], 'error': None, 'may_contain': ['peanut']} | Failed |
| generated control 1 | {'contains': [], 'error': 'unknown: mystery', 'may_contain': []} | {'contains': [], 'error': 'unknown: mystery', 'may_contain': []} | Passed |
| generated control 2 | {'contains': ['milk'], 'error': None, 'may_contain': ['tree nut']} | {'contains': ['milk'], 'error': None, 'may_contain': ['tree nut']} | Passed |
| generated control 3 | {'contains': [], 'error': 'cycle: base', 'may_contain': []} | {'contains': [], 'error': 'cycle: base', 'may_contain': []} | Passed |
SHA-256 / 03934bcc751f3ab179844a2e4b32301b280b97d766ecc9427f00731b7171f78a
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(recipes, root):
SYN = {'dairy': 'milk', 'peanuts': 'peanut', 'tree nuts': 'tree nut', 'eggs': 'egg'}
def canon(a):
a = a.strip().lower()
return SYN.get(a, a)
contains, may = set(), set()
state = {}
def visit(n):
if n not in recipes:
return 'unknown: ' + n
if state.get(n) == 1:
return 'cycle: ' + n
if state.get(n) == 2:
return None
state[n] = 1
r = recipes[n]
contains.update(canon(a) for a in r.get('allergens', []))
if n == root or not r.get('uses'):
may.update(canon(a) for a in r.get('may_contain', []))
for s in r.get('uses', []):
e = visit(s)
if e:
return e
state[n] = 2
return None
err = visit(root)
if err:
return {'contains': [], 'may_contain': [], 'error': err}
return {'contains': sorted(contains), 'may_contain': sorted(may - contains), 'error': None}
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['flat recipe', [{'base': {'allergens': ['milk'], 'may_contain': ['soy'], 'uses': []}}, 'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['soy']}],
['diamond sharing',
[{'a': {'allergens': ['wheat'], 'uses': ['c']},
'b': {'allergens': [], 'uses': ['c']},
'base': {'allergens': [], 'uses': ['a', 'b']},
'c': {'allergens': ['Dairy'], 'uses': []}},
'base'],
{'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []}],
['cycle', [{'a': {'uses': ['base']}, 'base': {'uses': ['a']}}, 'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['regression: nested precautionary labels',
[{'base': {'allergens': ['soy'], 'may_contain': [' sesame ', 'soy'], 'uses': ['sauce', 'crust']},
'crust': {'allergens': ['soy', ' sesame '], 'may_contain': [], 'uses': ['sauce']},
'filling': {'allergens': ['tree nuts'], 'may_contain': ['soy'], 'uses': ['topping']},
'sauce': {'allergens': ['peanuts'], 'may_contain': ['Egg', 'wheat'], 'uses': []},
'topping': {'allergens': [], 'may_contain': ['soy'], 'uses': []}},
'base'],
{'contains': ['peanut', 'sesame', 'soy'], 'error': None, 'may_contain': ['egg', 'wheat']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': ['wheat'], 'may_contain': [], 'uses': ['sauce', 'filling']},
'crust': {'allergens': ['eggs'], 'may_contain': ['eggs'], 'uses': []},
'filling': {'allergens': ['tree nuts', 'soy'], 'may_contain': ['soy', 'tree nuts'], 'uses': []},
'sauce': {'allergens': ['soy'], 'may_contain': ['peanuts', 'tree nuts'], 'uses': ['crust']},
'topping': {'allergens': [], 'may_contain': ['wheat', 'Egg'], 'uses': []}},
'base'],
{'contains': ['egg', 'soy', 'tree nut', 'wheat'], 'error': None, 'may_contain': ['peanut']}],
['generated control 1',
[{'base': {'allergens': ['eggs'], 'may_contain': [], 'uses': ['topping']},
'crust': {'allergens': ['eggs'], 'may_contain': ['dairy', 'Egg'], 'uses': ['filling', 'topping']},
'filling': {'allergens': ['wheat', 'soy'], 'may_contain': [], 'uses': ['topping']},
'sauce': {'allergens': ['tree nuts', ' sesame '],
'may_contain': ['wheat', 'soy'],
'uses': ['topping', 'base']},
'topping': {'allergens': [], 'may_contain': ['peanuts'], 'uses': ['mystery']}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}],
['generated control 2',
[{'base': {'allergens': ['milk', 'dairy'], 'may_contain': ['dairy', 'tree nuts'], 'uses': ['topping']},
'crust': {'allergens': [' sesame ', 'Egg'], 'may_contain': ['peanuts', 'tree nuts'], 'uses': []},
'filling': {'allergens': [' sesame ', 'peanuts'], 'may_contain': [], 'uses': []},
'sauce': {'allergens': ['tree nuts'], 'may_contain': ['Egg', 'eggs'], 'uses': ['filling']},
'topping': {'allergens': [], 'may_contain': [], 'uses': []}},
'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['tree nut']}],
['generated control 3',
[{'base': {'allergens': ['milk'], 'may_contain': [], 'uses': ['crust', 'topping']},
'crust': {'allergens': ['soy'], 'may_contain': ['peanuts'], 'uses': ['topping', 'base']},
'filling': {'allergens': ['wheat'], 'may_contain': ['tree nuts'], 'uses': ['topping']},
'sauce': {'allergens': [' sesame '], 'may_contain': [], 'uses': ['topping']},
'topping': {'allergens': [' sesame ', 'milk'], 'may_contain': [], 'uses': []}},
'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}]],
[['diamond sharing',
[{'a': {'allergens': ['wheat'], 'uses': ['c']},
'b': {'allergens': [], 'uses': ['c']},
'base': {'allergens': [], 'uses': ['a', 'b']},
'c': {'allergens': ['Dairy'], 'uses': []}},
'base'],
{'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []}],
['cycle', [{'a': {'uses': ['base']}, 'base': {'uses': ['a']}}, 'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['unknown sub', [{'base': {'uses': ['ghost']}}, 'base'],
{'contains': [], 'error': 'unknown: ghost', 'may_contain': []}],
['regression: nested precautionary labels',
[{'base': {'allergens': ['dairy'], 'may_contain': ['soy'], 'uses': ['crust', 'topping']},
'crust': {'allergens': [], 'may_contain': [' sesame ', 'Egg'], 'uses': ['filling', 'sauce']},
'filling': {'allergens': [], 'may_contain': ['milk', 'eggs'], 'uses': []},
'sauce': {'allergens': ['Egg'], 'may_contain': [], 'uses': ['filling']},
'topping': {'allergens': [], 'may_contain': ['peanuts', 'milk'], 'uses': []}},
'base'],
{'contains': ['egg', 'milk'], 'error': None, 'may_contain': ['peanut', 'sesame', 'soy']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': ['dairy'],
'may_contain': ['milk', 'eggs'],
'uses': ['sauce', 'filling', 'topping']},
'crust': {'allergens': ['eggs'], 'may_contain': ['tree nuts'], 'uses': ['topping']},
'filling': {'allergens': [], 'may_contain': ['soy'], 'uses': ['topping']},
'sauce': {'allergens': [' sesame '], 'may_contain': ['Egg'], 'uses': ['crust']},
'topping': {'allergens': [], 'may_contain': [], 'uses': []}},
'base'],
{'contains': ['egg', 'milk', 'sesame'], 'error': None, 'may_contain': ['soy', 'tree nut']}],
['generated control 1',
[{'base': {'allergens': ['wheat', 'peanuts'], 'may_contain': [], 'uses': ['sauce', 'crust']},
'crust': {'allergens': [], 'may_contain': ['wheat'], 'uses': ['topping']},
'filling': {'allergens': [], 'may_contain': ['wheat', 'milk'], 'uses': []},
'sauce': {'allergens': ['dairy'], 'may_contain': ['dairy'], 'uses': ['crust', 'topping']},
'topping': {'allergens': [], 'may_contain': [], 'uses': []}},
'base'],
{'contains': ['milk', 'peanut', 'wheat'], 'error': None, 'may_contain': []}],
['generated control 2',
[{'base': {'allergens': [], 'may_contain': [], 'uses': ['sauce', 'topping', 'mystery']},
'crust': {'allergens': [], 'may_contain': ['wheat'], 'uses': ['filling', 'topping']},
'filling': {'allergens': ['tree nuts', 'Egg'], 'may_contain': ['Egg', 'milk'], 'uses': []},
'sauce': {'allergens': [], 'may_contain': [], 'uses': ['crust', 'filling']},
'topping': {'allergens': ['wheat', 'eggs'], 'may_contain': [' sesame '], 'uses': ['sauce']}},
'base'],
{'contains': [], 'error': 'cycle: sauce', 'may_contain': []}],
['generated control 3',
[{'base': {'allergens': ['eggs', 'tree nuts'],
'may_contain': [],
'uses': ['sauce', 'crust', 'filling', 'topping']},
'crust': {'allergens': ['dairy'], 'may_contain': ['tree nuts', 'eggs'], 'uses': ['topping', 'base']},
'filling': {'allergens': ['eggs', 'peanuts'], 'may_contain': ['soy', 'eggs'], 'uses': ['topping']},
'sauce': {'allergens': [], 'may_contain': ['tree nuts', 'wheat'], 'uses': ['crust', 'filling']},
'topping': {'allergens': ['wheat'], 'may_contain': [], 'uses': []}},
'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}]],
[['cycle', [{'a': {'uses': ['base']}, 'base': {'uses': ['a']}}, 'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['unknown sub', [{'base': {'uses': ['ghost']}}, 'base'],
{'contains': [], 'error': 'unknown: ghost', 'may_contain': []}],
['flat recipe', [{'base': {'allergens': ['milk'], 'may_contain': ['soy'], 'uses': []}}, 'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['soy']}],
['regression: nested precautionary labels',
[{'base': {'allergens': [' sesame '], 'may_contain': ['wheat'], 'uses': ['filling']},
'crust': {'allergens': ['wheat', 'milk'], 'may_contain': ['eggs', 'tree nuts'], 'uses': ['filling']},
'filling': {'allergens': ['peanuts'], 'may_contain': ['soy', 'peanuts'], 'uses': []},
'sauce': {'allergens': [], 'may_contain': ['eggs'], 'uses': ['filling']},
'topping': {'allergens': ['soy'], 'may_contain': ['dairy', 'peanuts'], 'uses': []}},
'base'],
{'contains': ['peanut', 'sesame'], 'error': None, 'may_contain': ['soy', 'wheat']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': [], 'may_contain': ['soy'], 'uses': ['sauce', 'crust']},
'crust': {'allergens': [], 'may_contain': ['eggs', 'milk'], 'uses': ['filling', 'topping']},
'filling': {'allergens': [], 'may_contain': [' sesame '], 'uses': []},
'sauce': {'allergens': ['tree nuts'], 'may_contain': ['Egg', 'tree nuts'], 'uses': ['topping']},
'topping': {'allergens': ['dairy', 'milk'], 'may_contain': ['soy'], 'uses': ['filling']}},
'base'],
{'contains': ['milk', 'tree nut'], 'error': None, 'may_contain': ['egg', 'sesame', 'soy']}],
['generated control 1',
[{'base': {'allergens': ['wheat'],
'may_contain': ['peanuts', 'Egg'],
'uses': ['crust', 'filling', 'topping']},
'crust': {'allergens': ['eggs'], 'may_contain': ['soy', 'wheat'], 'uses': ['topping', 'base']},
'filling': {'allergens': ['Egg'], 'may_contain': ['Egg', ' sesame '], 'uses': []},
'sauce': {'allergens': [], 'may_contain': [' sesame ', 'tree nuts'], 'uses': ['crust', 'topping']},
'topping': {'allergens': ['Egg', 'eggs'], 'may_contain': ['dairy'], 'uses': []}},
'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['generated control 2',
[{'base': {'allergens': [], 'may_contain': ['wheat'], 'uses': ['sauce', 'topping']},
'crust': {'allergens': ['milk', 'dairy'], 'may_contain': [' sesame '], 'uses': ['topping']},
'filling': {'allergens': [], 'may_contain': ['peanuts', 'eggs'], 'uses': []},
'sauce': {'allergens': ['milk', 'peanuts'],
'may_contain': ['eggs', 'dairy'],
'uses': ['filling', 'topping']},
'topping': {'allergens': [' sesame ', 'milk'], 'may_contain': [], 'uses': ['mystery']}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}],
['generated control 3',
[{'base': {'allergens': ['Egg'], 'may_contain': ['tree nuts'], 'uses': ['crust', 'topping']},
'crust': {'allergens': [], 'may_contain': [], 'uses': ['mystery']},
'filling': {'allergens': [], 'may_contain': [], 'uses': ['topping']},
'sauce': {'allergens': ['Egg'], 'may_contain': ['Egg', ' sesame '], 'uses': ['base']},
'topping': {'allergens': ['eggs', 'milk'], 'may_contain': [], 'uses': ['mystery']}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}]],
[['unknown sub', [{'base': {'uses': ['ghost']}}, 'base'],
{'contains': [], 'error': 'unknown: ghost', 'may_contain': []}],
['flat recipe', [{'base': {'allergens': ['milk'], 'may_contain': ['soy'], 'uses': []}}, 'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['soy']}],
['diamond sharing',
[{'a': {'allergens': ['wheat'], 'uses': ['c']},
'b': {'allergens': [], 'uses': ['c']},
'base': {'allergens': [], 'uses': ['a', 'b']},
'c': {'allergens': ['Dairy'], 'uses': []}},
'base'],
{'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []}],
['regression: nested precautionary labels',
[{'base': {'allergens': ['peanuts'], 'may_contain': ['peanuts', ' sesame '], 'uses': ['filling']},
'crust': {'allergens': [], 'may_contain': [], 'uses': ['topping']},
'filling': {'allergens': [], 'may_contain': ['dairy', 'milk'], 'uses': ['topping']},
'sauce': {'allergens': ['Egg', 'soy'], 'may_contain': [], 'uses': []},
'topping': {'allergens': [], 'may_contain': ['eggs'], 'uses': []}},
'base'],
{'contains': ['peanut'], 'error': None, 'may_contain': ['egg', 'milk', 'sesame']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': ['wheat', 'milk'], 'may_contain': [' sesame '], 'uses': ['crust', 'topping']},
'crust': {'allergens': [' sesame '], 'may_contain': ['eggs', 'Egg'], 'uses': ['filling']},
'filling': {'allergens': [' sesame ', 'wheat'], 'may_contain': [], 'uses': []},
'sauce': {'allergens': [], 'may_contain': [], 'uses': ['filling']},
'topping': {'allergens': ['dairy'], 'may_contain': [], 'uses': ['sauce']}},
'base'],
{'contains': ['milk', 'sesame', 'wheat'], 'error': None, 'may_contain': ['egg']}],
['generated control 1',
[{'base': {'allergens': ['tree nuts'], 'may_contain': ['tree nuts'], 'uses': []},
'crust': {'allergens': [], 'may_contain': ['dairy', 'peanuts'], 'uses': ['filling', 'topping']},
'filling': {'allergens': ['milk'], 'may_contain': [], 'uses': []},
'sauce': {'allergens': ['peanuts', 'soy'], 'may_contain': [], 'uses': ['filling', 'base']},
'topping': {'allergens': [], 'may_contain': [' sesame '], 'uses': []}},
'base'],
{'contains': ['tree nut'], 'error': None, 'may_contain': []}],
['generated control 2',
[{'base': {'allergens': [], 'may_contain': [], 'uses': ['sauce', 'crust', 'topping']},
'crust': {'allergens': ['tree nuts'],
'may_contain': ['eggs', 'tree nuts'],
'uses': ['filling', 'topping']},
'filling': {'allergens': [], 'may_contain': [], 'uses': ['mystery']},
'sauce': {'allergens': ['eggs'], 'may_contain': ['tree nuts', 'wheat'], 'uses': ['crust', 'base']},
'topping': {'allergens': ['tree nuts', 'wheat'], 'may_contain': ['dairy'], 'uses': []}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}],
['generated control 3',
[{'base': {'allergens': [' sesame ', 'wheat'], 'may_contain': [], 'uses': ['sauce', 'filling']},
'crust': {'allergens': ['wheat'], 'may_contain': [], 'uses': []},
'filling': {'allergens': ['soy'], 'may_contain': [], 'uses': ['mystery']},
'sauce': {'allergens': ['eggs'],
'may_contain': ['tree nuts', 'dairy'],
'uses': ['crust', 'filling', 'topping']},
'topping': {'allergens': [], 'may_contain': ['eggs'], 'uses': []}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}]],
[['flat recipe', [{'base': {'allergens': ['milk'], 'may_contain': ['soy'], 'uses': []}}, 'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['soy']}],
['diamond sharing',
[{'a': {'allergens': ['wheat'], 'uses': ['c']},
'b': {'allergens': [], 'uses': ['c']},
'base': {'allergens': [], 'uses': ['a', 'b']},
'c': {'allergens': ['Dairy'], 'uses': []}},
'base'],
{'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []}],
['cycle', [{'a': {'uses': ['base']}, 'base': {'uses': ['a']}}, 'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['regression: nested precautionary labels',
[{'base': {'allergens': [], 'may_contain': ['milk', 'peanuts'], 'uses': ['sauce']},
'crust': {'allergens': ['milk'], 'may_contain': ['peanuts', 'wheat'], 'uses': ['filling']},
'filling': {'allergens': ['dairy'], 'may_contain': ['dairy'], 'uses': []},
'sauce': {'allergens': ['eggs', 'Egg'],
'may_contain': ['eggs', 'soy'],
'uses': ['crust', 'filling', 'topping']},
'topping': {'allergens': [' sesame ', 'soy'], 'may_contain': ['soy', ' sesame '], 'uses': []}},
'base'],
{'contains': ['egg', 'milk', 'sesame', 'soy'], 'error': None, 'may_contain': ['peanut', 'wheat']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': ['dairy'], 'may_contain': [], 'uses': ['sauce', 'filling']},
'crust': {'allergens': ['soy', 'milk'], 'may_contain': [], 'uses': []},
'filling': {'allergens': ['wheat'], 'may_contain': ['wheat'], 'uses': []},
'sauce': {'allergens': ['soy'], 'may_contain': ['Egg'], 'uses': ['crust', 'filling']},
'topping': {'allergens': ['milk'], 'may_contain': ['tree nuts', 'dairy'], 'uses': ['mystery']}},
'base'],
{'contains': ['milk', 'soy', 'wheat'], 'error': None, 'may_contain': ['egg']}],
['generated control 1',
[{'base': {'allergens': ['peanuts'],
'may_contain': ['dairy', 'eggs'],
'uses': ['sauce', 'crust', 'filling']},
'crust': {'allergens': [' sesame ', 'wheat'], 'may_contain': [], 'uses': []},
'filling': {'allergens': ['dairy'], 'may_contain': ['dairy'], 'uses': []},
'sauce': {'allergens': [], 'may_contain': ['milk'], 'uses': []},
'topping': {'allergens': ['tree nuts'], 'may_contain': ['tree nuts', 'soy'], 'uses': ['filling']}},
'base'],
{'contains': ['milk', 'peanut', 'sesame', 'wheat'], 'error': None, 'may_contain': ['egg']}],
['generated control 2',
[{'base': {'allergens': ['peanuts'], 'may_contain': ['eggs', 'dairy'], 'uses': ['sauce']},
'crust': {'allergens': ['peanuts'], 'may_contain': [], 'uses': ['filling', 'sauce']},
'filling': {'allergens': [], 'may_contain': [], 'uses': ['topping', 'mystery']},
'sauce': {'allergens': ['Egg', 'dairy'], 'may_contain': [], 'uses': ['topping', 'base']},
'topping': {'allergens': ['tree nuts', 'soy'], 'may_contain': [' sesame ', 'milk'], 'uses': []}},
'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['generated control 3',
[{'base': {'allergens': [], 'may_contain': ['eggs', ' sesame '], 'uses': []},
'crust': {'allergens': ['eggs', 'milk'], 'may_contain': ['eggs'], 'uses': []},
'filling': {'allergens': ['milk'], 'may_contain': [], 'uses': []},
'sauce': {'allergens': ['tree nuts', 'Egg'], 'may_contain': [], 'uses': ['filling']},
'topping': {'allergens': [], 'may_contain': ['soy'], 'uses': []}},
'base'],
{'contains': [], 'error': None, 'may_contain': ['egg', 'sesame']}]]]
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 |
|---|---|---|---|
| flat recipe | {'contains': ['milk'], 'error': None, 'may_contain': ['soy']} | {'contains': ['milk'], 'error': None, 'may_contain': ['soy']} | Passed |
| diamond sharing | {'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []} | {'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []} | Passed |
| cycle | {'contains': [], 'error': 'cycle: base', 'may_contain': []} | {'contains': [], 'error': 'cycle: base', 'may_contain': []} | Passed |
| regression: nested precautionary labels | {'contains': ['peanut', 'sesame', 'soy'], 'error': None, 'may_contain': ['egg', 'wheat']} | {'contains': ['peanut', 'sesame', 'soy'], 'error': None, 'may_contain': ['egg', 'wheat']} | Passed |
| repair check: nested precautionary labels | {'contains': ['egg', 'soy', 'tree nut', 'wheat'], 'error': None, 'may_contain': []} | {'contains': ['egg', 'soy', 'tree nut', 'wheat'], 'error': None, 'may_contain': ['peanut']} | Failed |
| generated control 1 | {'contains': [], 'error': 'unknown: mystery', 'may_contain': []} | {'contains': [], 'error': 'unknown: mystery', 'may_contain': []} | Passed |
| generated control 2 | {'contains': ['milk'], 'error': None, 'may_contain': ['tree nut']} | {'contains': ['milk'], 'error': None, 'may_contain': ['tree nut']} | Passed |
| generated control 3 | {'contains': [], 'error': 'cycle: base', 'may_contain': []} | {'contains': [], 'error': 'cycle: base', 'may_contain': []} | Passed |
SHA-256 / 9d4d720e11150d1bee3bbe0cbb686f26513e17fa0169f22b03dc671fc7b45a20
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(recipes, root):
SYN = {'dairy': 'milk', 'peanuts': 'peanut', 'tree nuts': 'tree nut', 'eggs': 'egg'}
def canon(a):
a = a.strip().lower()
return SYN.get(a, a)
contains, may = set(), set()
state = {}
def visit(n):
if n not in recipes:
return 'unknown: ' + n
if state.get(n) == 1:
return 'cycle: ' + n
if state.get(n) == 2:
return None
state[n] = 1
r = recipes[n]
contains.update(canon(a) for a in r.get('allergens', []))
may.update(canon(a) for a in r.get('may_contain', []))
for s in r.get('uses', []):
e = visit(s)
if e:
return e
state[n] = 2
return None
err = visit(root)
if err:
return {'contains': [], 'may_contain': [], 'error': err}
return {'contains': sorted(contains), 'may_contain': sorted(may - contains), 'error': None}
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['flat recipe', [{'base': {'allergens': ['milk'], 'may_contain': ['soy'], 'uses': []}}, 'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['soy']}],
['diamond sharing',
[{'a': {'allergens': ['wheat'], 'uses': ['c']},
'b': {'allergens': [], 'uses': ['c']},
'base': {'allergens': [], 'uses': ['a', 'b']},
'c': {'allergens': ['Dairy'], 'uses': []}},
'base'],
{'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []}],
['cycle', [{'a': {'uses': ['base']}, 'base': {'uses': ['a']}}, 'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['regression: nested precautionary labels',
[{'base': {'allergens': ['soy'], 'may_contain': [' sesame ', 'soy'], 'uses': ['sauce', 'crust']},
'crust': {'allergens': ['soy', ' sesame '], 'may_contain': [], 'uses': ['sauce']},
'filling': {'allergens': ['tree nuts'], 'may_contain': ['soy'], 'uses': ['topping']},
'sauce': {'allergens': ['peanuts'], 'may_contain': ['Egg', 'wheat'], 'uses': []},
'topping': {'allergens': [], 'may_contain': ['soy'], 'uses': []}},
'base'],
{'contains': ['peanut', 'sesame', 'soy'], 'error': None, 'may_contain': ['egg', 'wheat']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': ['wheat'], 'may_contain': [], 'uses': ['sauce', 'filling']},
'crust': {'allergens': ['eggs'], 'may_contain': ['eggs'], 'uses': []},
'filling': {'allergens': ['tree nuts', 'soy'], 'may_contain': ['soy', 'tree nuts'], 'uses': []},
'sauce': {'allergens': ['soy'], 'may_contain': ['peanuts', 'tree nuts'], 'uses': ['crust']},
'topping': {'allergens': [], 'may_contain': ['wheat', 'Egg'], 'uses': []}},
'base'],
{'contains': ['egg', 'soy', 'tree nut', 'wheat'], 'error': None, 'may_contain': ['peanut']}],
['generated control 1',
[{'base': {'allergens': ['eggs'], 'may_contain': [], 'uses': ['topping']},
'crust': {'allergens': ['eggs'], 'may_contain': ['dairy', 'Egg'], 'uses': ['filling', 'topping']},
'filling': {'allergens': ['wheat', 'soy'], 'may_contain': [], 'uses': ['topping']},
'sauce': {'allergens': ['tree nuts', ' sesame '],
'may_contain': ['wheat', 'soy'],
'uses': ['topping', 'base']},
'topping': {'allergens': [], 'may_contain': ['peanuts'], 'uses': ['mystery']}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}],
['generated control 2',
[{'base': {'allergens': ['milk', 'dairy'], 'may_contain': ['dairy', 'tree nuts'], 'uses': ['topping']},
'crust': {'allergens': [' sesame ', 'Egg'], 'may_contain': ['peanuts', 'tree nuts'], 'uses': []},
'filling': {'allergens': [' sesame ', 'peanuts'], 'may_contain': [], 'uses': []},
'sauce': {'allergens': ['tree nuts'], 'may_contain': ['Egg', 'eggs'], 'uses': ['filling']},
'topping': {'allergens': [], 'may_contain': [], 'uses': []}},
'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['tree nut']}],
['generated control 3',
[{'base': {'allergens': ['milk'], 'may_contain': [], 'uses': ['crust', 'topping']},
'crust': {'allergens': ['soy'], 'may_contain': ['peanuts'], 'uses': ['topping', 'base']},
'filling': {'allergens': ['wheat'], 'may_contain': ['tree nuts'], 'uses': ['topping']},
'sauce': {'allergens': [' sesame '], 'may_contain': [], 'uses': ['topping']},
'topping': {'allergens': [' sesame ', 'milk'], 'may_contain': [], 'uses': []}},
'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}]],
[['diamond sharing',
[{'a': {'allergens': ['wheat'], 'uses': ['c']},
'b': {'allergens': [], 'uses': ['c']},
'base': {'allergens': [], 'uses': ['a', 'b']},
'c': {'allergens': ['Dairy'], 'uses': []}},
'base'],
{'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []}],
['cycle', [{'a': {'uses': ['base']}, 'base': {'uses': ['a']}}, 'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['unknown sub', [{'base': {'uses': ['ghost']}}, 'base'],
{'contains': [], 'error': 'unknown: ghost', 'may_contain': []}],
['regression: nested precautionary labels',
[{'base': {'allergens': ['dairy'], 'may_contain': ['soy'], 'uses': ['crust', 'topping']},
'crust': {'allergens': [], 'may_contain': [' sesame ', 'Egg'], 'uses': ['filling', 'sauce']},
'filling': {'allergens': [], 'may_contain': ['milk', 'eggs'], 'uses': []},
'sauce': {'allergens': ['Egg'], 'may_contain': [], 'uses': ['filling']},
'topping': {'allergens': [], 'may_contain': ['peanuts', 'milk'], 'uses': []}},
'base'],
{'contains': ['egg', 'milk'], 'error': None, 'may_contain': ['peanut', 'sesame', 'soy']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': ['dairy'],
'may_contain': ['milk', 'eggs'],
'uses': ['sauce', 'filling', 'topping']},
'crust': {'allergens': ['eggs'], 'may_contain': ['tree nuts'], 'uses': ['topping']},
'filling': {'allergens': [], 'may_contain': ['soy'], 'uses': ['topping']},
'sauce': {'allergens': [' sesame '], 'may_contain': ['Egg'], 'uses': ['crust']},
'topping': {'allergens': [], 'may_contain': [], 'uses': []}},
'base'],
{'contains': ['egg', 'milk', 'sesame'], 'error': None, 'may_contain': ['soy', 'tree nut']}],
['generated control 1',
[{'base': {'allergens': ['wheat', 'peanuts'], 'may_contain': [], 'uses': ['sauce', 'crust']},
'crust': {'allergens': [], 'may_contain': ['wheat'], 'uses': ['topping']},
'filling': {'allergens': [], 'may_contain': ['wheat', 'milk'], 'uses': []},
'sauce': {'allergens': ['dairy'], 'may_contain': ['dairy'], 'uses': ['crust', 'topping']},
'topping': {'allergens': [], 'may_contain': [], 'uses': []}},
'base'],
{'contains': ['milk', 'peanut', 'wheat'], 'error': None, 'may_contain': []}],
['generated control 2',
[{'base': {'allergens': [], 'may_contain': [], 'uses': ['sauce', 'topping', 'mystery']},
'crust': {'allergens': [], 'may_contain': ['wheat'], 'uses': ['filling', 'topping']},
'filling': {'allergens': ['tree nuts', 'Egg'], 'may_contain': ['Egg', 'milk'], 'uses': []},
'sauce': {'allergens': [], 'may_contain': [], 'uses': ['crust', 'filling']},
'topping': {'allergens': ['wheat', 'eggs'], 'may_contain': [' sesame '], 'uses': ['sauce']}},
'base'],
{'contains': [], 'error': 'cycle: sauce', 'may_contain': []}],
['generated control 3',
[{'base': {'allergens': ['eggs', 'tree nuts'],
'may_contain': [],
'uses': ['sauce', 'crust', 'filling', 'topping']},
'crust': {'allergens': ['dairy'], 'may_contain': ['tree nuts', 'eggs'], 'uses': ['topping', 'base']},
'filling': {'allergens': ['eggs', 'peanuts'], 'may_contain': ['soy', 'eggs'], 'uses': ['topping']},
'sauce': {'allergens': [], 'may_contain': ['tree nuts', 'wheat'], 'uses': ['crust', 'filling']},
'topping': {'allergens': ['wheat'], 'may_contain': [], 'uses': []}},
'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}]],
[['cycle', [{'a': {'uses': ['base']}, 'base': {'uses': ['a']}}, 'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['unknown sub', [{'base': {'uses': ['ghost']}}, 'base'],
{'contains': [], 'error': 'unknown: ghost', 'may_contain': []}],
['flat recipe', [{'base': {'allergens': ['milk'], 'may_contain': ['soy'], 'uses': []}}, 'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['soy']}],
['regression: nested precautionary labels',
[{'base': {'allergens': [' sesame '], 'may_contain': ['wheat'], 'uses': ['filling']},
'crust': {'allergens': ['wheat', 'milk'], 'may_contain': ['eggs', 'tree nuts'], 'uses': ['filling']},
'filling': {'allergens': ['peanuts'], 'may_contain': ['soy', 'peanuts'], 'uses': []},
'sauce': {'allergens': [], 'may_contain': ['eggs'], 'uses': ['filling']},
'topping': {'allergens': ['soy'], 'may_contain': ['dairy', 'peanuts'], 'uses': []}},
'base'],
{'contains': ['peanut', 'sesame'], 'error': None, 'may_contain': ['soy', 'wheat']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': [], 'may_contain': ['soy'], 'uses': ['sauce', 'crust']},
'crust': {'allergens': [], 'may_contain': ['eggs', 'milk'], 'uses': ['filling', 'topping']},
'filling': {'allergens': [], 'may_contain': [' sesame '], 'uses': []},
'sauce': {'allergens': ['tree nuts'], 'may_contain': ['Egg', 'tree nuts'], 'uses': ['topping']},
'topping': {'allergens': ['dairy', 'milk'], 'may_contain': ['soy'], 'uses': ['filling']}},
'base'],
{'contains': ['milk', 'tree nut'], 'error': None, 'may_contain': ['egg', 'sesame', 'soy']}],
['generated control 1',
[{'base': {'allergens': ['wheat'],
'may_contain': ['peanuts', 'Egg'],
'uses': ['crust', 'filling', 'topping']},
'crust': {'allergens': ['eggs'], 'may_contain': ['soy', 'wheat'], 'uses': ['topping', 'base']},
'filling': {'allergens': ['Egg'], 'may_contain': ['Egg', ' sesame '], 'uses': []},
'sauce': {'allergens': [], 'may_contain': [' sesame ', 'tree nuts'], 'uses': ['crust', 'topping']},
'topping': {'allergens': ['Egg', 'eggs'], 'may_contain': ['dairy'], 'uses': []}},
'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['generated control 2',
[{'base': {'allergens': [], 'may_contain': ['wheat'], 'uses': ['sauce', 'topping']},
'crust': {'allergens': ['milk', 'dairy'], 'may_contain': [' sesame '], 'uses': ['topping']},
'filling': {'allergens': [], 'may_contain': ['peanuts', 'eggs'], 'uses': []},
'sauce': {'allergens': ['milk', 'peanuts'],
'may_contain': ['eggs', 'dairy'],
'uses': ['filling', 'topping']},
'topping': {'allergens': [' sesame ', 'milk'], 'may_contain': [], 'uses': ['mystery']}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}],
['generated control 3',
[{'base': {'allergens': ['Egg'], 'may_contain': ['tree nuts'], 'uses': ['crust', 'topping']},
'crust': {'allergens': [], 'may_contain': [], 'uses': ['mystery']},
'filling': {'allergens': [], 'may_contain': [], 'uses': ['topping']},
'sauce': {'allergens': ['Egg'], 'may_contain': ['Egg', ' sesame '], 'uses': ['base']},
'topping': {'allergens': ['eggs', 'milk'], 'may_contain': [], 'uses': ['mystery']}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}]],
[['unknown sub', [{'base': {'uses': ['ghost']}}, 'base'],
{'contains': [], 'error': 'unknown: ghost', 'may_contain': []}],
['flat recipe', [{'base': {'allergens': ['milk'], 'may_contain': ['soy'], 'uses': []}}, 'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['soy']}],
['diamond sharing',
[{'a': {'allergens': ['wheat'], 'uses': ['c']},
'b': {'allergens': [], 'uses': ['c']},
'base': {'allergens': [], 'uses': ['a', 'b']},
'c': {'allergens': ['Dairy'], 'uses': []}},
'base'],
{'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []}],
['regression: nested precautionary labels',
[{'base': {'allergens': ['peanuts'], 'may_contain': ['peanuts', ' sesame '], 'uses': ['filling']},
'crust': {'allergens': [], 'may_contain': [], 'uses': ['topping']},
'filling': {'allergens': [], 'may_contain': ['dairy', 'milk'], 'uses': ['topping']},
'sauce': {'allergens': ['Egg', 'soy'], 'may_contain': [], 'uses': []},
'topping': {'allergens': [], 'may_contain': ['eggs'], 'uses': []}},
'base'],
{'contains': ['peanut'], 'error': None, 'may_contain': ['egg', 'milk', 'sesame']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': ['wheat', 'milk'], 'may_contain': [' sesame '], 'uses': ['crust', 'topping']},
'crust': {'allergens': [' sesame '], 'may_contain': ['eggs', 'Egg'], 'uses': ['filling']},
'filling': {'allergens': [' sesame ', 'wheat'], 'may_contain': [], 'uses': []},
'sauce': {'allergens': [], 'may_contain': [], 'uses': ['filling']},
'topping': {'allergens': ['dairy'], 'may_contain': [], 'uses': ['sauce']}},
'base'],
{'contains': ['milk', 'sesame', 'wheat'], 'error': None, 'may_contain': ['egg']}],
['generated control 1',
[{'base': {'allergens': ['tree nuts'], 'may_contain': ['tree nuts'], 'uses': []},
'crust': {'allergens': [], 'may_contain': ['dairy', 'peanuts'], 'uses': ['filling', 'topping']},
'filling': {'allergens': ['milk'], 'may_contain': [], 'uses': []},
'sauce': {'allergens': ['peanuts', 'soy'], 'may_contain': [], 'uses': ['filling', 'base']},
'topping': {'allergens': [], 'may_contain': [' sesame '], 'uses': []}},
'base'],
{'contains': ['tree nut'], 'error': None, 'may_contain': []}],
['generated control 2',
[{'base': {'allergens': [], 'may_contain': [], 'uses': ['sauce', 'crust', 'topping']},
'crust': {'allergens': ['tree nuts'],
'may_contain': ['eggs', 'tree nuts'],
'uses': ['filling', 'topping']},
'filling': {'allergens': [], 'may_contain': [], 'uses': ['mystery']},
'sauce': {'allergens': ['eggs'], 'may_contain': ['tree nuts', 'wheat'], 'uses': ['crust', 'base']},
'topping': {'allergens': ['tree nuts', 'wheat'], 'may_contain': ['dairy'], 'uses': []}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}],
['generated control 3',
[{'base': {'allergens': [' sesame ', 'wheat'], 'may_contain': [], 'uses': ['sauce', 'filling']},
'crust': {'allergens': ['wheat'], 'may_contain': [], 'uses': []},
'filling': {'allergens': ['soy'], 'may_contain': [], 'uses': ['mystery']},
'sauce': {'allergens': ['eggs'],
'may_contain': ['tree nuts', 'dairy'],
'uses': ['crust', 'filling', 'topping']},
'topping': {'allergens': [], 'may_contain': ['eggs'], 'uses': []}},
'base'],
{'contains': [], 'error': 'unknown: mystery', 'may_contain': []}]],
[['flat recipe', [{'base': {'allergens': ['milk'], 'may_contain': ['soy'], 'uses': []}}, 'base'],
{'contains': ['milk'], 'error': None, 'may_contain': ['soy']}],
['diamond sharing',
[{'a': {'allergens': ['wheat'], 'uses': ['c']},
'b': {'allergens': [], 'uses': ['c']},
'base': {'allergens': [], 'uses': ['a', 'b']},
'c': {'allergens': ['Dairy'], 'uses': []}},
'base'],
{'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []}],
['cycle', [{'a': {'uses': ['base']}, 'base': {'uses': ['a']}}, 'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['regression: nested precautionary labels',
[{'base': {'allergens': [], 'may_contain': ['milk', 'peanuts'], 'uses': ['sauce']},
'crust': {'allergens': ['milk'], 'may_contain': ['peanuts', 'wheat'], 'uses': ['filling']},
'filling': {'allergens': ['dairy'], 'may_contain': ['dairy'], 'uses': []},
'sauce': {'allergens': ['eggs', 'Egg'],
'may_contain': ['eggs', 'soy'],
'uses': ['crust', 'filling', 'topping']},
'topping': {'allergens': [' sesame ', 'soy'], 'may_contain': ['soy', ' sesame '], 'uses': []}},
'base'],
{'contains': ['egg', 'milk', 'sesame', 'soy'], 'error': None, 'may_contain': ['peanut', 'wheat']}],
['repair check: nested precautionary labels',
[{'base': {'allergens': ['dairy'], 'may_contain': [], 'uses': ['sauce', 'filling']},
'crust': {'allergens': ['soy', 'milk'], 'may_contain': [], 'uses': []},
'filling': {'allergens': ['wheat'], 'may_contain': ['wheat'], 'uses': []},
'sauce': {'allergens': ['soy'], 'may_contain': ['Egg'], 'uses': ['crust', 'filling']},
'topping': {'allergens': ['milk'], 'may_contain': ['tree nuts', 'dairy'], 'uses': ['mystery']}},
'base'],
{'contains': ['milk', 'soy', 'wheat'], 'error': None, 'may_contain': ['egg']}],
['generated control 1',
[{'base': {'allergens': ['peanuts'],
'may_contain': ['dairy', 'eggs'],
'uses': ['sauce', 'crust', 'filling']},
'crust': {'allergens': [' sesame ', 'wheat'], 'may_contain': [], 'uses': []},
'filling': {'allergens': ['dairy'], 'may_contain': ['dairy'], 'uses': []},
'sauce': {'allergens': [], 'may_contain': ['milk'], 'uses': []},
'topping': {'allergens': ['tree nuts'], 'may_contain': ['tree nuts', 'soy'], 'uses': ['filling']}},
'base'],
{'contains': ['milk', 'peanut', 'sesame', 'wheat'], 'error': None, 'may_contain': ['egg']}],
['generated control 2',
[{'base': {'allergens': ['peanuts'], 'may_contain': ['eggs', 'dairy'], 'uses': ['sauce']},
'crust': {'allergens': ['peanuts'], 'may_contain': [], 'uses': ['filling', 'sauce']},
'filling': {'allergens': [], 'may_contain': [], 'uses': ['topping', 'mystery']},
'sauce': {'allergens': ['Egg', 'dairy'], 'may_contain': [], 'uses': ['topping', 'base']},
'topping': {'allergens': ['tree nuts', 'soy'], 'may_contain': [' sesame ', 'milk'], 'uses': []}},
'base'],
{'contains': [], 'error': 'cycle: base', 'may_contain': []}],
['generated control 3',
[{'base': {'allergens': [], 'may_contain': ['eggs', ' sesame '], 'uses': []},
'crust': {'allergens': ['eggs', 'milk'], 'may_contain': ['eggs'], 'uses': []},
'filling': {'allergens': ['milk'], 'may_contain': [], 'uses': []},
'sauce': {'allergens': ['tree nuts', 'Egg'], 'may_contain': [], 'uses': ['filling']},
'topping': {'allergens': [], 'may_contain': ['soy'], 'uses': []}},
'base'],
{'contains': [], 'error': None, 'may_contain': ['egg', 'sesame']}]]]
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 |
|---|---|---|---|
| flat recipe | {'contains': ['milk'], 'error': None, 'may_contain': ['soy']} | {'contains': ['milk'], 'error': None, 'may_contain': ['soy']} | Passed |
| diamond sharing | {'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []} | {'contains': ['milk', 'wheat'], 'error': None, 'may_contain': []} | Passed |
| cycle | {'contains': [], 'error': 'cycle: base', 'may_contain': []} | {'contains': [], 'error': 'cycle: base', 'may_contain': []} | Passed |
| regression: nested precautionary labels | {'contains': ['peanut', 'sesame', 'soy'], 'error': None, 'may_contain': ['egg', 'wheat']} | {'contains': ['peanut', 'sesame', 'soy'], 'error': None, 'may_contain': ['egg', 'wheat']} | Passed |
| repair check: nested precautionary labels | {'contains': ['egg', 'soy', 'tree nut', 'wheat'], 'error': None, 'may_contain': ['peanut']} | {'contains': ['egg', 'soy', 'tree nut', 'wheat'], 'error': None, 'may_contain': ['peanut']} | Passed |
| generated control 1 | {'contains': [], 'error': 'unknown: mystery', 'may_contain': []} | {'contains': [], 'error': 'unknown: mystery', 'may_contain': []} | Passed |
| generated control 2 | {'contains': ['milk'], 'error': None, 'may_contain': ['tree nut']} | {'contains': ['milk'], 'error': None, 'may_contain': ['tree nut']} | Passed |
| generated control 3 | {'contains': [], 'error': 'cycle: base', 'may_contain': []} | {'contains': [], 'error': 'cycle: base', 'may_contain': []} | Passed |
SHA-256 / 09cb30a61b75a392c5b28519ec8b45f3547bc0fec0bd482887cd9705ea0bcfd7
Verification & scope
A deterministic toy contract stated in full here; it is a bounded teaching model, not an authoritative reference or standards implementation. 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:52:30.444254+00:00.
Case digest / f056af9718eca347895b1bb5a9cdeae6c5ee380a34e09f9d2d213a608caf8bcb