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['before']>0 and s['after']==0 and s['connected'] and not s['keep'],
        'delta': s['after'],
        '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'] and not s['keep'],
        'delta': max(0,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:
Subscriber accounting replaces a delta with the final count; 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['before']>0 and s['after']==0 and s['connected'] and not s['keep'],
        'delta': s['after']-s['before'],
        'generation': s['generation']+int(s['after']>0),
        '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'] 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),
        '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:
Unsuccessful connection attempts advance the generation; 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['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['connected'] and s['after']==0,
        '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'] 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['after']==0,
        '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:
Reference counting disposes a borrowed 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