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

N = 1
observations = []
def solve(tree, blocked_ext, max_size):
    actions = []
    def walk(n, signed):
        t = n['type'].lower()
        if t.startswith('multipart/'):
            for c in n['parts']:
                walk(c, signed or t == 'multipart/signed')
            return
        name = (n.get('filename') or '').strip().lower()
        ext = name.rsplit('.', 1)[-1] if '.' in name else ''
        bad = ext in blocked_ext or t == 'application/x-msdownload' or n.get('size', 0) >= max_size
        if (n.get('disp') or '').lower() != 'attachment' and not name:
            bad = False
        if bad and signed:
            actions.append('reject')
        else:
            actions.append('strip' if bad else 'keep')
    walk(tree, False)
    return 'reject' if 'reject' in actions else actions
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice1.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP1.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 1}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5001}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 1000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 1001}]}, [], 1000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r1.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 2: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice2.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP2.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 2}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5002}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 2000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 2001}]}, [], 2000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r2.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 3: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice3.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP3.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 3}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5003}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 3000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 3001}]}, [], 3000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r3.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 4: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice4.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP4.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 4}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5004}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 4000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 4001}]}, [], 4000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r4.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 5: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice5.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP5.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 5}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5005}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 5000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 5001}]}, [], 5000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r5.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])]}
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(tree, blocked_ext, max_size):
    actions = []
    def walk(n, signed):
        t = n['type'].lower()
        if t.startswith('multipart/'):
            for c in n['parts']:
                walk(c, signed or t == 'multipart/signed')
            return
        name = (n.get('filename') or '').strip().lower()
        ext = name.rsplit('.', 1)[-1] if '.' in name else ''
        bad = ext in blocked_ext or t == 'application/x-msdownload' or n.get('size', 0) > max_size + 1
        if (n.get('disp') or '').lower() != 'attachment' and not name:
            bad = False
        if bad and signed:
            actions.append('reject')
        else:
            actions.append('strip' if bad else 'keep')
    walk(tree, False)
    return 'reject' if 'reject' in actions else actions
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice1.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP1.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 1}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5001}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 1000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 1001}]}, [], 1000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r1.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 2: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice2.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP2.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 2}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5002}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 2000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 2001}]}, [], 2000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r2.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 3: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice3.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP3.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 3}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5003}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 3000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 3001}]}, [], 3000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r3.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 4: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice4.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP4.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 4}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5004}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 4000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 4001}]}, [], 4000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r4.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 5: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice5.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP5.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 5}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5005}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 5000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 5001}]}, [], 5000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r5.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])]}
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:
Attachments exactly at the size limit are stripped.

Leaves {type, filename, disp, size}. A leaf is blocked when its last filename extension (trimmed, lower-cased) is in blocked_ext, its type is application/x-msdownload, or its size exceeds max_size. A non-attachment leaf without a filename (a body) is never blocked. Blocked leaves become "strip", others "keep"; a blocked leaf anywhere under multipart/signed cannot be stripped, and then the whole message is "reject". Result: the per-leaf action list or "reject".

split: open-access


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

