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

N = 1
observations = []
def solve(s):
    return {
        'order': list(reversed([x for x in s['children'] if x not in s['borrowed']])),
        'borrowed': [x for x in s['children'] if x in s['borrowed']],
        'errors': list(s['errors']),
        'adoption': s['adopt'] and not s['closed'] and not s['closing'],
        'parent_release': [r for r in s['ownership_edges'] if r[1]!=s['scope_id']],
        'closed': max((s['phase'],s['requested_phase']),key=['open','closing','closed'].index),
        'pending': 0 if s['closing'] or s['closed'] else s['pending'],
        'remaining': len([x for x in s['children'] if x not in s['borrowed']]),
        'continue': len(s['children']),
        'parent_refs': max(0,s['refs']-1),
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'children': ['a', 'b', 'c'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': False, 'adopt': True, 'pending': 3, 'refs': 2, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'b'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 2}), ({'children': ['a'], 'borrowed': ['b'], 'errors': ['first'], 'closed': True, 'closing': False, 'parent': True, 'adopt': True, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'closing'}, {'order': ['a'], 'borrowed': [], 'errors': ['first'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 1, 'continue': 1, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'closed': True, 'closing': True, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 2, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': ['c', 'b'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 2}), ({'children': ['a'], 'borrowed': [], 'errors': ['first', 'second'], 'closed': True, 'closing': False, 'parent': True, 'adopt': True, 'pending': 3, 'refs': 2, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'p', 'phase': 'closing', 'requested_phase': 'closing'}, {'order': ['a'], 'borrowed': [], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 1, 'continue': 1, 'parent_refs': 1}), ({'children': [], 'borrowed': ['a'], 'errors': ['first'], 'closed': False, 'closing': False, 'parent': False, 'adopt': True, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': [], 'borrowed': [], 'errors': ['first'], 'adoption': True, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': [], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'closing'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': [], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 0}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': False, 'closing': False, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'open', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 3, 'refs': 2, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 2})], 2: [({'children': ['a', 'b', 'c'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': False, 'adopt': True, 'pending': 6, 'refs': 4, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': ['c', 'b'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 4}), ({'children': [], 'borrowed': ['a'], 'errors': [], 'closed': True, 'closing': False, 'parent': True, 'adopt': True, 'pending': 6, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'q', 'phase': 'open', 'requested_phase': 'closing'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'closing', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a'], 'borrowed': [], 'errors': ['first'], 'closed': False, 'closing': False, 'parent': True, 'adopt': False, 'pending': 6, 'refs': 4, 'already_released': True, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'closed'}, {'order': ['a'], 'borrowed': [], 'errors': ['first'], 'adoption': False, 'parent_release': [], 'closed': 'closed', 'pending': 6, 'remaining': 1, 'continue': 1, 'parent_refs': 4}), ({'children': ['a'], 'borrowed': [], 'errors': ['first'], 'closed': False, 'closing': True, 'parent': False, 'adopt': False, 'pending': 6, 'refs': 4, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closing', 'requested_phase': 'open'}, {'order': ['a'], 'borrowed': [], 'errors': ['first'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 1, 'continue': 1, 'parent_refs': 4}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': [], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 6, 'refs': 0, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': [], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': [], 'errors': [], 'closed': True, 'closing': True, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 4, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closing', 'requested_phase': 'closing'}, {'order': ['c', 'b', 'a'], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 3, 'continue': 3, 'parent_refs': 4}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': False, 'closing': False, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'open', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 6, 'refs': 4, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 4})], 3: [({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': False, 'adopt': True, 'pending': 9, 'refs': 6, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 6}), ({'children': ['a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': False, 'parent': True, 'adopt': True, 'pending': 9, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': ['a'], 'borrowed': [], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 1, 'continue': 1, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': [], 'closed': False, 'closing': True, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'open', 'requested_phase': 'closing'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'closing', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': [], 'closed': True, 'closing': False, 'parent': True, 'adopt': False, 'pending': 0, 'refs': 6, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': [], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 5}), ({'children': ['a'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': False, 'adopt': True, 'pending': 0, 'refs': 0, 'already_released': True, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'open'}, {'order': [], 'borrowed': ['a'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [], 'closed': 'open', 'pending': 0, 'remaining': 0, 'continue': 1, 'parent_refs': 0}), ({'children': [], 'borrowed': ['b'], 'errors': [], 'closed': False, 'closing': True, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 6, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 6}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': False, 'closing': False, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'open', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 9, 'refs': 6, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 6})], 4: [({'children': ['a', 'b', 'c'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': True, 'adopt': False, 'pending': 12, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closing', 'requested_phase': 'closing'}, {'order': ['c', 'b'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 0}), ({'children': ['a'], 'borrowed': ['b'], 'errors': ['first'], 'closed': False, 'closing': True, 'parent': False, 'adopt': True, 'pending': 12, 'refs': 8, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': ['a'], 'borrowed': [], 'errors': ['first'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 1, 'continue': 1, 'parent_refs': 8}), ({'children': ['a', 'b', 'c'], 'borrowed': ['a'], 'errors': ['first'], 'closed': True, 'closing': False, 'parent': False, 'adopt': True, 'pending': 0, 'refs': 0, 'already_released': True, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'q', 'phase': 'closing', 'requested_phase': 'closing'}, {'order': ['c', 'b'], 'borrowed': ['a'], 'errors': ['first'], 'adoption': False, 'parent_release': [], 'closed': 'closing', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 0}), ({'children': [], 'borrowed': ['a'], 'errors': ['first'], 'closed': True, 'closing': True, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closing', 'requested_phase': 'closing'}, {'order': [], 'borrowed': [], 'errors': ['first'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': [], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': False, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 8, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 8}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': False, 'closing': False, 'parent': False, 'adopt': True, 'pending': 0, 'refs': 0, 'already_released': True, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'q', 'phase': 'closing', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': True, 'parent_release': [], 'closed': 'closing', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': False, 'closing': False, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'open', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 12, 'refs': 8, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 8})], 5: [({'children': ['a', 'b', 'c'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': True, 'adopt': True, 'pending': 15, 'refs': 10, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'q', 'phase': 'open', 'requested_phase': 'closing'}, {'order': ['c', 'b'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 9}), ({'children': [], 'borrowed': ['b'], 'errors': [], 'closed': True, 'closing': False, 'parent': False, 'adopt': True, 'pending': 15, 'refs': 10, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closing', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 10}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 15, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a'], 'borrowed': ['a'], 'errors': [], 'closed': False, 'closing': True, 'parent': False, 'adopt': True, 'pending': 0, 'refs': 10, 'already_released': True, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': [], 'borrowed': ['a'], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'closed', 'pending': 0, 'remaining': 0, 'continue': 1, 'parent_refs': 10}), ({'children': ['a'], 'borrowed': [], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': True, 'adopt': False, 'pending': 15, 'refs': 10, 'already_released': False, 'ownership_edges': [], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'open'}, {'order': ['a'], 'borrowed': [], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [], 'closed': 'closed', 'pending': 0, 'remaining': 1, 'continue': 1, 'parent_refs': 9}), ({'children': [], 'borrowed': [], 'errors': ['first', 'second'], 'closed': True, 'closing': False, 'parent': False, 'adopt': False, 'pending': 15, 'refs': 10, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': [], 'borrowed': [], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 10}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': False, 'closing': False, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'open', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 15, 'refs': 10, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 10})]}
for i,(state,expected) in enumerate(fixtures[N]):
    check("transition "+str(i), solve(state), 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(s):
    return {
        'order': list(reversed([x for x in s['children'] if x not in s['borrowed']])),
        'borrowed': [x for x in s['children'] if x in s['borrowed']],
        'errors': list(s['errors']),
        'adoption': s['adopt'] and not s['closed'] and not s['closing'],
        'parent_release': [r for r in s['ownership_edges'] if r[1]!=s['scope_id']],
        'closed': max((s['phase'],s['requested_phase']),key=['open','closing','closed'].index),
        'pending': 0 if s['closing'] or s['closed'] else s['pending'],
        'remaining': len([x for x in s['children'] if x not in s['borrowed']]),
        'continue': len(s['children']),
        'parent_refs': s['refs']-int(s['parent'] and not s['already_released']),
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'children': ['a', 'b', 'c'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': False, 'adopt': True, 'pending': 3, 'refs': 2, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'b'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 2}), ({'children': ['a'], 'borrowed': ['b'], 'errors': ['first'], 'closed': True, 'closing': False, 'parent': True, 'adopt': True, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'closing'}, {'order': ['a'], 'borrowed': [], 'errors': ['first'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 1, 'continue': 1, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'closed': True, 'closing': True, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 2, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': ['c', 'b'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 2}), ({'children': ['a'], 'borrowed': [], 'errors': ['first', 'second'], 'closed': True, 'closing': False, 'parent': True, 'adopt': True, 'pending': 3, 'refs': 2, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'p', 'phase': 'closing', 'requested_phase': 'closing'}, {'order': ['a'], 'borrowed': [], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 1, 'continue': 1, 'parent_refs': 1}), ({'children': [], 'borrowed': ['a'], 'errors': ['first'], 'closed': False, 'closing': False, 'parent': False, 'adopt': True, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': [], 'borrowed': [], 'errors': ['first'], 'adoption': True, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': [], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'closing'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': [], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 0}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': False, 'closing': False, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'open', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 3, 'refs': 2, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 2})], 2: [({'children': ['a', 'b', 'c'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': False, 'adopt': True, 'pending': 6, 'refs': 4, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': ['c', 'b'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 4}), ({'children': [], 'borrowed': ['a'], 'errors': [], 'closed': True, 'closing': False, 'parent': True, 'adopt': True, 'pending': 6, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'q', 'phase': 'open', 'requested_phase': 'closing'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'closing', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a'], 'borrowed': [], 'errors': ['first'], 'closed': False, 'closing': False, 'parent': True, 'adopt': False, 'pending': 6, 'refs': 4, 'already_released': True, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'closed'}, {'order': ['a'], 'borrowed': [], 'errors': ['first'], 'adoption': False, 'parent_release': [], 'closed': 'closed', 'pending': 6, 'remaining': 1, 'continue': 1, 'parent_refs': 4}), ({'children': ['a'], 'borrowed': [], 'errors': ['first'], 'closed': False, 'closing': True, 'parent': False, 'adopt': False, 'pending': 6, 'refs': 4, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closing', 'requested_phase': 'open'}, {'order': ['a'], 'borrowed': [], 'errors': ['first'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 1, 'continue': 1, 'parent_refs': 4}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': [], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 6, 'refs': 0, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': [], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': [], 'errors': [], 'closed': True, 'closing': True, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 4, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closing', 'requested_phase': 'closing'}, {'order': ['c', 'b', 'a'], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 3, 'continue': 3, 'parent_refs': 4}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': False, 'closing': False, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'open', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 6, 'refs': 4, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 4})], 3: [({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': False, 'adopt': True, 'pending': 9, 'refs': 6, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 6}), ({'children': ['a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': False, 'parent': True, 'adopt': True, 'pending': 9, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': ['a'], 'borrowed': [], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 1, 'continue': 1, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': [], 'closed': False, 'closing': True, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'open', 'requested_phase': 'closing'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'closing', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': [], 'closed': True, 'closing': False, 'parent': True, 'adopt': False, 'pending': 0, 'refs': 6, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': [], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 5}), ({'children': ['a'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': False, 'adopt': True, 'pending': 0, 'refs': 0, 'already_released': True, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'open'}, {'order': [], 'borrowed': ['a'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [], 'closed': 'open', 'pending': 0, 'remaining': 0, 'continue': 1, 'parent_refs': 0}), ({'children': [], 'borrowed': ['b'], 'errors': [], 'closed': False, 'closing': True, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 6, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 6}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': False, 'closing': False, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'open', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 9, 'refs': 6, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 6})], 4: [({'children': ['a', 'b', 'c'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': True, 'adopt': False, 'pending': 12, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closing', 'requested_phase': 'closing'}, {'order': ['c', 'b'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 0}), ({'children': ['a'], 'borrowed': ['b'], 'errors': ['first'], 'closed': False, 'closing': True, 'parent': False, 'adopt': True, 'pending': 12, 'refs': 8, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': ['a'], 'borrowed': [], 'errors': ['first'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 1, 'continue': 1, 'parent_refs': 8}), ({'children': ['a', 'b', 'c'], 'borrowed': ['a'], 'errors': ['first'], 'closed': True, 'closing': False, 'parent': False, 'adopt': True, 'pending': 0, 'refs': 0, 'already_released': True, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'q', 'phase': 'closing', 'requested_phase': 'closing'}, {'order': ['c', 'b'], 'borrowed': ['a'], 'errors': ['first'], 'adoption': False, 'parent_release': [], 'closed': 'closing', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 0}), ({'children': [], 'borrowed': ['a'], 'errors': ['first'], 'closed': True, 'closing': True, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closing', 'requested_phase': 'closing'}, {'order': [], 'borrowed': [], 'errors': ['first'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': [], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': False, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 8, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 8}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': False, 'closing': False, 'parent': False, 'adopt': True, 'pending': 0, 'refs': 0, 'already_released': True, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'q', 'phase': 'closing', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': True, 'parent_release': [], 'closed': 'closing', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': False, 'closing': False, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'open', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 12, 'refs': 8, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 8})], 5: [({'children': ['a', 'b', 'c'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': True, 'adopt': True, 'pending': 15, 'refs': 10, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'self', 'parent_id': 'q', 'phase': 'open', 'requested_phase': 'closing'}, {'order': ['c', 'b'], 'borrowed': ['a'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 9}), ({'children': [], 'borrowed': ['b'], 'errors': [], 'closed': True, 'closing': False, 'parent': False, 'adopt': True, 'pending': 15, 'refs': 10, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closing', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closing', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 10}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 15, 'refs': 0, 'already_released': False, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a'], 'borrowed': ['a'], 'errors': [], 'closed': False, 'closing': True, 'parent': False, 'adopt': True, 'pending': 0, 'refs': 10, 'already_released': True, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closing'}, {'order': [], 'borrowed': ['a'], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'closed', 'pending': 0, 'remaining': 0, 'continue': 1, 'parent_refs': 10}), ({'children': ['a'], 'borrowed': [], 'errors': ['first', 'second'], 'closed': False, 'closing': True, 'parent': True, 'adopt': False, 'pending': 15, 'refs': 10, 'already_released': False, 'ownership_edges': [], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'open'}, {'order': ['a'], 'borrowed': [], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [], 'closed': 'closed', 'pending': 0, 'remaining': 1, 'continue': 1, 'parent_refs': 9}), ({'children': [], 'borrowed': [], 'errors': ['first', 'second'], 'closed': True, 'closing': False, 'parent': False, 'adopt': False, 'pending': 15, 'refs': 10, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'p', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': [], 'borrowed': [], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 10}), ({'children': [], 'borrowed': [], 'errors': [], 'closed': False, 'closing': False, 'parent': False, 'adopt': False, 'pending': 0, 'refs': 0, 'already_released': False, 'ownership_edges': [], 'scope_id': 'self', 'parent_id': 'p', 'phase': 'open', 'requested_phase': 'open'}, {'order': [], 'borrowed': [], 'errors': [], 'adoption': False, 'parent_release': [], 'closed': 'open', 'pending': 0, 'remaining': 0, 'continue': 0, 'parent_refs': 0}), ({'children': ['a', 'b', 'c'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'closed': True, 'closing': True, 'parent': True, 'adopt': True, 'pending': 15, 'refs': 10, 'already_released': True, 'ownership_edges': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'scope_id': 'missing', 'parent_id': 'q', 'phase': 'closed', 'requested_phase': 'closed'}, {'order': ['c', 'a'], 'borrowed': ['b'], 'errors': ['first', 'second'], 'adoption': False, 'parent_release': [['p', 'self'], ['p', 'sibling'], ['q', 'other']], 'closed': 'closed', 'pending': 0, 'remaining': 2, 'continue': 3, 'parent_refs': 10})]}
for i,(state,expected) in enumerate(fixtures[N]):
    check("transition "+str(i), solve(state), 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:
An absent parent link decrements scope references; the returned lifecycle plan violates the stated transition contract.

Plan closing an observer ownership scope. Owned children close in reverse acquisition order, borrowed children remain alive. Every cleanup executes even if prior cleanup failed; aggregate the errors. Closed scopes reject adoption, preserve their terminal marker and release their parent link exactly once. Ownership edges are [parent,child]; detaching a scope removes edges targeting that scope, not siblings from the same parent. Scope lifecycle is monotonic open < closing < closed; delayed requests cannot regress it and closing is not already closed. Inputs are finite state snapshots, including transitional and stale states; output fields are the exact independent actions/metadata defined by the corrected expression table. Numeric fixture capacities and counts vary with N.
Field definitions, each independently computed from the input snapshot: order = list(reversed([x for x in s['children'] if x not in s['borrowed']])); borrowed = [x for x in s['children'] if x in s['borrowed']]; errors = list(s['errors']); adoption = s['adopt'] and not s['closed'] and not s['closing']; parent_release = [r for r in s['ownership_edges'] if r[1]!=s['scope_id']]; closed = max((s['phase'],s['requested_phase']),key=['open','closing','closed'].index); pending = 0 if s['closing'] or s['closed'] else s['pending']; remaining = len([x for x in s['children'] if x not in s['borrowed']]); continue = len(s['children']); parent_refs = max(0,s['refs']-int(s['parent'] and not s['already_released']))

split: open-access


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

N = 1
observations = []
def solve(s):
    return {
        'kind': 'complete' if s['complete'] else ('error' if s['error'] is not None else None),
        'closed': s['closed'] or s['complete'] or s['error'] is not None or s['cancel'],
        'drain': list(s['values']) if not s['closed'] and not s['cancel'] and s['error'] is None else [],
        'error_payload': s['error'] if not s['closed'] and not s['cancel'] else None,
        'release': s['owned'] and not s['closed'] and (s['complete'] or s['error'] is not None or s['cancel']),
        'teardown': not s['teardown'] and not s['closed'] and (s['complete'] or s['error'] is not None or s['cancel']),
        'inflight': 0 if s['error'] is not None or s['cancel'] else s['inflight'],
        'late_error': s['closed'] and s['error'] is not None and not s['reported'],
        'cancel_reason': s['reason'] if s['cancel'] else None,
        'completion_wait': s['complete'] and s['error'] is None and not s['cancel'] and not s['closed'] and s['inflight']>0,
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'closed': False, 'complete': False, 'error': None, 'cancel': True, 'values': [1, 2], 'owned': True, 'inflight': 2, 'teardown': True, 'reported': False, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [1, 2], 'owned': False, 'inflight': 2, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 2], 'owned': True, 'inflight': 2, 'teardown': True, 'reported': True, 'reason': None}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': False, 'error': 'boom', 'cancel': False, 'values': [1, 2], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': True, 'reason': None}, {'kind': 'error', 'closed': True, 'drain': [], 'error_payload': 'boom', 'release': False, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': None, 'cancel': False, 'values': [], 'owned': True, 'inflight': 2, 'teardown': True, 'reported': True, 'reason': None}, {'kind': 'complete', 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 2, 'late_error': False, 'cancel_reason': None, 'completion_wait': True}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [], 'owned': False, 'inflight': 2, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': True, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': False, 'error': None, 'cancel': False, 'values': [], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': False, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 2], 'owned': True, 'inflight': 2, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False})], 2: [({'closed': False, 'complete': False, 'error': None, 'cancel': True, 'values': [1, 4], 'owned': True, 'inflight': 4, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [], 'owned': True, 'inflight': 4, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 4], 'owned': False, 'inflight': 4, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [], 'owned': True, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': True, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': False, 'complete': False, 'error': 'boom', 'cancel': False, 'values': [], 'owned': False, 'inflight': 4, 'teardown': False, 'reported': False, 'reason': None}, {'kind': 'error', 'closed': True, 'drain': [], 'error_payload': 'boom', 'release': False, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': None, 'cancel': False, 'values': [1, 4], 'owned': True, 'inflight': 4, 'teardown': False, 'reported': False, 'reason': 'stop'}, {'kind': 'complete', 'closed': True, 'drain': [1, 4], 'error_payload': None, 'release': True, 'teardown': True, 'inflight': 4, 'late_error': False, 'cancel_reason': None, 'completion_wait': True}), ({'closed': False, 'complete': False, 'error': None, 'cancel': False, 'values': [], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': False, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 4], 'owned': True, 'inflight': 4, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False})], 3: [({'closed': False, 'complete': False, 'error': None, 'cancel': True, 'values': [1, 6], 'owned': True, 'inflight': 6, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [], 'owned': False, 'inflight': 6, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 6], 'owned': True, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [], 'owned': True, 'inflight': 0, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': 'error', 'closed': True, 'drain': [], 'error_payload': 'boom', 'release': True, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': True, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': None, 'cancel': False, 'values': [1, 6], 'owned': True, 'inflight': 6, 'teardown': True, 'reported': False, 'reason': None}, {'kind': 'complete', 'closed': True, 'drain': [1, 6], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 6, 'late_error': False, 'cancel_reason': None, 'completion_wait': True}), ({'closed': False, 'complete': False, 'error': None, 'cancel': False, 'values': [], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': False, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 6], 'owned': True, 'inflight': 6, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False})], 4: [({'closed': False, 'complete': False, 'error': None, 'cancel': True, 'values': [1, 8], 'owned': True, 'inflight': 8, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [], 'owned': True, 'inflight': 8, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': False, 'error': 'boom', 'cancel': True, 'values': [], 'owned': True, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': False, 'error': 'boom', 'cancel': True, 'values': [1, 8], 'owned': False, 'inflight': 8, 'teardown': False, 'reported': False, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': True, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': False, 'complete': False, 'error': 'boom', 'cancel': False, 'values': [1, 8], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': 'error', 'closed': True, 'drain': [], 'error_payload': 'boom', 'release': False, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': None, 'cancel': False, 'values': [], 'owned': True, 'inflight': 8, 'teardown': False, 'reported': False, 'reason': None}, {'kind': 'complete', 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': True, 'inflight': 8, 'late_error': False, 'cancel_reason': None, 'completion_wait': True}), ({'closed': False, 'complete': False, 'error': None, 'cancel': False, 'values': [], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': False, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 8], 'owned': True, 'inflight': 8, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False})], 5: [({'closed': False, 'complete': False, 'error': None, 'cancel': True, 'values': [1, 10], 'owned': True, 'inflight': 10, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [], 'owned': False, 'inflight': 10, 'teardown': False, 'reported': False, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': True, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 10], 'owned': True, 'inflight': 10, 'teardown': True, 'reported': False, 'reason': None}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': False, 'error': 'boom', 'cancel': False, 'values': [1, 10], 'owned': False, 'inflight': 0, 'teardown': True, 'reported': False, 'reason': None}, {'kind': 'error', 'closed': True, 'drain': [], 'error_payload': 'boom', 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [1, 10], 'owned': True, 'inflight': 10, 'teardown': False, 'reported': True, 'reason': None}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': None, 'cancel': False, 'values': [1, 10], 'owned': True, 'inflight': 10, 'teardown': True, 'reported': False, 'reason': 'stop'}, {'kind': 'complete', 'closed': True, 'drain': [1, 10], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 10, 'late_error': False, 'cancel_reason': None, 'completion_wait': True}), ({'closed': False, 'complete': False, 'error': None, 'cancel': False, 'values': [], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': False, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 10], 'owned': True, 'inflight': 10, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False})]}
for i,(state,expected) in enumerate(fixtures[N]):
    check("transition "+str(i), solve(state), 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(s):
    return {
        'kind': None if s['closed'] else ('error' if s['error'] is not None else ('complete' if s['complete'] else None)),
        'closed': s['closed'] or s['complete'] or s['error'] is not None or s['cancel'],
        'drain': list(s['values']) if not s['closed'] and not s['cancel'] and s['error'] is None else [],
        'error_payload': s['error'] if not s['closed'] and not s['cancel'] else None,
        'release': s['owned'] and not s['closed'] and (s['complete'] or s['error'] is not None or s['cancel']),
        'teardown': not s['teardown'] and not s['closed'] and (s['complete'] or s['error'] is not None or s['cancel']),
        'inflight': 0 if s['error'] is not None or s['cancel'] else s['inflight'],
        'late_error': s['closed'] and s['error'] is not None and not s['reported'],
        'cancel_reason': s['reason'] if s['cancel'] else None,
        'completion_wait': s['complete'] and s['error'] is None and not s['cancel'] and not s['closed'] and s['inflight']>0,
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'closed': False, 'complete': False, 'error': None, 'cancel': True, 'values': [1, 2], 'owned': True, 'inflight': 2, 'teardown': True, 'reported': False, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [1, 2], 'owned': False, 'inflight': 2, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 2], 'owned': True, 'inflight': 2, 'teardown': True, 'reported': True, 'reason': None}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': False, 'error': 'boom', 'cancel': False, 'values': [1, 2], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': True, 'reason': None}, {'kind': 'error', 'closed': True, 'drain': [], 'error_payload': 'boom', 'release': False, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': None, 'cancel': False, 'values': [], 'owned': True, 'inflight': 2, 'teardown': True, 'reported': True, 'reason': None}, {'kind': 'complete', 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 2, 'late_error': False, 'cancel_reason': None, 'completion_wait': True}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [], 'owned': False, 'inflight': 2, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': True, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': False, 'error': None, 'cancel': False, 'values': [], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': False, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 2], 'owned': True, 'inflight': 2, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False})], 2: [({'closed': False, 'complete': False, 'error': None, 'cancel': True, 'values': [1, 4], 'owned': True, 'inflight': 4, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [], 'owned': True, 'inflight': 4, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 4], 'owned': False, 'inflight': 4, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [], 'owned': True, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': True, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': False, 'complete': False, 'error': 'boom', 'cancel': False, 'values': [], 'owned': False, 'inflight': 4, 'teardown': False, 'reported': False, 'reason': None}, {'kind': 'error', 'closed': True, 'drain': [], 'error_payload': 'boom', 'release': False, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': None, 'cancel': False, 'values': [1, 4], 'owned': True, 'inflight': 4, 'teardown': False, 'reported': False, 'reason': 'stop'}, {'kind': 'complete', 'closed': True, 'drain': [1, 4], 'error_payload': None, 'release': True, 'teardown': True, 'inflight': 4, 'late_error': False, 'cancel_reason': None, 'completion_wait': True}), ({'closed': False, 'complete': False, 'error': None, 'cancel': False, 'values': [], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': False, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 4], 'owned': True, 'inflight': 4, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False})], 3: [({'closed': False, 'complete': False, 'error': None, 'cancel': True, 'values': [1, 6], 'owned': True, 'inflight': 6, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [], 'owned': False, 'inflight': 6, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 6], 'owned': True, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [], 'owned': True, 'inflight': 0, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': 'error', 'closed': True, 'drain': [], 'error_payload': 'boom', 'release': True, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': True, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': None, 'cancel': False, 'values': [1, 6], 'owned': True, 'inflight': 6, 'teardown': True, 'reported': False, 'reason': None}, {'kind': 'complete', 'closed': True, 'drain': [1, 6], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 6, 'late_error': False, 'cancel_reason': None, 'completion_wait': True}), ({'closed': False, 'complete': False, 'error': None, 'cancel': False, 'values': [], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': False, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 6], 'owned': True, 'inflight': 6, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False})], 4: [({'closed': False, 'complete': False, 'error': None, 'cancel': True, 'values': [1, 8], 'owned': True, 'inflight': 8, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [], 'owned': True, 'inflight': 8, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': False, 'error': 'boom', 'cancel': True, 'values': [], 'owned': True, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': False, 'error': 'boom', 'cancel': True, 'values': [1, 8], 'owned': False, 'inflight': 8, 'teardown': False, 'reported': False, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': True, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': False, 'complete': False, 'error': 'boom', 'cancel': False, 'values': [1, 8], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': True, 'reason': 'stop'}, {'kind': 'error', 'closed': True, 'drain': [], 'error_payload': 'boom', 'release': False, 'teardown': True, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': None, 'cancel': False, 'values': [], 'owned': True, 'inflight': 8, 'teardown': False, 'reported': False, 'reason': None}, {'kind': 'complete', 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': True, 'inflight': 8, 'late_error': False, 'cancel_reason': None, 'completion_wait': True}), ({'closed': False, 'complete': False, 'error': None, 'cancel': False, 'values': [], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': False, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 8], 'owned': True, 'inflight': 8, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False})], 5: [({'closed': False, 'complete': False, 'error': None, 'cancel': True, 'values': [1, 10], 'owned': True, 'inflight': 10, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [], 'owned': False, 'inflight': 10, 'teardown': False, 'reported': False, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': True, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 10], 'owned': True, 'inflight': 10, 'teardown': True, 'reported': False, 'reason': None}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': False, 'error': 'boom', 'cancel': False, 'values': [1, 10], 'owned': False, 'inflight': 0, 'teardown': True, 'reported': False, 'reason': None}, {'kind': 'error', 'closed': True, 'drain': [], 'error_payload': 'boom', 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': False, 'values': [1, 10], 'owned': True, 'inflight': 10, 'teardown': False, 'reported': True, 'reason': None}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': False, 'complete': True, 'error': None, 'cancel': False, 'values': [1, 10], 'owned': True, 'inflight': 10, 'teardown': True, 'reported': False, 'reason': 'stop'}, {'kind': 'complete', 'closed': True, 'drain': [1, 10], 'error_payload': None, 'release': True, 'teardown': False, 'inflight': 10, 'late_error': False, 'cancel_reason': None, 'completion_wait': True}), ({'closed': False, 'complete': False, 'error': None, 'cancel': False, 'values': [], 'owned': False, 'inflight': 0, 'teardown': False, 'reported': False, 'reason': None}, {'kind': None, 'closed': False, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': None, 'completion_wait': False}), ({'closed': True, 'complete': True, 'error': 'boom', 'cancel': True, 'values': [1, 10], 'owned': True, 'inflight': 10, 'teardown': True, 'reported': True, 'reason': 'stop'}, {'kind': None, 'closed': True, 'drain': [], 'error_payload': None, 'release': False, 'teardown': False, 'inflight': 0, 'late_error': False, 'cancel_reason': 'stop', 'completion_wait': False})]}
for i,(state,expected) in enumerate(fixtures[N]):
    check("transition "+str(i), solve(state), 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:
Completion wins over a simultaneous observer error; the returned lifecycle plan violates the stated transition contract.

Plan an observer terminal notification. First completion or error closes the observer. Cancellation is silent; an error has precedence over completion. An already closed observer never receives another terminal callback. Queued values are discarded on error or cancellation but drain before normal completion. Release owned resources on first closure. Inputs are finite state snapshots, including transitional and stale states; output fields are the exact independent actions/metadata defined by the corrected expression table. Numeric fixture capacities and counts vary with N.
Field definitions, each independently computed from the input snapshot: kind = None if s['closed'] or s['cancel'] else ('error' if s['error'] is not None else ('complete' if s['complete'] else None)); closed = s['closed'] or s['complete'] or s['error'] is not None or s['cancel']; drain = list(s['values']) if not s['closed'] and not s['cancel'] and s['error'] is None else []; error_payload = s['error'] if not s['closed'] and not s['cancel'] else None; release = s['owned'] and not s['closed'] and (s['complete'] or s['error'] is not None or s['cancel']); teardown = not s['teardown'] and not s['closed'] and (s['complete'] or s['error'] is not None or s['cancel']); inflight = 0 if s['error'] is not None or s['cancel'] else s['inflight']; late_error = s['closed'] and s['error'] is not None and not s['reported']; cancel_reason = s['reason'] if s['cancel'] else None; completion_wait = s['complete'] and s['error'] is None and not s['cancel'] and not s['closed'] and s['inflight']>0

split: open-access