broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(flags, name):
    def ev(key, stack):
        f = flags.get(key)
        if f is None:
            return [None, 'MISSING', key]
        if key in stack:
            return [None, 'CYCLE', key]
        if not f['on']:
            return [f['off'], 'OFF', None]
        for pre, want in f.get('prereqs', []):
            got = ev(pre, stack + [key])
            if got[1] == 'CYCLE':
                return got
            if got[0] != want or got[1] == 'OFF':
                return [f['off'], 'PREREQUISITE_FAILED', pre]
        return [f['serve'], 'ON', None]
    return ev(name, [])
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('off prerequisite whose off value equals the requirement fails',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'off']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('failure names the unmet prerequisite',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('cycle revisits the root',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('prerequisite graph sample 1',
   [{'a': {'on': False, 'off': 'x', 'serve': 'on', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['e', 'on'], ['e', 'off']]},
     'c': {'on': False, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'x'], ['e', 'x'], ['b', 'off']]}},
    'd'],
   [None, 'MISSING', 'd']),
  ('prerequisite graph sample 2',
   [{'a': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on'], ['c', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['z', 'on'], ['d', 'x'], ['c', 'x']]},
     'e': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on'], ['c', 'on']]}},
    'b'],
   [None, 'MISSING', 'z'])],
 [('failure names the unmet prerequisite',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('cycle revisits the root',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('prerequisite graph sample 13',
   [{'a': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'off'], ['d', 'x'], ['e', 'off']]},
     'b': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'on'], ['z', 'x'], ['b', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['e', 'on'], ['d', 'off']]},
     'd': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]}},
    'a'],
   [None, 'MISSING', 'z']),
  ('prerequisite graph sample 49',
   [{'a': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []},
     'c': {'on': False, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'off'], ['c', 'on'], ['b', 'x']]},
     'd': {'on': False, 'off': 'off', 'serve': 'x', 'prereqs': [['e', 'on'], ['a', 'off']]},
     'e': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'on'], ['a', 'off'], ['e', 'on']]}},
    'e'],
   [None, 'MISSING', 'z'])],
 [('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('nested failure serving required off variation passes',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('prerequisite graph sample 11',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'b': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'x'], ['b', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []},
     'd': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'x'], ['z', 'x'], ['e', 'on']]},
     'e': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'e'],
   ['on', 'ON', None]),
  ('prerequisite graph sample 12',
   [{'a': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['c', 'on'], ['z', 'x']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['b', 'on']]},
     'c': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []}},
    'c'],
   [None, 'CYCLE', 'b']),
  ('prerequisite graph sample 39',
   [{'a': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'on'], ['e', 'off']]},
     'b': {'on': False, 'off': 'x', 'serve': 'on', 'prereqs': [['e', 'on'], ['c', 'on']]},
     'c': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'x']]},
     'd': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'e': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['z', 'off'], ['c', 'on']]}},
    'c'],
   [None, 'MISSING', 'z'])],
 [('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('nested failure serving required off variation passes',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('cycle revisits the root',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('prerequisite graph sample 2',
   [{'a': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on'], ['c', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['z', 'on'], ['d', 'x'], ['c', 'x']]},
     'e': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on'], ['c', 'on']]}},
    'b'],
   [None, 'MISSING', 'z']),
  ('prerequisite graph sample 16',
   [{'a': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['c', 'on'], ['d', 'off']]},
     'c': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []},
     'd': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['z', 'off'], ['z', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['d', 'on']]}},
    'c'],
   ['x', 'ON', None]),
  ('prerequisite graph sample 49',
   [{'a': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []},
     'c': {'on': False, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'off'], ['c', 'on'], ['b', 'x']]},
     'd': {'on': False, 'off': 'off', 'serve': 'x', 'prereqs': [['e', 'on'], ['a', 'off']]},
     'e': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'on'], ['a', 'off'], ['e', 'on']]}},
    'e'],
   [None, 'MISSING', 'z'])],
 [('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('nested failure serving required off variation passes',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('cycle revisits the root',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('diamond dependency is not a cycle',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['d', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['d', 'on']]},
     'd': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('prerequisite graph sample 21',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['b', 'on'], ['d', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['d', 'off'], ['a', 'on'], ['d', 'x']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'x']]},
     'd': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'e'],
   [None, 'MISSING', 'e']),
  ('prerequisite graph sample 51',
   [{'c': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'off']]},
     'd': {'on': False, 'off': 'x', 'serve': 'x', 'prereqs': [['a', 'on'], ['a', 'on'], ['c', 'x']]},
     'e': {'on': False, 'off': 'x', 'serve': 'on', 'prereqs': [['e', 'off']]}},
    'c'],
   [None, 'MISSING', 'z'])]]
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)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(flags, name):
    def ev(key, stack):
        f = flags.get(key)
        if f is None:
            return [None, 'MISSING', key]
        if key in stack:
            return [None, 'CYCLE', key]
        if not f['on']:
            return [f['off'], 'OFF', None]
        for pre, want in f.get('prereqs', []):
            got = ev(pre, stack + [key])
            if got[1] in ('MISSING', 'CYCLE'):
                return [f['off'], got[1], got[2]]
            if got[0] != want or got[1] == 'OFF':
                return [f['off'], 'PREREQUISITE_FAILED', pre]
        return [f['serve'], 'ON', None]
    return ev(name, [])
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('off prerequisite whose off value equals the requirement fails',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'off']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('failure names the unmet prerequisite',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('cycle revisits the root',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('prerequisite graph sample 1',
   [{'a': {'on': False, 'off': 'x', 'serve': 'on', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['e', 'on'], ['e', 'off']]},
     'c': {'on': False, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'x'], ['e', 'x'], ['b', 'off']]}},
    'd'],
   [None, 'MISSING', 'd']),
  ('prerequisite graph sample 2',
   [{'a': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on'], ['c', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['z', 'on'], ['d', 'x'], ['c', 'x']]},
     'e': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on'], ['c', 'on']]}},
    'b'],
   [None, 'MISSING', 'z'])],
 [('failure names the unmet prerequisite',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('cycle revisits the root',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('prerequisite graph sample 13',
   [{'a': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'off'], ['d', 'x'], ['e', 'off']]},
     'b': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'on'], ['z', 'x'], ['b', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['e', 'on'], ['d', 'off']]},
     'd': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]}},
    'a'],
   [None, 'MISSING', 'z']),
  ('prerequisite graph sample 49',
   [{'a': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []},
     'c': {'on': False, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'off'], ['c', 'on'], ['b', 'x']]},
     'd': {'on': False, 'off': 'off', 'serve': 'x', 'prereqs': [['e', 'on'], ['a', 'off']]},
     'e': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'on'], ['a', 'off'], ['e', 'on']]}},
    'e'],
   [None, 'MISSING', 'z'])],
 [('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('nested failure serving required off variation passes',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('prerequisite graph sample 11',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'b': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'x'], ['b', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []},
     'd': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'x'], ['z', 'x'], ['e', 'on']]},
     'e': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'e'],
   ['on', 'ON', None]),
  ('prerequisite graph sample 12',
   [{'a': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['c', 'on'], ['z', 'x']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['b', 'on']]},
     'c': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []}},
    'c'],
   [None, 'CYCLE', 'b']),
  ('prerequisite graph sample 39',
   [{'a': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'on'], ['e', 'off']]},
     'b': {'on': False, 'off': 'x', 'serve': 'on', 'prereqs': [['e', 'on'], ['c', 'on']]},
     'c': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'x']]},
     'd': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'e': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['z', 'off'], ['c', 'on']]}},
    'c'],
   [None, 'MISSING', 'z'])],
 [('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('nested failure serving required off variation passes',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('cycle revisits the root',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('prerequisite graph sample 2',
   [{'a': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on'], ['c', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['z', 'on'], ['d', 'x'], ['c', 'x']]},
     'e': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on'], ['c', 'on']]}},
    'b'],
   [None, 'MISSING', 'z']),
  ('prerequisite graph sample 16',
   [{'a': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['c', 'on'], ['d', 'off']]},
     'c': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []},
     'd': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['z', 'off'], ['z', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['d', 'on']]}},
    'c'],
   ['x', 'ON', None]),
  ('prerequisite graph sample 49',
   [{'a': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []},
     'c': {'on': False, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'off'], ['c', 'on'], ['b', 'x']]},
     'd': {'on': False, 'off': 'off', 'serve': 'x', 'prereqs': [['e', 'on'], ['a', 'off']]},
     'e': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'on'], ['a', 'off'], ['e', 'on']]}},
    'e'],
   [None, 'MISSING', 'z'])],
 [('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('nested failure serving required off variation passes',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('cycle revisits the root',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('diamond dependency is not a cycle',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['d', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['d', 'on']]},
     'd': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('prerequisite graph sample 21',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['b', 'on'], ['d', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['d', 'off'], ['a', 'on'], ['d', 'x']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'x']]},
     'd': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'e'],
   [None, 'MISSING', 'e']),
  ('prerequisite graph sample 51',
   [{'c': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'off']]},
     'd': {'on': False, 'off': 'x', 'serve': 'x', 'prereqs': [['a', 'on'], ['a', 'on'], ['c', 'x']]},
     'e': {'on': False, 'off': 'x', 'serve': 'on', 'prereqs': [['e', 'off']]}},
    'c'],
   [None, 'MISSING', 'z'])]]
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)

