FA-76461 / Email MIME structure / Open access
Resolve and de-duplicate attachment file names: leading dot names · case 01
A dotfile copy becomes " (2).profile" instead of ".profile (2)".
ROOT CAUSE
The leading dot names decision evaluates `if dot` where the contract requires `if dot and stem`.
VERIFIED REPAIR
Use `if dot and stem` for the leading dot names decision and keep every other rule of the model unchanged.
Unsuccessful approach: Requiring a stem longer than one character mishandles one-letter names such as a.pdf. The attempted `if dot and len(stem) > 1` still disagrees with a fixture.
Case contract
Each part has optional fstar (already decoded filename*), fname (filename) and ctname (Content-Type name) and a type. Precedence is fstar, fname, ctname. Directory components are removed (both "/" and "\" separate). An empty name, "." or ".." becomes "attachment" plus an extension by type (.pdf, .png, .txt, else .bin). Names are de-duplicated case-insensitively: the k-th copy gets " (k)" before the last extension, or at the end when there is no extension or the name starts with its only dot.
Why this case matters
Attachment names end up on disk; wrong names overwrite files, leak paths or confuse users.
1 / The failure
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
N = 1
observations = []
def solve(parts):
exts = {'application/pdf': '.pdf', 'image/png': '.png', 'text/plain': '.txt'}
used = {}
out = []
for p in parts:
raw = p.get('fstar') or p.get('fname') or p.get('ctname') or ''
base = raw.replace('\\', '/').split('/')[-1]
if base in ('', '.', '..'):
base = 'attachment' + exts.get(p['type'], '.bin')
key = base.lower()
used[key] = used.get(key, 0) + 1
if used[key] > 1:
stem, dot, ext = base.rpartition('.')
base = (stem + ' (%d).' % used[key] + ext) if dot else base + ' (%d)' % used[key]
out.append(base)
return out
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('extended name wins', ([{'fstar': 'résumé1.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé1.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report1.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report1.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report1.PDF', 'type': 'application/pdf'}, {'ctname': 'report1.pdf', 'type': 'application/pdf'}],), ['Report1.PDF', 'report1 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}],), ['x1.png', 'x1 (2).png', 'x1 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 2: [('extended name wins', ([{'fstar': 'résumé2.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé2.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report2.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report2.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report2.PDF', 'type': 'application/pdf'}, {'ctname': 'report2.pdf', 'type': 'application/pdf'}],), ['Report2.PDF', 'report2 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}],), ['x2.png', 'x2 (2).png', 'x2 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 3: [('extended name wins', ([{'fstar': 'résumé3.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé3.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report3.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report3.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report3.PDF', 'type': 'application/pdf'}, {'ctname': 'report3.pdf', 'type': 'application/pdf'}],), ['Report3.PDF', 'report3 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}],), ['x3.png', 'x3 (2).png', 'x3 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 4: [('extended name wins', ([{'fstar': 'résumé4.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé4.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report4.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report4.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report4.PDF', 'type': 'application/pdf'}, {'ctname': 'report4.pdf', 'type': 'application/pdf'}],), ['Report4.PDF', 'report4 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}],), ['x4.png', 'x4 (2).png', 'x4 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 5: [('extended name wins', ([{'fstar': 'résumé5.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé5.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report5.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report5.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report5.PDF', 'type': 'application/pdf'}, {'ctname': 'report5.pdf', 'type': 'application/pdf'}],), ['Report5.PDF', 'report5 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}],), ['x5.png', 'x5 (2).png', 'x5 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])]}
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)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| extended name wins | ['résumé1.pdf'] | ['résumé1.pdf'] | Passed |
| windows path | ['report1.docx', 'passwd'] | ['report1.docx', 'passwd'] | Passed |
| dot names | ['attachment.png', 'attachment.txt'] | ['attachment.png', 'attachment.txt'] | Passed |
| case-insensitive duplicates | ['Report1.PDF', 'report1 (2).pdf'] | ['Report1.PDF', 'report1 (2).pdf'] | Passed |
| multi-dot duplicate | ['archive.tar.gz', 'archive.tar (2).gz'] | ['archive.tar.gz', 'archive.tar (2).gz'] | Passed |
| dotfile and short stem | ['.profile', ' (2).profile', 'a.pdf', 'a (2).pdf'] | ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf'] | Failed |
| third copy | ['x1.png', 'x1 (2).png', 'x1 (3).png'] | ['x1.png', 'x1 (2).png', 'x1 (3).png'] | Passed |
| no name at all | ['attachment.bin', 'attachment.pdf'] | ['attachment.bin', 'attachment.pdf'] | Passed |
SHA-256 / 29d3d05d04d460f8851ecdd2ae1aa9a3bebed9a757ccdb7c73ce330ba6d85e5b
2 / The unsuccessful fix
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
N = 1
observations = []
def solve(parts):
exts = {'application/pdf': '.pdf', 'image/png': '.png', 'text/plain': '.txt'}
used = {}
out = []
for p in parts:
raw = p.get('fstar') or p.get('fname') or p.get('ctname') or ''
base = raw.replace('\\', '/').split('/')[-1]
if base in ('', '.', '..'):
base = 'attachment' + exts.get(p['type'], '.bin')
key = base.lower()
used[key] = used.get(key, 0) + 1
if used[key] > 1:
stem, dot, ext = base.rpartition('.')
base = (stem + ' (%d).' % used[key] + ext) if dot and len(stem) > 1 else base + ' (%d)' % used[key]
out.append(base)
return out
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('extended name wins', ([{'fstar': 'résumé1.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé1.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report1.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report1.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report1.PDF', 'type': 'application/pdf'}, {'ctname': 'report1.pdf', 'type': 'application/pdf'}],), ['Report1.PDF', 'report1 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}],), ['x1.png', 'x1 (2).png', 'x1 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 2: [('extended name wins', ([{'fstar': 'résumé2.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé2.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report2.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report2.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report2.PDF', 'type': 'application/pdf'}, {'ctname': 'report2.pdf', 'type': 'application/pdf'}],), ['Report2.PDF', 'report2 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}],), ['x2.png', 'x2 (2).png', 'x2 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 3: [('extended name wins', ([{'fstar': 'résumé3.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé3.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report3.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report3.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report3.PDF', 'type': 'application/pdf'}, {'ctname': 'report3.pdf', 'type': 'application/pdf'}],), ['Report3.PDF', 'report3 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}],), ['x3.png', 'x3 (2).png', 'x3 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 4: [('extended name wins', ([{'fstar': 'résumé4.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé4.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report4.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report4.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report4.PDF', 'type': 'application/pdf'}, {'ctname': 'report4.pdf', 'type': 'application/pdf'}],), ['Report4.PDF', 'report4 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}],), ['x4.png', 'x4 (2).png', 'x4 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 5: [('extended name wins', ([{'fstar': 'résumé5.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé5.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report5.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report5.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report5.PDF', 'type': 'application/pdf'}, {'ctname': 'report5.pdf', 'type': 'application/pdf'}],), ['Report5.PDF', 'report5 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}],), ['x5.png', 'x5 (2).png', 'x5 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])]}
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)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| extended name wins | ['résumé1.pdf'] | ['résumé1.pdf'] | Passed |
| windows path | ['report1.docx', 'passwd'] | ['report1.docx', 'passwd'] | Passed |
| dot names | ['attachment.png', 'attachment.txt'] | ['attachment.png', 'attachment.txt'] | Passed |
| case-insensitive duplicates | ['Report1.PDF', 'report1 (2).pdf'] | ['Report1.PDF', 'report1 (2).pdf'] | Passed |
| multi-dot duplicate | ['archive.tar.gz', 'archive.tar (2).gz'] | ['archive.tar.gz', 'archive.tar (2).gz'] | Passed |
| dotfile and short stem | ['.profile', '.profile (2)', 'a.pdf', 'a.pdf (2)'] | ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf'] | Failed |
| third copy | ['x1.png', 'x1 (2).png', 'x1 (3).png'] | ['x1.png', 'x1 (2).png', 'x1 (3).png'] | Passed |
| no name at all | ['attachment.bin', 'attachment.pdf'] | ['attachment.bin', 'attachment.pdf'] | Passed |
SHA-256 / 0b5b9840511dad24e7c0a518910822e058910e3ad24089fa17776fc238ae2692
3 / The verified repair
Exit 0"""Failure Map reference implementation. Python standard library only."""
import json
N = 1
observations = []
def solve(parts):
exts = {'application/pdf': '.pdf', 'image/png': '.png', 'text/plain': '.txt'}
used = {}
out = []
for p in parts:
raw = p.get('fstar') or p.get('fname') or p.get('ctname') or ''
base = raw.replace('\\', '/').split('/')[-1]
if base in ('', '.', '..'):
base = 'attachment' + exts.get(p['type'], '.bin')
key = base.lower()
used[key] = used.get(key, 0) + 1
if used[key] > 1:
stem, dot, ext = base.rpartition('.')
base = (stem + ' (%d).' % used[key] + ext) if dot and stem else base + ' (%d)' % used[key]
out.append(base)
return out
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('extended name wins', ([{'fstar': 'résumé1.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé1.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report1.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report1.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report1.PDF', 'type': 'application/pdf'}, {'ctname': 'report1.pdf', 'type': 'application/pdf'}],), ['Report1.PDF', 'report1 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}],), ['x1.png', 'x1 (2).png', 'x1 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 2: [('extended name wins', ([{'fstar': 'résumé2.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé2.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report2.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report2.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report2.PDF', 'type': 'application/pdf'}, {'ctname': 'report2.pdf', 'type': 'application/pdf'}],), ['Report2.PDF', 'report2 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}],), ['x2.png', 'x2 (2).png', 'x2 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 3: [('extended name wins', ([{'fstar': 'résumé3.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé3.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report3.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report3.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report3.PDF', 'type': 'application/pdf'}, {'ctname': 'report3.pdf', 'type': 'application/pdf'}],), ['Report3.PDF', 'report3 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}],), ['x3.png', 'x3 (2).png', 'x3 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 4: [('extended name wins', ([{'fstar': 'résumé4.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé4.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report4.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report4.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report4.PDF', 'type': 'application/pdf'}, {'ctname': 'report4.pdf', 'type': 'application/pdf'}],), ['Report4.PDF', 'report4 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}],), ['x4.png', 'x4 (2).png', 'x4 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 5: [('extended name wins', ([{'fstar': 'résumé5.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé5.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report5.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report5.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report5.PDF', 'type': 'application/pdf'}, {'ctname': 'report5.pdf', 'type': 'application/pdf'}],), ['Report5.PDF', 'report5 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}],), ['x5.png', 'x5 (2).png', 'x5 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])]}
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)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| extended name wins | ['résumé1.pdf'] | ['résumé1.pdf'] | Passed |
| windows path | ['report1.docx', 'passwd'] | ['report1.docx', 'passwd'] | Passed |
| dot names | ['attachment.png', 'attachment.txt'] | ['attachment.png', 'attachment.txt'] | Passed |
| case-insensitive duplicates | ['Report1.PDF', 'report1 (2).pdf'] | ['Report1.PDF', 'report1 (2).pdf'] | Passed |
| multi-dot duplicate | ['archive.tar.gz', 'archive.tar (2).gz'] | ['archive.tar.gz', 'archive.tar (2).gz'] | Passed |
| dotfile and short stem | ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf'] | ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf'] | Passed |
| third copy | ['x1.png', 'x1 (2).png', 'x1 (3).png'] | ['x1.png', 'x1 (2).png', 'x1 (3).png'] | Passed |
| no name at all | ['attachment.bin', 'attachment.pdf'] | ['attachment.bin', 'attachment.pdf'] | Passed |
SHA-256 / 1d6c1d8f7b811741c661df82a23e61f0bc8447645eee6b8ea7e98117a34e1da9
Verification & scope
Stipulated offline model over pre-parsed MIME dictionaries; not a conforming MIME parser, generator or mail client. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.
Observations recorded using Python 3.12.14 at 2026-09-29T14:49:16.552420+00:00.
Case digest / a16b144f416e47c353759484848d14b4eaae73c76ef3f07bae6f0f29c793d23c