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

N = 1
observations = []
def solve(d):
    valid=d['saved_tree']==d['tree']
    wanted=set(d['saved_open']) if valid else set()
    branches=wanted & set(d['branches'])
    ancestors={a for x in branches for a in d['ancestors'].get(x,[])}
    expanded=branches|ancestors
    selected=[x for x in d['saved_selected'] if valid and x in d['present'] and x not in d['disabled']]
    visible=[x for x in d['present'] if all(a in expanded for a in d['ancestors'].get(x,[]))]
    candidate=d['saved_focus'] if valid else None
    fallback=next((a for a in reversed(d['ancestors'].get(candidate,[])) if a in visible),None)
    focus=candidate if candidate in visible else (fallback if fallback is not None else (visible[0] if visible else None))
    return [sorted(expanded),selected,visible,focus]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0'], 'leaf'])], 2: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1'], 'leaf'])], 3: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2'], 'leaf'])], 4: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3'], 'leaf'])], 5: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3', 'v4'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3', 'v4'], 'leaf'])]}
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):
    valid=d['saved_tree']==d['tree']
    wanted=set(d['saved_open']) if valid else set()
    branches=wanted & set(d['branches'])
    ancestors={a for x in branches for a in d['ancestors'].get(x,[])}
    expanded=branches-set(d['blocked'])
    selected=[x for x in d['saved_selected'] if valid and x in d['present'] and x not in d['disabled']]
    visible=[x for x in d['present'] if all(a in expanded for a in d['ancestors'].get(x,[]))]
    candidate=d['saved_focus'] if valid else None
    fallback=next((a for a in reversed(d['ancestors'].get(candidate,[])) if a in visible),None)
    focus=candidate if candidate in visible else (fallback if fallback is not None else (visible[0] if visible else None))
    return [sorted(expanded),selected,visible,focus]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0'], 'leaf'])], 2: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1'], 'leaf'])], 3: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2'], 'leaf'])], 4: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3'], 'leaf'])], 5: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3', 'v4'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3', 'v4'], 'leaf'])]}
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:
Blocked ancestor or required ancestry is ignored produces the wrong hierarchical widget state in a regression fixture.

Only present branch IDs may be restored. Ancestors of restored open branches are opened unless explicitly blocked. Selection and focus restore independently: selection keeps present enabled rows; hidden focus falls back to nearest visible ancestor. The persisted root instance must match.

split: open-access


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

N = 1
observations = []
def solve(d):
    valid=d['saved_tree']==d['tree']
    wanted=set(d['saved_open']) if valid else set()
    branches=wanted & set(d['branches'])
    ancestors={a for x in branches for a in d['ancestors'].get(x,[])}
    expanded=(branches|ancestors)-set(d['blocked'])
    selected=[x for x in d['saved_selected'] if valid and x in d['present']]
    visible=[x for x in d['present'] if all(a in expanded for a in d['ancestors'].get(x,[]))]
    candidate=d['saved_focus'] if valid else None
    fallback=next((a for a in reversed(d['ancestors'].get(candidate,[])) if a in visible),None)
    focus=candidate if candidate in visible else (fallback if fallback is not None else (visible[0] if visible else None))
    return [sorted(expanded),selected,visible,focus]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0'], 'leaf'])], 2: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1'], 'leaf'])], 3: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2'], 'leaf'])], 4: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3'], 'leaf'])], 5: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3', 'v4'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3', 'v4'], 'leaf'])]}
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):
    valid=d['saved_tree']==d['tree']
    wanted=set(d['saved_open']) if valid else set()
    branches=wanted & set(d['branches'])
    ancestors={a for x in branches for a in d['ancestors'].get(x,[])}
    expanded=(branches|ancestors)-set(d['blocked'])
    selected=[x for x in d['saved_selected'] if valid and x not in d['disabled']]
    visible=[x for x in d['present'] if all(a in expanded for a in d['ancestors'].get(x,[]))]
    candidate=d['saved_focus'] if valid else None
    fallback=next((a for a in reversed(d['ancestors'].get(candidate,[])) if a in visible),None)
    focus=candidate if candidate in visible else (fallback if fallback is not None else (visible[0] if visible else None))
    return [sorted(expanded),selected,visible,focus]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0'], 'leaf'])], 2: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1'], 'leaf'])], 3: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2'], 'leaf'])], 4: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3'], 'leaf'])], 5: [('deep saved', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('deep branch alone', {'saved_tree': 't', 'tree': 't', 'saved_open': ['b'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'leaf']), ('foreign snapshot', {'saved_tree': 'other', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'other'}, [[], [], ['r', 'other'], 'r']), ('blocked middle', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['b'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'r'], ['leaf'], ['r', 'a', 'b', 'other'], 'b']), ('blocked root', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': ['r'], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b'], ['leaf'], ['r', 'other'], 'r']), ('no saved branch', {'saved_tree': 't', 'tree': 't', 'saved_open': [], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [[], ['leaf'], ['r', 'other'], 'r']), ('missing focus', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf', 'gone', 'a'], 'present': ['r', 'a', 'b', 'leaf', 'other'], 'disabled': ['a'], 'saved_focus': 'gone'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other'], 'r']), ('deep varying chain', {'saved_tree': 't', 'tree': 't', 'saved_open': ['a', 'b', 'leaf', 'gone'], 'branches': ['r', 'a', 'b'], 'ancestors': {'r': [], 'a': ['r'], 'b': ['r', 'a'], 'leaf': ['r', 'a', 'b'], 'other': []}, 'blocked': [], 'saved_selected': ['leaf'], 'present': ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3', 'v4'], 'disabled': ['a'], 'saved_focus': 'leaf'}, [['a', 'b', 'r'], ['leaf'], ['r', 'a', 'b', 'leaf', 'other', 'v0', 'v1', 'v2', 'v3', 'v4'], 'leaf'])]}
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:
Restored selection retains disabled or missing rows produces the wrong hierarchical widget state in a regression fixture.

Only present branch IDs may be restored. Ancestors of restored open branches are opened unless explicitly blocked. Selection and focus restore independently: selection keeps present enabled rows; hidden focus falls back to nearest visible ancestor. The persisted root instance must match.

split: open-access