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

N = 1
observations = []
def solve(d):
    old=d['old']; new=d['new']
    index=old.index(d['anchor']) if d['anchor'] in old else 0
    candidates=[d['anchor']]+old[index+1:]+list(reversed(old[:index]))
    anchor=next((x for x in candidates if x in new),(new[0] if new else None))
    position=sum(d['heights'][x] for x in new[:new.index(anchor)]) if anchor is not None else 0
    inside=d['offset'] if anchor==d['anchor'] else 0
    requested=position+max(0,inside)
    content=sum(d['heights'][x] for x in new)
    scroll=max(0,min(requested,content-d['viewport']))
    active=d['active']
    return [anchor,scroll,content,active]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 64.5, 100, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 40, 100, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 76, 100, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 69, 'a'])], 2: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 65.5, 102, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 42, 102, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 78, 102, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 70, 'a'])], 3: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 66.5, 104, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 44, 104, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 80, 104, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 71, 'a'])], 4: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 67.5, 106, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 46, 106, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 82, 106, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 72, 'a'])], 5: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 68.5, 108, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 48, 108, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 84, 108, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 73, 'a'])]}
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):
    old=d['old']; new=d['new']
    index=old.index(d['anchor']) if d['anchor'] in old else 0
    candidates=[d['anchor']]+old[index+1:]+list(reversed(old[:index]))
    anchor=next((x for x in candidates if x in new),(new[0] if new else None))
    position=sum(d['heights'][x] for x in new[:new.index(anchor)]) if anchor is not None else 0
    inside=min(d['offset'],d['heights'][anchor]) if anchor==d['anchor'] else 0
    requested=position+max(0,inside)
    content=sum(d['heights'][x] for x in new)
    scroll=max(0,min(requested,content-d['viewport']))
    active=d['active']
    return [anchor,scroll,content,active]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 64.5, 100, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 40, 100, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 76, 100, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 69, 'a'])], 2: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 65.5, 102, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 42, 102, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 78, 102, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 70, 'a'])], 3: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 66.5, 104, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 44, 104, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 80, 104, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 71, 'a'])], 4: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 67.5, 106, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 46, 106, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 82, 106, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 72, 'a'])], 5: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 68.5, 108, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 48, 108, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 84, 108, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 73, 'a'])]}
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:
Anchor relative offset is reused for a replacement row produces the wrong hierarchical widget state in a regression fixture.

After branch expansion changes row heights/order, preserve the first visible surviving anchor and its pixel offset. If that row is removed, use the next surviving old row, then previous row. Clamp scroll to content extent, include fractional row offsets, and keep active-row identity independent from viewport anchoring.

split: open-access


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

N = 1
observations = []
def solve(d):
    old=d['old']; new=d['new']
    index=old.index(d['anchor']) if d['anchor'] in old else 0
    candidates=[d['anchor']]+old[index+1:]+list(reversed(old[:index]))
    anchor=next((x for x in candidates if x in new),(new[0] if new else None))
    position=sum(d['heights'][x] for x in new[:new.index(anchor)]) if anchor is not None else 0
    inside=min(d['offset'],d['heights'][anchor]-1) if anchor==d['anchor'] else 0
    requested=position-max(0,inside)
    content=sum(d['heights'][x] for x in new)
    scroll=max(0,min(requested,content-d['viewport']))
    active=d['active']
    return [anchor,scroll,content,active]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 64.5, 100, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 40, 100, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 76, 100, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 69, 'a'])], 2: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 65.5, 102, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 42, 102, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 78, 102, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 70, 'a'])], 3: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 66.5, 104, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 44, 104, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 80, 104, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 71, 'a'])], 4: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 67.5, 106, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 46, 106, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 82, 106, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 72, 'a'])], 5: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 68.5, 108, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 48, 108, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 84, 108, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 73, 'a'])]}
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):
    old=d['old']; new=d['new']
    index=old.index(d['anchor']) if d['anchor'] in old else 0
    candidates=[d['anchor']]+old[index+1:]+list(reversed(old[:index]))
    anchor=next((x for x in candidates if x in new),(new[0] if new else None))
    position=sum(d['heights'][x] for x in new[:new.index(anchor)]) if anchor is not None else 0
    inside=min(d['offset'],d['heights'][anchor]-1) if anchor==d['anchor'] else 0
    requested=position+int(max(0,inside))
    content=sum(d['heights'][x] for x in new)
    scroll=max(0,min(requested,content-d['viewport']))
    active=d['active']
    return [anchor,scroll,content,active]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 64.5, 100, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 40, 100, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 76, 100, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 69, 'a'])], 2: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 65.5, 102, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 42, 102, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 78, 102, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 70, 'a'])], 3: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 66.5, 104, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 44, 104, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 80, 104, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 71, 'a'])], 4: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 67.5, 106, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 46, 106, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 82, 106, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 72, 'a'])], 5: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 68.5, 108, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 48, 108, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 84, 108, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 73, 'a'])]}
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:
Anchor offset sign or fractional precision is lost produces the wrong hierarchical widget state in a regression fixture.

