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

N = 1
observations = []
def solve(variants, bucket):
    acc = 0
    last = None
    for name, weight in sorted(variants):
        if weight <= 0:
            continue
        last = name
        acc += round(weight * 100)
        if bucket < acc:
            return name
    return last
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('bucket at first cumulative edge goes to second arm',
   [[['control', 50], ['treatment', 50]], 5000],
   'treatment'),
  ('bucket just below first edge stays in first arm',
   [[['control', 50], ['treatment', 50]], 4999],
   'control'),
  ('zero weight arm is never selected', [[['control', 0], ['treatment', 100]], 0], 'treatment'),
  ('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('three way split middle arm', [[['a', 33.33], ['b', 33.33], ['c', 33.34]], 5000], 'b'),
  ('weight table sample 1',
   [[['green', 1.15], ['holdout', 0], ['alpha', 1.15], ['blue', 50]], 1135],
   'blue')],
 [('bucket just below first edge stays in first arm',
   [[['control', 50], ['treatment', 50]], 4999],
   'control'),
  ('zero weight arm is never selected', [[['control', 0], ['treatment', 100]], 0], 'treatment'),
  ('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('three way split middle arm', [[['a', 33.33], ['b', 33.33], ['c', 33.34]], 5000], 'b'),
  ('weight table sample 13',
   [[['alpha', -5], ['green', 12.5], ['treatment', 50], ['blue', 33.34]], 7194],
   'blue'),
  ('weight table sample 28', [[['alpha', 1.15], ['holdout', 4.35]], 1437], 'holdout')],
 [('zero weight arm is never selected', [[['control', 0], ['treatment', 100]], 0], 'treatment'),
  ('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('no positive weights yields None', [[['a', 0], ['b', 0]], 10], None),
  ('weight table sample 11', [[['holdout', 12.5], ['blue', 12.5], ['alpha', 0.57]], 2500], 'alpha'),
  ('weight table sample 31', [[['green', 1.15], ['blue', 33.33], ['treatment', 4.35]], 3447], 'blue'),
  ('weight table sample 46',
   [[['control', 0], ['alpha', 4.35], ['green', 33.33], ['holdout', 33.33]], 6769],
   'holdout')],
 [('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('no positive weights yields None', [[['a', 0], ['b', 0]], 10], None),
  ('three way split middle arm', [[['a', 33.33], ['b', 33.33], ['c', 33.34]], 5000], 'b'),
  ('weight table sample 16', [[['green', 0.57], ['alpha', 12.5], ['control', 25]], 1307], 'control'),
  ('weight table sample 17', [[['treatment', 1.15], ['control', 0.29], ['blue', 0.29]], 2597], 'blue'),
  ('weight table sample 53',
   [[['treatment', 33.33], ['holdout', 33.34], ['control', 4.35]], 0],
   'treatment')],
 [('bucket at first cumulative edge goes to second arm',
   [[['control', 50], ['treatment', 50]], 5000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('no positive weights yields None', [[['a', 0], ['b', 0]], 10], None),
  ('three way split middle arm', [[['a', 33.33], ['b', 33.33], ['c', 33.34]], 5000], 'b'),
  ('weight table sample 3', [[['treatment', 12.5], ['control', 10], ['holdout', 0.57]], 6309], 'holdout'),
  ('weight table sample 21', [[['green', 33.33], ['treatment', 0]], 0], 'green'),
  ('weight table sample 31', [[['green', 1.15], ['blue', 33.33], ['treatment', 4.35]], 3447], 'blue')]]
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(variants, bucket):
    acc = 0
    last = None
    for name, weight in sorted(variants, key=lambda v: -v[1]):
        if weight <= 0:
            continue
        last = name
        acc += round(weight * 100)
        if bucket < acc:
            return name
    return last
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('bucket at first cumulative edge goes to second arm',
   [[['control', 50], ['treatment', 50]], 5000],
   'treatment'),
  ('bucket just below first edge stays in first arm',
   [[['control', 50], ['treatment', 50]], 4999],
   'control'),
  ('zero weight arm is never selected', [[['control', 0], ['treatment', 100]], 0], 'treatment'),
  ('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('three way split middle arm', [[['a', 33.33], ['b', 33.33], ['c', 33.34]], 5000], 'b'),
  ('weight table sample 1',
   [[['green', 1.15], ['holdout', 0], ['alpha', 1.15], ['blue', 50]], 1135],
   'blue')],
 [('bucket just below first edge stays in first arm',
   [[['control', 50], ['treatment', 50]], 4999],
   'control'),
  ('zero weight arm is never selected', [[['control', 0], ['treatment', 100]], 0], 'treatment'),
  ('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('three way split middle arm', [[['a', 33.33], ['b', 33.33], ['c', 33.34]], 5000], 'b'),
  ('weight table sample 13',
   [[['alpha', -5], ['green', 12.5], ['treatment', 50], ['blue', 33.34]], 7194],
   'blue'),
  ('weight table sample 28', [[['alpha', 1.15], ['holdout', 4.35]], 1437], 'holdout')],
 [('zero weight arm is never selected', [[['control', 0], ['treatment', 100]], 0], 'treatment'),
  ('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('no positive weights yields None', [[['a', 0], ['b', 0]], 10], None),
  ('weight table sample 11', [[['holdout', 12.5], ['blue', 12.5], ['alpha', 0.57]], 2500], 'alpha'),
  ('weight table sample 31', [[['green', 1.15], ['blue', 33.33], ['treatment', 4.35]], 3447], 'blue'),
  ('weight table sample 46',
   [[['control', 0], ['alpha', 4.35], ['green', 33.33], ['holdout', 33.33]], 6769],
   'holdout')],
 [('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('no positive weights yields None', [[['a', 0], ['b', 0]], 10], None),
  ('three way split middle arm', [[['a', 33.33], ['b', 33.33], ['c', 33.34]], 5000], 'b'),
  ('weight table sample 16', [[['green', 0.57], ['alpha', 12.5], ['control', 25]], 1307], 'control'),
  ('weight table sample 17', [[['treatment', 1.15], ['control', 0.29], ['blue', 0.29]], 2597], 'blue'),
  ('weight table sample 53',
   [[['treatment', 33.33], ['holdout', 33.34], ['control', 4.35]], 0],
   'treatment')],
 [('bucket at first cumulative edge goes to second arm',
   [[['control', 50], ['treatment', 50]], 5000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('no positive weights yields None', [[['a', 0], ['b', 0]], 10], None),
  ('three way split middle arm', [[['a', 33.33], ['b', 33.33], ['c', 33.34]], 5000], 'b'),
  ('weight table sample 3', [[['treatment', 12.5], ['control', 10], ['holdout', 0.57]], 6309], 'holdout'),
  ('weight table sample 21', [[['green', 33.33], ['treatment', 0]], 0], 'green'),
  ('weight table sample 31', [[['green', 1.15], ['blue', 33.33], ['treatment', 4.35]], 3447], 'blue')]]
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:
Renaming an arm silently moves which bucket ranges it owns.

variants is an ordered list of [name, weight percent with at most two decimals]; bucket is an integer in [0, 10000). Walk variants in declared order, skipping non-positive weights, adding round(weight * 100) to a running total, and return the first variant whose total exceeds the bucket. Buckets beyond the total go to the last positively weighted variant; with none, None.

split: open-access


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

N = 1
observations = []
def solve(variants, bucket):
    acc = 0
    last = None
    for name, weight in variants:
        if weight <= 0:
            continue
        last = name
        acc += int(weight * 100)
        if bucket < acc:
            return name
    return last
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('bucket at first cumulative edge goes to second arm',
   [[['control', 50], ['treatment', 50]], 5000],
   'treatment'),
  ('bucket just below first edge stays in first arm',
   [[['control', 50], ['treatment', 50]], 4999],
   'control'),
  ('zero weight arm is never selected', [[['control', 0], ['treatment', 100]], 0], 'treatment'),
  ('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('weight table sample 1', [[['green', 1.15], ['holdout', 0], ['alpha', 1.15], ['blue', 50]], 1135], 'blue'),
  ('weight table sample 2', [[['blue', 50], ['treatment', 10]], 2986], 'blue')],
 [('bucket just below first edge stays in first arm',
   [[['control', 50], ['treatment', 50]], 4999],
   'control'),
  ('zero weight arm is never selected', [[['control', 0], ['treatment', 100]], 0], 'treatment'),
  ('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('weight table sample 6',
   [[['alpha', 0.29], ['holdout', 12.5], ['blue', 0.57], ['treatment', 0]], 204],
   'holdout'),
  ('weight table sample 7', [[['treatment', -5], ['holdout', 10], ['green', 50]], 5999], 'green'),
  ('weight table sample 23', [[['blue', 33.33], ['treatment', 0.29], ['alpha', 10]], 3361], 'treatment')],
 [('zero weight arm is never selected', [[['control', 0], ['treatment', 100]], 0], 'treatment'),
  ('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('no positive weights yields None', [[['a', 0], ['b', 0]], 10], None),
  ('weight table sample 11', [[['holdout', 12.5], ['blue', 12.5], ['alpha', 0.57]], 2500], 'alpha'),
  ('weight table sample 12', [[['blue', 12.5], ['holdout', 0]], 1249], 'blue'),
  ('weight table sample 31', [[['green', 1.15], ['blue', 33.33], ['treatment', 4.35]], 3447], 'blue')],
 [('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('no positive weights yields None', [[['a', 0], ['b', 0]], 10], None),
  ('three way split middle arm', [[['a', 33.33], ['b', 33.33], ['c', 33.34]], 5000], 'b'),
  ('weight table sample 16', [[['green', 0.57], ['alpha', 12.5], ['control', 25]], 1307], 'control'),
  ('weight table sample 17', [[['treatment', 1.15], ['control', 0.29], ['blue', 0.29]], 2597], 'blue'),
  ('weight table sample 45',
   [[['control', 4.35], ['blue', 50], ['treatment', 50], ['alpha', 50]], 5434],
   'blue')],
 [('bucket at first cumulative edge goes to second arm',
   [[['control', 50], ['treatment', 50]], 5000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('no positive weights yields None', [[['a', 0], ['b', 0]], 10], None),
  ('three way split middle arm', [[['a', 33.33], ['b', 33.33], ['c', 33.34]], 5000], 'b'),
  ('weight table sample 21', [[['green', 33.33], ['treatment', 0]], 0], 'green'),
  ('weight table sample 22', [[['alpha', 0], ['control', 4.35], ['blue', 1.15]], 9182], 'blue'),
  ('weight table sample 50',
   [[['blue', 12.5], ['control', 0.57], ['green', 4.35], ['holdout', 0.29]], 1741],
   'green')]]
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(variants, bucket):
    acc = 0
    last = None
    for name, weight in variants:
        if weight <= 0:
            continue
        last = name
        acc += int(weight) * 100
        if bucket < acc:
            return name
    return last
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('bucket at first cumulative edge goes to second arm',
   [[['control', 50], ['treatment', 50]], 5000],
   'treatment'),
  ('bucket just below first edge stays in first arm',
   [[['control', 50], ['treatment', 50]], 4999],
   'control'),
  ('zero weight arm is never selected', [[['control', 0], ['treatment', 100]], 0], 'treatment'),
  ('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('weight table sample 1', [[['green', 1.15], ['holdout', 0], ['alpha', 1.15], ['blue', 50]], 1135], 'blue'),
  ('weight table sample 2', [[['blue', 50], ['treatment', 10]], 2986], 'blue')],
 [('bucket just below first edge stays in first arm',
   [[['control', 50], ['treatment', 50]], 4999],
   'control'),
  ('zero weight arm is never selected', [[['control', 0], ['treatment', 100]], 0], 'treatment'),
  ('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('weight table sample 6',
   [[['alpha', 0.29], ['holdout', 12.5], ['blue', 0.57], ['treatment', 0]], 204],
   'holdout'),
  ('weight table sample 7', [[['treatment', -5], ['holdout', 10], ['green', 50]], 5999], 'green'),
  ('weight table sample 23', [[['blue', 33.33], ['treatment', 0.29], ['alpha', 10]], 3361], 'treatment')],
 [('zero weight arm is never selected', [[['control', 0], ['treatment', 100]], 0], 'treatment'),
  ('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('no positive weights yields None', [[['a', 0], ['b', 0]], 10], None),
  ('weight table sample 11', [[['holdout', 12.5], ['blue', 12.5], ['alpha', 0.57]], 2500], 'alpha'),
  ('weight table sample 12', [[['blue', 12.5], ['holdout', 0]], 1249], 'blue'),
  ('weight table sample 31', [[['green', 1.15], ['blue', 33.33], ['treatment', 4.35]], 3447], 'blue')],
 [('trailing zero weight arm does not absorb remainder',
   [[['control', 40], ['treatment', 40], ['off', 0]], 9000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('no positive weights yields None', [[['a', 0], ['b', 0]], 10], None),
  ('three way split middle arm', [[['a', 33.33], ['b', 33.33], ['c', 33.34]], 5000], 'b'),
  ('weight table sample 16', [[['green', 0.57], ['alpha', 12.5], ['control', 25]], 1307], 'control'),
  ('weight table sample 17', [[['treatment', 1.15], ['control', 0.29], ['blue', 0.29]], 2597], 'blue'),
  ('weight table sample 45',
   [[['control', 4.35], ['blue', 50], ['treatment', 50], ['alpha', 50]], 5434],
   'blue')],
 [('bucket at first cumulative edge goes to second arm',
   [[['control', 50], ['treatment', 50]], 5000],
   'treatment'),
  ('declared order beats alphabetical order', [[['zeta', 10], ['alpha', 90]], 500], 'zeta'),
  ('fractional weight 0.29 covers 29 buckets', [[['tiny', 0.29], ['rest', 99.71]], 28], 'tiny'),
  ('no positive weights yields None', [[['a', 0], ['b', 0]], 10], None),
  ('three way split middle arm', [[['a', 33.33], ['b', 33.33], ['c', 33.34]], 5000], 'b'),
  ('weight table sample 21', [[['green', 33.33], ['treatment', 0]], 0], 'green'),
  ('weight table sample 22', [[['alpha', 0], ['control', 4.35], ['blue', 1.15]], 9182], 'blue'),
  ('weight table sample 50',
   [[['blue', 12.5], ['control', 0.57], ['green', 4.35], ['holdout', 0.29]], 1741],
   'green')]]
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 0.29 percent arm covers only 28 buckets and 33.33 percent splits leak buckets.

variants is an ordered list of [name, weight percent with at most two decimals]; bucket is an integer in [0, 10000). Walk variants in declared order, skipping non-positive weights, adding round(weight * 100) to a running total, and return the first variant whose total exceeds the bucket. Buckets beyond the total go to the last positively weighted variant; with none, None.

split: open-access


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

N = 1
observations = []
def solve(flag, user):
    if not flag.get('on'):
        return [flag.get('off'), 'OFF', -1]
    key = user.get('key')
    for index, rule in enumerate(flag.get('rules', [])):
        if rule['attr'] not in user:
            continue
        value = user[rule['attr']]
        if rule['op'] == 'in':
            hit = value in rule['values']
        elif rule['op'] == 'startsWith':
            hit = isinstance(value, str) and any(value.startswith(v) for v in rule['values'])
        else:
            hit = isinstance(value, (int, float)) and not isinstance(value, bool) and any(value < v for v in rule['values'])
        if rule.get('negate'):
            hit = not hit
        if hit:
            return [rule['variation'], 'RULE', index]
    for variation in sorted(flag.get('targets', {})):
        if key in flag['targets'][variation]:
            return [variation, 'TARGET', -1]
    return [flag.get('fallthrough'), 'FALLTHROUGH', -1]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('individual target beats a matching rule',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': ['k1']},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['US'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k1', 'country': 'US'}],
   ['v1', 'TARGET', -1]),
  ('negated rule does not match a missing attribute',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k2'}],
   ['v3', 'FALLTHROUGH', -1]),
  ('off flag serves off variation',
   [{'on': False, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []}, {'key': 'k1'}],
   ['v1', 'OFF', -1]),
  ('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('targeting sample 1',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': []},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['a', 'Ann'],
                'negate': False,
                'variation': 'v2'},
               {'attr': 'age', 'op': 'lt', 'values': [21, 18], 'negate': False, 'variation': 'v0'},
               {'attr': 'age', 'op': 'lt', 'values': [18, 30], 'negate': True, 'variation': 'v2'},
               {'attr': 'email',
                'op': 'startsWith',
                'values': ['a', 'Ann'],
                'negate': False,
                'variation': 'v2'}]},
    {'key': 'k2', 'country': 'DE', 'age': 20, 'plan': 'team'}],
   ['v0', 'RULE', 1]),
  ('targeting sample 2',
   [{'on': True, 'off': 'v1', 'fallthrough': 'v0', 'targets': {'v2': []}, 'rules': []},
    {'key': 'k6', 'age': 40, 'plan': 'pro'}],
   ['v0', 'FALLTHROUGH', -1])],
 [('negated rule does not match a missing attribute',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k2'}],
   ['v3', 'FALLTHROUGH', -1]),
  ('off flag serves off variation',
   [{'on': False, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []}, {'key': 'k1'}],
   ['v1', 'OFF', -1]),
  ('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('targeting sample 6',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [18, 65], 'negate': False, 'variation': 'v1'},
               {'attr': 'plan', 'op': 'in', 'values': ['DE', 'free'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k5', 'country': 'DE', 'plan': 'team'}],
   ['v2', 'RULE', 1]),
  ('targeting sample 7',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['FR', 'free'], 'negate': True, 'variation': 'v1'},
               {'attr': 'plan', 'op': 'in', 'values': ['FR', 'US'], 'negate': False, 'variation': 'v0'}]},
    {'key': 'k6', 'country': 'JP', 'email': 'annie', 'age': 17, 'plan': 'free'}],
   ['v0', 'FALLTHROUGH', -1])],
 [('individual target beats a matching rule',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': ['k1']},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['US'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k1', 'country': 'US'}],
   ['v1', 'TARGET', -1]),
  ('off flag serves off variation',
   [{'on': False, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []}, {'key': 'k1'}],
   ['v1', 'OFF', -1]),
  ('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('targeting sample 11',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {'v3': ['k2'], 'v2': ['k4', 'k2']},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['qa-', 'a'],
                'negate': False,
                'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE', 'FR'], 'negate': True, 'variation': 'v3'},
               {'attr': 'email',
                'op': 'startsWith',
                'values': ['qa-', 'a'],
                'negate': False,
                'variation': 'v1'}]},
    {'key': 'k6', 'email': 'annie', 'age': 40, 'plan': 'free'}],
   ['v1', 'RULE', 0]),
  ('targeting sample 12',
   [{'on': True, 'off': 'v1', 'fallthrough': 'v0', 'targets': {'v1': ['k1', 'k4'], 'v2': []}, 'rules': []},
    {'key': 'k1', 'age': 30, 'plan': 'free'}],
   ['v1', 'TARGET', -1])],
 [('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('no rule matches uses fallthrough',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {'v1': ['k9']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [18], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 40}],
   ['v3', 'FALLTHROUGH', -1]),
  ('targeting sample 16',
   [{'on': True, 'off': 'v0', 'fallthrough': 'v2', 'targets': {}, 'rules': []},
    {'key': 'k5', 'age': 40, 'plan': 'free'}],
   ['v2', 'FALLTHROUGH', -1]),
  ('targeting sample 17',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['ann', 'a'],
                'negate': True,
                'variation': 'v0'},
               {'attr': 'plan', 'op': 'lt', 'values': [30, 18], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k6', 'country': 'JP', 'email': 'ann@x.io', 'plan': 'team'}],
   ['v3', 'FALLTHROUGH', -1]),
  ('targeting sample 18',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {'v2': []},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [21, 18], 'negate': False, 'variation': 'v0'},
               {'attr': 'plan', 'op': 'in', 'values': ['FR', 'DE'], 'negate': False, 'variation': 'v3'},
               {'attr': 'age', 'op': 'lt', 'values': [21, 18], 'negate': False, 'variation': 'v0'}]},
    {'key': 'k1', 'country': 'JP', 'age': 20, 'plan': 'free'}],
   ['v0', 'RULE', 0])],
 [('individual target beats a matching rule',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': ['k1']},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['US'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k1', 'country': 'US'}],
   ['v1', 'TARGET', -1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('no rule matches uses fallthrough',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {'v1': ['k9']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [18], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 40}],
   ['v3', 'FALLTHROUGH', -1]),
  ('prefix operand order matters',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['annie@corp'],
                'negate': False,
                'variation': 'v1'}]},
    {'key': 'k2', 'email': 'annie'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('targeting sample 21',
   [{'on': True, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []},
    {'key': 'k3', 'email': 'qa-1@z.io', 'plan': 'free'}],
   ['v2', 'FALLTHROUGH', -1]),
  ('targeting sample 22',
   [{'on': False, 'off': 'v0', 'fallthrough': 'v2', 'targets': {'v1': [], 'v3': ['k3']}, 'rules': []},
    {'key': 'k6', 'country': 'DE', 'email': 'Ann@x.io', 'plan': 'team'}],
   ['v0', 'OFF', -1])]]
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(flag, user):
    if not flag.get('on'):
        return [flag.get('off'), 'OFF', -1]
    key = user.get('key')
    for variation in sorted(flag.get('targets', {})):
        if key in flag['targets'][variation] and variation != flag.get('fallthrough'):
            return [variation, 'TARGET', -1]
    for index, rule in enumerate(flag.get('rules', [])):
        if rule['attr'] not in user:
            continue
        value = user[rule['attr']]
        if rule['op'] == 'in':
            hit = value in rule['values']
        elif rule['op'] == 'startsWith':
            hit = isinstance(value, str) and any(value.startswith(v) for v in rule['values'])
        else:
            hit = isinstance(value, (int, float)) and not isinstance(value, bool) and any(value < v for v in rule['values'])
        if rule.get('negate'):
            hit = not hit
        if hit:
            return [rule['variation'], 'RULE', index]
    return [flag.get('fallthrough'), 'FALLTHROUGH', -1]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('individual target beats a matching rule',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': ['k1']},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['US'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k1', 'country': 'US'}],
   ['v1', 'TARGET', -1]),
  ('negated rule does not match a missing attribute',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k2'}],
   ['v3', 'FALLTHROUGH', -1]),
  ('off flag serves off variation',
   [{'on': False, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []}, {'key': 'k1'}],
   ['v1', 'OFF', -1]),
  ('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('targeting sample 1',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': []},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['a', 'Ann'],
                'negate': False,
                'variation': 'v2'},
               {'attr': 'age', 'op': 'lt', 'values': [21, 18], 'negate': False, 'variation': 'v0'},
               {'attr': 'age', 'op': 'lt', 'values': [18, 30], 'negate': True, 'variation': 'v2'},
               {'attr': 'email',
                'op': 'startsWith',
                'values': ['a', 'Ann'],
                'negate': False,
                'variation': 'v2'}]},
    {'key': 'k2', 'country': 'DE', 'age': 20, 'plan': 'team'}],
   ['v0', 'RULE', 1]),
  ('targeting sample 2',
   [{'on': True, 'off': 'v1', 'fallthrough': 'v0', 'targets': {'v2': []}, 'rules': []},
    {'key': 'k6', 'age': 40, 'plan': 'pro'}],
   ['v0', 'FALLTHROUGH', -1])],
 [('negated rule does not match a missing attribute',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k2'}],
   ['v3', 'FALLTHROUGH', -1]),
  ('off flag serves off variation',
   [{'on': False, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []}, {'key': 'k1'}],
   ['v1', 'OFF', -1]),
  ('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('targeting sample 6',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [18, 65], 'negate': False, 'variation': 'v1'},
               {'attr': 'plan', 'op': 'in', 'values': ['DE', 'free'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k5', 'country': 'DE', 'plan': 'team'}],
   ['v2', 'RULE', 1]),
  ('targeting sample 7',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['FR', 'free'], 'negate': True, 'variation': 'v1'},
               {'attr': 'plan', 'op': 'in', 'values': ['FR', 'US'], 'negate': False, 'variation': 'v0'}]},
    {'key': 'k6', 'country': 'JP', 'email': 'annie', 'age': 17, 'plan': 'free'}],
   ['v0', 'FALLTHROUGH', -1])],
 [('individual target beats a matching rule',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': ['k1']},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['US'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k1', 'country': 'US'}],
   ['v1', 'TARGET', -1]),
  ('off flag serves off variation',
   [{'on': False, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []}, {'key': 'k1'}],
   ['v1', 'OFF', -1]),
  ('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('targeting sample 11',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {'v3': ['k2'], 'v2': ['k4', 'k2']},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['qa-', 'a'],
                'negate': False,
                'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE', 'FR'], 'negate': True, 'variation': 'v3'},
               {'attr': 'email',
                'op': 'startsWith',
                'values': ['qa-', 'a'],
                'negate': False,
                'variation': 'v1'}]},
    {'key': 'k6', 'email': 'annie', 'age': 40, 'plan': 'free'}],
   ['v1', 'RULE', 0]),
  ('targeting sample 12',
   [{'on': True, 'off': 'v1', 'fallthrough': 'v0', 'targets': {'v1': ['k1', 'k4'], 'v2': []}, 'rules': []},
    {'key': 'k1', 'age': 30, 'plan': 'free'}],
   ['v1', 'TARGET', -1])],
 [('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('no rule matches uses fallthrough',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {'v1': ['k9']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [18], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 40}],
   ['v3', 'FALLTHROUGH', -1]),
  ('targeting sample 16',
   [{'on': True, 'off': 'v0', 'fallthrough': 'v2', 'targets': {}, 'rules': []},
    {'key': 'k5', 'age': 40, 'plan': 'free'}],
   ['v2', 'FALLTHROUGH', -1]),
  ('targeting sample 17',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['ann', 'a'],
                'negate': True,
                'variation': 'v0'},
               {'attr': 'plan', 'op': 'lt', 'values': [30, 18], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k6', 'country': 'JP', 'email': 'ann@x.io', 'plan': 'team'}],
   ['v3', 'FALLTHROUGH', -1]),
  ('targeting sample 18',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {'v2': []},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [21, 18], 'negate': False, 'variation': 'v0'},
               {'attr': 'plan', 'op': 'in', 'values': ['FR', 'DE'], 'negate': False, 'variation': 'v3'},
               {'attr': 'age', 'op': 'lt', 'values': [21, 18], 'negate': False, 'variation': 'v0'}]},
    {'key': 'k1', 'country': 'JP', 'age': 20, 'plan': 'free'}],
   ['v0', 'RULE', 0])],
 [('individual target beats a matching rule',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': ['k1']},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['US'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k1', 'country': 'US'}],
   ['v1', 'TARGET', -1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('no rule matches uses fallthrough',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {'v1': ['k9']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [18], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 40}],
   ['v3', 'FALLTHROUGH', -1]),
  ('prefix operand order matters',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['annie@corp'],
                'negate': False,
                'variation': 'v1'}]},
    {'key': 'k2', 'email': 'annie'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('targeting sample 21',
   [{'on': True, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []},
    {'key': 'k3', 'email': 'qa-1@z.io', 'plan': 'free'}],
   ['v2', 'FALLTHROUGH', -1]),
  ('targeting sample 22',
   [{'on': False, 'off': 'v0', 'fallthrough': 'v2', 'targets': {'v1': [], 'v3': ['k3']}, 'rules': []},
    {'key': 'k6', 'country': 'DE', 'email': 'Ann@x.io', 'plan': 'team'}],
   ['v0', 'OFF', -1])]]
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 QA engineer added to an individual target list still receives the variation of a broad rule.

