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

N = 1
observations = []
def solve(s):
    return {
        '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': False,
    }
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)

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

N = 1
observations = []
def solve(s):
    return {
        '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['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 bypasses outstanding callback work; 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


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

N = 1
observations = []
def solve(s):
    return {
        'connect': s['after']>0,
        'disconnect': s['before']>0 and s['after']==0 and s['connected'] and not s['keep'],
        'delta': s['after']-s['before'],
        'generation': s['generation']+int(not s['connected'] and s['after']>0 and s['successful']),
        'dispose': s['owned'] and s['connected'] and s['after']==0 and not s['keep'],
        'retry': s['retry'] and s['failed'] and s['after']>0 and not s['connected'],
        'idle': s['after']==0 and s['connected'] and s['keep'],
        'pending': s['pending'] if s['after']>0 else 0,
        'first': s['before']==0 and s['after']>0,
        'last': s['before']>0 and s['after']==0,
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'before': 3, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': False, 'pending': 2}, {'connect': False, 'disconnect': False, 'delta': -3, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 3, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': False, 'generation': 2, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 2, 'generation': 2, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': True, 'connected': False, 'failed': False, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 3, 'after': 1, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 0, 'successful': True, 'pending': 2}, {'connect': False, 'disconnect': False, 'delta': -2, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 2, 'first': False, 'last': False}), ({'before': 1, 'after': 3, 'keep': True, 'connected': False, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': True, 'disconnect': False, 'delta': 2, 'generation': 0, 'dispose': False, 'retry': True, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 1, 'keep': True, 'connected': True, 'failed': False, 'retry': True, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 3, 'after': 3, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 2, 'successful': True, 'pending': 2}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 2, 'dispose': False, 'retry': False, 'idle': False, 'pending': 2, 'first': False, 'last': False})], 2: [({'before': 1, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': -1, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 6, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': 5, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 4, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': False, 'generation': 4, 'successful': False, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 6, 'after': 1, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 4, 'successful': False, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': -5, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 4, 'first': False, 'last': False}), ({'before': 1, 'after': 6, 'keep': True, 'connected': True, 'failed': True, 'retry': False, 'owned': False, 'generation': 4, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 5, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 6, 'keep': True, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 4, 'successful': False, 'pending': 0}, {'connect': True, 'disconnect': False, 'delta': 6, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': True, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 6, 'after': 6, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 4, 'successful': True, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 4, 'first': False, 'last': False})], 3: [({'before': 9, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': -9, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 9, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 6, 'successful': False, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': 8, 'generation': 6, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': True, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 9, 'after': 1, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 6, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': -8, 'generation': 6, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False}), ({'before': 1, 'after': 1, 'keep': False, 'connected': True, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 1, 'keep': False, 'connected': True, 'failed': False, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 9, 'after': 9, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 6, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 6, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False})], 4: [({'before': 12, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': -12, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 12, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 11, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 8, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 8, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 8, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 12, 'after': 1, 'keep': False, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 8, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': -11, 'generation': 8, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 12, 'keep': True, 'connected': True, 'failed': False, 'retry': False, 'owned': True, 'generation': 0, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 11, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 8, 'first': False, 'last': False}), ({'before': 0, 'after': 12, 'keep': False, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 0, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 12, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': True, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 12, 'after': 12, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 8, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 8, 'dispose': False, 'retry': False, 'idle': False, 'pending': 8, 'first': False, 'last': False})], 5: [({'before': 1, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 10, 'successful': False, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': -1, 'generation': 10, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 15, 'after': 1, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 10, 'successful': False, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': -14, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 10, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': True, 'generation': 10, 'successful': True, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 15, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 10, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 14, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 15, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 15, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': True, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 15, 'after': 15, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 10, 'successful': True, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 10, 'first': False, 'last': 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 {
        'connect': s['after']>0 and not s['connected'],
        'disconnect': s['before']>0 and s['after']==0 and s['connected'] and not s['keep'],
        'delta': s['after']-s['before'],
        'generation': s['generation']+int(not s['connected'] and s['after']>0 and s['successful']),
        'dispose': s['owned'] and s['connected'] and s['after']==0 and not s['keep'],
        'retry': s['retry'] and s['failed'] and s['after']>0 and not s['connected'],
        'idle': s['after']==0 and s['connected'] and s['keep'],
        'pending': s['pending'] if s['after']>0 else 0,
        'first': s['before']==0 and s['after']>0,
        'last': s['before']>0 and s['after']==0,
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'before': 3, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': False, 'pending': 2}, {'connect': False, 'disconnect': False, 'delta': -3, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 3, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': False, 'generation': 2, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 2, 'generation': 2, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': True, 'connected': False, 'failed': False, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 3, 'after': 1, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 0, 'successful': True, 'pending': 2}, {'connect': False, 'disconnect': False, 'delta': -2, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 2, 'first': False, 'last': False}), ({'before': 1, 'after': 3, 'keep': True, 'connected': False, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': True, 'disconnect': False, 'delta': 2, 'generation': 0, 'dispose': False, 'retry': True, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 1, 'keep': True, 'connected': True, 'failed': False, 'retry': True, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 3, 'after': 3, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 2, 'successful': True, 'pending': 2}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 2, 'dispose': False, 'retry': False, 'idle': False, 'pending': 2, 'first': False, 'last': False})], 2: [({'before': 1, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': -1, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 6, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': 5, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 4, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': False, 'generation': 4, 'successful': False, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 6, 'after': 1, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 4, 'successful': False, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': -5, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 4, 'first': False, 'last': False}), ({'before': 1, 'after': 6, 'keep': True, 'connected': True, 'failed': True, 'retry': False, 'owned': False, 'generation': 4, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 5, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 6, 'keep': True, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 4, 'successful': False, 'pending': 0}, {'connect': True, 'disconnect': False, 'delta': 6, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': True, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 6, 'after': 6, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 4, 'successful': True, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 4, 'first': False, 'last': False})], 3: [({'before': 9, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': -9, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 9, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 6, 'successful': False, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': 8, 'generation': 6, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': True, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 9, 'after': 1, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 6, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': -8, 'generation': 6, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False}), ({'before': 1, 'after': 1, 'keep': False, 'connected': True, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 1, 'keep': False, 'connected': True, 'failed': False, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 9, 'after': 9, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 6, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 6, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False})], 4: [({'before': 12, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': -12, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 12, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 11, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 8, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 8, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 8, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 12, 'after': 1, 'keep': False, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 8, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': -11, 'generation': 8, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 12, 'keep': True, 'connected': True, 'failed': False, 'retry': False, 'owned': True, 'generation': 0, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 11, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 8, 'first': False, 'last': False}), ({'before': 0, 'after': 12, 'keep': False, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 0, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 12, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': True, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 12, 'after': 12, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 8, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 8, 'dispose': False, 'retry': False, 'idle': False, 'pending': 8, 'first': False, 'last': False})], 5: [({'before': 1, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 10, 'successful': False, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': -1, 'generation': 10, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 15, 'after': 1, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 10, 'successful': False, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': -14, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 10, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': True, 'generation': 10, 'successful': True, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 15, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 10, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 14, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 15, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 15, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': True, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 15, 'after': 15, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 10, 'successful': True, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 10, 'first': False, 'last': 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:
Adding a second subscriber reconnects the shared upstream; the returned lifecycle plan violates the stated transition contract.

A reference-counted observer connection transitions from before subscribers to after subscribers. Connect on zero-to-positive, disconnect on positive-to-zero unless keepalive; a failed connection is retried only when requested. A generation advances only on a fresh successful connection. Ownership controls upstream disposal, not subscriber accounting. 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: connect = s['after']>0 and not s['connected'] and (not s['failed'] or s['retry']); disconnect = s['before']>0 and s['after']==0 and s['connected'] and not s['keep']; delta = s['after']-s['before']; generation = s['generation']+int(not s['connected'] and s['after']>0 and s['successful']); dispose = s['owned'] and s['connected'] and s['after']==0 and not s['keep']; retry = s['retry'] and s['failed'] and s['after']>0 and not s['connected']; idle = s['after']==0 and s['connected'] and s['keep']; pending = s['pending'] if s['after']>0 else 0; first = s['before']==0 and s['after']>0; last = s['before']>0 and s['after']==0

split: open-access


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

N = 1
observations = []
def solve(s):
    return {
        'connect': s['after']>0 and not s['connected'] and (not s['failed'] or s['retry']),
        'disconnect': s['after']==0 and s['connected'],
        'delta': s['after']-s['before'],
        'generation': s['generation']+int(not s['connected'] and s['after']>0 and s['successful']),
        'dispose': s['owned'] and s['connected'] and s['after']==0 and not s['keep'],
        'retry': s['retry'] and s['failed'] and s['after']>0 and not s['connected'],
        'idle': s['after']==0 and s['connected'] and s['keep'],
        'pending': s['pending'] if s['after']>0 else 0,
        'first': s['before']==0 and s['after']>0,
        'last': s['before']>0 and s['after']==0,
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'before': 3, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': False, 'pending': 2}, {'connect': False, 'disconnect': False, 'delta': -3, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 3, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': False, 'generation': 2, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 2, 'generation': 2, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': True, 'connected': False, 'failed': False, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 3, 'after': 1, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 0, 'successful': True, 'pending': 2}, {'connect': False, 'disconnect': False, 'delta': -2, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 2, 'first': False, 'last': False}), ({'before': 1, 'after': 3, 'keep': True, 'connected': False, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': True, 'disconnect': False, 'delta': 2, 'generation': 0, 'dispose': False, 'retry': True, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 1, 'keep': True, 'connected': True, 'failed': False, 'retry': True, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 3, 'after': 3, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 2, 'successful': True, 'pending': 2}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 2, 'dispose': False, 'retry': False, 'idle': False, 'pending': 2, 'first': False, 'last': False})], 2: [({'before': 1, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': -1, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 6, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': 5, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 4, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': False, 'generation': 4, 'successful': False, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 6, 'after': 1, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 4, 'successful': False, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': -5, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 4, 'first': False, 'last': False}), ({'before': 1, 'after': 6, 'keep': True, 'connected': True, 'failed': True, 'retry': False, 'owned': False, 'generation': 4, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 5, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 6, 'keep': True, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 4, 'successful': False, 'pending': 0}, {'connect': True, 'disconnect': False, 'delta': 6, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': True, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 6, 'after': 6, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 4, 'successful': True, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 4, 'first': False, 'last': False})], 3: [({'before': 9, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': -9, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 9, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 6, 'successful': False, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': 8, 'generation': 6, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': True, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 9, 'after': 1, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 6, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': -8, 'generation': 6, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False}), ({'before': 1, 'after': 1, 'keep': False, 'connected': True, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 1, 'keep': False, 'connected': True, 'failed': False, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 9, 'after': 9, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 6, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 6, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False})], 4: [({'before': 12, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': -12, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 12, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 11, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 8, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 8, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 8, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 12, 'after': 1, 'keep': False, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 8, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': -11, 'generation': 8, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 12, 'keep': True, 'connected': True, 'failed': False, 'retry': False, 'owned': True, 'generation': 0, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 11, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 8, 'first': False, 'last': False}), ({'before': 0, 'after': 12, 'keep': False, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 0, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 12, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': True, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 12, 'after': 12, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 8, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 8, 'dispose': False, 'retry': False, 'idle': False, 'pending': 8, 'first': False, 'last': False})], 5: [({'before': 1, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 10, 'successful': False, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': -1, 'generation': 10, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 15, 'after': 1, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 10, 'successful': False, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': -14, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 10, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': True, 'generation': 10, 'successful': True, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 15, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 10, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 14, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 15, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 15, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': True, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 15, 'after': 15, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 10, 'successful': True, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 10, 'first': False, 'last': 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)

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

N = 1
observations = []
def solve(s):
    return {
        'connect': s['after']>0 and not s['connected'] and (not s['failed'] or s['retry']),
        'disconnect': s['before']>0 and s['after']==0 and s['connected'],
        'delta': s['after']-s['before'],
        'generation': s['generation']+int(not s['connected'] and s['after']>0 and s['successful']),
        'dispose': s['owned'] and s['connected'] and s['after']==0 and not s['keep'],
        'retry': s['retry'] and s['failed'] and s['after']>0 and not s['connected'],
        'idle': s['after']==0 and s['connected'] and s['keep'],
        'pending': s['pending'] if s['after']>0 else 0,
        'first': s['before']==0 and s['after']>0,
        'last': s['before']>0 and s['after']==0,
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'before': 3, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': False, 'pending': 2}, {'connect': False, 'disconnect': False, 'delta': -3, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 3, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': False, 'generation': 2, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 2, 'generation': 2, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': True, 'connected': False, 'failed': False, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 3, 'after': 1, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 0, 'successful': True, 'pending': 2}, {'connect': False, 'disconnect': False, 'delta': -2, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 2, 'first': False, 'last': False}), ({'before': 1, 'after': 3, 'keep': True, 'connected': False, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': True, 'disconnect': False, 'delta': 2, 'generation': 0, 'dispose': False, 'retry': True, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 1, 'keep': True, 'connected': True, 'failed': False, 'retry': True, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 3, 'after': 3, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 2, 'successful': True, 'pending': 2}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 2, 'dispose': False, 'retry': False, 'idle': False, 'pending': 2, 'first': False, 'last': False})], 2: [({'before': 1, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': -1, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 6, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': 5, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 4, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': False, 'generation': 4, 'successful': False, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 6, 'after': 1, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 4, 'successful': False, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': -5, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 4, 'first': False, 'last': False}), ({'before': 1, 'after': 6, 'keep': True, 'connected': True, 'failed': True, 'retry': False, 'owned': False, 'generation': 4, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 5, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 6, 'keep': True, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 4, 'successful': False, 'pending': 0}, {'connect': True, 'disconnect': False, 'delta': 6, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': True, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 6, 'after': 6, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 4, 'successful': True, 'pending': 4}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 4, 'dispose': False, 'retry': False, 'idle': False, 'pending': 4, 'first': False, 'last': False})], 3: [({'before': 9, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': -9, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 9, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 6, 'successful': False, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': 8, 'generation': 6, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': True, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 9, 'after': 1, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 6, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': -8, 'generation': 6, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False}), ({'before': 1, 'after': 1, 'keep': False, 'connected': True, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 1, 'keep': False, 'connected': True, 'failed': False, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 9, 'after': 9, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 6, 'successful': True, 'pending': 6}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 6, 'dispose': False, 'retry': False, 'idle': False, 'pending': 6, 'first': False, 'last': False})], 4: [({'before': 12, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': -12, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 1, 'after': 12, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 11, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 8, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 8, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 8, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 12, 'after': 1, 'keep': False, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 8, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': -11, 'generation': 8, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 12, 'keep': True, 'connected': True, 'failed': False, 'retry': False, 'owned': True, 'generation': 0, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 11, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 8, 'first': False, 'last': False}), ({'before': 0, 'after': 12, 'keep': False, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 0, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 12, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': True, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 12, 'after': 12, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 8, 'successful': True, 'pending': 8}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 8, 'dispose': False, 'retry': False, 'idle': False, 'pending': 8, 'first': False, 'last': False})], 5: [({'before': 1, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 10, 'successful': False, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': -1, 'generation': 10, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': True}), ({'before': 15, 'after': 1, 'keep': True, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 10, 'successful': False, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': -14, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 10, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': True, 'generation': 10, 'successful': True, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 1, 'after': 15, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': False, 'generation': 10, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 14, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 0, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 0, 'successful': True, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': True, 'pending': 0, 'first': False, 'last': False}), ({'before': 0, 'after': 15, 'keep': False, 'connected': False, 'failed': True, 'retry': False, 'owned': True, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 15, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': True, 'last': False}), ({'before': 0, 'after': 0, 'keep': False, 'connected': False, 'failed': False, 'retry': False, 'owned': False, 'generation': 0, 'successful': False, 'pending': 0}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 0, 'dispose': False, 'retry': False, 'idle': False, 'pending': 0, 'first': False, 'last': False}), ({'before': 15, 'after': 15, 'keep': True, 'connected': True, 'failed': True, 'retry': True, 'owned': True, 'generation': 10, 'successful': True, 'pending': 10}, {'connect': False, 'disconnect': False, 'delta': 0, 'generation': 10, 'dispose': False, 'retry': False, 'idle': False, 'pending': 10, 'first': False, 'last': 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:
A keepalive connection disconnects on last unsubscribe; the returned lifecycle plan violates the stated transition contract.

A reference-counted observer connection transitions from before subscribers to after subscribers. Connect on zero-to-positive, disconnect on positive-to-zero unless keepalive; a failed connection is retried only when requested. A generation advances only on a fresh successful connection. Ownership controls upstream disposal, not subscriber accounting. 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: connect = s['after']>0 and not s['connected'] and (not s['failed'] or s['retry']); disconnect = s['before']>0 and s['after']==0 and s['connected'] and not s['keep']; delta = s['after']-s['before']; generation = s['generation']+int(not s['connected'] and s['after']>0 and s['successful']); dispose = s['owned'] and s['connected'] and s['after']==0 and not s['keep']; retry = s['retry'] and s['failed'] and s['after']>0 and not s['connected']; idle = s['after']==0 and s['connected'] and s['keep']; pending = s['pending'] if s['after']>0 else 0; first = s['before']==0 and s['after']>0; last = s['before']>0 and s['after']==0

split: open-access