N = 1
observations = []
def solve(tree, blocked_ext, max_size):
    actions = []
    def walk(n, signed):
        t = n['type'].lower()
        if t.startswith('multipart/'):
            for c in n['parts']:
                walk(c, signed or t == 'multipart/signed')
            return
        name = (n.get('filename') or '').strip().lower()
        ext = name.rsplit('.', 1)[-1] if '.' in name else ''
        bad = ext in blocked_ext or t == 'application/x-msdownload' or n.get('size', 0) > max_size
        if (n.get('disp') or '').lower() != 'attachment' and not name:
            bad = False
        if bad and signed:
            actions.append('reject')
        else:
            actions.append('strip' if bad else 'keep')
    walk(tree, False)
    return actions
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice1.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP1.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 1}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5001}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 1000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 1001}]}, [], 1000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r1.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 2: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice2.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP2.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 2}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5002}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 2000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 2001}]}, [], 2000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r2.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 3: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice3.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP3.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 3}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5003}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 3000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 3001}]}, [], 3000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r3.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 4: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice4.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP4.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 4}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5004}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 4000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 4001}]}, [], 4000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r4.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 5: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice5.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP5.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 5}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5005}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 5000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 5001}]}, [], 5000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r5.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])]}
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(tree, blocked_ext, max_size):
    actions = []
    def walk(n, signed):
        t = n['type'].lower()
        if t.startswith('multipart/'):
            for c in n['parts']:
                walk(c, signed or t == 'multipart/signed')
            return
        name = (n.get('filename') or '').strip().lower()
        ext = name.rsplit('.', 1)[-1] if '.' in name else ''
        bad = ext in blocked_ext or t == 'application/x-msdownload' or n.get('size', 0) > max_size
        if (n.get('disp') or '').lower() != 'attachment' and not name:
            bad = False
        if bad and signed:
            actions.append('reject')
        else:
            actions.append('strip' if bad else 'keep')
    walk(tree, False)
    return 'reject' if actions and actions[0] == 'reject' else actions
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice1.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP1.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 1}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5001}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 1000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 1001}]}, [], 1000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r1.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 2: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice2.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP2.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 2}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5002}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 2000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 2001}]}, [], 2000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r2.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 3: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice3.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP3.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 3}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5003}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 3000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 3001}]}, [], 3000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r3.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 4: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice4.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP4.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 4}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5004}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 4000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 4001}]}, [], 4000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r4.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])], 5: [('double extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 10}, {'type': 'application/pdf', 'filename': 'invoice5.pdf.exe', 'disp': 'attachment', 'size': 10}]}, ['exe'], 1000), ['keep', 'strip']), ('upper-case padded extension', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream', 'filename': 'SETUP5.EXE ', 'disp': 'attachment', 'size': 5}]}, ['exe'], 1000), ['keep', 'strip']), ('blocked part inside signed mixed', ({'type': 'multipart/signed', 'parts': [{'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/zip', 'filename': 'x.js', 'disp': 'attachment', 'size': 5}]}, {'type': 'application/pgp-signature'}]}, ['js'], 1000), 'reject'), ('large body and named inline part', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain', 'size': 5005}, {'type': 'application/octet-stream', 'filename': 'tool.exe', 'disp': 'inline', 'size': 3}, {'type': 'application/zip', 'disp': 'attachment', 'size': 5000}]}, ['exe'], 1000), ['keep', 'strip', 'strip']), ('size boundaries', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/png', 'filename': 'a.png', 'disp': 'attachment', 'size': 5000}, {'type': 'image/png', 'filename': 'b.png', 'disp': 'attachment', 'size': 5001}]}, [], 5000), ['keep', 'keep', 'strip']), ('executable type in signed', ({'type': 'multipart/signed', 'parts': [{'type': 'text/plain'}, {'type': 'application/x-msdownload', 'filename': 'a.bin', 'disp': 'attachment', 'size': 1}]}, [], 50), 'reject'), ('clean message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/pdf', 'filename': 'r5.pdf', 'disp': 'attachment', 'size': 10}]}, ['exe'], 100), ['keep', 'keep'])]}
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 signed message that cannot be cleaned is delivered with a partial action list instead of being rejected.

Leaves {type, filename, disp, size}. A leaf is blocked when its last filename extension (trimmed, lower-cased) is in blocked_ext, its type is application/x-msdownload, or its size exceeds max_size. A non-attachment leaf without a filename (a body) is never blocked. Blocked leaves become "strip", others "keep"; a blocked leaf anywhere under multipart/signed cannot be stripped, and then the whole message is "reject". Result: the per-leaf action list or "reject".

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import re
N = 1
observations = []
def solve(headers):
    version = None
    ctype = None
    for name, value in headers:
        key = name.strip().lower()
        if key == 'mime-version':
            version = value
        elif key == 'content-type' and ctype is None:
            ctype = value
    clean = re.sub(r'\([^()]*\)', '', version or '')
    clean = ''.join(clean.split())
    if clean != '1.0':
        return ['text/plain', 'non-mime']
    mtype = (ctype or 'text/plain').split(';')[0].strip().lower() or 'text/plain'
    return [mtype, 'mime']
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v1)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b1']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.1'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 2: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v2)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b2']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.2'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 3: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v3)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b3']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.3'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 4: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v4)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b4']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.4'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 5: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v5)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b5']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.5'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])]}
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
import re
N = 1
observations = []
def solve(headers):
    version = None
    ctype = None
    for name, value in headers:
        key = name.strip().lower()
        if key == 'mime-version' and not version:
            version = value
        elif key == 'content-type' and ctype is None:
            ctype = value
    clean = re.sub(r'\([^()]*\)', '', version or '')
    clean = ''.join(clean.split())
    if clean != '1.0':
        return ['text/plain', 'non-mime']
    mtype = (ctype or 'text/plain').split(';')[0].strip().lower() or 'text/plain'
    return [mtype, 'mime']
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v1)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b1']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.1'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 2: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v2)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b2']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.2'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 3: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v3)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b3']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.3'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 4: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v4)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b4']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.4'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 5: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v5)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b5']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.5'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])]}
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 later MIME-Version header overrides an empty first one and turns a legacy message into MIME.

