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

N = 1
observations = []
def solve(events, max_depth):
    stack = []
    skip = 0
    leaves = 0
    depth = 0
    errors = []
    for ev in events:
        kind = ev[0]
        if skip:
            if kind == 'open':
                skip = 1
            elif kind == 'close':
                skip -= 1
            continue
        if kind == 'open':
            if stack:
                stack[-1] += 1
            if len(stack) + 1 > max_depth:
                errors.append('too-deep')
                skip = 1
                continue
            stack.append(0)
            depth = max(depth, len(stack))
        elif kind == 'leaf':
            leaves += 1
            if stack:
                stack[-1] += 1
        elif not stack:
            errors.append('stray-close')
        elif stack.pop() == 0:
            errors.append('empty-multipart')
    if stack:
        errors.append('unclosed')
    return {'leaves': leaves, 'depth': depth, 'errors': errors}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 1}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 4], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 1}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 1], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 1], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 2: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 2}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 5], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 2], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 2}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 2], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 2], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 3: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 3}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 6], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 3], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 3}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 3], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 3], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 4: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 4}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 7], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 4], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 4}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 4], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 4], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 5: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['leaf', 'image/png', 4], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 5}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 8], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 5], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3], ['leaf', 'text/plain', 4]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 5}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 5], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})]}
for _label, _args, _expected in _CASES[N]:
    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(events, max_depth):
    stack = []
    skip = 0
    leaves = 0
    depth = 0
    errors = []
    for ev in events:
        kind = ev[0]
        if skip:
            if kind == 'open':
                skip = min(skip + 1, 2)
            elif kind == 'close':
                skip -= 1
            continue
        if kind == 'open':
            if stack:
                stack[-1] += 1
            if len(stack) + 1 > max_depth:
                errors.append('too-deep')
                skip = 1
                continue
            stack.append(0)
            depth = max(depth, len(stack))
        elif kind == 'leaf':
            leaves += 1
            if stack:
                stack[-1] += 1
        elif not stack:
            errors.append('stray-close')
        elif stack.pop() == 0:
            errors.append('empty-multipart')
    if stack:
        errors.append('unclosed')
    return {'leaves': leaves, 'depth': depth, 'errors': errors}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 1}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 4], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 1}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 1], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 1], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 2: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 2}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 5], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 2], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 2}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 2], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 2], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 3: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 3}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 6], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 3], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 3}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 3], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 3], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 4: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 4}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 7], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 4], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 4}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 4], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 4], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 5: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['leaf', 'image/png', 4], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 5}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 8], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 5], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3], ['leaf', 'text/plain', 4]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 5}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 5], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})]}
for _label, _args, _expected in _CASES[N]:
    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:
After skipping an over-deep subtree with nested containers, parsing resumes inside it and misattributes its parts.

events are ["open", type] (multipart start), ["leaf", type, size] and ["close"]. The root multipart has depth 1. An open that would exceed max_depth records "too-deep" and its whole subtree (including nested opens) is skipped, but it still counts as a child of its parent. A multipart closing with no children records "empty-multipart", a close with nothing open records "stray-close", and containers left open record "unclosed". Result {leaves, depth (maximum reached), errors}.

split: open-access


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

N = 1
observations = []
def solve(events, max_depth):
    stack = []
    skip = 0
    leaves = 0
    depth = 0
    errors = []
    for ev in events:
        kind = ev[0]
        if skip:
            if kind == 'open':
                skip += 1
            elif kind == 'close':
                skip -= 1
            continue
        if kind == 'open':
            if len(stack) + 1 > max_depth:
                errors.append('too-deep')
                skip = 1
                continue
            if stack:
                stack[-1] += 1
            stack.append(0)
            depth = max(depth, len(stack))
        elif kind == 'leaf':
            leaves += 1
            if stack:
                stack[-1] += 1
        elif not stack:
            errors.append('stray-close')
        elif stack.pop() == 0:
            errors.append('empty-multipart')
    if stack:
        errors.append('unclosed')
    return {'leaves': leaves, 'depth': depth, 'errors': errors}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 1}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 4], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 1}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 1], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 1], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 2: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 2}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 5], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 2], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 2}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 2], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 2], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 3: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 3}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 6], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 3], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 3}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 3], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 3], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 4: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 4}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 7], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 4], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 4}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 4], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 4], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 5: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['leaf', 'image/png', 4], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 5}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 8], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 5], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3], ['leaf', 'text/plain', 4]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 5}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 5], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})]}
