broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from urllib.parse import unquote_to_bytes
N = 1
observations = []
def solve(params):
    single = None
    plain = None
    segs = {}
    for name, value in params:
        n = name.lower()
        if n == 'filename*':
            single = value
        elif n == 'filename':
            plain = value
        elif n.startswith('filename*'):
            rest = n[9:]
            enc = rest.endswith('*')
            num = rest[:-1] if enc else rest
            if num.isdigit() and (num == '0' or not num.startswith('0')):
                segs[int(num)] = (enc, value)
    def codec(cs):
        cs = (cs or 'us-ascii').lower()
        return cs if cs in ('utf-8', 'iso-8859-1', 'us-ascii') else 'utf-8'
    if single is not None:
        cs, _, rest = single.partition("'")
        _, _, value = rest.partition("'")
        return unquote_to_bytes(value).decode(codec(cs), 'replace')
    if 0 not in segs:
        return plain
    charset = 'us-ascii'
    chunks = []
    expect = 0
    for i in sorted(segs):
        if i != expect:
            break
        enc, v = segs[i]
        if enc and i == 0:
            charset, _, rest = v.partition("'")
            _, _, v = rest.partition("'")
        chunks.append(unquote_to_bytes(v) if enc else v.encode('latin-1', 'replace'))
        expect += 1
    return ''.join(c.decode(codec(charset), 'replace') for c in chunks)
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short1']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a1'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a1b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b1'], ['filename*01', 'X']],), 'ab1'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_1.txt']],), 'café_1.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine1']],), "it'sfine1"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve1.txt"], ['filename*0', 'seg']],), 'naïve1.txt'), ('orphan continuation', ([['filename', 'plain1.txt'], ['filename*1', 'orphan']],), 'plain1.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per1']],), 'upper1')], 2: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short2']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a2'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a2b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b2'], ['filename*01', 'X']],), 'ab2'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_2.txt']],), 'café_2.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine2']],), "it'sfine2"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve2.txt"], ['filename*0', 'seg']],), 'naïve2.txt'), ('orphan continuation', ([['filename', 'plain2.txt'], ['filename*1', 'orphan']],), 'plain2.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per2']],), 'upper2')], 3: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short3']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a3'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a3b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b3'], ['filename*01', 'X']],), 'ab3'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_3.txt']],), 'café_3.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine3']],), "it'sfine3"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve3.txt"], ['filename*0', 'seg']],), 'naïve3.txt'), ('orphan continuation', ([['filename', 'plain3.txt'], ['filename*1', 'orphan']],), 'plain3.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per3']],), 'upper3')], 4: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short4']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a4'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a4b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b4'], ['filename*01', 'X']],), 'ab4'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_4.txt']],), 'café_4.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine4']],), "it'sfine4"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve4.txt"], ['filename*0', 'seg']],), 'naïve4.txt'), ('orphan continuation', ([['filename', 'plain4.txt'], ['filename*1', 'orphan']],), 'plain4.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per4']],), 'upper4')], 5: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short5']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a5'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a5b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b5'], ['filename*01', 'X']],), 'ab5'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_5.txt']],), 'café_5.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine5']],), "it'sfine5"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve5.txt"], ['filename*0', 'seg']],), 'naïve5.txt'), ('orphan continuation', ([['filename', 'plain5.txt'], ['filename*1', 'orphan']],), 'plain5.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per5']],), 'upper5')]}
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
from urllib.parse import unquote_to_bytes
N = 1
observations = []
def solve(params):
    single = None
    plain = None
    segs = {}
    for name, value in params:
        n = name.lower()
        if n == 'filename*':
            single = value
        elif n == 'filename':
            plain = value
        elif n.startswith('filename*'):
            rest = n[9:]
            enc = rest.endswith('*')
            num = rest[:-1] if enc else rest
            if num.isdigit() and (num == '0' or not num.startswith('0')):
                segs[int(num)] = (enc, value)
    def codec(cs):
        cs = (cs or 'us-ascii').lower()
        return cs if cs in ('utf-8', 'iso-8859-1', 'us-ascii') else 'utf-8'
    if single is not None:
        cs, _, rest = single.partition("'")
        _, _, value = rest.partition("'")
        return unquote_to_bytes(value).decode(codec(cs), 'replace')
    if 0 not in segs:
        return plain
    charset = 'us-ascii'
    chunks = []
    expect = 0
    for i in sorted(segs):
        if i != expect:
            break
        enc, v = segs[i]
        if enc and i == 0:
            charset, _, rest = v.partition("'")
            _, _, v = rest.partition("'")
        chunks.append(unquote_to_bytes(v) if enc else v.encode('latin-1', 'replace'))
        expect += 1
    return b''.join(chunks).decode('latin-1')
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short1']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a1'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a1b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b1'], ['filename*01', 'X']],), 'ab1'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_1.txt']],), 'café_1.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine1']],), "it'sfine1"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve1.txt"], ['filename*0', 'seg']],), 'naïve1.txt'), ('orphan continuation', ([['filename', 'plain1.txt'], ['filename*1', 'orphan']],), 'plain1.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per1']],), 'upper1')], 2: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short2']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a2'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a2b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b2'], ['filename*01', 'X']],), 'ab2'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_2.txt']],), 'café_2.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine2']],), "it'sfine2"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve2.txt"], ['filename*0', 'seg']],), 'naïve2.txt'), ('orphan continuation', ([['filename', 'plain2.txt'], ['filename*1', 'orphan']],), 'plain2.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per2']],), 'upper2')], 3: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short3']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a3'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a3b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b3'], ['filename*01', 'X']],), 'ab3'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_3.txt']],), 'café_3.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine3']],), "it'sfine3"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve3.txt"], ['filename*0', 'seg']],), 'naïve3.txt'), ('orphan continuation', ([['filename', 'plain3.txt'], ['filename*1', 'orphan']],), 'plain3.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per3']],), 'upper3')], 4: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short4']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a4'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a4b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b4'], ['filename*01', 'X']],), 'ab4'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_4.txt']],), 'café_4.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine4']],), "it'sfine4"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve4.txt"], ['filename*0', 'seg']],), 'naïve4.txt'), ('orphan continuation', ([['filename', 'plain4.txt'], ['filename*1', 'orphan']],), 'plain4.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per4']],), 'upper4')], 5: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short5']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a5'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a5b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b5'], ['filename*01', 'X']],), 'ab5'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_5.txt']],), 'café_5.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine5']],), "it'sfine5"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve5.txt"], ['filename*0', 'seg']],), 'naïve5.txt'), ('orphan continuation', ([['filename', 'plain5.txt'], ['filename*1', 'orphan']],), 'plain5.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per5']],), 'upper5')]}
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 multi-byte character split across two segments turns into replacement characters.