prompt:
A typo in a prerequisite key is reported as a routine prerequisite failure.

flags maps key -> {on, off, serve, prereqs:[[key, required variation]]}. Missing flags yield [None, MISSING, key]; revisiting a flag on the current path yields [None, CYCLE, key] (checked before on/off). An off flag serves [off, OFF, None]. Prerequisites are evaluated in order; MISSING/CYCLE results propagate unchanged; the first prerequisite that is OFF or serves a different variation stops evaluation with [off, PREREQUISITE_FAILED, that prerequisite key]. A prerequisite that itself failed counts as serving its off variation. Otherwise [serve, ON, None].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(flags, name):
    def ev(key, stack):
        f = flags.get(key)
        if f is None:
            return [None, 'MISSING', key]
        if key in stack:
            return [None, 'CYCLE', key]
        if not f['on']:
            return [f['off'], 'OFF', None]
        failed = None
        for pre, want in f.get('prereqs', []):
            got = ev(pre, stack + [key])
            if got[1] in ('MISSING', 'CYCLE'):
                return got
            if got[0] != want or got[1] == 'OFF':
                failed = pre
        if failed is not None:
            return [f['off'], 'PREREQUISITE_FAILED', failed]
        return [f['serve'], 'ON', None]
    return ev(name, [])
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('off prerequisite whose off value equals the requirement fails',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'off']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('failure names the unmet prerequisite',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('prerequisite graph sample 1',
   [{'a': {'on': False, 'off': 'x', 'serve': 'on', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['e', 'on'], ['e', 'off']]},
     'c': {'on': False, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'x'], ['e', 'x'], ['b', 'off']]}},
    'd'],
   [None, 'MISSING', 'd']),
  ('prerequisite graph sample 2',
   [{'a': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on'], ['c', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['z', 'on'], ['d', 'x'], ['c', 'x']]},
     'e': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on'], ['c', 'on']]}},
    'b'],
   [None, 'MISSING', 'z'])],
 [('failure names the unmet prerequisite',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('prerequisite graph sample 6',
   [{'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': [['z', 'on'], ['d', 'off'], ['a', 'on']]},
     'd': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['d', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['z', 'off']]}},
    'c'],
   ['off', 'OFF', None]),
  ('prerequisite graph sample 7',
   [{'a': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['z', 'x'], ['a', 'x'], ['a', 'on']]},
     'b': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'on']]},
     'd': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['z', 'on']]},
     'e': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['b', 'x'], ['d', 'on'], ['b', 'on']]}},
    'c'],
   [None, 'MISSING', 'c']),
  ('prerequisite graph sample 32',
   [{'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'off'], ['e', 'off']]},
     'c': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['d', 'on'], ['a', 'off']]},
     'd': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': [['z', 'on']]}},
    'c'],
   ['x', 'PREREQUISITE_FAILED', 'd'])],
 [('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('nested failure serving required off variation passes',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('prerequisite graph sample 11',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'b': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'x'], ['b', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []},
     'd': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'x'], ['z', 'x'], ['e', 'on']]},
     'e': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'e'],
   ['on', 'ON', None]),
  ('prerequisite graph sample 12',
   [{'a': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['c', 'on'], ['z', 'x']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['b', 'on']]},
     'c': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []}},
    'c'],
   [None, 'CYCLE', 'b']),
  ('prerequisite graph sample 32',
   [{'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'off'], ['e', 'off']]},
     'c': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['d', 'on'], ['a', 'off']]},
     'd': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': [['z', 'on']]}},
    'c'],
   ['x', 'PREREQUISITE_FAILED', 'd'])],
 [('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('nested failure serving required off variation passes',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('cycle revisits the root',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('prerequisite graph sample 16',
   [{'a': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['c', 'on'], ['d', 'off']]},
     'c': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []},
     'd': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['z', 'off'], ['z', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['d', 'on']]}},
    'c'],
   ['x', 'ON', None]),
  ('prerequisite graph sample 17',
   [{'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['c', 'off']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'd'],
   [None, 'MISSING', 'd']),
  ('prerequisite graph sample 32',
   [{'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'off'], ['e', 'off']]},
     'c': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['d', 'on'], ['a', 'off']]},
     'd': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': [['z', 'on']]}},
    'c'],
   ['x', 'PREREQUISITE_FAILED', 'd'])],
 [('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('nested failure serving required off variation passes',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('cycle revisits the root',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('diamond dependency is not a cycle',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['d', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['d', 'on']]},
     'd': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('prerequisite graph sample 21',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['b', 'on'], ['d', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['d', 'off'], ['a', 'on'], ['d', 'x']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'x']]},
     'd': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'e'],
   [None, 'MISSING', 'e']),
  ('prerequisite graph sample 22',
   [{'a': {'on': False, 'off': 'off', 'serve': 'x', 'prereqs': [['d', 'x'], ['e', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['c', 'off'], ['a', 'x']]},
     'c': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'on']]},
     'd': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': []},
     'e': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   ['off', 'OFF', None]),
  ('prerequisite graph sample 23',
   [{'a': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'e': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'x'], ['c', 'on'], ['z', 'on']]}},
    'a'],
   ['x', 'ON', None])]]
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)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(flags, name):
    def ev(key, stack):
        f = flags.get(key)
        if f is None:
            return [None, 'MISSING', key]
        if key in stack:
            return [None, 'CYCLE', key]
        if not f['on']:
            return [f['off'], 'OFF', None]
        failed = None
        for pre, want in f.get('prereqs', []):
            got = ev(pre, stack + [key])
            if got[1] in ('MISSING', 'CYCLE'):
                return got
            if got[0] != want or got[1] == 'OFF':
                if failed is None:
                    failed = pre
        if failed is not None:
            return [f['off'], 'PREREQUISITE_FAILED', failed]
        return [f['serve'], 'ON', None]
    return ev(name, [])
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('off prerequisite whose off value equals the requirement fails',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'off']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('failure names the unmet prerequisite',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('prerequisite graph sample 1',
   [{'a': {'on': False, 'off': 'x', 'serve': 'on', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['e', 'on'], ['e', 'off']]},
     'c': {'on': False, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'x'], ['e', 'x'], ['b', 'off']]}},
    'd'],
   [None, 'MISSING', 'd']),
  ('prerequisite graph sample 2',
   [{'a': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on'], ['c', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['z', 'on'], ['d', 'x'], ['c', 'x']]},
     'e': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on'], ['c', 'on']]}},
    'b'],
   [None, 'MISSING', 'z'])],
 [('failure names the unmet prerequisite',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('prerequisite graph sample 6',
   [{'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': [['z', 'on'], ['d', 'off'], ['a', 'on']]},
     'd': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['d', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['z', 'off']]}},
    'c'],
   ['off', 'OFF', None]),
  ('prerequisite graph sample 7',
   [{'a': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['z', 'x'], ['a', 'x'], ['a', 'on']]},
     'b': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'on']]},
     'd': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['z', 'on']]},
     'e': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['b', 'x'], ['d', 'on'], ['b', 'on']]}},
    'c'],
   [None, 'MISSING', 'c']),
  ('prerequisite graph sample 32',
   [{'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'off'], ['e', 'off']]},
     'c': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['d', 'on'], ['a', 'off']]},
     'd': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': [['z', 'on']]}},
    'c'],
   ['x', 'PREREQUISITE_FAILED', 'd'])],
 [('cycle through an off member reports CYCLE',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('nested failure serving required off variation passes',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('prerequisite graph sample 11',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'b': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'x'], ['b', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []},
     'd': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'x'], ['z', 'x'], ['e', 'on']]},
     'e': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'e'],
   ['on', 'ON', None]),
  ('prerequisite graph sample 12',
   [{'a': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['c', 'on'], ['z', 'x']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['b', 'on']]},
     'c': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []}},
    'c'],
   [None, 'CYCLE', 'b']),
  ('prerequisite graph sample 32',
   [{'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'off'], ['e', 'off']]},
     'c': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['d', 'on'], ['a', 'off']]},
     'd': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': [['z', 'on']]}},
    'c'],
   ['x', 'PREREQUISITE_FAILED', 'd'])],
 [('missing prerequisite propagates MISSING',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['zz', 'on']]}}, 'a'],
   [None, 'MISSING', 'zz']),
  ('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('nested failure serving required off variation passes',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('cycle revisits the root',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('prerequisite graph sample 16',
   [{'a': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['c', 'on'], ['d', 'off']]},
     'c': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': []},
     'd': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['z', 'off'], ['z', 'on']]},
     'e': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['d', 'on']]}},
    'c'],
   ['x', 'ON', None]),
  ('prerequisite graph sample 17',
   [{'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['c', 'off']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'd'],
   [None, 'MISSING', 'd']),
  ('prerequisite graph sample 32',
   [{'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['b', 'off'], ['e', 'off']]},
     'c': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['d', 'on'], ['a', 'off']]},
     'd': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': [['z', 'on']]}},
    'c'],
   ['x', 'PREREQUISITE_FAILED', 'd'])],
 [('first unmet prerequisite stops evaluation',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'c': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('later missing prerequisite is never reached',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['zz', 'on']]},
     'b': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['off', 'PREREQUISITE_FAILED', 'b']),
  ('nested failure serving required off variation passes',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'x']]},
     'b': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': [['c', 'on']]},
     'c': {'on': False, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('cycle revisits the root',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   [None, 'CYCLE', 'a']),
  ('diamond dependency is not a cycle',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['d', 'on']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['d', 'on']]},
     'd': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'a'],
   ['on', 'ON', None]),
  ('prerequisite graph sample 21',
   [{'a': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['b', 'on'], ['b', 'on'], ['d', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': [['d', 'off'], ['a', 'on'], ['d', 'x']]},
     'c': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'x']]},
     'd': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': []}},
    'e'],
   [None, 'MISSING', 'e']),
  ('prerequisite graph sample 22',
   [{'a': {'on': False, 'off': 'off', 'serve': 'x', 'prereqs': [['d', 'x'], ['e', 'on'], ['c', 'on']]},
     'b': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['c', 'off'], ['a', 'x']]},
     'c': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['c', 'on']]},
     'd': {'on': True, 'off': 'x', 'serve': 'on', 'prereqs': []},
     'e': {'on': True, 'off': 'off', 'serve': 'on', 'prereqs': [['a', 'on']]}},
    'a'],
   ['off', 'OFF', None]),
  ('prerequisite graph sample 23',
   [{'a': {'on': True, 'off': 'off', 'serve': 'x', 'prereqs': []},
     'e': {'on': True, 'off': 'x', 'serve': 'x', 'prereqs': [['z', 'x'], ['c', 'on'], ['z', 'on']]}},
    'a'],
   ['x', 'ON', None])]]
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)