After branch expansion changes row heights/order, preserve the first visible surviving anchor and its pixel offset. If that row is removed, use the next surviving old row, then previous row. Clamp scroll to content extent, include fractional row offsets, and keep active-row identity independent from viewport anchoring.

split: open-access


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

N = 1
observations = []
def solve(d):
    old=d['old']; new=d['new']
    index=old.index(d['anchor']) if d['anchor'] in old else 0
    candidates=[d['anchor']]+old[index+1:]+list(reversed(old[:index]))
    anchor=next((x for x in candidates if x in new),(new[0] if new else None))
    position=sum(d['heights'][x] for x in new[:new.index(anchor)]) if anchor is not None else 0
    inside=min(d['offset'],d['heights'][anchor]-1) if anchor==d['anchor'] else 0
    requested=position+max(0,inside)
    content=sum(d['heights'][x] for x in old)
    scroll=max(0,min(requested,content-d['viewport']))
    active=d['active']
    return [anchor,scroll,content,active]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 64.5, 100, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 40, 100, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 76, 100, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 69, 'a'])], 2: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 65.5, 102, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 42, 102, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 78, 102, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 70, 'a'])], 3: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 66.5, 104, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 44, 104, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 80, 104, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 71, 'a'])], 4: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 67.5, 106, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 46, 106, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 82, 106, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 72, 'a'])], 5: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 68.5, 108, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 48, 108, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 84, 108, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 73, 'a'])]}
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):
    old=d['old']; new=d['new']
    index=old.index(d['anchor']) if d['anchor'] in old else 0
    candidates=[d['anchor']]+old[index+1:]+list(reversed(old[:index]))
    anchor=next((x for x in candidates if x in new),(new[0] if new else None))
    position=sum(d['heights'][x] for x in new[:new.index(anchor)]) if anchor is not None else 0
    inside=min(d['offset'],d['heights'][anchor]-1) if anchor==d['anchor'] else 0
    requested=position+max(0,inside)
    content=len(new)*d['heights'].get(new[0],0) if new else 0
    scroll=max(0,min(requested,content-d['viewport']))
    active=d['active']
    return [anchor,scroll,content,active]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 64.5, 100, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 40, 100, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 76, 100, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 69, 'a'])], 2: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 65.5, 102, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 42, 102, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 78, 102, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 70, 'a'])], 3: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 66.5, 104, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 44, 104, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 80, 104, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 71, 'a'])], 4: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 67.5, 106, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 46, 106, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 82, 106, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 72, 'a'])], 5: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 68.5, 108, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 48, 108, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 84, 108, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 73, 'a'])]}
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:
Tree scroll extent assumes stale or uniform row heights produces the wrong hierarchical widget state in a regression fixture.

After branch expansion changes row heights/order, preserve the first visible surviving anchor and its pixel offset. If that row is removed, use the next surviving old row, then previous row. Clamp scroll to content extent, include fractional row offsets, and keep active-row identity independent from viewport anchoring.