params are [name, value] pairs; names are case-insensitive. "filename*" (a single extended value charset'lang'text) wins over everything. Otherwise segments "filename*<n>" (plain) and "filename*<n>*" (percent-encoded) with decimal n without leading zeros ("0" allowed) are joined in numeric order from 0, stopping at the first gap; only an encoded segment 0 carries the charset'lang' prefix. Bytes of all segments are concatenated before decoding with the charset (utf-8, iso-8859-1 or us-ascii; others decode as utf-8; undecodable bytes become U+FFFD). Without a segment 0 the plain "filename" value (or None) is returned.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from urllib.parse import unquote_to_bytes
N = 1
observations = []
def solve(params):
    single = None
    plain = None
    segs = {}
    for name, value in params:
        n = name.lower()
        if n == 'filename*':
            single = value
        elif n == 'filename':
            plain = value
        elif n.startswith('filename*'):
            rest = n[9:]
            enc = rest.endswith('*')
            num = rest[:-1] if enc else rest
            if num.isdigit() and (num == '0' or not num.startswith('0')):
                segs[int(num)] = (enc, value)
    def codec(cs):
        cs = (cs or 'us-ascii').lower()
        return cs if cs in ('utf-8', 'iso-8859-1', 'us-ascii') else 'utf-8'
    if single is not None:
        cs, _, rest = single.partition("'")
        _, _, value = rest.partition("'")
        return unquote_to_bytes(value).decode(codec(cs), 'replace')
    if not segs:
        return plain
    charset = 'us-ascii'
    chunks = []
    expect = 0
    for i in sorted(segs):
        if i != expect:
            break
        enc, v = segs[i]
        if enc and i == 0:
            charset, _, rest = v.partition("'")
            _, _, v = rest.partition("'")
        chunks.append(unquote_to_bytes(v) if enc else v.encode('latin-1', 'replace'))
        expect += 1
    return b''.join(chunks).decode(codec(charset), 'replace')
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short1']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a1'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a1b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b1'], ['filename*01', 'X']],), 'ab1'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_1.txt']],), 'café_1.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine1']],), "it'sfine1"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve1.txt"], ['filename*0', 'seg']],), 'naïve1.txt'), ('orphan continuation', ([['filename', 'plain1.txt'], ['filename*1', 'orphan']],), 'plain1.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per1']],), 'upper1')], 2: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short2']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a2'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a2b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b2'], ['filename*01', 'X']],), 'ab2'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_2.txt']],), 'café_2.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine2']],), "it'sfine2"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve2.txt"], ['filename*0', 'seg']],), 'naïve2.txt'), ('orphan continuation', ([['filename', 'plain2.txt'], ['filename*1', 'orphan']],), 'plain2.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per2']],), 'upper2')], 3: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short3']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a3'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a3b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b3'], ['filename*01', 'X']],), 'ab3'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_3.txt']],), 'café_3.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine3']],), "it'sfine3"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve3.txt"], ['filename*0', 'seg']],), 'naïve3.txt'), ('orphan continuation', ([['filename', 'plain3.txt'], ['filename*1', 'orphan']],), 'plain3.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per3']],), 'upper3')], 4: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short4']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a4'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a4b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b4'], ['filename*01', 'X']],), 'ab4'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_4.txt']],), 'café_4.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine4']],), "it'sfine4"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve4.txt"], ['filename*0', 'seg']],), 'naïve4.txt'), ('orphan continuation', ([['filename', 'plain4.txt'], ['filename*1', 'orphan']],), 'plain4.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per4']],), 'upper4')], 5: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short5']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a5'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a5b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b5'], ['filename*01', 'X']],), 'ab5'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_5.txt']],), 'café_5.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine5']],), "it'sfine5"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve5.txt"], ['filename*0', 'seg']],), 'naïve5.txt'), ('orphan continuation', ([['filename', 'plain5.txt'], ['filename*1', 'orphan']],), 'plain5.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per5']],), 'upper5')]}
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
from urllib.parse import unquote_to_bytes
N = 1
observations = []
def solve(params):
    single = None
    plain = None
    segs = {}
    for name, value in params:
        n = name.lower()
        if n == 'filename*':
            single = value
        elif n == 'filename':
            plain = value
        elif n.startswith('filename*'):
            rest = n[9:]
            enc = rest.endswith('*')
            num = rest[:-1] if enc else rest
            if num.isdigit() and (num == '0' or not num.startswith('0')):
                segs[int(num)] = (enc, value)
    def codec(cs):
        cs = (cs or 'us-ascii').lower()
        return cs if cs in ('utf-8', 'iso-8859-1', 'us-ascii') else 'utf-8'
    if single is not None:
        cs, _, rest = single.partition("'")
        _, _, value = rest.partition("'")
        return unquote_to_bytes(value).decode(codec(cs), 'replace')
    if not segs or min(segs) > 1:
        return plain
    charset = 'us-ascii'
    chunks = []
    expect = 0
    for i in sorted(segs):
        if i != expect:
            break
        enc, v = segs[i]
        if enc and i == 0:
            charset, _, rest = v.partition("'")
            _, _, v = rest.partition("'")
        chunks.append(unquote_to_bytes(v) if enc else v.encode('latin-1', 'replace'))
        expect += 1
    return b''.join(chunks).decode(codec(charset), 'replace')
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short1']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a1'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a1b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b1'], ['filename*01', 'X']],), 'ab1'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_1.txt']],), 'café_1.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine1']],), "it'sfine1"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve1.txt"], ['filename*0', 'seg']],), 'naïve1.txt'), ('orphan continuation', ([['filename', 'plain1.txt'], ['filename*1', 'orphan']],), 'plain1.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per1']],), 'upper1')], 2: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short2']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a2'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a2b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b2'], ['filename*01', 'X']],), 'ab2'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_2.txt']],), 'café_2.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine2']],), "it'sfine2"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve2.txt"], ['filename*0', 'seg']],), 'naïve2.txt'), ('orphan continuation', ([['filename', 'plain2.txt'], ['filename*1', 'orphan']],), 'plain2.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per2']],), 'upper2')], 3: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short3']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a3'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a3b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b3'], ['filename*01', 'X']],), 'ab3'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_3.txt']],), 'café_3.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine3']],), "it'sfine3"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve3.txt"], ['filename*0', 'seg']],), 'naïve3.txt'), ('orphan continuation', ([['filename', 'plain3.txt'], ['filename*1', 'orphan']],), 'plain3.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per3']],), 'upper3')], 4: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short4']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a4'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a4b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b4'], ['filename*01', 'X']],), 'ab4'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_4.txt']],), 'café_4.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine4']],), "it'sfine4"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve4.txt"], ['filename*0', 'seg']],), 'naïve4.txt'), ('orphan continuation', ([['filename', 'plain4.txt'], ['filename*1', 'orphan']],), 'plain4.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per4']],), 'upper4')], 5: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short5']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a5'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a5b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b5'], ['filename*01', 'X']],), 'ab5'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_5.txt']],), 'café_5.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine5']],), "it'sfine5"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve5.txt"], ['filename*0', 'seg']],), 'naïve5.txt'), ('orphan continuation', ([['filename', 'plain5.txt'], ['filename*1', 'orphan']],), 'plain5.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per5']],), 'upper5')]}
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 stray continuation without segment 0 replaces the plain filename with an empty name.

