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

N = 1
observations = []
def solve(d):
    roots=[x for x in d['order'] if d['parents'].get(x)==d['parent']]
    key=lambda x:(d['labels'].get(x) is None,(d['labels'].get(x) or '').casefold())
    ordered=sorted(roots,key=key,reverse=d['descending'])
    blocks={r:[x for x in d['order'] if x==r or r in d['ancestors'].get(x,[])] for r in roots}
    closure={x for block in blocks.values() for x in block}
    first=min((d['order'].index(x) for x in closure),default=len(d['order']))
    prefix=[x for x in d['order'][:first] if x not in closure]
    suffix=[x for x in d['order'][first:] if x not in closure]
    result=prefix+[x for r in ordered for x in blocks[r]]+suffix
    positions={x:i+1 for i,x in enumerate(ordered)}
    return [result,positions,result[d['order'].index(d['focus'])] if d['focus'] in d['order'] and d['order'].index(d['focus'])<len(result) else None,d['selected'],d['expanded']]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('ascending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'a', 'z', 'zz', 'zzz', 'b', 'q'], {'a': 1, 'z': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']]), ('descending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': True, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('stable tie', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'SAME', 'a': 'same', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('case fold', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'Straße', 'a': 'STRASSE', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('empty siblings', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'absent', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {}, 'z', ['zzz'], ['r', 'z']]), ('empty label', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': '', 'a': 'X1', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {'z': 1, 'a': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']])], 2: [('ascending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'a', 'z', 'zz', 'zzz', 'b', 'q'], {'a': 1, 'z': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']]), ('descending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': True, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('stable tie', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'SAME', 'a': 'same', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('case fold', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'Straße', 'a': 'STRASSE', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('empty siblings', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'absent', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {}, 'z', ['zzz'], ['r', 'z']]), ('empty label', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': '', 'a': 'X2', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {'z': 1, 'a': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']])], 3: [('ascending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'a', 'z', 'zz', 'zzz', 'b', 'q'], {'a': 1, 'z': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']]), ('descending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': True, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('stable tie', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'SAME', 'a': 'same', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('case fold', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'Straße', 'a': 'STRASSE', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('empty siblings', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'absent', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {}, 'z', ['zzz'], ['r', 'z']]), ('empty label', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': '', 'a': 'X3', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {'z': 1, 'a': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']])], 4: [('ascending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'a', 'z', 'zz', 'zzz', 'b', 'q'], {'a': 1, 'z': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']]), ('descending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': True, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('stable tie', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'SAME', 'a': 'same', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('case fold', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'Straße', 'a': 'STRASSE', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('empty siblings', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'absent', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {}, 'z', ['zzz'], ['r', 'z']]), ('empty label', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': '', 'a': 'X4', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {'z': 1, 'a': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']])], 5: [('ascending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'a', 'z', 'zz', 'zzz', 'b', 'q'], {'a': 1, 'z': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']]), ('descending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': True, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('stable tie', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'SAME', 'a': 'same', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('case fold', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'Straße', 'a': 'STRASSE', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('empty siblings', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'absent', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {}, 'z', ['zzz'], ['r', 'z']]), ('empty label', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': '', 'a': 'X5', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {'z': 1, 'a': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']])]}
