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

N = 1
observations = []
def solve(s):
    return {
        'install': not s['installed'] and s['starting'],
        'pending_dispose': sum(op=='cancel' for op in s['handshake_events'][:s['handshake_events'].index('return')]) if 'return' in s['handshake_events'] else s['handshake_events'].count('cancel'),
        'dispose_returned': s['cancel'] and s['subscribe_returned'] and s['has_teardown'] and not s['disposed'],
        'presence': s['has_handle'],
        'source_error': ('unhandled' if 'complete' in s['start_trace'][:s['start_trace'].index('throw')] else 'error-channel') if 'throw' in s['start_trace'] else None,
        'callback_error': {'source':'subscription-error','callback':'callback-error'}[s['raised_by']] if s['raised_by'] is not None else None,
        'ready': s['subscribe_returned'],
        'retain_teardown': [item for group in s['teardown_groups'] for item in group],
        'reentrant_start': [x for x in dict.fromkeys(s['start_tokens']) if x not in s['finished_tokens']],
        'handle_value': s['handle'] if s['has_handle'] else None,
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 'h', 'has_handle': False, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': ['missing'], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': True, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 0, 'has_handle': True, 'starting': True, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': False, 'handshake_events': ['next', 'cancel', 'cancel', 'return'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': []}, {'install': False, 'pending_dispose': 2, 'dispose_returned': False, 'presence': True, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a', 'b'], 'handle_value': 0}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': False, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 0, 'has_handle': False, 'starting': False, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': [], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': True, 'has_teardown': False, 'disposed': True, 'source_error': None, 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': [], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': []}, {'install': True, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a', 'b'], 'handle_value': 'h'}), ({'installed': False, 'subscribe_returned': False, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': True, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': None, 'inside_start': False, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': [], 'teardown_groups': [[]]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a'], 'handle_value': 0}), ({'installed': True, 'subscribe_returned': False, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': None, 'has_handle': True, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': None, 'inside_start': True, 'handshake_events': [], 'start_tokens': ['a'], 'finished_tokens': ['missing'], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': False, 'start_trace': [], 'raised_by': None, 'inside_start': False, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': None, 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': 'h'})], 2: [({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': None, 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': False, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': [], 'finished_tokens': ['a'], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': [], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': None, 'callback_error': 'callback-error', 'handle': 0, 'has_handle': True, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': True, 'presence': True, 'source_error': 'unhandled', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': 0}), ({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': False, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': True, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': ['next', 'cancel', 'cancel', 'return'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['a'], 'teardown_groups': []}, {'install': True, 'pending_dispose': 2, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['b'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': False, 'starting': True, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': [], 'start_tokens': ['a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': True, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': True, 'subscribe_returned': False, 'cancel': True, 'has_teardown': False, 'disposed': True, 'source_error': None, 'callback_error': None, 'handle': 0, 'has_handle': True, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': 'callback', 'inside_start': False, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': [[]]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a', 'b'], 'handle_value': 0}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': False, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 'h', 'has_handle': False, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': 'callback', 'inside_start': False, 'handshake_events': [], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': [[]]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a', 'b'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': False, 'start_trace': [], 'raised_by': None, 'inside_start': False, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': None, 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': 'h'})], 3: [({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': None, 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': False, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': False, 'handshake_events': [], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': True, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 'h', 'has_handle': False, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': ['missing'], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': False, 'presence': False, 'source_error': 'error-channel', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': True, 'subscribe_returned': False, 'cancel': False, 'has_teardown': True, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': False, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': None, 'inside_start': True, 'handshake_events': ['next', 'cancel', 'cancel', 'return'], 'start_tokens': ['a'], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 2, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': None, 'handle': 'h', 'has_handle': True, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': False, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': 'h'}), ({'installed': True, 'subscribe_returned': False, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': True, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': None, 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': [], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': None, 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': ['a'], 'handle_value': 0}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': False, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': False, 'starting': True, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': None, 'inside_start': True, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': None, 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': [], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': False, 'start_trace': [], 'raised_by': None, 'inside_start': False, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': None, 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': 'h'})], 4: [({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': None, 'callback_error': None, 'handle': 'h', 'has_handle': False, 'starting': True, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': False, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': ['a', 'b'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': None, 'callback_error': 'callback-error', 'handle': None, 'has_handle': True, 'starting': False, 'start_trace': [], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'cancel', 'cancel', 'return'], 'start_tokens': ['a'], 'finished_tokens': ['missing'], 'teardown_groups': []}, {'install': False, 'pending_dispose': 2, 'dispose_returned': True, 'presence': True, 'source_error': None, 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 0, 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'cancel', 'cancel', 'return'], 'start_tokens': ['a'], 'finished_tokens': ['a'], 'teardown_groups': [[], ['d']]}, {'install': True, 'pending_dispose': 2, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': [], 'handle_value': 0}), ({'installed': False, 'subscribe_returned': True, 'cancel': False, 'has_teardown': False, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': [], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': [[]]}, {'install': True, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a', 'b'], 'handle_value': 0}), ({'installed': True, 'subscribe_returned': False, 'cancel': False, 'has_teardown': True, 'disposed': True, 'source_error': None, 'callback_error': None, 'handle': 'h', 'has_handle': False, 'starting': True, 'start_trace': ['next', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'error-channel', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a', 'b'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': False, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': True, 'starting': False, 'start_trace': [], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': [[]]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': False, 'presence': True, 'source_error': None, 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': 0}), ({'installed': False, 'subscribe_returned': False, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': False, 'start_trace': [], 'raised_by': None, 'inside_start': False, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': None, 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': 'h'})], 5: [({'installed': False, 'subscribe_returned': True, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': False, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': None, 'inside_start': True, 'handshake_events': ['next', 'cancel', 'cancel', 'return'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 2, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': None, 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': ['a', 'b'], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': None, 'callback_error': 'callback-error', 'handle': 0, 'has_handle': False, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': False, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': [], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': True, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': [], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': None, 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['next', 'throw'], 'raised_by': 'callback', 'inside_start': False, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['a'], 'teardown_groups': [[]]}, {'install': True, 'pending_dispose': 1, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['b'], 'handle_value': 'h'}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': False, 'start_trace': [], 'raised_by': 'source', 'inside_start': True, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': False, 'presence': False, 'source_error': None, 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': True, 'subscribe_returned': False, 'cancel': False, 'has_teardown': True, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 0, 'has_handle': True, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': None, 'inside_start': False, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': ['a'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': None, 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': [], 'handle_value': 0}), ({'installed': True, 'subscribe_returned': True, 'cancel': False, 'has_teardown': True, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': False, 'starting': False, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': False, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': [], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': False, 'start_trace': [], 'raised_by': None, 'inside_start': False, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': None, 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': 'h'})]}
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 {
        'install': not s['installed'] and s['starting'],
        'pending_dispose': sum(op=='cancel' for op in s['handshake_events'][:s['handshake_events'].index('return')]) if 'return' in s['handshake_events'] else s['handshake_events'].count('cancel'),
        'dispose_returned': s['cancel'] and s['subscribe_returned'] and s['has_teardown'] and not s['disposed'],
        'presence': s['has_handle'],
        'source_error': ('unhandled' if 'complete' in s['start_trace'][:s['start_trace'].index('throw')] else 'error-channel') if 'throw' in s['start_trace'] else None,
        'callback_error': {'source':'subscription-error','callback':'callback-error'}[s['raised_by']] if s['raised_by'] is not None else None,
        'ready': s['installed'] and s['subscribe_returned'],
        'retain_teardown': [item for group in s['teardown_groups'] for item in group],
        'reentrant_start': [x for x in dict.fromkeys(s['start_tokens']) if x not in s['finished_tokens']],
        'handle_value': s['handle'] if s['has_handle'] else None,
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 'h', 'has_handle': False, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': ['missing'], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': True, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 0, 'has_handle': True, 'starting': True, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': False, 'handshake_events': ['next', 'cancel', 'cancel', 'return'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': []}, {'install': False, 'pending_dispose': 2, 'dispose_returned': False, 'presence': True, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a', 'b'], 'handle_value': 0}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': False, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 0, 'has_handle': False, 'starting': False, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': [], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': True, 'has_teardown': False, 'disposed': True, 'source_error': None, 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': [], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': []}, {'install': True, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a', 'b'], 'handle_value': 'h'}), ({'installed': False, 'subscribe_returned': False, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': True, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': None, 'inside_start': False, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': [], 'teardown_groups': [[]]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a'], 'handle_value': 0}), ({'installed': True, 'subscribe_returned': False, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': None, 'has_handle': True, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': None, 'inside_start': True, 'handshake_events': [], 'start_tokens': ['a'], 'finished_tokens': ['missing'], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': False, 'start_trace': [], 'raised_by': None, 'inside_start': False, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': None, 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': 'h'})], 2: [({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': None, 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': False, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': [], 'finished_tokens': ['a'], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': [], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': None, 'callback_error': 'callback-error', 'handle': 0, 'has_handle': True, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': True, 'presence': True, 'source_error': 'unhandled', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': 0}), ({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': False, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': True, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': ['next', 'cancel', 'cancel', 'return'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['a'], 'teardown_groups': []}, {'install': True, 'pending_dispose': 2, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['b'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': False, 'starting': True, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': [], 'start_tokens': ['a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': True, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': True, 'subscribe_returned': False, 'cancel': True, 'has_teardown': False, 'disposed': True, 'source_error': None, 'callback_error': None, 'handle': 0, 'has_handle': True, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': 'callback', 'inside_start': False, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': [[]]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a', 'b'], 'handle_value': 0}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': False, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 'h', 'has_handle': False, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': 'callback', 'inside_start': False, 'handshake_events': [], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': [[]]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a', 'b'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': False, 'start_trace': [], 'raised_by': None, 'inside_start': False, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': None, 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': 'h'})], 3: [({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': None, 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': False, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': False, 'handshake_events': [], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': True, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 'h', 'has_handle': False, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': ['missing'], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': False, 'presence': False, 'source_error': 'error-channel', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': True, 'subscribe_returned': False, 'cancel': False, 'has_teardown': True, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': False, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': None, 'inside_start': True, 'handshake_events': ['next', 'cancel', 'cancel', 'return'], 'start_tokens': ['a'], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 2, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': None, 'handle': 'h', 'has_handle': True, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': False, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': 'h'}), ({'installed': True, 'subscribe_returned': False, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': True, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': None, 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': [], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': None, 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': ['a'], 'handle_value': 0}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': False, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': False, 'starting': True, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': None, 'inside_start': True, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': None, 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': [], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': False, 'start_trace': [], 'raised_by': None, 'inside_start': False, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': None, 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': 'h'})], 4: [({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': None, 'callback_error': None, 'handle': 'h', 'has_handle': False, 'starting': True, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': False, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': ['a', 'b'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': None, 'callback_error': 'callback-error', 'handle': None, 'has_handle': True, 'starting': False, 'start_trace': [], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'cancel', 'cancel', 'return'], 'start_tokens': ['a'], 'finished_tokens': ['missing'], 'teardown_groups': []}, {'install': False, 'pending_dispose': 2, 'dispose_returned': True, 'presence': True, 'source_error': None, 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 0, 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'cancel', 'cancel', 'return'], 'start_tokens': ['a'], 'finished_tokens': ['a'], 'teardown_groups': [[], ['d']]}, {'install': True, 'pending_dispose': 2, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': [], 'handle_value': 0}), ({'installed': False, 'subscribe_returned': True, 'cancel': False, 'has_teardown': False, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': [], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': [[]]}, {'install': True, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a', 'b'], 'handle_value': 0}), ({'installed': True, 'subscribe_returned': False, 'cancel': False, 'has_teardown': True, 'disposed': True, 'source_error': None, 'callback_error': None, 'handle': 'h', 'has_handle': False, 'starting': True, 'start_trace': ['next', 'throw'], 'raised_by': 'source', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'error-channel', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['a', 'b'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': True, 'cancel': True, 'has_teardown': False, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': True, 'starting': False, 'start_trace': [], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': [[]]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': False, 'presence': True, 'source_error': None, 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': 0}), ({'installed': False, 'subscribe_returned': False, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': False, 'start_trace': [], 'raised_by': None, 'inside_start': False, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': None, 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': 'h'})], 5: [({'installed': False, 'subscribe_returned': True, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': False, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': None, 'inside_start': True, 'handshake_events': ['next', 'cancel', 'cancel', 'return'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': [], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 2, 'dispose_returned': False, 'presence': False, 'source_error': 'unhandled', 'callback_error': None, 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': ['a', 'b'], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': None, 'callback_error': 'callback-error', 'handle': 0, 'has_handle': False, 'starting': False, 'start_trace': ['next', 'complete', 'throw'], 'raised_by': 'source', 'inside_start': False, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': [], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': True, 'presence': False, 'source_error': 'unhandled', 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': [], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': True, 'has_teardown': True, 'disposed': False, 'source_error': None, 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['next', 'throw'], 'raised_by': 'callback', 'inside_start': False, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['a'], 'teardown_groups': [[]]}, {'install': True, 'pending_dispose': 1, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': [], 'reentrant_start': ['b'], 'handle_value': 'h'}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': False, 'start_trace': [], 'raised_by': 'source', 'inside_start': True, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': False, 'presence': False, 'source_error': None, 'callback_error': 'subscription-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': True, 'subscribe_returned': False, 'cancel': False, 'has_teardown': True, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': None, 'handle': 0, 'has_handle': True, 'starting': False, 'start_trace': ['next', 'throw'], 'raised_by': None, 'inside_start': False, 'handshake_events': ['cancel', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': ['a'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 1, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': None, 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': [], 'handle_value': 0}), ({'installed': True, 'subscribe_returned': True, 'cancel': False, 'has_teardown': True, 'disposed': False, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 0, 'has_handle': False, 'starting': False, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': False, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a'], 'finished_tokens': [], 'teardown_groups': [['a'], ['b', 'c']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['a', 'b', 'c'], 'reentrant_start': ['a'], 'handle_value': None}), ({'installed': False, 'subscribe_returned': False, 'cancel': False, 'has_teardown': False, 'disposed': False, 'source_error': None, 'callback_error': None, 'handle': None, 'has_handle': False, 'starting': False, 'start_trace': [], 'raised_by': None, 'inside_start': False, 'handshake_events': [], 'start_tokens': [], 'finished_tokens': [], 'teardown_groups': []}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': False, 'source_error': None, 'callback_error': None, 'ready': False, 'retain_teardown': [], 'reentrant_start': [], 'handle_value': None}), ({'installed': True, 'subscribe_returned': True, 'cancel': True, 'has_teardown': True, 'disposed': True, 'source_error': 'subscribe-error', 'callback_error': 'callback-error', 'handle': 'h', 'has_handle': True, 'starting': True, 'start_trace': ['throw', 'complete'], 'raised_by': 'callback', 'inside_start': True, 'handshake_events': ['next', 'return', 'cancel'], 'start_tokens': ['a', 'b', 'a'], 'finished_tokens': ['missing'], 'teardown_groups': [[], ['d']]}, {'install': False, 'pending_dispose': 0, 'dispose_returned': False, 'presence': True, 'source_error': 'error-channel', 'callback_error': 'callback-error', 'ready': False, 'retain_teardown': ['d'], 'reentrant_start': ['a', 'b'], 'handle_value': 'h'})]}
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:
A subscription is considered ready before handle installation; the returned lifecycle plan violates the stated transition contract.

Plan synchronous subscription handshaking. The subscription handle must be installed before source callbacks may request cancellation. If cancellation happens before the source returns teardown, retain a pending-dispose marker and dispose the returned teardown immediately. A thrown subscribe call is distinct from callback failure. A returned empty handle is valid and must not be treated as absence; handle presence has a separate bit. A source throw uses the error channel unless synchronous completion preceded it; after completion it is reported unhandled. First throw is the modeled failure. Error routing follows exception origin, independent of whether a callback ran synchronously inside source initialization. Count cancellation requests received before the first teardown return; if no return has arrived, all cancellations remain deferred. Pending handshakes are first-occurrence unique startup tokens excluding tokens that have finished; a reentrant reuse cannot create a second handle. The source may return a finite list of disposer groups; normalize exactly one grouping level in group order and preserve order within each group. 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: install = not s['installed'] and s['starting']; pending_dispose = sum(op=='cancel' for op in s['handshake_events'][:s['handshake_events'].index('return')]) if 'return' in s['handshake_events'] else s['handshake_events'].count('cancel'); dispose_returned = s['cancel'] and s['subscribe_returned'] and s['has_teardown'] and not s['disposed']; presence = s['has_handle']; source_error = ('unhandled' if 'complete' in s['start_trace'][:s['start_trace'].index('throw')] else 'error-channel') if 'throw' in s['start_trace'] else None; callback_error = {'source':'subscription-error','callback':'callback-error'}[s['raised_by']] if s['raised_by'] is not None else None; ready = s['installed'] and s['subscribe_returned'] and s['source_error'] is None and not s['cancel']; retain_teardown = [item for group in s['teardown_groups'] for item in group]; reentrant_start = [x for x in dict.fromkeys(s['start_tokens']) if x not in s['finished_tokens']]; handle_value = s['handle'] if s['has_handle'] else None

split: open-access