for _label, _args, _expected in _CASES[N]:
    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(events, max_depth):
    stack = []
    skip = 0
    leaves = 0
    depth = 0
    errors = []
    for ev in events:
        kind = ev[0]
        if skip:
            if kind == 'open':
                skip += 1
            elif kind == 'close':
                skip -= 1
            continue
        if kind == 'open':
            if stack and len(stack) < max_depth:
                stack[-1] += 1
            if len(stack) + 1 > max_depth:
                errors.append('too-deep')
                skip = 1
                continue
            stack.append(0)
            depth = max(depth, len(stack))
        elif kind == 'leaf':
            leaves += 1
            if stack:
                stack[-1] += 1
        elif not stack:
            errors.append('stray-close')
        elif stack.pop() == 0:
            errors.append('empty-multipart')
    if stack:
        errors.append('unclosed')
    return {'leaves': leaves, 'depth': depth, 'errors': errors}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 1}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 4], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 1}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 1], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 1], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 2: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 2}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 5], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 2], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 2}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 2], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 2], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 3: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 3}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 6], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 3], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 3}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 3], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 3], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 4: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 4}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 7], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 4], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 4}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 4], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 4], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 5: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['leaf', 'image/png', 4], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 5}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 8], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 5], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3], ['leaf', 'text/plain', 4]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 5}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 5], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})]}
for _label, _args, _expected in _CASES[N]:
    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 container whose only child was skipped as too deep is additionally reported as an empty multipart.

events are ["open", type] (multipart start), ["leaf", type, size] and ["close"]. The root multipart has depth 1. An open that would exceed max_depth records "too-deep" and its whole subtree (including nested opens) is skipped, but it still counts as a child of its parent. A multipart closing with no children records "empty-multipart", a close with nothing open records "stray-close", and containers left open record "unclosed". Result {leaves, depth (maximum reached), errors}.

split: open-access


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

N = 1
observations = []
def solve(events, max_depth):
    stack = []
    skip = 0
    leaves = 0
    depth = 0
    errors = []
    for ev in events:
        kind = ev[0]
        if skip:
            if kind == 'open':
                skip += 1
            elif kind == 'close':
                skip -= 1
            continue
        if kind == 'open':
            if stack:
                stack[-1] += 1
            if len(stack) + 1 > max_depth:
                errors.append('too-deep')
                skip = 1
                continue
            stack.append(0)
            depth = max(depth, len(stack))
        elif kind == 'leaf':
            leaves += 1
            if stack:
                stack[-1] += 1
        elif not stack:
            errors.append('stray-close')
        elif stack.pop() == 0:
            errors.append('empty-multipart')
    if len(stack) > 1:
        errors.append('unclosed')
    return {'leaves': leaves, 'depth': depth, 'errors': errors}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 1}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 4], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 1}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 1], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 1], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 2: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 2}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 5], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 2], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 2}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 2], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 2], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 3: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 3}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 6], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 3], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 3}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 3], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 3], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 4: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 4}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 7], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 4], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 4}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 4], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 4], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 5: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['leaf', 'image/png', 4], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 5}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 8], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 5], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3], ['leaf', 'text/plain', 4]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 5}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 5], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})]}
for _label, _args, _expected in _CASES[N]:
    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(events, max_depth):
    stack = []
    skip = 0
    leaves = 0
    depth = 0
    errors = []
    for ev in events:
        kind = ev[0]
        if skip:
            if kind == 'open':
                skip += 1
            elif kind == 'close':
                skip -= 1
            continue
        if kind == 'open':
            if stack:
                stack[-1] += 1
            if len(stack) + 1 > max_depth:
                errors.append('too-deep')
                skip = 1
                continue
            stack.append(0)
            depth = max(depth, len(stack))
        elif kind == 'leaf':
            leaves += 1
            if stack:
                stack[-1] += 1
        elif not stack:
            errors.append('stray-close')
        elif stack.pop() == 0:
            errors.append('empty-multipart')
    if skip or len(stack) > 1:
        errors.append('unclosed')
    return {'leaves': leaves, 'depth': depth, 'errors': errors}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 1}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 4], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 1}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 1], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 1], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 2: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 2}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 5], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 2], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 2}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 2], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 2], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 3: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 3}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 6], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 3], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 3}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 3], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 3], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 4: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 4}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 7], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 4], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 4}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 4], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 4], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 5: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['leaf', 'image/png', 4], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 5}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 8], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 5], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3], ['leaf', 'text/plain', 4]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 5}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 5], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})]}