params are [name, value] pairs; names are case-insensitive. "filename*" (a single extended value charset'lang'text) wins over everything. Otherwise segments "filename*<n>" (plain) and "filename*<n>*" (percent-encoded) with decimal n without leading zeros ("0" allowed) are joined in numeric order from 0, stopping at the first gap; only an encoded segment 0 carries the charset'lang' prefix. Bytes of all segments are concatenated before decoding with the charset (utf-8, iso-8859-1 or us-ascii; others decode as utf-8; undecodable bytes become U+FFFD). Without a segment 0 the plain "filename" value (or None) is returned.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from urllib.parse import unquote_to_bytes
N = 1
observations = []
def solve(params):
    single = None
    plain = None
    segs = {}
    for name, value in params:
        n = name
        if n == 'filename*':
            single = value
        elif n == 'filename':
            plain = value
        elif n.startswith('filename*'):
            rest = n[9:]
            enc = rest.endswith('*')
            num = rest[:-1] if enc else rest
            if num.isdigit() and (num == '0' or not num.startswith('0')):
                segs[int(num)] = (enc, value)
    def codec(cs):
        cs = (cs or 'us-ascii').lower()
        return cs if cs in ('utf-8', 'iso-8859-1', 'us-ascii') else 'utf-8'
    if single is not None:
        cs, _, rest = single.partition("'")
        _, _, value = rest.partition("'")
        return unquote_to_bytes(value).decode(codec(cs), 'replace')
    if 0 not in segs:
        return plain
    charset = 'us-ascii'
    chunks = []
    expect = 0
    for i in sorted(segs):
        if i != expect:
            break
        enc, v = segs[i]
        if enc and i == 0:
            charset, _, rest = v.partition("'")
            _, _, v = rest.partition("'")
        chunks.append(unquote_to_bytes(v) if enc else v.encode('latin-1', 'replace'))
        expect += 1
    return b''.join(chunks).decode(codec(charset), 'replace')
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short1']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a1'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a1b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b1'], ['filename*01', 'X']],), 'ab1'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_1.txt']],), 'café_1.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine1']],), "it'sfine1"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve1.txt"], ['filename*0', 'seg']],), 'naïve1.txt'), ('orphan continuation', ([['filename', 'plain1.txt'], ['filename*1', 'orphan']],), 'plain1.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per1']],), 'upper1')], 2: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short2']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a2'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a2b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b2'], ['filename*01', 'X']],), 'ab2'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_2.txt']],), 'café_2.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine2']],), "it'sfine2"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve2.txt"], ['filename*0', 'seg']],), 'naïve2.txt'), ('orphan continuation', ([['filename', 'plain2.txt'], ['filename*1', 'orphan']],), 'plain2.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per2']],), 'upper2')], 3: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short3']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a3'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a3b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b3'], ['filename*01', 'X']],), 'ab3'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_3.txt']],), 'café_3.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine3']],), "it'sfine3"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve3.txt"], ['filename*0', 'seg']],), 'naïve3.txt'), ('orphan continuation', ([['filename', 'plain3.txt'], ['filename*1', 'orphan']],), 'plain3.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per3']],), 'upper3')], 4: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short4']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a4'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a4b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b4'], ['filename*01', 'X']],), 'ab4'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_4.txt']],), 'café_4.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine4']],), "it'sfine4"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve4.txt"], ['filename*0', 'seg']],), 'naïve4.txt'), ('orphan continuation', ([['filename', 'plain4.txt'], ['filename*1', 'orphan']],), 'plain4.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per4']],), 'upper4')], 5: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short5']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a5'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a5b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b5'], ['filename*01', 'X']],), 'ab5'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_5.txt']],), 'café_5.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine5']],), "it'sfine5"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve5.txt"], ['filename*0', 'seg']],), 'naïve5.txt'), ('orphan continuation', ([['filename', 'plain5.txt'], ['filename*1', 'orphan']],), 'plain5.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per5']],), 'upper5')]}
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
from urllib.parse import unquote_to_bytes
N = 1
observations = []
def solve(params):
    single = None
    plain = None
    segs = {}
    for name, value in params:
        n = name.strip()
        if n == 'filename*':
            single = value
        elif n == 'filename':
            plain = value
        elif n.startswith('filename*'):
            rest = n[9:]
            enc = rest.endswith('*')
            num = rest[:-1] if enc else rest
            if num.isdigit() and (num == '0' or not num.startswith('0')):
                segs[int(num)] = (enc, value)
    def codec(cs):
        cs = (cs or 'us-ascii').lower()
        return cs if cs in ('utf-8', 'iso-8859-1', 'us-ascii') else 'utf-8'
    if single is not None:
        cs, _, rest = single.partition("'")
        _, _, value = rest.partition("'")
        return unquote_to_bytes(value).decode(codec(cs), 'replace')
    if 0 not in segs:
        return plain
    charset = 'us-ascii'
    chunks = []
    expect = 0
    for i in sorted(segs):
        if i != expect:
            break
        enc, v = segs[i]
        if enc and i == 0:
            charset, _, rest = v.partition("'")
            _, _, v = rest.partition("'")
        chunks.append(unquote_to_bytes(v) if enc else v.encode('latin-1', 'replace'))
        expect += 1
    return b''.join(chunks).decode(codec(charset), 'replace')
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short1']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a1'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a1b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b1'], ['filename*01', 'X']],), 'ab1'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_1.txt']],), 'café_1.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine1']],), "it'sfine1"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve1.txt"], ['filename*0', 'seg']],), 'naïve1.txt'), ('orphan continuation', ([['filename', 'plain1.txt'], ['filename*1', 'orphan']],), 'plain1.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per1']],), 'upper1')], 2: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short2']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a2'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a2b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b2'], ['filename*01', 'X']],), 'ab2'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_2.txt']],), 'café_2.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine2']],), "it'sfine2"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve2.txt"], ['filename*0', 'seg']],), 'naïve2.txt'), ('orphan continuation', ([['filename', 'plain2.txt'], ['filename*1', 'orphan']],), 'plain2.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per2']],), 'upper2')], 3: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short3']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a3'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a3b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b3'], ['filename*01', 'X']],), 'ab3'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_3.txt']],), 'café_3.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine3']],), "it'sfine3"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve3.txt"], ['filename*0', 'seg']],), 'naïve3.txt'), ('orphan continuation', ([['filename', 'plain3.txt'], ['filename*1', 'orphan']],), 'plain3.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per3']],), 'upper3')], 4: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short4']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a4'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a4b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b4'], ['filename*01', 'X']],), 'ab4'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_4.txt']],), 'café_4.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine4']],), "it'sfine4"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve4.txt"], ['filename*0', 'seg']],), 'naïve4.txt'), ('orphan continuation', ([['filename', 'plain4.txt'], ['filename*1', 'orphan']],), 'plain4.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per4']],), 'upper4')], 5: [('twelve segments', ([['filename*0', 'p0.'], ['filename*1', 'p1.'], ['filename*10', 'p10.'], ['filename*11', 'p11.'], ['filename*2', 'p2.'], ['filename*3', 'p3.'], ['filename*4', 'p4.'], ['filename*5', 'p5.'], ['filename*6', 'p6.'], ['filename*7', 'p7.'], ['filename*8', 'p8.'], ['filename*9', 'p9.'], ['filename', 'short5']],), 'p0.p1.p2.p3.p4.p5.p6.p7.p8.p9.p10.p11.'), ('gap in segments', ([['filename*0', 'a5'], ['filename*1', 'b'], ['filename*3', 'd']],), 'a5b'), ('leading zero index', ([['filename*0', 'a'], ['filename*1', 'b5'], ['filename*01', 'X']],), 'ab5'), ('charset only on first segment', ([['filename*0*', "utf-8''caf%C3"], ['filename*1*', '%A9_5.txt']],), 'café_5.txt'), ('quote inside plain first segment', ([['filename*0', "it's"], ['filename*1', 'fine5']],), "it'sfine5"), ('single extended value wins', ([['filename', 'plain.txt'], ['filename*', "utf-8''na%C3%AFve5.txt"], ['filename*0', 'seg']],), 'naïve5.txt'), ('orphan continuation', ([['filename', 'plain5.txt'], ['filename*1', 'orphan']],), 'plain5.txt'), ('upper-case names', ([['FileName*0', 'up'], ['FILENAME*1', 'per5']],), 'upper5')]}
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:
Continuations written with capital letters are ignored and the attachment loses its name.