headers are [name, value] pairs with case-insensitive, trimmed names; the first occurrence of MIME-Version and of Content-Type is used. The MIME-Version value, after removing parenthesised comments and all whitespace, must equal "1.0"; otherwise the message is ["text/plain", "non-mime"]. MIME messages report [type, "mime"] with the type taken from the first Content-Type (before ";", trimmed, lower-cased; text/plain when absent or empty).

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import re
N = 1
observations = []
def solve(headers):
    version = None
    ctype = None
    for name, value in headers:
        key = name.strip().lower()
        if key == 'mime-version' and version is None:
            version = value
        elif key == 'content-type' and ctype is None:
            ctype = value
    clean = version or ''
    clean = ''.join(clean.split())
    if clean != '1.0':
        return ['text/plain', 'non-mime']
    mtype = (ctype or 'text/plain').split(';')[0].strip().lower() or 'text/plain'
    return [mtype, 'mime']
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v1)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b1']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.1'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 2: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v2)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b2']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.2'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 3: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v3)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b3']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.3'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 4: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v4)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b4']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.4'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 5: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v5)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b5']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.5'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])]}
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
import re
N = 1
observations = []
def solve(headers):
    version = None
    ctype = None
    for name, value in headers:
        key = name.strip().lower()
        if key == 'mime-version' and version is None:
            version = value
        elif key == 'content-type' and ctype is None:
            ctype = value
    clean = (version or '').split('(')[0]
    clean = ''.join(clean.split())
    if clean != '1.0':
        return ['text/plain', 'non-mime']
    mtype = (ctype or 'text/plain').split(';')[0].strip().lower() or 'text/plain'
    return [mtype, 'mime']
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v1)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b1']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.1'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 2: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v2)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b2']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.2'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 3: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v3)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b3']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.3'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 4: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v4)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b4']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.4'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 5: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v5)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b5']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.5'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])]}
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 MIME-Version header carrying a comment is not recognised, so the message is shown as raw text.