for _label, _args, _expected in _CASES[N]:
    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 message whose outermost multipart is never closed is accepted as complete.

events are ["open", type] (multipart start), ["leaf", type, size] and ["close"]. The root multipart has depth 1. An open that would exceed max_depth records "too-deep" and its whole subtree (including nested opens) is skipped, but it still counts as a child of its parent. A multipart closing with no children records "empty-multipart", a close with nothing open records "stray-close", and containers left open record "unclosed". Result {leaves, depth (maximum reached), errors}.

split: open-access


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

N = 1
observations = []
def solve(events, max_depth):
    stack = []
    skip = 0
    leaves = 0
    depth = 0
    errors = []
    for ev in events:
        kind = ev[0]
        if skip:
            if kind == 'open':
                skip += 1
            elif kind == 'close':
                skip -= 1
            continue
        if kind == 'open':
            if stack:
                stack[-1] += 1
            if len(stack) + 1 > max_depth:
                errors.append('too-deep')
                skip = 1
                continue
            stack.append(0)
            depth = len(stack)
        elif kind == 'leaf':
            leaves += 1
            if stack:
                stack[-1] += 1
        elif not stack:
            errors.append('stray-close')
        elif stack.pop() == 0:
            errors.append('empty-multipart')
    if stack:
        errors.append('unclosed')
    return {'leaves': leaves, 'depth': depth, 'errors': errors}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 1}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 4], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 1}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 1], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 1], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 2: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 2}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 5], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 2], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 2}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 2], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 2], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 3: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 3}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 6], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 3], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 3}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 3], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 3], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 4: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 4}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 7], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 4], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 4}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 4], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 4], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 5: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['leaf', 'image/png', 4], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 5}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 8], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 5], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3], ['leaf', 'text/plain', 4]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 5}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 5], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})]}
for _label, _args, _expected in _CASES[N]:
    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(events, max_depth):
    stack = []
    skip = 0
    leaves = 0
    depth = 0
    errors = []
    for ev in events:
        kind = ev[0]
        if skip:
            if kind == 'open':
                skip += 1
            elif kind == 'close':
                skip -= 1
            continue
        if kind == 'open':
            if stack:
                stack[-1] += 1
            if len(stack) + 1 > max_depth:
                errors.append('too-deep')
                skip = 1
                continue
            stack.append(0)
            depth = max(depth, len(stack) - 1)
        elif kind == 'leaf':
            leaves += 1
            if stack:
                stack[-1] += 1
        elif not stack:
            errors.append('stray-close')
        elif stack.pop() == 0:
            errors.append('empty-multipart')
    if stack:
        errors.append('unclosed')
    return {'leaves': leaves, 'depth': depth, 'errors': errors}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 1}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 4], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 1}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 1], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 1], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 2: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 2}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 5], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 2], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 2}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 2], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 2], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 3: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 3}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 6], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 3], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 3}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 3], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 3], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 4: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 4}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 7], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 4], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 4}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 4], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 4], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})], 5: [('three levels with limit two', ([['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close'], ['leaf', 'image/png', 0], ['leaf', 'image/png', 1], ['leaf', 'image/png', 2], ['leaf', 'image/png', 3], ['leaf', 'image/png', 4], ['close']], 2), {'depth': 2, 'errors': ['too-deep'], 'leaves': 5}), ('deep skipped subtree', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['leaf', 'text/plain', 2], ['close'], ['leaf', 'text/plain', 8], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 1}), ('skipped child keeps parent non-empty', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['leaf', 'text/plain', 5], ['close'], ['close']], 1), {'depth': 1, 'errors': ['too-deep'], 'leaves': 0}), ('unterminated root', ([['open', 'multipart/mixed'], ['leaf', 'text/plain', 0], ['leaf', 'text/plain', 1], ['leaf', 'text/plain', 2], ['leaf', 'text/plain', 3], ['leaf', 'text/plain', 4]], 3), {'depth': 1, 'errors': ['unclosed'], 'leaves': 5}), ('deepest branch first', ([['open', 'multipart/mixed'], ['open', 'multipart/mixed'], ['open', 'multipart/alternative'], ['leaf', 'text/plain', 1], ['close'], ['close'], ['open', 'multipart/related'], ['leaf', 'text/html', 5], ['close'], ['close']], 5), {'depth': 3, 'errors': [], 'leaves': 2}), ('stray close', ([['leaf', 'text/plain', 5], ['close']], 2), {'depth': 0, 'errors': ['stray-close'], 'leaves': 1}), ('empty multipart', ([['open', 'multipart/mixed'], ['close']], 2), {'depth': 1, 'errors': ['empty-multipart'], 'leaves': 0})]}