An off flag serves its off variation (reason OFF). Otherwise individual targets (checked by sorted variation name) win with reason TARGET, then rules in order, then the fallthrough. A rule matches only when the user has its attribute: op in = membership, startsWith = case-sensitive string prefix, lt = numeric less-than any value; negate inverts a present attribute result. Return [variation, reason, zero-based rule index or -1].

split: open-access


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

N = 1
observations = []
def solve(flag, user):
    if not flag.get('on'):
        return [flag.get('off'), 'OFF', -1]
    key = user.get('key')
    for variation in sorted(flag.get('targets', {})):
        if key in flag['targets'][variation]:
            return [variation, 'TARGET', -1]
    for index, rule in enumerate(flag.get('rules', [])):
        value = user.get(rule['attr'])
        if rule['op'] == 'in':
            hit = value in rule['values']
        elif rule['op'] == 'startsWith':
            hit = isinstance(value, str) and any(value.startswith(v) for v in rule['values'])
        else:
            hit = isinstance(value, (int, float)) and not isinstance(value, bool) and any(value < v for v in rule['values'])
        if rule.get('negate'):
            hit = not hit
        if hit:
            return [rule['variation'], 'RULE', index]
    return [flag.get('fallthrough'), 'FALLTHROUGH', -1]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('individual target beats a matching rule',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': ['k1']},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['US'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k1', 'country': 'US'}],
   ['v1', 'TARGET', -1]),
  ('negated rule does not match a missing attribute',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k2'}],
   ['v3', 'FALLTHROUGH', -1]),
  ('off flag serves off variation',
   [{'on': False, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []}, {'key': 'k1'}],
   ['v1', 'OFF', -1]),
  ('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('targeting sample 1',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': []},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['a', 'Ann'],
                'negate': False,
                'variation': 'v2'},
               {'attr': 'age', 'op': 'lt', 'values': [21, 18], 'negate': False, 'variation': 'v0'},
               {'attr': 'age', 'op': 'lt', 'values': [18, 30], 'negate': True, 'variation': 'v2'},
               {'attr': 'email',
                'op': 'startsWith',
                'values': ['a', 'Ann'],
                'negate': False,
                'variation': 'v2'}]},
    {'key': 'k2', 'country': 'DE', 'age': 20, 'plan': 'team'}],
   ['v0', 'RULE', 1]),
  ('targeting sample 2',
   [{'on': True, 'off': 'v1', 'fallthrough': 'v0', 'targets': {'v2': []}, 'rules': []},
    {'key': 'k6', 'age': 40, 'plan': 'pro'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('targeting sample 3',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v0',
     'targets': {'v1': ['k3'], 'v3': ['k3']},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['Ann', 'a'],
                'negate': False,
                'variation': 'v1'},
               {'attr': 'email',
                'op': 'startsWith',
                'values': ['Ann', 'a'],
                'negate': False,
                'variation': 'v1'}]},
    {'key': 'k1', 'country': 'US', 'email': 'ann@x.io', 'age': 40, 'plan': 'pro'}],
   ['v1', 'RULE', 0])],
 [('negated rule does not match a missing attribute',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k2'}],
   ['v3', 'FALLTHROUGH', -1]),
  ('off flag serves off variation',
   [{'on': False, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []}, {'key': 'k1'}],
   ['v1', 'OFF', -1]),
  ('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('targeting sample 6',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [18, 65], 'negate': False, 'variation': 'v1'},
               {'attr': 'plan', 'op': 'in', 'values': ['DE', 'free'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k5', 'country': 'DE', 'plan': 'team'}],
   ['v2', 'RULE', 1]),
  ('targeting sample 7',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['FR', 'free'], 'negate': True, 'variation': 'v1'},
               {'attr': 'plan', 'op': 'in', 'values': ['FR', 'US'], 'negate': False, 'variation': 'v0'}]},
    {'key': 'k6', 'country': 'JP', 'email': 'annie', 'age': 17, 'plan': 'free'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('targeting sample 57',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v1': ['k2', 'k3'], 'v2': ['k5']},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['DE', 'free'], 'negate': True, 'variation': 'v0'},
               {'attr': 'age', 'op': 'lt', 'values': [18, 30], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k4', 'email': 'bob@y.io'}],
   ['v2', 'FALLTHROUGH', -1])],
 [('off flag serves off variation',
   [{'on': False, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []}, {'key': 'k1'}],
   ['v1', 'OFF', -1]),
  ('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('targeting sample 11',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {'v3': ['k2'], 'v2': ['k4', 'k2']},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['qa-', 'a'],
                'negate': False,
                'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE', 'FR'], 'negate': True, 'variation': 'v3'},
               {'attr': 'email',
                'op': 'startsWith',
                'values': ['qa-', 'a'],
                'negate': False,
                'variation': 'v1'}]},
    {'key': 'k6', 'email': 'annie', 'age': 40, 'plan': 'free'}],
   ['v1', 'RULE', 0]),
  ('targeting sample 12',
   [{'on': True, 'off': 'v1', 'fallthrough': 'v0', 'targets': {'v1': ['k1', 'k4'], 'v2': []}, 'rules': []},
    {'key': 'k1', 'age': 30, 'plan': 'free'}],
   ['v1', 'TARGET', -1]),
  ('targeting sample 36',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': [], 'v2': ['k2', 'k3']},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['a', 'qa-'],
                'negate': True,
                'variation': 'v1'},
               {'attr': 'plan', 'op': 'in', 'values': ['pro', 'free'], 'negate': True, 'variation': 'v0'}]},
    {'key': 'k1', 'country': 'US', 'email': 'qa-1@z.io', 'age': 70}],
   ['v0', 'FALLTHROUGH', -1])],
 [('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('no rule matches uses fallthrough',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {'v1': ['k9']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [18], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 40}],
   ['v3', 'FALLTHROUGH', -1]),
  ('targeting sample 15',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v2',
     'targets': {},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['qa-', 'a'],
                'negate': True,
                'variation': 'v0'},
               {'attr': 'plan', 'op': 'in', 'values': ['free', 'US'], 'negate': True, 'variation': 'v0'}]},
    {'key': 'k5', 'country': 'DE', 'age': 17}],
   ['v2', 'FALLTHROUGH', -1]),
  ('targeting sample 16',
   [{'on': True, 'off': 'v0', 'fallthrough': 'v2', 'targets': {}, 'rules': []},
    {'key': 'k5', 'age': 40, 'plan': 'free'}],
   ['v2', 'FALLTHROUGH', -1]),
  ('targeting sample 17',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['ann', 'a'],
                'negate': True,
                'variation': 'v0'},
               {'attr': 'plan', 'op': 'lt', 'values': [30, 18], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k6', 'country': 'JP', 'email': 'ann@x.io', 'plan': 'team'}],
   ['v3', 'FALLTHROUGH', -1])],
 [('negated rule does not match a missing attribute',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k2'}],
   ['v3', 'FALLTHROUGH', -1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('no rule matches uses fallthrough',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {'v1': ['k9']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [18], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 40}],
   ['v3', 'FALLTHROUGH', -1]),
  ('prefix operand order matters',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['annie@corp'],
                'negate': False,
                'variation': 'v1'}]},
    {'key': 'k2', 'email': 'annie'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('targeting sample 21',
   [{'on': True, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []},
    {'key': 'k3', 'email': 'qa-1@z.io', 'plan': 'free'}],
   ['v2', 'FALLTHROUGH', -1]),
  ('targeting sample 22',
   [{'on': False, 'off': 'v0', 'fallthrough': 'v2', 'targets': {'v1': [], 'v3': ['k3']}, 'rules': []},
    {'key': 'k6', 'country': 'DE', 'email': 'Ann@x.io', 'plan': 'team'}],
   ['v0', 'OFF', -1])]]
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(flag, user):
    if not flag.get('on'):
        return [flag.get('off'), 'OFF', -1]
    key = user.get('key')
    for variation in sorted(flag.get('targets', {})):
        if key in flag['targets'][variation]:
            return [variation, 'TARGET', -1]
    for index, rule in enumerate(flag.get('rules', [])):
        value = user.get(rule['attr'], '')
        if rule['op'] == 'in':
            hit = value in rule['values']
        elif rule['op'] == 'startsWith':
            hit = isinstance(value, str) and any(value.startswith(v) for v in rule['values'])
        else:
            hit = isinstance(value, (int, float)) and not isinstance(value, bool) and any(value < v for v in rule['values'])
        if rule.get('negate'):
            hit = not hit
        if hit:
            return [rule['variation'], 'RULE', index]
    return [flag.get('fallthrough'), 'FALLTHROUGH', -1]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('individual target beats a matching rule',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': ['k1']},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['US'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k1', 'country': 'US'}],
   ['v1', 'TARGET', -1]),
  ('negated rule does not match a missing attribute',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k2'}],
   ['v3', 'FALLTHROUGH', -1]),
  ('off flag serves off variation',
   [{'on': False, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []}, {'key': 'k1'}],
   ['v1', 'OFF', -1]),
  ('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('targeting sample 1',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': []},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['a', 'Ann'],
                'negate': False,
                'variation': 'v2'},
               {'attr': 'age', 'op': 'lt', 'values': [21, 18], 'negate': False, 'variation': 'v0'},
               {'attr': 'age', 'op': 'lt', 'values': [18, 30], 'negate': True, 'variation': 'v2'},
               {'attr': 'email',
                'op': 'startsWith',
                'values': ['a', 'Ann'],
                'negate': False,
                'variation': 'v2'}]},
    {'key': 'k2', 'country': 'DE', 'age': 20, 'plan': 'team'}],
   ['v0', 'RULE', 1]),
  ('targeting sample 2',
   [{'on': True, 'off': 'v1', 'fallthrough': 'v0', 'targets': {'v2': []}, 'rules': []},
    {'key': 'k6', 'age': 40, 'plan': 'pro'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('targeting sample 3',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v0',
     'targets': {'v1': ['k3'], 'v3': ['k3']},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['Ann', 'a'],
                'negate': False,
                'variation': 'v1'},
               {'attr': 'email',
                'op': 'startsWith',
                'values': ['Ann', 'a'],
                'negate': False,
                'variation': 'v1'}]},
    {'key': 'k1', 'country': 'US', 'email': 'ann@x.io', 'age': 40, 'plan': 'pro'}],
   ['v1', 'RULE', 0])],
 [('negated rule does not match a missing attribute',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k2'}],
   ['v3', 'FALLTHROUGH', -1]),
  ('off flag serves off variation',
   [{'on': False, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []}, {'key': 'k1'}],
   ['v1', 'OFF', -1]),
  ('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('targeting sample 6',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [18, 65], 'negate': False, 'variation': 'v1'},
               {'attr': 'plan', 'op': 'in', 'values': ['DE', 'free'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k5', 'country': 'DE', 'plan': 'team'}],
   ['v2', 'RULE', 1]),
  ('targeting sample 7',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['FR', 'free'], 'negate': True, 'variation': 'v1'},
               {'attr': 'plan', 'op': 'in', 'values': ['FR', 'US'], 'negate': False, 'variation': 'v0'}]},
    {'key': 'k6', 'country': 'JP', 'email': 'annie', 'age': 17, 'plan': 'free'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('targeting sample 57',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v1': ['k2', 'k3'], 'v2': ['k5']},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['DE', 'free'], 'negate': True, 'variation': 'v0'},
               {'attr': 'age', 'op': 'lt', 'values': [18, 30], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k4', 'email': 'bob@y.io'}],
   ['v2', 'FALLTHROUGH', -1])],
 [('off flag serves off variation',
   [{'on': False, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []}, {'key': 'k1'}],
   ['v1', 'OFF', -1]),
  ('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('targeting sample 11',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {'v3': ['k2'], 'v2': ['k4', 'k2']},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['qa-', 'a'],
                'negate': False,
                'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE', 'FR'], 'negate': True, 'variation': 'v3'},
               {'attr': 'email',
                'op': 'startsWith',
                'values': ['qa-', 'a'],
                'negate': False,
                'variation': 'v1'}]},
    {'key': 'k6', 'email': 'annie', 'age': 40, 'plan': 'free'}],
   ['v1', 'RULE', 0]),
  ('targeting sample 12',
   [{'on': True, 'off': 'v1', 'fallthrough': 'v0', 'targets': {'v1': ['k1', 'k4'], 'v2': []}, 'rules': []},
    {'key': 'k1', 'age': 30, 'plan': 'free'}],
   ['v1', 'TARGET', -1]),
  ('targeting sample 36',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {'v1': [], 'v2': ['k2', 'k3']},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['a', 'qa-'],
                'negate': True,
                'variation': 'v1'},
               {'attr': 'plan', 'op': 'in', 'values': ['pro', 'free'], 'negate': True, 'variation': 'v0'}]},
    {'key': 'k1', 'country': 'US', 'email': 'qa-1@z.io', 'age': 70}],
   ['v0', 'FALLTHROUGH', -1])],
 [('second rule reports index one',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'country', 'op': 'in', 'values': ['FR'], 'negate': False, 'variation': 'v1'},
               {'attr': 'country', 'op': 'in', 'values': ['DE'], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k3', 'country': 'DE'}],
   ['v2', 'RULE', 1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('no rule matches uses fallthrough',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {'v1': ['k9']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [18], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 40}],
   ['v3', 'FALLTHROUGH', -1]),
  ('targeting sample 15',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v2',
     'targets': {},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['qa-', 'a'],
                'negate': True,
                'variation': 'v0'},
               {'attr': 'plan', 'op': 'in', 'values': ['free', 'US'], 'negate': True, 'variation': 'v0'}]},
    {'key': 'k5', 'country': 'DE', 'age': 17}],
   ['v2', 'FALLTHROUGH', -1]),
  ('targeting sample 16',
   [{'on': True, 'off': 'v0', 'fallthrough': 'v2', 'targets': {}, 'rules': []},
    {'key': 'k5', 'age': 40, 'plan': 'free'}],
   ['v2', 'FALLTHROUGH', -1]),
  ('targeting sample 17',
   [{'on': True,
     'off': 'v1',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['ann', 'a'],
                'negate': True,
                'variation': 'v0'},
               {'attr': 'plan', 'op': 'lt', 'values': [30, 18], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k6', 'country': 'JP', 'email': 'ann@x.io', 'plan': 'team'}],
   ['v3', 'FALLTHROUGH', -1])],
 [('negated rule does not match a missing attribute',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': True, 'variation': 'v2'}]},
    {'key': 'k2'}],
   ['v3', 'FALLTHROUGH', -1]),
  ('prefix match is case sensitive',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email', 'op': 'startsWith', 'values': ['ann'], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k4', 'email': 'Ann@x.io'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('rule index skips rules on absent attributes',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'plan', 'op': 'in', 'values': ['pro'], 'negate': False, 'variation': 'v1'},
               {'attr': 'age', 'op': 'lt', 'values': [21], 'negate': False, 'variation': 'v2'}]},
    {'key': 'k5', 'age': 19}],
   ['v2', 'RULE', 1]),
  ('target to fallthrough variation still reports TARGET',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v2',
     'targets': {'v2': ['k1']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [30], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 20}],
   ['v2', 'TARGET', -1]),
  ('no rule matches uses fallthrough',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v3',
     'targets': {'v1': ['k9']},
     'rules': [{'attr': 'age', 'op': 'lt', 'values': [18], 'negate': False, 'variation': 'v1'}]},
    {'key': 'k1', 'age': 40}],
   ['v3', 'FALLTHROUGH', -1]),
  ('prefix operand order matters',
   [{'on': True,
     'off': 'v0',
     'fallthrough': 'v0',
     'targets': {},
     'rules': [{'attr': 'email',
                'op': 'startsWith',
                'values': ['annie@corp'],
                'negate': False,
                'variation': 'v1'}]},
    {'key': 'k2', 'email': 'annie'}],
   ['v0', 'FALLTHROUGH', -1]),
  ('targeting sample 21',
   [{'on': True, 'off': 'v1', 'fallthrough': 'v2', 'targets': {}, 'rules': []},
    {'key': 'k3', 'email': 'qa-1@z.io', 'plan': 'free'}],
   ['v2', 'FALLTHROUGH', -1]),
  ('targeting sample 22',
   [{'on': False, 'off': 'v0', 'fallthrough': 'v2', 'targets': {'v1': [], 'v3': ['k3']}, 'rules': []},
    {'key': 'k6', 'country': 'DE', 'email': 'Ann@x.io', 'plan': 'team'}],
   ['v0', 'OFF', -1])]]
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:
Anonymous users without a plan attribute are served the "not pro" variation.

An off flag serves its off variation (reason OFF). Otherwise individual targets (checked by sorted variation name) win with reason TARGET, then rules in order, then the fallthrough. A rule matches only when the user has its attribute: op in = membership, startsWith = case-sensitive string prefix, lt = numeric less-than any value; negate inverts a present attribute result. Return [variation, reason, zero-based rule index or -1].

split: open-access