prompt:
The reported blocking prerequisite is the last failing one, and later broken references surface.

flags maps key -> {on, off, serve, prereqs:[[key, required variation]]}. Missing flags yield [None, MISSING, key]; revisiting a flag on the current path yields [None, CYCLE, key] (checked before on/off). An off flag serves [off, OFF, None]. Prerequisites are evaluated in order; MISSING/CYCLE results propagate unchanged; the first prerequisite that is OFF or serves a different variation stops evaluation with [off, PREREQUISITE_FAILED, that prerequisite key]. A prerequisite that itself failed counts as serving its off variation. Otherwise [serve, ON, None].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(segment, user):
    key = user.get('key')
    def hit(clause):
        attr, values = clause
        if attr not in user:
            return False
        value = user[attr]
        items = value if isinstance(value, list) else [value]
        return any(item in values for item in items)
    if key in segment.get('excluded', []):
        return [False, 'excluded']
    if key in segment.get('included', []):
        return [True, 'included']
    for i, rule in enumerate(segment.get('rules', [])):
        clauses = rule.get('clauses', [])
        if clauses and all(hit(c) for c in clauses):
            return [True, 'rule:%d' % i]
    return [False, 'none']
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('explicit inclusion beats exclusion',
   [{'included': ['k1'], 'excluded': ['k1'], 'rules': []}, {'key': 'k1'}],
   [True, 'included']),
  ('exclusion blocks a matching rule',
   [{'included': ['k2'], 'excluded': ['k1'], 'rules': [{'clauses': [['region', ['eu']]]}]},
    {'key': 'k1', 'region': 'eu'}],
   [False, 'excluded']),
  ('empty clause list never matches',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('segment sample 1',
   [{'included': ['k3'], 'excluded': ['k2', 'k1'], 'rules': [{'clauses': []}, {'clauses': []}]},
    {'key': 'k2', 'groups': ['beta'], 'tier': 'gold'}],
   [False, 'excluded']),
  ('segment sample 2',
   [{'included': [], 'excluded': ['k4', 'k1'], 'rules': [{'clauses': [['groups', ['staff', 'gold']]]}]},
    {'key': 'k4', 'region': 'eu'}],
   [False, 'excluded']),
  ('segment sample 3',
   [{'included': ['k4', 'k3'], 'excluded': ['k4'], 'rules': []}, {'key': 'k3'}],
   [True, 'included'])],
 [('explicit inclusion beats exclusion',
   [{'included': ['k1'], 'excluded': ['k1'], 'rules': []}, {'key': 'k1'}],
   [True, 'included']),
  ('exclusion blocks a matching rule',
   [{'included': ['k2'], 'excluded': ['k1'], 'rules': [{'clauses': [['region', ['eu']]]}]},
    {'key': 'k1', 'region': 'eu'}],
   [False, 'excluded']),
  ('empty clause list never matches',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('segment sample 15',
   [{'included': ['k3', 'k4'],
     'excluded': ['k4', 'k1'],
     'rules': [{'clauses': [['tier', ['gold', '']], ['region', ['eu', 'beta']]]}, {'clauses': []}]},
    {'key': 'k4', 'groups': ['beta'], 'region': ''}],
   [True, 'included']),
  ('segment sample 36',
   [{'included': ['k1'], 'excluded': ['k2', 'k3'], 'rules': []},
    {'key': 'k2', 'groups': ['x'], 'tier': None}],
   [False, 'excluded'])],
 [('empty clause list never matches',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('empty list attribute is not a member',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': []}],
   [False, 'none']),
  ('segment sample 11',
   [{'included': ['k1'], 'excluded': ['k3'], 'rules': [{'clauses': []}]},
    {'key': 'k2', 'groups': ['staff', 'eu'], 'tier': 'gold'}],
   [False, 'none']),
  ('segment sample 49',
   [{'included': ['k2', 'k4'], 'excluded': ['k3'], 'rules': []}, {'key': 'k3'}],
   [False, 'excluded']),
  ('segment sample 55',
   [{'included': ['k1', 'k2'], 'excluded': ['k2'], 'rules': [{'clauses': [['tier', ['', 'beta']]]}]},
    {'key': 'k2', 'groups': ['beta']}],
   [True, 'included'])],
 [('explicit inclusion beats exclusion',
   [{'included': ['k1'], 'excluded': ['k1'], 'rules': []}, {'key': 'k1'}],
   [True, 'included']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('empty list attribute is not a member',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': []}],
   [False, 'none']),
  ('all clauses must match',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['eu']], ['groups', ['beta']]]}]},
    {'key': 'k1', 'region': 'eu', 'groups': ['x']}],
   [False, 'none']),
  ('segment sample 1',
   [{'included': ['k3'], 'excluded': ['k2', 'k1'], 'rules': [{'clauses': []}, {'clauses': []}]},
    {'key': 'k2', 'groups': ['beta'], 'tier': 'gold'}],
   [False, 'excluded']),
  ('segment sample 16',
   [{'included': ['k3', 'k1'], 'excluded': [], 'rules': [{'clauses': [['groups', ['gold', 'staff']]]}]},
    {'key': 'k2', 'groups': ['beta', 'staff'], 'region': 'eu'}],
   [True, 'rule:0'])],
 [('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('empty list attribute is not a member',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': []}],
   [False, 'none']),
  ('all clauses must match',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['eu']], ['groups', ['beta']]]}]},
    {'key': 'k1', 'region': 'eu', 'groups': ['x']}],
   [False, 'none']),
  ('second rule index',
   [{'included': [],
     'excluded': [],
     'rules': [{'clauses': [['region', ['us']]]}, {'clauses': [['region', ['eu']]]}]},
    {'key': 'k1', 'region': 'eu'}],
   [True, 'rule:1']),
  ('segment sample 15',
   [{'included': ['k3', 'k4'],
     'excluded': ['k4', 'k1'],
     'rules': [{'clauses': [['tier', ['gold', '']], ['region', ['eu', 'beta']]]}, {'clauses': []}]},
    {'key': 'k4', 'groups': ['beta'], 'region': ''}],
   [True, 'included']),
  ('segment sample 21',
   [{'included': ['k3'], 'excluded': [], 'rules': []}, {'key': 'k9', 'groups': 'beta'}],
   [False, 'none']),
  ('segment sample 39',
   [{'included': ['k2'], 'excluded': ['k4', 'k1'], 'rules': [{'clauses': [['region', ['eu', 'staff']]]}]},
    {'key': 'k4', 'groups': ['beta']}],
   [False, 'excluded'])]]
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)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(segment, user):
    key = user.get('key')
    def hit(clause):
        attr, values = clause
        if attr not in user:
            return False
        value = user[attr]
        items = value if isinstance(value, list) else [value]
        return any(item in values for item in items)
    if key in segment.get('included', []):
        return [True, 'included']
    if not segment.get('included') and key in segment.get('excluded', []):
        return [False, 'excluded']
    for i, rule in enumerate(segment.get('rules', [])):
        clauses = rule.get('clauses', [])
        if clauses and all(hit(c) for c in clauses):
            return [True, 'rule:%d' % i]
    return [False, 'none']
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('explicit inclusion beats exclusion',
   [{'included': ['k1'], 'excluded': ['k1'], 'rules': []}, {'key': 'k1'}],
   [True, 'included']),
  ('exclusion blocks a matching rule',
   [{'included': ['k2'], 'excluded': ['k1'], 'rules': [{'clauses': [['region', ['eu']]]}]},
    {'key': 'k1', 'region': 'eu'}],
   [False, 'excluded']),
  ('empty clause list never matches',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('segment sample 1',
   [{'included': ['k3'], 'excluded': ['k2', 'k1'], 'rules': [{'clauses': []}, {'clauses': []}]},
    {'key': 'k2', 'groups': ['beta'], 'tier': 'gold'}],
   [False, 'excluded']),
  ('segment sample 2',
   [{'included': [], 'excluded': ['k4', 'k1'], 'rules': [{'clauses': [['groups', ['staff', 'gold']]]}]},
    {'key': 'k4', 'region': 'eu'}],
   [False, 'excluded']),
  ('segment sample 3',
   [{'included': ['k4', 'k3'], 'excluded': ['k4'], 'rules': []}, {'key': 'k3'}],
   [True, 'included'])],
 [('explicit inclusion beats exclusion',
   [{'included': ['k1'], 'excluded': ['k1'], 'rules': []}, {'key': 'k1'}],
   [True, 'included']),
  ('exclusion blocks a matching rule',
   [{'included': ['k2'], 'excluded': ['k1'], 'rules': [{'clauses': [['region', ['eu']]]}]},
    {'key': 'k1', 'region': 'eu'}],
   [False, 'excluded']),
  ('empty clause list never matches',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('segment sample 15',
   [{'included': ['k3', 'k4'],
     'excluded': ['k4', 'k1'],
     'rules': [{'clauses': [['tier', ['gold', '']], ['region', ['eu', 'beta']]]}, {'clauses': []}]},
    {'key': 'k4', 'groups': ['beta'], 'region': ''}],
   [True, 'included']),
  ('segment sample 36',
   [{'included': ['k1'], 'excluded': ['k2', 'k3'], 'rules': []},
    {'key': 'k2', 'groups': ['x'], 'tier': None}],
   [False, 'excluded'])],
 [('empty clause list never matches',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('empty list attribute is not a member',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': []}],
   [False, 'none']),
  ('segment sample 11',
   [{'included': ['k1'], 'excluded': ['k3'], 'rules': [{'clauses': []}]},
    {'key': 'k2', 'groups': ['staff', 'eu'], 'tier': 'gold'}],
   [False, 'none']),
  ('segment sample 49',
   [{'included': ['k2', 'k4'], 'excluded': ['k3'], 'rules': []}, {'key': 'k3'}],
   [False, 'excluded']),
  ('segment sample 55',
   [{'included': ['k1', 'k2'], 'excluded': ['k2'], 'rules': [{'clauses': [['tier', ['', 'beta']]]}]},
    {'key': 'k2', 'groups': ['beta']}],
   [True, 'included'])],
 [('explicit inclusion beats exclusion',
   [{'included': ['k1'], 'excluded': ['k1'], 'rules': []}, {'key': 'k1'}],
   [True, 'included']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('empty list attribute is not a member',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': []}],
   [False, 'none']),
  ('all clauses must match',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['eu']], ['groups', ['beta']]]}]},
    {'key': 'k1', 'region': 'eu', 'groups': ['x']}],
   [False, 'none']),
  ('segment sample 1',
   [{'included': ['k3'], 'excluded': ['k2', 'k1'], 'rules': [{'clauses': []}, {'clauses': []}]},
    {'key': 'k2', 'groups': ['beta'], 'tier': 'gold'}],
   [False, 'excluded']),
  ('segment sample 16',
   [{'included': ['k3', 'k1'], 'excluded': [], 'rules': [{'clauses': [['groups', ['gold', 'staff']]]}]},
    {'key': 'k2', 'groups': ['beta', 'staff'], 'region': 'eu'}],
   [True, 'rule:0'])],
 [('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('empty list attribute is not a member',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': []}],
   [False, 'none']),
  ('all clauses must match',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['eu']], ['groups', ['beta']]]}]},
    {'key': 'k1', 'region': 'eu', 'groups': ['x']}],
   [False, 'none']),
  ('second rule index',
   [{'included': [],
     'excluded': [],
     'rules': [{'clauses': [['region', ['us']]]}, {'clauses': [['region', ['eu']]]}]},
    {'key': 'k1', 'region': 'eu'}],
   [True, 'rule:1']),
  ('segment sample 15',
   [{'included': ['k3', 'k4'],
     'excluded': ['k4', 'k1'],
     'rules': [{'clauses': [['tier', ['gold', '']], ['region', ['eu', 'beta']]]}, {'clauses': []}]},
    {'key': 'k4', 'groups': ['beta'], 'region': ''}],
   [True, 'included']),
  ('segment sample 21',
   [{'included': ['k3'], 'excluded': [], 'rules': []}, {'key': 'k9', 'groups': 'beta'}],
   [False, 'none']),
  ('segment sample 39',
   [{'included': ['k2'], 'excluded': ['k4', 'k1'], 'rules': [{'clauses': [['region', ['eu', 'staff']]]}]},
    {'key': 'k4', 'groups': ['beta']}],
   [False, 'excluded'])]]
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)