for label, data, expected in cases[N]:
    check(label, solve(data), 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(d):
    roots=[x for x in d['order'] if d['parents'].get(x)==d['parent']]
    key=lambda x:(d['labels'].get(x) is None,(d['labels'].get(x) or '').casefold())
    ordered=sorted(roots,key=key,reverse=d['descending'])
    blocks={r:[x for x in d['order'] if x==r or r in d['ancestors'].get(x,[])] for r in roots}
    closure={x for block in blocks.values() for x in block}
    first=min((d['order'].index(x) for x in closure),default=len(d['order']))
    prefix=[x for x in d['order'][:first] if x not in closure]
    suffix=[x for x in d['order'][first:] if x not in closure]
    result=prefix+[x for r in ordered for x in blocks[r]]+suffix
    positions={x:i+1 for i,x in enumerate(ordered)}
    return [result,positions,ordered[0] if ordered else None,d['selected'],d['expanded']]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('ascending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'a', 'z', 'zz', 'zzz', 'b', 'q'], {'a': 1, 'z': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']]), ('descending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': True, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('stable tie', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'SAME', 'a': 'same', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('case fold', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'Straße', 'a': 'STRASSE', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('empty siblings', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'absent', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {}, 'z', ['zzz'], ['r', 'z']]), ('empty label', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': '', 'a': 'X1', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {'z': 1, 'a': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']])], 2: [('ascending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'a', 'z', 'zz', 'zzz', 'b', 'q'], {'a': 1, 'z': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']]), ('descending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': True, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('stable tie', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'SAME', 'a': 'same', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('case fold', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'Straße', 'a': 'STRASSE', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('empty siblings', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'absent', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {}, 'z', ['zzz'], ['r', 'z']]), ('empty label', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': '', 'a': 'X2', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {'z': 1, 'a': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']])], 3: [('ascending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'a', 'z', 'zz', 'zzz', 'b', 'q'], {'a': 1, 'z': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']]), ('descending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': True, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('stable tie', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'SAME', 'a': 'same', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('case fold', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'Straße', 'a': 'STRASSE', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('empty siblings', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'absent', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {}, 'z', ['zzz'], ['r', 'z']]), ('empty label', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': '', 'a': 'X3', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {'z': 1, 'a': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']])], 4: [('ascending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'a', 'z', 'zz', 'zzz', 'b', 'q'], {'a': 1, 'z': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']]), ('descending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': True, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('stable tie', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'SAME', 'a': 'same', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('case fold', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'Straße', 'a': 'STRASSE', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('empty siblings', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'absent', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {}, 'z', ['zzz'], ['r', 'z']]), ('empty label', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': '', 'a': 'X4', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {'z': 1, 'a': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']])], 5: [('ascending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'a', 'z', 'zz', 'zzz', 'b', 'q'], {'a': 1, 'z': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']]), ('descending', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': True, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('stable tie', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'SAME', 'a': 'same', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('case fold', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'Straße', 'a': 'STRASSE', 'b': 'other'}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'b', 'z', 'zz', 'zzz', 'a', 'q'], {'b': 1, 'z': 2, 'a': 3}, 'z', ['zzz'], ['r', 'z']]), ('empty siblings', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'absent', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': 'beta', 'a': 'Alpha', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {}, 'z', ['zzz'], ['r', 'z']]), ('empty label', {'order': ['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], 'parents': {'outside': None, 'r': None, 'z': 'r', 'zz': 'z', 'zzz': 'zz', 'a': 'r', 'b': 'r', 'q': None}, 'parent': 'r', 'ancestors': {'z': ['r'], 'zz': ['r', 'z'], 'zzz': ['r', 'z', 'zz'], 'a': ['r'], 'b': ['r']}, 'labels': {'z': '', 'a': 'X5', 'b': None}, 'descending': False, 'focus': 'z', 'selected': ['zzz'], 'expanded': ['r', 'z']}, [['outside', 'r', 'z', 'zz', 'zzz', 'a', 'b', 'q'], {'z': 1, 'a': 2, 'b': 3}, 'z', ['zzz'], ['r', 'z']])]}
for label, data, expected in cases[N]:
    check(label, solve(data), 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:
Focus follows pre-sort index instead of row identity produces the wrong hierarchical widget state in a regression fixture.

Only direct children of the target parent are sorted by casefolded display label, with original order retained on ties; each child subtree moves as one block. Missing labels sort last in ascending order; descending reverses the complete comparison order. Focus, selection and expansion remain ID-based; sibling positions are recomputed from sorted roots.

split: open-access


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

N = 1
observations = []
def solve(d):
    locks={}
    if d['complete_node'] in locks and locks[d['complete_node']]==d['complete_token']: del locks[d['complete_node']]
    chain=d['ancestors'].get(d['target'],[])+[d['target']]
    owner=next((x for x in reversed(chain) if x in locks),None)
    mutating=d['command'] in ['rename','delete','move','check']
    allowed=d['target'] in d['rows'] and (not mutating or owner is None) and (not mutating or d['target'] not in d['readonly'])
    reason=('missing' if d['target'] not in d['rows'] else ('busy:'+owner if owner is not None and mutating else ('readonly' if mutating and d['target'] in d['readonly'] else None)))
    newlocks=dict(locks)
    if allowed and d['command'] in ['delete','move']: newlocks[d['target']]=d['new_token']
    busy=[x for x in d['rows'] if x in newlocks or any(a in newlocks for a in d['ancestors'].get(x,[]))]
    return [allowed,reason,newlocks,busy]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('nested lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('locked delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('inspection', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'inspect', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release nearest', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'B', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [False, 'busy:r', {'r': 'R', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('stale completion', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'old', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('self lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'b', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('free sibling delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q', 'v0': 'N1'}, ['r', 'b', 'c', 'q', 's', 'v0']]), ('protected free', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': ['v0'], 'new_token': 'N1'}, [False, 'readonly', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('missing target', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'missing', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [False, 'missing', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release and move', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'q', 'complete_token': 'Q', 'target': 'q', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'move', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'N1'}, ['r', 'b', 'c', 'q', 's']])], 2: [('nested lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('locked delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('inspection', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'inspect', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release nearest', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'B', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [False, 'busy:r', {'r': 'R', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('stale completion', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'old', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('self lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'b', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('free sibling delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q', 'v0': 'N2'}, ['r', 'b', 'c', 'q', 's', 'v0']]), ('protected free', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': ['v0'], 'new_token': 'N2'}, [False, 'readonly', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('missing target', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'missing', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [False, 'missing', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release and move', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'q', 'complete_token': 'Q', 'target': 'q', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'move', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'N2'}, ['r', 'b', 'c', 'q', 's']])], 3: [('nested lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('locked delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('inspection', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'inspect', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release nearest', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'B', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [False, 'busy:r', {'r': 'R', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('stale completion', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'old', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('self lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'b', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('free sibling delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q', 'v0': 'N3'}, ['r', 'b', 'c', 'q', 's', 'v0']]), ('protected free', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': ['v0'], 'new_token': 'N3'}, [False, 'readonly', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('missing target', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'missing', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [False, 'missing', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release and move', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'q', 'complete_token': 'Q', 'target': 'q', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'move', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'N3'}, ['r', 'b', 'c', 'q', 's']])], 4: [('nested lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('locked delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('inspection', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'inspect', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release nearest', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'B', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [False, 'busy:r', {'r': 'R', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('stale completion', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'old', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('self lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'b', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('free sibling delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q', 'v0': 'N4'}, ['r', 'b', 'c', 'q', 's', 'v0']]), ('protected free', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': ['v0'], 'new_token': 'N4'}, [False, 'readonly', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('missing target', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'missing', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [False, 'missing', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release and move', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'q', 'complete_token': 'Q', 'target': 'q', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'move', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'N4'}, ['r', 'b', 'c', 'q', 's']])], 5: [('nested lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('locked delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('inspection', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'inspect', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release nearest', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'B', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [False, 'busy:r', {'r': 'R', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('stale completion', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'old', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('self lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'b', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('free sibling delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q', 'v0': 'N5'}, ['r', 'b', 'c', 'q', 's', 'v0']]), ('protected free', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': ['v0'], 'new_token': 'N5'}, [False, 'readonly', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('missing target', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'missing', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [False, 'missing', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release and move', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'q', 'complete_token': 'Q', 'target': 'q', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'move', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'N5'}, ['r', 'b', 'c', 'q', 's']])]}