for _label, _args, _expected in _CASES[N]:
    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 depth is the depth of the last container opened, not the deepest one.

events are ["open", type] (multipart start), ["leaf", type, size] and ["close"]. The root multipart has depth 1. An open that would exceed max_depth records "too-deep" and its whole subtree (including nested opens) is skipped, but it still counts as a child of its parent. A multipart closing with no children records "empty-multipart", a close with nothing open records "stray-close", and containers left open record "unclosed". Result {leaves, depth (maximum reached), errors}.

split: open-access


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

N = 1
observations = []
def solve(has_text, has_html, inline_images, attachments):
    atts = attachments
    html = None
    if has_html:
        html = 'html' if inline_images == 0 else 'related(' + ','.join(['html'] + ['img'] * inline_images) + ')'
    else:
        atts = atts
    if has_text and html:
        body = 'alternative(text,' + html + ')'
    elif html:
        body = html
    else:
        body = 'text'
    if atts:
        return 'mixed(' + ','.join([body] + ['att'] * atts) + ')'
    return body
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('images without html', (False, False, 2, 0), 'mixed(text,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 2, 0), 'alternative(text,related(html,img,img))'), ('html images and attachment', (False, True, 1, 1), 'mixed(related(html,img),att)'), ('text and attachments', (True, False, 0, 1), 'mixed(text,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 1), 'mixed(alternative(text,related(html,img,img)),att)')], 2: [('images without html', (False, False, 3, 0), 'mixed(text,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 3, 0), 'alternative(text,related(html,img,img,img))'), ('html images and attachment', (False, True, 2, 1), 'mixed(related(html,img,img),att)'), ('text and attachments', (True, False, 0, 2), 'mixed(text,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 2), 'mixed(alternative(text,related(html,img,img)),att,att)')], 3: [('images without html', (False, False, 4, 0), 'mixed(text,att,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 4, 0), 'alternative(text,related(html,img,img,img,img))'), ('html images and attachment', (False, True, 3, 1), 'mixed(related(html,img,img,img),att)'), ('text and attachments', (True, False, 0, 3), 'mixed(text,att,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 3), 'mixed(alternative(text,related(html,img,img)),att,att,att)')], 4: [('images without html', (False, False, 5, 0), 'mixed(text,att,att,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 5, 0), 'alternative(text,related(html,img,img,img,img,img))'), ('html images and attachment', (False, True, 4, 1), 'mixed(related(html,img,img,img,img),att)'), ('text and attachments', (True, False, 0, 4), 'mixed(text,att,att,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 4), 'mixed(alternative(text,related(html,img,img)),att,att,att,att)')], 5: [('images without html', (False, False, 6, 0), 'mixed(text,att,att,att,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 6, 0), 'alternative(text,related(html,img,img,img,img,img,img))'), ('html images and attachment', (False, True, 5, 1), 'mixed(related(html,img,img,img,img,img),att)'), ('text and attachments', (True, False, 0, 5), 'mixed(text,att,att,att,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 5), 'mixed(alternative(text,related(html,img,img)),att,att,att,att,att)')]}
for _label, _args, _expected in _CASES[N]:
    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(has_text, has_html, inline_images, attachments):
    atts = attachments
    html = None
    if has_html:
        html = 'html' if inline_images == 0 else 'related(' + ','.join(['html'] + ['img'] * inline_images) + ')'
    else:
        atts += 1 if inline_images else 0
    if has_text and html:
        body = 'alternative(text,' + html + ')'
    elif html:
        body = html
    else:
        body = 'text'
    if atts:
        return 'mixed(' + ','.join([body] + ['att'] * atts) + ')'
    return body
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('images without html', (False, False, 2, 0), 'mixed(text,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 2, 0), 'alternative(text,related(html,img,img))'), ('html images and attachment', (False, True, 1, 1), 'mixed(related(html,img),att)'), ('text and attachments', (True, False, 0, 1), 'mixed(text,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 1), 'mixed(alternative(text,related(html,img,img)),att)')], 2: [('images without html', (False, False, 3, 0), 'mixed(text,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 3, 0), 'alternative(text,related(html,img,img,img))'), ('html images and attachment', (False, True, 2, 1), 'mixed(related(html,img,img),att)'), ('text and attachments', (True, False, 0, 2), 'mixed(text,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 2), 'mixed(alternative(text,related(html,img,img)),att,att)')], 3: [('images without html', (False, False, 4, 0), 'mixed(text,att,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 4, 0), 'alternative(text,related(html,img,img,img,img))'), ('html images and attachment', (False, True, 3, 1), 'mixed(related(html,img,img,img),att)'), ('text and attachments', (True, False, 0, 3), 'mixed(text,att,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 3), 'mixed(alternative(text,related(html,img,img)),att,att,att)')], 4: [('images without html', (False, False, 5, 0), 'mixed(text,att,att,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 5, 0), 'alternative(text,related(html,img,img,img,img,img))'), ('html images and attachment', (False, True, 4, 1), 'mixed(related(html,img,img,img,img),att)'), ('text and attachments', (True, False, 0, 4), 'mixed(text,att,att,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 4), 'mixed(alternative(text,related(html,img,img)),att,att,att,att)')], 5: [('images without html', (False, False, 6, 0), 'mixed(text,att,att,att,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 6, 0), 'alternative(text,related(html,img,img,img,img,img,img))'), ('html images and attachment', (False, True, 5, 1), 'mixed(related(html,img,img,img,img,img),att)'), ('text and attachments', (True, False, 0, 5), 'mixed(text,att,att,att,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 5), 'mixed(alternative(text,related(html,img,img)),att,att,att,att,att)')]}
for _label, _args, _expected in _CASES[N]:
    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:
Inline images of a message without an HTML body are silently dropped.

The body is multipart/alternative(text, html) when both versions exist, the html alone when only html exists, else text. Inline images go into multipart/related with the html root first; without html they become attachments. When there are attachments the result is multipart/mixed(body, attachments...). Output is a compact string such as "mixed(alternative(text,related(html,img)),att)".

split: open-access


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

N = 1
observations = []
def solve(has_text, has_html, inline_images, attachments):
    atts = attachments
    html = None
    if has_html:
        html = 'html' if inline_images == 0 else 'related(' + ','.join(['html'] + ['img'] * inline_images) + ')'
    else:
        atts += inline_images
    if has_text and html:
        body = 'alternative(' + html + ',text)'
    elif html:
        body = html
    else:
        body = 'text'
    if atts:
        return 'mixed(' + ','.join([body] + ['att'] * atts) + ')'
    return body
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('images without html', (False, False, 2, 0), 'mixed(text,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 2, 0), 'alternative(text,related(html,img,img))'), ('html images and attachment', (False, True, 1, 1), 'mixed(related(html,img),att)'), ('text and attachments', (True, False, 0, 1), 'mixed(text,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 1), 'mixed(alternative(text,related(html,img,img)),att)')], 2: [('images without html', (False, False, 3, 0), 'mixed(text,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 3, 0), 'alternative(text,related(html,img,img,img))'), ('html images and attachment', (False, True, 2, 1), 'mixed(related(html,img,img),att)'), ('text and attachments', (True, False, 0, 2), 'mixed(text,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 2), 'mixed(alternative(text,related(html,img,img)),att,att)')], 3: [('images without html', (False, False, 4, 0), 'mixed(text,att,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 4, 0), 'alternative(text,related(html,img,img,img,img))'), ('html images and attachment', (False, True, 3, 1), 'mixed(related(html,img,img,img),att)'), ('text and attachments', (True, False, 0, 3), 'mixed(text,att,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 3), 'mixed(alternative(text,related(html,img,img)),att,att,att)')], 4: [('images without html', (False, False, 5, 0), 'mixed(text,att,att,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 5, 0), 'alternative(text,related(html,img,img,img,img,img))'), ('html images and attachment', (False, True, 4, 1), 'mixed(related(html,img,img,img,img),att)'), ('text and attachments', (True, False, 0, 4), 'mixed(text,att,att,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 4), 'mixed(alternative(text,related(html,img,img)),att,att,att,att)')], 5: [('images without html', (False, False, 6, 0), 'mixed(text,att,att,att,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 6, 0), 'alternative(text,related(html,img,img,img,img,img,img))'), ('html images and attachment', (False, True, 5, 1), 'mixed(related(html,img,img,img,img,img),att)'), ('text and attachments', (True, False, 0, 5), 'mixed(text,att,att,att,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 5), 'mixed(alternative(text,related(html,img,img)),att,att,att,att,att)')]}
for _label, _args, _expected in _CASES[N]:
    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(has_text, has_html, inline_images, attachments):
    atts = attachments
    html = None
    if has_html:
        html = 'html' if inline_images == 0 else 'related(' + ','.join(['html'] + ['img'] * inline_images) + ')'
    else:
        atts += inline_images
    if has_text and html:
        body = 'alternative(' + html + ',text)' if inline_images else 'alternative(text,' + html + ')'
    elif html:
        body = html
    else:
        body = 'text'
    if atts:
        return 'mixed(' + ','.join([body] + ['att'] * atts) + ')'
    return body
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('images without html', (False, False, 2, 0), 'mixed(text,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 2, 0), 'alternative(text,related(html,img,img))'), ('html images and attachment', (False, True, 1, 1), 'mixed(related(html,img),att)'), ('text and attachments', (True, False, 0, 1), 'mixed(text,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 1), 'mixed(alternative(text,related(html,img,img)),att)')], 2: [('images without html', (False, False, 3, 0), 'mixed(text,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 3, 0), 'alternative(text,related(html,img,img,img))'), ('html images and attachment', (False, True, 2, 1), 'mixed(related(html,img,img),att)'), ('text and attachments', (True, False, 0, 2), 'mixed(text,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 2), 'mixed(alternative(text,related(html,img,img)),att,att)')], 3: [('images without html', (False, False, 4, 0), 'mixed(text,att,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 4, 0), 'alternative(text,related(html,img,img,img,img))'), ('html images and attachment', (False, True, 3, 1), 'mixed(related(html,img,img,img),att)'), ('text and attachments', (True, False, 0, 3), 'mixed(text,att,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 3), 'mixed(alternative(text,related(html,img,img)),att,att,att)')], 4: [('images without html', (False, False, 5, 0), 'mixed(text,att,att,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 5, 0), 'alternative(text,related(html,img,img,img,img,img))'), ('html images and attachment', (False, True, 4, 1), 'mixed(related(html,img,img,img,img),att)'), ('text and attachments', (True, False, 0, 4), 'mixed(text,att,att,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 4), 'mixed(alternative(text,related(html,img,img)),att,att,att,att)')], 5: [('images without html', (False, False, 6, 0), 'mixed(text,att,att,att,att,att,att)'), ('text and html', (True, True, 0, 0), 'alternative(text,html)'), ('text html and images', (True, True, 6, 0), 'alternative(text,related(html,img,img,img,img,img,img))'), ('html images and attachment', (False, True, 5, 1), 'mixed(related(html,img,img,img,img,img),att)'), ('text and attachments', (True, False, 0, 5), 'mixed(text,att,att,att,att,att)'), ('plain text only', (True, False, 0, 0), 'text'), ('everything', (True, True, 2, 5), 'mixed(alternative(text,related(html,img,img)),att,att,att,att,att)')]}
for _label, _args, _expected in _CASES[N]:
    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 plain-text alternative is placed after the HTML one, so clients prefer plain text.

The body is multipart/alternative(text, html) when both versions exist, the html alone when only html exists, else text. Inline images go into multipart/related with the html root first; without html they become attachments. When there are attachments the result is multipart/mixed(body, attachments...). Output is a compact string such as "mixed(alternative(text,related(html,img)),att)".

split: open-access