params are [name, value] pairs; names are case-insensitive. "filename*" (a single extended value charset'lang'text) wins over everything. Otherwise segments "filename*<n>" (plain) and "filename*<n>*" (percent-encoded) with decimal n without leading zeros ("0" allowed) are joined in numeric order from 0, stopping at the first gap; only an encoded segment 0 carries the charset'lang' prefix. Bytes of all segments are concatenated before decoding with the charset (utf-8, iso-8859-1 or us-ascii; others decode as utf-8; undecodable bytes become U+FFFD). Without a segment 0 the plain "filename" value (or None) is returned.

split: open-access


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

N = 1
observations = []
def solve(report):
    t = report['type'].lower()
    params = {k.lower(): v for k, v in report.get('params', {}).items()}
    rt = params.get('report-type', '').lower()
    parts = report.get('parts', [])
    if t != 'multipart/report' or rt != 'delivery-status' or len(parts) < 2 or parts[1]['type'].lower() not in ('message/delivery-status',):
        return {'valid': False, 'recipients': [], 'has_original': False}
    rows = []
    for r in parts[1]['recipients']:
        action = r['action'].strip().lower()
        klass = r['status'].strip()[:1]
        if action == 'failed':
            verdict = 'hard' if klass == '5' else ('soft' if klass == '4' else 'unknown')
        elif action == 'delayed':
            verdict = 'delayed'
        elif action in ('delivered', 'relayed', 'expanded'):
            verdict = 'ok'
        else:
            verdict = 'unknown'
        rows.append([r['final'].strip(), verdict])
    orig = len(parts) > 2 and parts[2]['type'].lower() in ('message/rfc822', 'text/rfc822-headers')
    return {'valid': True, 'recipients': rows, 'has_original': orig}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a1@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a1@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.1'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd1@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d1@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.1'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.1'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 2: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a2@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a2@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.2'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd2@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d2@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.2'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.2'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 3: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a3@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a3@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.3'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd3@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d3@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.3'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.3'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 4: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a4@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a4@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.4'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd4@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d4@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.4'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.4'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 5: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a5@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a5@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.5'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd5@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d5@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.5'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.5'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})]}
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(report):
    t = report['type'].lower()
    params = {k.lower(): v for k, v in report.get('params', {}).items()}
    rt = params.get('report-type', '').lower()
    parts = report.get('parts', [])
    if t != 'multipart/report' or rt != 'delivery-status' or len(parts) < 2 or parts[1]['type'].lower() not in ('message/delivery-status', 'message/global', 'message/global-delivery-status'):
        return {'valid': False, 'recipients': [], 'has_original': False}
    rows = []
    for r in parts[1]['recipients']:
        action = r['action'].strip().lower()
        klass = r['status'].strip()[:1]
        if action == 'failed':
            verdict = 'hard' if klass == '5' else ('soft' if klass == '4' else 'unknown')
        elif action == 'delayed':
            verdict = 'delayed'
        elif action in ('delivered', 'relayed', 'expanded'):
            verdict = 'ok'
        else:
            verdict = 'unknown'
        rows.append([r['final'].strip(), verdict])
    orig = len(parts) > 2 and parts[2]['type'].lower() in ('message/rfc822', 'text/rfc822-headers')
    return {'valid': True, 'recipients': rows, 'has_original': orig}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a1@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a1@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.1'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd1@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d1@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.1'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.1'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 2: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a2@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a2@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.2'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd2@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d2@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.2'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.2'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 3: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a3@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a3@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.3'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd3@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d3@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.3'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.3'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 4: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a4@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a4@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.4'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd4@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d4@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.4'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.4'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 5: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a5@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a5@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.5'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd5@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d5@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.5'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.5'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})]}
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:
Bounces for internationalised addresses are ignored as invalid reports.

A report is valid only when its type is multipart/report, its report-type parameter (name and value case-insensitive) is delivery-status, and its second part is message/delivery-status or message/global-delivery-status. Each recipient {final, action, status} gets: failed -> hard for status class 5, soft for 4, unknown otherwise; delayed -> delayed; delivered, relayed or expanded -> ok; anything else unknown (actions are case-insensitive and trimmed). has_original is true when a third part is message/rfc822 or text/rfc822-headers. Invalid reports give {valid False, recipients [], has_original False}.

split: open-access