for label, data, expected in cases[N]:
    check(label, solve(data), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

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

N = 1
observations = []
def solve(d):
    locks={d['target']:d['locks'][d['target']]} if d['target'] in d['locks'] else {}
    if d['complete_node'] in locks and locks[d['complete_node']]==d['complete_token']: del locks[d['complete_node']]
    chain=d['ancestors'].get(d['target'],[])+[d['target']]
    owner=next((x for x in reversed(chain) if x in locks),None)
    mutating=d['command'] in ['rename','delete','move','check']
    allowed=d['target'] in d['rows'] and (not mutating or owner is None) and (not mutating or d['target'] not in d['readonly'])
    reason=('missing' if d['target'] not in d['rows'] else ('busy:'+owner if owner is not None and mutating else ('readonly' if mutating and d['target'] in d['readonly'] else None)))
    newlocks=dict(locks)
    if allowed and d['command'] in ['delete','move']: newlocks[d['target']]=d['new_token']
    busy=[x for x in d['rows'] if x in newlocks or any(a in newlocks for a in d['ancestors'].get(x,[]))]
    return [allowed,reason,newlocks,busy]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('nested lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('locked delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('inspection', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'inspect', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release nearest', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'B', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [False, 'busy:r', {'r': 'R', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('stale completion', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'old', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('self lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'b', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('free sibling delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q', 'v0': 'N1'}, ['r', 'b', 'c', 'q', 's', 'v0']]), ('protected free', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': ['v0'], 'new_token': 'N1'}, [False, 'readonly', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('missing target', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'missing', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [False, 'missing', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release and move', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'q', 'complete_token': 'Q', 'target': 'q', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'move', 'rows': ['r', 'b', 'c', 'q', 's', 'v0'], 'readonly': [], 'new_token': 'N1'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'N1'}, ['r', 'b', 'c', 'q', 's']])], 2: [('nested lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('locked delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('inspection', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'inspect', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release nearest', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'B', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [False, 'busy:r', {'r': 'R', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('stale completion', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'old', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('self lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'b', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('free sibling delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q', 'v0': 'N2'}, ['r', 'b', 'c', 'q', 's', 'v0']]), ('protected free', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': ['v0'], 'new_token': 'N2'}, [False, 'readonly', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('missing target', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'missing', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [False, 'missing', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release and move', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'q', 'complete_token': 'Q', 'target': 'q', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'move', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1'], 'readonly': [], 'new_token': 'N2'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'N2'}, ['r', 'b', 'c', 'q', 's']])], 3: [('nested lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('locked delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('inspection', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'inspect', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release nearest', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'B', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [False, 'busy:r', {'r': 'R', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('stale completion', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'old', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('self lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'b', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('free sibling delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q', 'v0': 'N3'}, ['r', 'b', 'c', 'q', 's', 'v0']]), ('protected free', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': ['v0'], 'new_token': 'N3'}, [False, 'readonly', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('missing target', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'missing', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [False, 'missing', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release and move', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'q', 'complete_token': 'Q', 'target': 'q', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'move', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2'], 'readonly': [], 'new_token': 'N3'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'N3'}, ['r', 'b', 'c', 'q', 's']])], 4: [('nested lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('locked delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('inspection', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'inspect', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release nearest', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'B', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [False, 'busy:r', {'r': 'R', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('stale completion', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'old', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('self lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'b', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('free sibling delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q', 'v0': 'N4'}, ['r', 'b', 'c', 'q', 's', 'v0']]), ('protected free', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': ['v0'], 'new_token': 'N4'}, [False, 'readonly', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('missing target', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'missing', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [False, 'missing', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release and move', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'q', 'complete_token': 'Q', 'target': 'q', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'move', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3'], 'readonly': [], 'new_token': 'N4'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'N4'}, ['r', 'b', 'c', 'q', 's']])], 5: [('nested lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('locked delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('inspection', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'inspect', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release nearest', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'B', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [False, 'busy:r', {'r': 'R', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('stale completion', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'b', 'complete_token': 'old', 'target': 'c', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('self lock', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'b', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [False, 'busy:b', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('free sibling delete', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'delete', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'Q', 'v0': 'N5'}, ['r', 'b', 'c', 'q', 's', 'v0']]), ('protected free', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'v0', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': ['v0'], 'new_token': 'N5'}, [False, 'readonly', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('missing target', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': None, 'complete_token': None, 'target': 'missing', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'rename', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [False, 'missing', {'r': 'R', 'b': 'B', 'q': 'Q'}, ['r', 'b', 'c', 'q', 's']]), ('release and move', {'locks': {'r': 'R', 'b': 'B', 'q': 'Q'}, 'complete_node': 'q', 'complete_token': 'Q', 'target': 'q', 'ancestors': {'b': ['r'], 'c': ['r', 'b'], 's': ['q']}, 'command': 'move', 'rows': ['r', 'b', 'c', 'q', 's', 'v0', 'v1', 'v2', 'v3', 'v4'], 'readonly': [], 'new_token': 'N5'}, [True, None, {'r': 'R', 'b': 'B', 'q': 'N5'}, ['r', 'b', 'c', 'q', 's']])]}
for label, data, expected in cases[N]:
    check(label, solve(data), 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:
Command lock lookup drops ancestor locks produces the wrong hierarchical widget state in a regression fixture.

A pending subtree mutation locks that branch and descendants, but does not lock siblings. Read-only inspection remains available. New commands use nearest locking ancestor for reason attribution; completed operation tokens release only their owned locks and never a replacement lock.

split: open-access