prompt:
A user deliberately added to a segment is dropped because an older exclusion list also names them.

A user is in the segment if its key is in included (reason included, which beats excluded); else not if its key is in excluded; else if some rule with a non-empty clause list has every clause matching (reason rule:i). A clause [attr, values] matches only when the attribute is present and its value (or any element of a list value) is in values. Otherwise [False, none].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(segment, user):
    key = user.get('key')
    def hit(clause):
        attr, values = clause
        if attr not in user:
            return False
        value = user[attr]
        items = value if isinstance(value, list) else [value]
        return any(item in values for item in items)
    if key in segment.get('included', []):
        return [True, 'included']
    if key in segment.get('excluded', []):
        return [False, 'excluded']
    for i, rule in enumerate(segment.get('rules', [])):
        clauses = rule.get('clauses', [])
        if all(hit(c) for c in clauses):
            return [True, 'rule:%d' % i]
    return [False, 'none']
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('explicit inclusion beats exclusion',
   [{'included': ['k1'], 'excluded': ['k1'], 'rules': []}, {'key': 'k1'}],
   [True, 'included']),
  ('exclusion blocks a matching rule',
   [{'included': ['k2'], 'excluded': ['k1'], 'rules': [{'clauses': [['region', ['eu']]]}]},
    {'key': 'k1', 'region': 'eu'}],
   [False, 'excluded']),
  ('empty clause list never matches',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('all clauses must match',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['eu']], ['groups', ['beta']]]}]},
    {'key': 'k1', 'region': 'eu', 'groups': ['x']}],
   [False, 'none']),
  ('segment sample 1',
   [{'included': ['k3'], 'excluded': ['k2', 'k1'], 'rules': [{'clauses': []}, {'clauses': []}]},
    {'key': 'k2', 'groups': ['beta'], 'tier': 'gold'}],
   [False, 'excluded']),
  ('segment sample 2',
   [{'included': [], 'excluded': ['k4', 'k1'], 'rules': [{'clauses': [['groups', ['staff', 'gold']]]}]},
    {'key': 'k4', 'region': 'eu'}],
   [False, 'excluded'])],
 [('exclusion blocks a matching rule',
   [{'included': ['k2'], 'excluded': ['k1'], 'rules': [{'clauses': [['region', ['eu']]]}]},
    {'key': 'k1', 'region': 'eu'}],
   [False, 'excluded']),
  ('empty clause list never matches',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('all clauses must match',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['eu']], ['groups', ['beta']]]}]},
    {'key': 'k1', 'region': 'eu', 'groups': ['x']}],
   [False, 'none']),
  ('segment sample 33',
   [{'included': ['k1', 'k2'], 'excluded': [], 'rules': [{'clauses': []}, {'clauses': []}]},
    {'key': 'k9', 'groups': ['staff', 'eu'], 'tier': 'gold'}],
   [False, 'none']),
  ('segment sample 42',
   [{'included': [],
     'excluded': ['k4'],
     'rules': [{'clauses': [['region', ['gold', 'eu']], ['region', ['staff', '']]]},
               {'clauses': [['tier', [None, 'eu']]]}]},
    {'key': 'k3', 'groups': ['x'], 'region': ''}],
   [False, 'none'])],
 [('empty clause list never matches',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('empty list attribute is not a member',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': []}],
   [False, 'none']),
  ('segment sample 11',
   [{'included': ['k1'], 'excluded': ['k3'], 'rules': [{'clauses': []}]},
    {'key': 'k2', 'groups': ['staff', 'eu'], 'tier': 'gold'}],
   [False, 'none']),
  ('segment sample 12',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['staff', '']]]}, {'clauses': []}]},
    {'key': 'k9', 'groups': 'beta'}],
   [False, 'none']),
  ('segment sample 34',
   [{'included': [],
     'excluded': ['k3'],
     'rules': [{'clauses': [['tier', ['beta', None]], ['groups', ['staff', 'gold']]]},
               {'clauses': [['groups', ['eu', '']], ['groups', ['', 'eu']]]}]},
    {'key': 'k2', 'groups': ['x'], 'tier': None}],
   [False, 'none'])],
 [('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('empty list attribute is not a member',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': []}],
   [False, 'none']),
  ('all clauses must match',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['eu']], ['groups', ['beta']]]}]},
    {'key': 'k1', 'region': 'eu', 'groups': ['x']}],
   [False, 'none']),
  ('segment sample 16',
   [{'included': ['k3', 'k1'], 'excluded': [], 'rules': [{'clauses': [['groups', ['gold', 'staff']]]}]},
    {'key': 'k2', 'groups': ['beta', 'staff'], 'region': 'eu'}],
   [True, 'rule:0']),
  ('segment sample 19',
   [{'included': ['k3', 'k1'],
     'excluded': ['k4'],
     'rules': [{'clauses': [['groups', ['staff', None]], ['groups', ['gold', '']]]}]},
    {'key': 'k2', 'groups': ['beta', 'staff']}],
   [False, 'none']),
  ('segment sample 59',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}, {'clauses': [['tier', ['staff', 'beta']]]}]},
    {'key': 'k1', 'groups': ['x']}],
   [False, 'none'])],
 [('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('empty list attribute is not a member',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': []}],
   [False, 'none']),
  ('all clauses must match',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['eu']], ['groups', ['beta']]]}]},
    {'key': 'k1', 'region': 'eu', 'groups': ['x']}],
   [False, 'none']),
  ('second rule index',
   [{'included': [],
     'excluded': [],
     'rules': [{'clauses': [['region', ['us']]]}, {'clauses': [['region', ['eu']]]}]},
    {'key': 'k1', 'region': 'eu'}],
   [True, 'rule:1']),
  ('segment sample 21',
   [{'included': ['k3'], 'excluded': [], 'rules': []}, {'key': 'k9', 'groups': 'beta'}],
   [False, 'none']),
  ('segment sample 22',
   [{'included': [], 'excluded': ['k1'], 'rules': []}, {'key': 'k2', 'region': 'us'}],
   [False, 'none']),
  ('segment sample 30',
   [{'included': ['k1', 'k2'], 'excluded': ['k2', 'k4'], 'rules': [{'clauses': []}]},
    {'key': 'k3', 'tier': 'gold'}],
   [False, 'none'])]]
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)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(segment, user):
    key = user.get('key')
    def hit(clause):
        attr, values = clause
        if attr not in user:
            return False
        value = user[attr]
        items = value if isinstance(value, list) else [value]
        return any(item in values for item in items)
    if key in segment.get('included', []):
        return [True, 'included']
    if key in segment.get('excluded', []):
        return [False, 'excluded']
    for i, rule in enumerate(segment.get('rules', [])):
        clauses = rule.get('clauses', [])
        if any(hit(c) for c in clauses):
            return [True, 'rule:%d' % i]
    return [False, 'none']
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('explicit inclusion beats exclusion',
   [{'included': ['k1'], 'excluded': ['k1'], 'rules': []}, {'key': 'k1'}],
   [True, 'included']),
  ('exclusion blocks a matching rule',
   [{'included': ['k2'], 'excluded': ['k1'], 'rules': [{'clauses': [['region', ['eu']]]}]},
    {'key': 'k1', 'region': 'eu'}],
   [False, 'excluded']),
  ('empty clause list never matches',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('all clauses must match',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['eu']], ['groups', ['beta']]]}]},
    {'key': 'k1', 'region': 'eu', 'groups': ['x']}],
   [False, 'none']),
  ('segment sample 1',
   [{'included': ['k3'], 'excluded': ['k2', 'k1'], 'rules': [{'clauses': []}, {'clauses': []}]},
    {'key': 'k2', 'groups': ['beta'], 'tier': 'gold'}],
   [False, 'excluded']),
  ('segment sample 2',
   [{'included': [], 'excluded': ['k4', 'k1'], 'rules': [{'clauses': [['groups', ['staff', 'gold']]]}]},
    {'key': 'k4', 'region': 'eu'}],
   [False, 'excluded'])],
 [('exclusion blocks a matching rule',
   [{'included': ['k2'], 'excluded': ['k1'], 'rules': [{'clauses': [['region', ['eu']]]}]},
    {'key': 'k1', 'region': 'eu'}],
   [False, 'excluded']),
  ('empty clause list never matches',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('all clauses must match',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['eu']], ['groups', ['beta']]]}]},
    {'key': 'k1', 'region': 'eu', 'groups': ['x']}],
   [False, 'none']),
  ('segment sample 33',
   [{'included': ['k1', 'k2'], 'excluded': [], 'rules': [{'clauses': []}, {'clauses': []}]},
    {'key': 'k9', 'groups': ['staff', 'eu'], 'tier': 'gold'}],
   [False, 'none']),
  ('segment sample 42',
   [{'included': [],
     'excluded': ['k4'],
     'rules': [{'clauses': [['region', ['gold', 'eu']], ['region', ['staff', '']]]},
               {'clauses': [['tier', [None, 'eu']]]}]},
    {'key': 'k3', 'groups': ['x'], 'region': ''}],
   [False, 'none'])],
 [('empty clause list never matches',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('empty list attribute is not a member',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': []}],
   [False, 'none']),
  ('segment sample 11',
   [{'included': ['k1'], 'excluded': ['k3'], 'rules': [{'clauses': []}]},
    {'key': 'k2', 'groups': ['staff', 'eu'], 'tier': 'gold'}],
   [False, 'none']),
  ('segment sample 12',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['staff', '']]]}, {'clauses': []}]},
    {'key': 'k9', 'groups': 'beta'}],
   [False, 'none']),
  ('segment sample 34',
   [{'included': [],
     'excluded': ['k3'],
     'rules': [{'clauses': [['tier', ['beta', None]], ['groups', ['staff', 'gold']]]},
               {'clauses': [['groups', ['eu', '']], ['groups', ['', 'eu']]]}]},
    {'key': 'k2', 'groups': ['x'], 'tier': None}],
   [False, 'none'])],
 [('absent attribute does not match a null value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['tier', [None, 'gold']]]}]}, {'key': 'k3'}],
   [False, 'none']),
  ('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('empty list attribute is not a member',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': []}],
   [False, 'none']),
  ('all clauses must match',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['eu']], ['groups', ['beta']]]}]},
    {'key': 'k1', 'region': 'eu', 'groups': ['x']}],
   [False, 'none']),
  ('segment sample 16',
   [{'included': ['k3', 'k1'], 'excluded': [], 'rules': [{'clauses': [['groups', ['gold', 'staff']]]}]},
    {'key': 'k2', 'groups': ['beta', 'staff'], 'region': 'eu'}],
   [True, 'rule:0']),
  ('segment sample 19',
   [{'included': ['k3', 'k1'],
     'excluded': ['k4'],
     'rules': [{'clauses': [['groups', ['staff', None]], ['groups', ['gold', '']]]}]},
    {'key': 'k2', 'groups': ['beta', 'staff']}],
   [False, 'none']),
  ('segment sample 59',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': []}, {'clauses': [['tier', ['staff', 'beta']]]}]},
    {'key': 'k1', 'groups': ['x']}],
   [False, 'none'])],
 [('absent attribute does not match an empty string value',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['']]]}]}, {'key': 'k4'}],
   [False, 'none']),
  ('list attribute matches through any element',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': ['staff', 'beta']}],
   [True, 'rule:0']),
  ('empty list attribute is not a member',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['groups', ['beta']]]}]},
    {'key': 'k2', 'groups': []}],
   [False, 'none']),
  ('all clauses must match',
   [{'included': [], 'excluded': [], 'rules': [{'clauses': [['region', ['eu']], ['groups', ['beta']]]}]},
    {'key': 'k1', 'region': 'eu', 'groups': ['x']}],
   [False, 'none']),
  ('second rule index',
   [{'included': [],
     'excluded': [],
     'rules': [{'clauses': [['region', ['us']]]}, {'clauses': [['region', ['eu']]]}]},
    {'key': 'k1', 'region': 'eu'}],
   [True, 'rule:1']),
  ('segment sample 21',
   [{'included': ['k3'], 'excluded': [], 'rules': []}, {'key': 'k9', 'groups': 'beta'}],
   [False, 'none']),
  ('segment sample 22',
   [{'included': [], 'excluded': ['k1'], 'rules': []}, {'key': 'k2', 'region': 'us'}],
   [False, 'none']),
  ('segment sample 30',
   [{'included': ['k1', 'k2'], 'excluded': ['k2', 'k4'], 'rules': [{'clauses': []}]},
    {'key': 'k3', 'tier': 'gold'}],
   [False, 'none'])]]
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)

prompt:
A half-configured segment rule suddenly places every remaining user in the segment.

A user is in the segment if its key is in included (reason included, which beats excluded); else not if its key is in excluded; else if some rule with a non-empty clause list has every clause matching (reason rule:i). A clause [attr, values] matches only when the attribute is present and its value (or any element of a list value) is in values. Otherwise [False, none].

split: open-access