headers are [name, value] pairs with case-insensitive, trimmed names; the first occurrence of MIME-Version and of Content-Type is used. The MIME-Version value, after removing parenthesised comments and all whitespace, must equal "1.0"; otherwise the message is ["text/plain", "non-mime"]. MIME messages report [type, "mime"] with the type taken from the first Content-Type (before ";", trimmed, lower-cased; text/plain when absent or empty).

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import re
N = 1
observations = []
def solve(headers):
    version = None
    ctype = None
    for name, value in headers:
        key = name.strip().lower()
        if key == 'mime-version' and version is None:
            version = value
        elif key == 'content-type' and ctype is None:
            ctype = value
    clean = re.sub(r'\([^()]*\)', '', version or '')
    clean = clean.strip()
    if clean != '1.0':
        return ['text/plain', 'non-mime']
    mtype = (ctype or 'text/plain').split(';')[0].strip().lower() or 'text/plain'
    return [mtype, 'mime']
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v1)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b1']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.1'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 2: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v2)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b2']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.2'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 3: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v3)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b3']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.3'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 4: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v4)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b4']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.4'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 5: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v5)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b5']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.5'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])]}
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
import re
N = 1
observations = []
def solve(headers):
    version = None
    ctype = None
    for name, value in headers:
        key = name.strip().lower()
        if key == 'mime-version' and version is None:
            version = value
        elif key == 'content-type' and ctype is None:
            ctype = value
    clean = re.sub(r'\([^()]*\)', '', version or '')
    clean = clean.replace(' ', '')
    if clean != '1.0':
        return ['text/plain', 'non-mime']
    mtype = (ctype or 'text/plain').split(';')[0].strip().lower() or 'text/plain'
    return [mtype, 'mime']
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v1)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b1']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.1'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 2: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v2)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b2']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.2'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 3: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v3)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b3']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.3'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 4: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v4)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b4']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.4'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])], 5: [('empty first version header', ([['MIME-Version', ''], ['MIME-Version', '1.0'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('trailing comment', ([['Mime-Version', '1.0 (produced by MetaSend v5)'], ['Content-Type', 'Text/HTML; charset=utf-8']],), ['text/html', 'mime']), ('leading comment', ([['MIME-Version', '(generated) 1.0'], ['content-type', 'image/png']],), ['image/png', 'mime']), ('inner whitespace', ([['MIME-Version', '1 .\t0'], ['Content-Type', 'multipart/mixed; boundary=b5']],), ['multipart/mixed', 'mime']), ('no version header', ([['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('other version', ([['MIME-Version', '1.5'], ['Content-Type', 'text/html']],), ['text/plain', 'non-mime']), ('empty content type', ([[' MIME-Version ', '1.0'], ['Content-Type', ' ; charset=x']],), ['text/plain', 'mime'])]}
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 version written with spaces or tabs between its tokens is rejected.

headers are [name, value] pairs with case-insensitive, trimmed names; the first occurrence of MIME-Version and of Content-Type is used. The MIME-Version value, after removing parenthesised comments and all whitespace, must equal "1.0"; otherwise the message is ["text/plain", "non-mime"]. MIME messages report [type, "mime"] with the type taken from the first Content-Type (before ";", trimmed, lower-cased; text/plain when absent or empty).

split: open-access


prompt:
With a Sunday week start and INTERVAL=2, the active weeks are shifted so Sunday instances land in the skipped week.

FREQ=WEEKLY expansion. byday lists weekday numbers (0=Monday..6=Sunday); an empty list means the weekday of dtstart. Weeks begin on wkst; the week containing dtstart is week 0 and only weeks whose index is a multiple of interval are active. Instances are chronological, never before dtstart, and dtstart itself is an instance only if its weekday is listed. Return the first count ISO dates.

split: open-access


prompt:
An every-other-week series produces instances in every week.

FREQ=WEEKLY expansion. byday lists weekday numbers (0=Monday..6=Sunday); an empty list means the weekday of dtstart. Weeks begin on wkst; the week containing dtstart is week 0 and only weeks whose index is a multiple of interval are active. Instances are chronological, never before dtstart, and dtstart itself is an instance only if its weekday is listed. Return the first count ISO dates.

split: open-access


prompt:
With WKST=SU a Sunday instance is emitted after later weekdays, so COUNT keeps the wrong instances.

FREQ=WEEKLY expansion. byday lists weekday numbers (0=Monday..6=Sunday); an empty list means the weekday of dtstart. Weeks begin on wkst; the week containing dtstart is week 0 and only weeks whose index is a multiple of interval are active. Instances are chronological, never before dtstart, and dtstart itself is an instance only if its weekday is listed. Return the first count ISO dates.

split: open-access


prompt:
Days of the first week that precede dtstart are reported as instances.

FREQ=WEEKLY expansion. byday lists weekday numbers (0=Monday..6=Sunday); an empty list means the weekday of dtstart. Weeks begin on wkst; the week containing dtstart is week 0 and only weeks whose index is a multiple of interval are active. Instances are chronological, never before dtstart, and dtstart itself is an instance only if its weekday is listed. Return the first count ISO dates.

split: open-access


source:
"""Failure Map reference implementation. Python standard library only."""
import json
import datetime as dt
import calendar
N = 1
observations = []
def solve(dtstart, interval, byday, wkst, count):
    start = dt.date.fromisoformat(dtstart)
    days = sorted(set(byday)) if byday else [start.weekday()]
    week0 = start - dt.timedelta(days=(start.weekday() - wkst) % 7)
    out = [start.isoformat()] if start.weekday() not in days else []
    for k in range(600):
        week = week0 + dt.timedelta(weeks=k * interval)
        for wd in sorted(days, key=lambda w: (w - wkst) % 7):
            d = week + dt.timedelta(days=(wd - wkst) % 7)
            if d < start:
                continue
            if len(out) >= count:
                return out
            out.append(d.isoformat())
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: WKST=SU interval 2 TU,SU',
   ['1997-08-05', 2, [1, 6], 6, 4],
   ['1997-08-05', '1997-08-17', '1997-08-19', '1997-08-31']),
  ('regression: WKST=MO interval 2 TU,SU',
   ['1997-08-05', 2, [1, 6], 0, 4],
   ['1997-08-05', '1997-08-10', '1997-08-19', '1997-08-24']),
  ('non-matching dtstart Wednesday',
   ['2024-01-03', 1, [0, 4], 0, 3],
   ['2024-01-05', '2024-01-08', '2024-01-12']),
  ('dtstart mid-week weekly MO,WE,FR',
   ['2024-01-04', 1, [0, 2, 4], 0, 5],
   ['2024-01-05', '2024-01-08', '2024-01-10', '2024-01-12', '2024-01-15']),
  ('empty BYDAY uses dtstart weekday',
   ['2024-02-29', 3, [], 0, 3],
   ['2024-02-29', '2024-03-21', '2024-04-11']),
  ('count zero', ['2024-01-01', 1, [0], 0, 0], []),
  ('WKST=SU weekly SA,SU',
   ['2024-03-09', 1, [5, 6], 6, 4],
   ['2024-03-09', '2024-03-10', '2024-03-16', '2024-03-17'])],
 [('non-matching dtstart Wednesday',
   ['2024-01-03', 1, [0, 4], 0, 3],
   ['2024-01-05', '2024-01-08', '2024-01-12']),
  ('dtstart mid-week weekly MO,WE,FR',
   ['2024-01-04', 1, [0, 2, 4], 0, 5],
   ['2024-01-05', '2024-01-08', '2024-01-10', '2024-01-12', '2024-01-15']),
  ('empty BYDAY uses dtstart weekday',
   ['2024-02-29', 3, [], 0, 3],
   ['2024-02-29', '2024-03-21', '2024-04-11']),
  ('count zero', ['2024-01-01', 1, [0], 0, 0], []),
  ('WKST=SU weekly SA,SU',
   ['2024-03-09', 1, [5, 6], 6, 4],
   ['2024-03-09', '2024-03-10', '2024-03-16', '2024-03-17']),
  ('interval 3 friday from monday', ['2024-05-06', 3, [4], 0, 3], ['2024-05-10', '2024-05-31', '2024-06-21']),
  ('WKST=WE interval 2 MO,TH',
   ['2024-06-04', 2, [0, 3], 2, 5],
   ['2024-06-13', '2024-06-17', '2024-06-27', '2024-07-01', '2024-07-11'])],
 [('empty BYDAY uses dtstart weekday',
   ['2024-02-29', 3, [], 0, 3],
   ['2024-02-29', '2024-03-21', '2024-04-11']),
  ('count zero', ['2024-01-01', 1, [0], 0, 0], []),
  ('WKST=SU weekly SA,SU',
   ['2024-03-09', 1, [5, 6], 6, 4],
   ['2024-03-09', '2024-03-10', '2024-03-16', '2024-03-17']),
  ('interval 3 friday from monday', ['2024-05-06', 3, [4], 0, 3], ['2024-05-10', '2024-05-31', '2024-06-21']),
  ('WKST=WE interval 2 MO,TH',
   ['2024-06-04', 2, [0, 3], 2, 5],
   ['2024-06-13', '2024-06-17', '2024-06-27', '2024-07-01', '2024-07-11']),
  ('single sunday series', ['2024-12-29', 2, [6], 0, 3], ['2024-12-29', '2025-01-12', '2025-01-26']),
  ('dtstart on listed day',
   ['2025-01-06', 2, [0, 3], 0, 4],
   ['2025-01-06', '2025-01-09', '2025-01-20', '2025-01-23'])],
 [('WKST=SU weekly SA,SU',
   ['2024-03-09', 1, [5, 6], 6, 4],
   ['2024-03-09', '2024-03-10', '2024-03-16', '2024-03-17']),
  ('interval 3 friday from monday', ['2024-05-06', 3, [4], 0, 3], ['2024-05-10', '2024-05-31', '2024-06-21']),
  ('WKST=WE interval 2 MO,TH',
   ['2024-06-04', 2, [0, 3], 2, 5],
   ['2024-06-13', '2024-06-17', '2024-06-27', '2024-07-01', '2024-07-11']),
  ('single sunday series', ['2024-12-29', 2, [6], 0, 3], ['2024-12-29', '2025-01-12', '2025-01-26']),
  ('dtstart on listed day',
   ['2025-01-06', 2, [0, 3], 0, 4],
   ['2025-01-06', '2025-01-09', '2025-01-20', '2025-01-23']),
  ('WKST=SU interval 2 from saturday',
   ['2025-03-01', 2, [6, 5], 6, 5],
   ['2025-03-01', '2025-03-09', '2025-03-15', '2025-03-23', '2025-03-29']),
  ('regression: WKST=SU interval 2 TU,SU',
   ['1997-08-05', 2, [1, 6], 6, 4],
   ['1997-08-05', '1997-08-17', '1997-08-19', '1997-08-31'])],
 [('WKST=WE interval 2 MO,TH',
   ['2024-06-04', 2, [0, 3], 2, 5],
   ['2024-06-13', '2024-06-17', '2024-06-27', '2024-07-01', '2024-07-11']),
  ('single sunday series', ['2024-12-29', 2, [6], 0, 3], ['2024-12-29', '2025-01-12', '2025-01-26']),
  ('dtstart on listed day',
   ['2025-01-06', 2, [0, 3], 0, 4],
   ['2025-01-06', '2025-01-09', '2025-01-20', '2025-01-23']),
  ('WKST=SU interval 2 from saturday',
   ['2025-03-01', 2, [6, 5], 6, 5],
   ['2025-03-01', '2025-03-09', '2025-03-15', '2025-03-23', '2025-03-29']),
  ('regression: WKST=SU interval 2 TU,SU',
   ['1997-08-05', 2, [1, 6], 6, 4],
   ['1997-08-05', '1997-08-17', '1997-08-19', '1997-08-31']),
  ('regression: WKST=MO interval 2 TU,SU',
   ['1997-08-05', 2, [1, 6], 0, 4],
   ['1997-08-05', '1997-08-10', '1997-08-19', '1997-08-24']),
  ('non-matching dtstart Wednesday',
   ['2024-01-03', 1, [0, 4], 0, 3],
   ['2024-01-05', '2024-01-08', '2024-01-12'])]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A dtstart that is not a listed weekday is emitted as an extra first instance and consumes COUNT.

FREQ=WEEKLY expansion. byday lists weekday numbers (0=Monday..6=Sunday); an empty list means the weekday of dtstart. Weeks begin on wkst; the week containing dtstart is week 0 and only weeks whose index is a multiple of interval are active. Instances are chronological, never before dtstart, and dtstart itself is an instance only if its weekday is listed. Return the first count ISO dates.

split: open-access


prompt:
A series that crosses December reports January instances in the previous year.

FREQ=MONTHLY with a BYMONTHDAY list. Positive values count from the first day, negative values from the last day (-1 is the last day). A value that does not exist in a month is skipped for that month, never clamped. Months step by interval from the dtstart month; instances before dtstart are dropped; coinciding values yield one instance. Return the first count ISO dates in chronological order.

split: open-access


prompt:
BYMONTHDAY=-1 produces the second-to-last day of each month.

FREQ=MONTHLY with a BYMONTHDAY list. Positive values count from the first day, negative values from the last day (-1 is the last day). A value that does not exist in a month is skipped for that month, never clamped. Months step by interval from the dtstart month; instances before dtstart are dropped; coinciding values yield one instance. Return the first count ISO dates in chronological order.

split: open-access


prompt:
BYMONTHDAY=31 yields February 28 and April 30 instead of skipping those months.

FREQ=MONTHLY with a BYMONTHDAY list. Positive values count from the first day, negative values from the last day (-1 is the last day). A value that does not exist in a month is skipped for that month, never clamped. Months step by interval from the dtstart month; instances before dtstart are dropped; coinciding values yield one instance. Return the first count ISO dates in chronological order.

split: open-access


prompt:
BYMONTHDAY=31,-1 emits the 31st twice in long months.

FREQ=MONTHLY with a BYMONTHDAY list. Positive values count from the first day, negative values from the last day (-1 is the last day). A value that does not exist in a month is skipped for that month, never clamped. Months step by interval from the dtstart month; instances before dtstart are dropped; coinciding values yield one instance. Return the first count ISO dates in chronological order.

split: open-access


prompt:
Days of the first month that precede dtstart are emitted.

FREQ=MONTHLY with a BYMONTHDAY list. Positive values count from the first day, negative values from the last day (-1 is the last day). A value that does not exist in a month is skipped for that month, never clamped. Months step by interval from the dtstart month; instances before dtstart are dropped; coinciding values yield one instance. Return the first count ISO dates in chronological order.

split: open-access


prompt:
The second Tuesday is computed on the wrong date in months that do not begin on Tuesday.

FREQ=MONTHLY;BYDAY=<ordinal><weekday>. weekday is 0=Monday..6=Sunday; ordinal 1..5 counts from the start of the month and -1..-5 from the end. A month lacking that occurrence is skipped and does not consume COUNT. Months step by interval from the dtstart month; instances before dtstart are dropped. Return the first count ISO dates.

split: open-access


prompt:
The last Friday is placed on a non-Friday in most months.

FREQ=MONTHLY;BYDAY=<ordinal><weekday>. weekday is 0=Monday..6=Sunday; ordinal 1..5 counts from the start of the month and -1..-5 from the end. A month lacking that occurrence is skipped and does not consume COUNT. Months step by interval from the dtstart month; instances before dtstart are dropped. Return the first count ISO dates.

split: open-access


prompt:
BYDAY=-1FR lands a week before the last Friday.

FREQ=MONTHLY;BYDAY=<ordinal><weekday>. weekday is 0=Monday..6=Sunday; ordinal 1..5 counts from the start of the month and -1..-5 from the end. A month lacking that occurrence is skipped and does not consume COUNT. Months step by interval from the dtstart month; instances before dtstart are dropped. Return the first count ISO dates.

split: open-access


prompt:
A 5th-Monday series reports the 4th Monday in months without a fifth one.

FREQ=MONTHLY;BYDAY=<ordinal><weekday>. weekday is 0=Monday..6=Sunday; ordinal 1..5 counts from the start of the month and -1..-5 from the end. A month lacking that occurrence is skipped and does not consume COUNT. Months step by interval from the dtstart month; instances before dtstart are dropped. Return the first count ISO dates.

split: open-access


prompt:
A 5th-weekday series returns fewer than COUNT instances because skipped months are counted.

FREQ=MONTHLY;BYDAY=<ordinal><weekday>. weekday is 0=Monday..6=Sunday; ordinal 1..5 counts from the start of the month and -1..-5 from the end. A month lacking that occurrence is skipped and does not consume COUNT. Months step by interval from the dtstart month; instances before dtstart are dropped. Return the first count ISO dates.

split: open-access


source:
"""Failure Map reference implementation. Python standard library only."""
import json
import datetime as dt
import calendar
N = 1
observations = []
def solve(dtstart, weekdays, setpos, count):
    start = dt.date.fromisoformat(dtstart)
    out = []
    for k in range(240):
        mi = start.month - 1 + k
        y, m = start.year + mi // 12, mi % 12 + 1
        dim = calendar.monthrange(y, m)[1]
        cand = [x for x in [dt.date(y, m, d) for d in range(1, dim + 1)] if x.weekday() in weekdays and x >= start + dt.timedelta(days=1)]
        picked = []
        for p in setpos:
            idx = p - 1 if p > 0 else len(cand) + p
            if 0 <= idx < len(cand) and cand[idx] not in picked:
                picked.append(cand[idx])
        for d in sorted(picked):
            if d < start:
                continue
            out.append(d.isoformat())
            if len(out) >= count:
                return out
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: last weekday of month',
   ['2024-01-01', [0, 1, 2, 3, 4], [-1], 4],
   ['2024-01-31', '2024-02-29', '2024-03-29', '2024-04-30']),
  ('first weekday, dtstart mid-month',
   ['2024-01-10', [0, 1, 2, 3, 4], [1], 3],
   ['2024-02-01', '2024-03-01', '2024-04-01']),
  ('first and last in reverse order',
   ['2024-03-01', [0, 1, 2, 3, 4], [-1, 1], 4],
   ['2024-03-01', '2024-03-29', '2024-04-01', '2024-04-30']),
  ('coinciding positions',
   ['2024-01-01', [0], [1, -5], 4],
   ['2024-01-01', '2024-02-05', '2024-03-04', '2024-04-01']),
  ('three positions with repeat',
   ['2024-04-01', [2], [1, -1, -5], 5],
   ['2024-04-03', '2024-04-24', '2024-05-01', '2024-05-29', '2024-06-05']),
  ('out of range negative', ['2024-01-01', [5, 6], [-7, 1], 3], ['2024-01-06', '2024-01-07', '2024-02-03']),
  ('out of range positive', ['2024-02-01', [0], [5], 3], ['2024-04-29', '2024-07-29', '2024-09-30'])],
 [('first and last in reverse order',
   ['2024-03-01', [0, 1, 2, 3, 4], [-1, 1], 4],
   ['2024-03-01', '2024-03-29', '2024-04-01', '2024-04-30']),
  ('coinciding positions',
   ['2024-01-01', [0], [1, -5], 4],
   ['2024-01-01', '2024-02-05', '2024-03-04', '2024-04-01']),
  ('three positions with repeat',
   ['2024-04-01', [2], [1, -1, -5], 5],
   ['2024-04-03', '2024-04-24', '2024-05-01', '2024-05-29', '2024-06-05']),
  ('out of range negative', ['2024-01-01', [5, 6], [-7, 1], 3], ['2024-01-06', '2024-01-07', '2024-02-03']),
  ('out of range positive', ['2024-02-01', [0], [5], 3], ['2024-04-29', '2024-07-29', '2024-09-30']),
  ('second weekend day', ['2024-06-15', [5, 6], [2], 3], ['2024-07-07', '2024-08-04', '2024-09-07']),
  ('second to last weekday',
   ['2024-07-01', [0, 1, 2, 3, 4], [-2], 3],
   ['2024-07-30', '2024-08-29', '2024-09-27'])],
 [('three positions with repeat',
   ['2024-04-01', [2], [1, -1, -5], 5],
   ['2024-04-03', '2024-04-24', '2024-05-01', '2024-05-29', '2024-06-05']),
  ('out of range negative', ['2024-01-01', [5, 6], [-7, 1], 3], ['2024-01-06', '2024-01-07', '2024-02-03']),
  ('out of range positive', ['2024-02-01', [0], [5], 3], ['2024-04-29', '2024-07-29', '2024-09-30']),
  ('second weekend day', ['2024-06-15', [5, 6], [2], 3], ['2024-07-07', '2024-08-04', '2024-09-07']),
  ('second to last weekday',
   ['2024-07-01', [0, 1, 2, 3, 4], [-2], 3],
   ['2024-07-30', '2024-08-29', '2024-09-27']),
  ('position 3 of TU,TH mid month',
   ['2024-09-12', [1, 3], [3, -1], 4],
   ['2024-09-26', '2024-10-08', '2024-10-31', '2024-11-12']),
  ('december rollover',
   ['2024-12-01', [4], [1, -1], 4],
   ['2024-12-06', '2024-12-27', '2025-01-03', '2025-01-31'])],
 [('out of range positive', ['2024-02-01', [0], [5], 3], ['2024-04-29', '2024-07-29', '2024-09-30']),
  ('second weekend day', ['2024-06-15', [5, 6], [2], 3], ['2024-07-07', '2024-08-04', '2024-09-07']),
  ('second to last weekday',
   ['2024-07-01', [0, 1, 2, 3, 4], [-2], 3],
   ['2024-07-30', '2024-08-29', '2024-09-27']),
  ('position 3 of TU,TH mid month',
   ['2024-09-12', [1, 3], [3, -1], 4],
   ['2024-09-26', '2024-10-08', '2024-10-31', '2024-11-12']),
  ('december rollover',
   ['2024-12-01', [4], [1, -1], 4],
   ['2024-12-06', '2024-12-27', '2025-01-03', '2025-01-31']),
  ('dtstart equals picked day', ['2024-05-31', [0, 1, 2, 3, 4], [-1], 2], ['2024-05-31', '2024-06-28']),
  ('regression: last weekday of month',
   ['2024-01-01', [0, 1, 2, 3, 4], [-1], 4],
   ['2024-01-31', '2024-02-29', '2024-03-29', '2024-04-30'])],
 [('second to last weekday',
   ['2024-07-01', [0, 1, 2, 3, 4], [-2], 3],
   ['2024-07-30', '2024-08-29', '2024-09-27']),
  ('position 3 of TU,TH mid month',
   ['2024-09-12', [1, 3], [3, -1], 4],
   ['2024-09-26', '2024-10-08', '2024-10-31', '2024-11-12']),
  ('december rollover',
   ['2024-12-01', [4], [1, -1], 4],
   ['2024-12-06', '2024-12-27', '2025-01-03', '2025-01-31']),
  ('dtstart equals picked day', ['2024-05-31', [0, 1, 2, 3, 4], [-1], 2], ['2024-05-31', '2024-06-28']),
  ('regression: last weekday of month',
   ['2024-01-01', [0, 1, 2, 3, 4], [-1], 4],
   ['2024-01-31', '2024-02-29', '2024-03-29', '2024-04-30']),
  ('first weekday, dtstart mid-month',
   ['2024-01-10', [0, 1, 2, 3, 4], [1], 3],
   ['2024-02-01', '2024-03-01', '2024-04-01']),
  ('first and last in reverse order',
   ['2024-03-01', [0, 1, 2, 3, 4], [-1, 1], 4],
   ['2024-03-01', '2024-03-29', '2024-04-01', '2024-04-30'])]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
In the first month BYSETPOS=1 picks the first weekday after dtstart instead of the first of the month.

FREQ=MONTHLY;BYDAY=<weekdays>;BYSETPOS=<positions>. For each month from the dtstart month, the candidate set is every day of the whole month whose weekday is listed, in date order. Positions are 1-based, negative from the end; out-of-range positions are ignored and positions naming the same day yield it once. Selected days are emitted chronologically; days before dtstart are dropped after selection. Return the first count ISO dates.

split: open-access


prompt:
BYSETPOS=1 selects the second weekday of the month.

FREQ=MONTHLY;BYDAY=<weekdays>;BYSETPOS=<positions>. For each month from the dtstart month, the candidate set is every day of the whole month whose weekday is listed, in date order. Positions are 1-based, negative from the end; out-of-range positions are ignored and positions naming the same day yield it once. Selected days are emitted chronologically; days before dtstart are dropped after selection. Return the first count ISO dates.

split: open-access


prompt:
BYSETPOS=-7 in a month with five candidates selects a day through Python negative indexing.

FREQ=MONTHLY;BYDAY=<weekdays>;BYSETPOS=<positions>. For each month from the dtstart month, the candidate set is every day of the whole month whose weekday is listed, in date order. Positions are 1-based, negative from the end; out-of-range positions are ignored and positions naming the same day yield it once. Selected days are emitted chronologically; days before dtstart are dropped after selection. Return the first count ISO dates.

split: open-access