split: open-access


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

N = 1
observations = []
def solve(d):
    old=d['old']; new=d['new']
    index=old.index(d['anchor']) if d['anchor'] in old else 0
    candidates=[d['anchor']]+old[index+1:]+list(reversed(old[:index]))
    anchor=next((x for x in candidates if x in new),(new[0] if new else None))
    position=sum(d['heights'][x] for x in new[:new.index(anchor)]) if anchor is not None else 0
    inside=min(d['offset'],d['heights'][anchor]-1) if anchor==d['anchor'] else 0
    requested=position+max(0,inside)
    content=sum(d['heights'][x] for x in new)
    scroll=max(0,requested)
    active=d['active']
    return [anchor,scroll,content,active]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 64.5, 100, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 40, 100, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 76, 100, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 69, 'a'])], 2: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 65.5, 102, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 42, 102, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 78, 102, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 70, 'a'])], 3: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 66.5, 104, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 44, 104, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 80, 104, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 71, 'a'])], 4: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 67.5, 106, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 46, 106, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 82, 106, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 72, 'a'])], 5: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 68.5, 108, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 48, 108, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 84, 108, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 73, 'a'])]}
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):
    old=d['old']; new=d['new']
    index=old.index(d['anchor']) if d['anchor'] in old else 0
    candidates=[d['anchor']]+old[index+1:]+list(reversed(old[:index]))
    anchor=next((x for x in candidates if x in new),(new[0] if new else None))
    position=sum(d['heights'][x] for x in new[:new.index(anchor)]) if anchor is not None else 0
    inside=min(d['offset'],d['heights'][anchor]-1) if anchor==d['anchor'] else 0
    requested=position+max(0,inside)
    content=sum(d['heights'][x] for x in new)
    scroll=min(requested,content-d['viewport'])
    active=d['active']
    return [anchor,scroll,content,active]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 64.5, 100, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 40, 100, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 76, 100, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 16, 'c': 11, 'z': 12, 'x': 31, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 69, 'a'])], 2: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 65.5, 102, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 42, 102, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 78, 102, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 17, 'c': 11, 'z': 12, 'x': 32, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 70, 'a'])], 3: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 66.5, 104, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 44, 104, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 80, 104, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 18, 'c': 11, 'z': 12, 'x': 33, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 71, 'a'])], 4: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 67.5, 106, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 46, 106, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 82, 106, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 19, 'c': 11, 'z': 12, 'x': 34, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 72, 'a'])], 5: [('insert before anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 68.5, 108, 'a']), ('deleted anchor', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['c', 30, 53, 'a']), ('previous fallback', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'a'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['a', 10, 30, 'a']), ('new-only tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['q'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['q', 0, 5, 'a']), ('empty tree', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': [], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, [None, 0, 0, 'a']), ('bottom clamp', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 60, 'active': 'a'}, ['b', 48, 108, 'a']), ('oversized offset', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['r', 'x', 'a', 'b', 'c', 'z'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 100, 'viewport': 20, 'active': 'a'}, ['b', 84, 108, 'a']), ('reordered', {'old': ['r', 'a', 'b', 'c', 'z'], 'new': ['z', 'c', 'b', 'a', 'r'], 'anchor': 'b', 'heights': {'r': 10, 'a': 20, 'b': 20, 'c': 11, 'z': 12, 'x': 35, 'q': 5}, 'offset': 3.5, 'viewport': 20, 'active': 'a'}, ['b', 26.5, 73, 'a'])]}
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:
Anchor scroll fails bottom or empty-content clamp produces the wrong hierarchical widget state in a regression fixture.

After branch expansion changes row heights/order, preserve the first visible surviving anchor and its pixel offset. If that row is removed, use the next surviving old row, then previous row. Clamp scroll to content extent, include fractional row offsets, and keep active-row identity independent from viewport anchoring.

split: open-access