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

N = 1
observations = []
def solve(phase, events, seed):
    payload = list(range(seed, seed * 2 + 2))
    r = {'listing': ['stale'] if phase=='live' else [], 'staged': ['first'] if phase=='snapshotting' else [], 'tombstones': [], 'notifications': [], 'watch_owned': phase!='closed'}
    frames = []
    observed = []
    table = {('idle', 'start_watch'): ('snapshotting', 'request_listing'), ('snapshotting', 'listed_service'): ('snapshotting', 'stage_service'), ('snapshotting', 'listing_end'): ('live', 'publish_listing'), ('live', 'service_added'): ('live', 'insert_notify'), ('live', 'service_removed'): ('live', 'keep_service'), ('live', 'watch_gap'): ('snapshotting', 'clear_request_listing'), ('live', 'lease_expired'): ('live', 'expire_notify'), ('live', 'stop_watch'): ('closed', 'release_watch'), ('closed', 'service_added'): ('closed', 'ignore_closed'), ('snapshotting', 'service_removed'): ('snapshotting', 'record_tombstone')}
    for event in events:
        phase, action = table.get((phase, event), (phase, "reject"))
        if action == 'request_listing':
            frames.append(['LIST',seed])
        elif action == 'stage_service':
            r['staged'].append('svc'+str(seed))
        elif action == 'publish_listing':
            r['listing']=[x for x in r['staged'] if x not in r['tombstones']]; r['notifications'].append(r['listing'][:])
        elif action == 'insert_notify':
            r['listing'].append('svc'+str(seed)); r['notifications'].append(['add','svc'+str(seed)])
        elif action == 'remove_notify':
            r['listing']=[]; r['notifications'].append(['remove','stale'])
        elif action == 'clear_request_listing':
            r['listing']=[]; r['staged']=[]; frames.append(['LIST',seed])
        elif action == 'expire_notify':
            r['listing']=[]; r['notifications'].append(['expired','stale'])
        elif action == 'release_watch':
            r['watch_owned']=False
        elif action == 'ignore_closed':
            frames.append(['IGNORED_CLOSED',seed])
        elif action == 'record_tombstone':
            r['tombstones'].append('svc'+str(seed))
        elif action == 'publish_ready':
            r['notifications'].append('ready')
        elif action == 'request_delta':
            frames.append(['DELTA',seed])
        elif action == 'publish_service':
            r['notifications'].append('svc'+str(seed))
        elif action == 'replace_listing':
            r['staged']=['svc'+str(seed)]
        elif action == 'publish_last_only':
            r['listing']=r['staged'][-1:]
        elif action == 'replace_all':
            r['listing']=['svc'+str(seed)]
        elif action == 'insert_silent':
            r['listing'].append('svc'+str(seed))
        elif action == 'notify_without_remove':
            r['notifications'].append(['remove','stale'])
        elif action == 'merge_listing':
            r['listing'].extend(r['staged'])
        elif action == 'refresh_lease':
            frames.append(['REFRESH','stale'])
        elif action == 'expire_silent':
            r['listing']=[]
        elif action == 'publish_remove':
            r['notifications'].append(['remove','svc'+str(seed)])
        elif action == 'continue':
            r['notifications'].append(['add','svc'+str(seed)])
        else:
            if action not in ("wait", "ignore", "keep", "retain", "keep_peer", "keep_offer", "keep_hold", "keep_batch", "retain_staging", "keep_staging", "keep_local_owner", "keep_old_session", "keep_address", "keep_old_expiry", "ignore_conflict", "keep_service", "keep_subscription", "retain_shadow", "keep_live", "keep_suspicion", "keep_roles", "keep_staged"):
                frames.append([action, payload[:]])
        observed.append(json.loads(json.dumps({"phase": phase, "registers": r, "frames": frames})))
    return observed
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('idle/start_watch', 'idle', ['start_watch'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 1]]}]), ('snapshotting/listed_service', 'snapshotting', ['listed_service'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first', 'svc1'], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('snapshotting/listing_end', 'snapshotting', ['listing_end'], [{'phase': 'live', 'registers': {'listing': ['first'], 'staged': ['first'], 'tombstones': [], 'notifications': [['first']], 'watch_owned': True}, 'frames': []}]), ('live/service_added', 'live', ['service_added'], [{'phase': 'live', 'registers': {'listing': ['stale', 'svc1'], 'staged': [], 'tombstones': [], 'notifications': [['add', 'svc1']], 'watch_owned': True}, 'frames': []}]), ('live/service_removed', 'live', ['service_removed'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['remove', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/watch_gap', 'live', ['watch_gap'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 1]]}]), ('live/lease_expired', 'live', ['lease_expired'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/stop_watch', 'live', ['stop_watch'], [{'phase': 'closed', 'registers': {'listing': ['stale'], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': []}]), ('closed/service_added', 'closed', ['service_added'], [{'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 1]]}]), ('snapshotting/service_removed', 'snapshotting', ['service_removed'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first'], 'tombstones': ['svc1'], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('ordered trace 0', 'idle', ['start_watch', 'listing_end', 'watch_gap', 'listed_service'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 1]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 1]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 1], ['LIST', 1]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['svc1'], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 1], ['LIST', 1]]}]), ('ordered trace 3', 'live', ['lease_expired', 'stop_watch', 'service_added', 'service_added'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 1]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 1], ['IGNORED_CLOSED', 1]]}]), ('empty input', 'idle', [], []), ('unknown event', 'idle', ['unsupported'], [{'phase': 'idle', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['reject', [1, 2, 3]]]}])], 2: [('idle/start_watch', 'idle', ['start_watch'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 2]]}]), ('snapshotting/listed_service', 'snapshotting', ['listed_service'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first', 'svc2'], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('snapshotting/listing_end', 'snapshotting', ['listing_end'], [{'phase': 'live', 'registers': {'listing': ['first'], 'staged': ['first'], 'tombstones': [], 'notifications': [['first']], 'watch_owned': True}, 'frames': []}]), ('live/service_added', 'live', ['service_added'], [{'phase': 'live', 'registers': {'listing': ['stale', 'svc2'], 'staged': [], 'tombstones': [], 'notifications': [['add', 'svc2']], 'watch_owned': True}, 'frames': []}]), ('live/service_removed', 'live', ['service_removed'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['remove', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/watch_gap', 'live', ['watch_gap'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 2]]}]), ('live/lease_expired', 'live', ['lease_expired'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/stop_watch', 'live', ['stop_watch'], [{'phase': 'closed', 'registers': {'listing': ['stale'], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': []}]), ('closed/service_added', 'closed', ['service_added'], [{'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 2]]}]), ('snapshotting/service_removed', 'snapshotting', ['service_removed'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first'], 'tombstones': ['svc2'], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('ordered trace 0', 'idle', ['start_watch', 'listing_end', 'watch_gap', 'listed_service', 'listing_end'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 2]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 2]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 2], ['LIST', 2]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['svc2'], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 2], ['LIST', 2]]}, {'phase': 'live', 'registers': {'listing': ['svc2'], 'staged': ['svc2'], 'tombstones': [], 'notifications': [[], ['svc2']], 'watch_owned': True}, 'frames': [['LIST', 2], ['LIST', 2]]}]), ('ordered trace 3', 'live', ['lease_expired', 'stop_watch', 'service_added', 'service_added', 'service_added'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 2]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 2], ['IGNORED_CLOSED', 2]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 2], ['IGNORED_CLOSED', 2], ['IGNORED_CLOSED', 2]]}]), ('empty input', 'idle', [], []), ('unknown event', 'idle', ['unsupported'], [{'phase': 'idle', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['reject', [2, 3, 4, 5]]]}])], 3: [('idle/start_watch', 'idle', ['start_watch'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 3]]}]), ('snapshotting/listed_service', 'snapshotting', ['listed_service'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first', 'svc3'], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('snapshotting/listing_end', 'snapshotting', ['listing_end'], [{'phase': 'live', 'registers': {'listing': ['first'], 'staged': ['first'], 'tombstones': [], 'notifications': [['first']], 'watch_owned': True}, 'frames': []}]), ('live/service_added', 'live', ['service_added'], [{'phase': 'live', 'registers': {'listing': ['stale', 'svc3'], 'staged': [], 'tombstones': [], 'notifications': [['add', 'svc3']], 'watch_owned': True}, 'frames': []}]), ('live/service_removed', 'live', ['service_removed'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['remove', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/watch_gap', 'live', ['watch_gap'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 3]]}]), ('live/lease_expired', 'live', ['lease_expired'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/stop_watch', 'live', ['stop_watch'], [{'phase': 'closed', 'registers': {'listing': ['stale'], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': []}]), ('closed/service_added', 'closed', ['service_added'], [{'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 3]]}]), ('snapshotting/service_removed', 'snapshotting', ['service_removed'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first'], 'tombstones': ['svc3'], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('ordered trace 0', 'idle', ['start_watch', 'listing_end', 'watch_gap', 'listed_service', 'listing_end', 'service_added'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 3]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 3]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 3], ['LIST', 3]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['svc3'], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 3], ['LIST', 3]]}, {'phase': 'live', 'registers': {'listing': ['svc3'], 'staged': ['svc3'], 'tombstones': [], 'notifications': [[], ['svc3']], 'watch_owned': True}, 'frames': [['LIST', 3], ['LIST', 3]]}, {'phase': 'live', 'registers': {'listing': ['svc3', 'svc3'], 'staged': ['svc3'], 'tombstones': [], 'notifications': [[], ['svc3'], ['add', 'svc3']], 'watch_owned': True}, 'frames': [['LIST', 3], ['LIST', 3]]}]), ('ordered trace 3', 'live', ['lease_expired', 'stop_watch', 'service_added', 'service_added', 'service_added', 'service_added'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 3]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 3], ['IGNORED_CLOSED', 3]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 3], ['IGNORED_CLOSED', 3], ['IGNORED_CLOSED', 3]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 3], ['IGNORED_CLOSED', 3], ['IGNORED_CLOSED', 3], ['IGNORED_CLOSED', 3]]}]), ('empty input', 'idle', [], []), ('unknown event', 'idle', ['unsupported'], [{'phase': 'idle', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['reject', [3, 4, 5, 6, 7]]]}])], 4: [('idle/start_watch', 'idle', ['start_watch'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 4]]}]), ('snapshotting/listed_service', 'snapshotting', ['listed_service'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first', 'svc4'], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('snapshotting/listing_end', 'snapshotting', ['listing_end'], [{'phase': 'live', 'registers': {'listing': ['first'], 'staged': ['first'], 'tombstones': [], 'notifications': [['first']], 'watch_owned': True}, 'frames': []}]), ('live/service_added', 'live', ['service_added'], [{'phase': 'live', 'registers': {'listing': ['stale', 'svc4'], 'staged': [], 'tombstones': [], 'notifications': [['add', 'svc4']], 'watch_owned': True}, 'frames': []}]), ('live/service_removed', 'live', ['service_removed'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['remove', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/watch_gap', 'live', ['watch_gap'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 4]]}]), ('live/lease_expired', 'live', ['lease_expired'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/stop_watch', 'live', ['stop_watch'], [{'phase': 'closed', 'registers': {'listing': ['stale'], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': []}]), ('closed/service_added', 'closed', ['service_added'], [{'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 4]]}]), ('snapshotting/service_removed', 'snapshotting', ['service_removed'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first'], 'tombstones': ['svc4'], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('ordered trace 0', 'idle', ['start_watch', 'listing_end', 'watch_gap', 'listed_service', 'listing_end', 'service_added', 'service_removed'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 4]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 4]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 4], ['LIST', 4]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['svc4'], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 4], ['LIST', 4]]}, {'phase': 'live', 'registers': {'listing': ['svc4'], 'staged': ['svc4'], 'tombstones': [], 'notifications': [[], ['svc4']], 'watch_owned': True}, 'frames': [['LIST', 4], ['LIST', 4]]}, {'phase': 'live', 'registers': {'listing': ['svc4', 'svc4'], 'staged': ['svc4'], 'tombstones': [], 'notifications': [[], ['svc4'], ['add', 'svc4']], 'watch_owned': True}, 'frames': [['LIST', 4], ['LIST', 4]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': ['svc4'], 'tombstones': [], 'notifications': [[], ['svc4'], ['add', 'svc4'], ['remove', 'stale']], 'watch_owned': True}, 'frames': [['LIST', 4], ['LIST', 4]]}]), ('ordered trace 3', 'live', ['lease_expired', 'stop_watch', 'service_added', 'service_added', 'service_added', 'service_added', 'service_added'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 4]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4]]}]), ('empty input', 'idle', [], []), ('unknown event', 'idle', ['unsupported'], [{'phase': 'idle', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['reject', [4, 5, 6, 7, 8, 9]]]}])], 5: [('idle/start_watch', 'idle', ['start_watch'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 5]]}]), ('snapshotting/listed_service', 'snapshotting', ['listed_service'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first', 'svc5'], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('snapshotting/listing_end', 'snapshotting', ['listing_end'], [{'phase': 'live', 'registers': {'listing': ['first'], 'staged': ['first'], 'tombstones': [], 'notifications': [['first']], 'watch_owned': True}, 'frames': []}]), ('live/service_added', 'live', ['service_added'], [{'phase': 'live', 'registers': {'listing': ['stale', 'svc5'], 'staged': [], 'tombstones': [], 'notifications': [['add', 'svc5']], 'watch_owned': True}, 'frames': []}]), ('live/service_removed', 'live', ['service_removed'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['remove', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/watch_gap', 'live', ['watch_gap'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 5]]}]), ('live/lease_expired', 'live', ['lease_expired'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/stop_watch', 'live', ['stop_watch'], [{'phase': 'closed', 'registers': {'listing': ['stale'], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': []}]), ('closed/service_added', 'closed', ['service_added'], [{'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5]]}]), ('snapshotting/service_removed', 'snapshotting', ['service_removed'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first'], 'tombstones': ['svc5'], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('ordered trace 0', 'idle', ['start_watch', 'listing_end', 'watch_gap', 'listed_service', 'listing_end', 'service_added', 'service_removed', 'watch_gap'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 5]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 5]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 5], ['LIST', 5]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['svc5'], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 5], ['LIST', 5]]}, {'phase': 'live', 'registers': {'listing': ['svc5'], 'staged': ['svc5'], 'tombstones': [], 'notifications': [[], ['svc5']], 'watch_owned': True}, 'frames': [['LIST', 5], ['LIST', 5]]}, {'phase': 'live', 'registers': {'listing': ['svc5', 'svc5'], 'staged': ['svc5'], 'tombstones': [], 'notifications': [[], ['svc5'], ['add', 'svc5']], 'watch_owned': True}, 'frames': [['LIST', 5], ['LIST', 5]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': ['svc5'], 'tombstones': [], 'notifications': [[], ['svc5'], ['add', 'svc5'], ['remove', 'stale']], 'watch_owned': True}, 'frames': [['LIST', 5], ['LIST', 5]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[], ['svc5'], ['add', 'svc5'], ['remove', 'stale']], 'watch_owned': True}, 'frames': [['LIST', 5], ['LIST', 5], ['LIST', 5]]}]), ('ordered trace 3', 'live', ['lease_expired', 'stop_watch', 'service_added', 'service_added', 'service_added', 'service_added', 'service_added', 'service_added'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5]]}]), ('empty input', 'idle', [], []), ('unknown event', 'idle', ['unsupported'], [{'phase': 'idle', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['reject', [5, 6, 7, 8, 9, 10, 11]]]}])]}
for label, phase, events, expected in cases[N]:
    check(label, solve(phase, events, N), 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(phase, events, seed):
    payload = list(range(seed, seed * 2 + 2))
    r = {'listing': ['stale'] if phase=='live' else [], 'staged': ['first'] if phase=='snapshotting' else [], 'tombstones': [], 'notifications': [], 'watch_owned': phase!='closed'}
    frames = []
    observed = []
    table = {('idle', 'start_watch'): ('snapshotting', 'request_listing'), ('snapshotting', 'listed_service'): ('snapshotting', 'stage_service'), ('snapshotting', 'listing_end'): ('live', 'publish_listing'), ('live', 'service_added'): ('live', 'insert_notify'), ('live', 'service_removed'): ('live', 'notify_without_remove'), ('live', 'watch_gap'): ('snapshotting', 'clear_request_listing'), ('live', 'lease_expired'): ('live', 'expire_notify'), ('live', 'stop_watch'): ('closed', 'release_watch'), ('closed', 'service_added'): ('closed', 'ignore_closed'), ('snapshotting', 'service_removed'): ('snapshotting', 'record_tombstone')}
    for event in events:
        phase, action = table.get((phase, event), (phase, "reject"))
        if action == 'request_listing':
            frames.append(['LIST',seed])
        elif action == 'stage_service':
            r['staged'].append('svc'+str(seed))
        elif action == 'publish_listing':
            r['listing']=[x for x in r['staged'] if x not in r['tombstones']]; r['notifications'].append(r['listing'][:])
        elif action == 'insert_notify':
            r['listing'].append('svc'+str(seed)); r['notifications'].append(['add','svc'+str(seed)])
        elif action == 'remove_notify':
            r['listing']=[]; r['notifications'].append(['remove','stale'])
        elif action == 'clear_request_listing':
            r['listing']=[]; r['staged']=[]; frames.append(['LIST',seed])
        elif action == 'expire_notify':
            r['listing']=[]; r['notifications'].append(['expired','stale'])
        elif action == 'release_watch':
            r['watch_owned']=False
        elif action == 'ignore_closed':
            frames.append(['IGNORED_CLOSED',seed])
        elif action == 'record_tombstone':
            r['tombstones'].append('svc'+str(seed))
        elif action == 'publish_ready':
            r['notifications'].append('ready')
        elif action == 'request_delta':
            frames.append(['DELTA',seed])
        elif action == 'publish_service':
            r['notifications'].append('svc'+str(seed))
        elif action == 'replace_listing':
            r['staged']=['svc'+str(seed)]
        elif action == 'publish_last_only':
            r['listing']=r['staged'][-1:]
        elif action == 'replace_all':
            r['listing']=['svc'+str(seed)]
        elif action == 'insert_silent':
            r['listing'].append('svc'+str(seed))
        elif action == 'notify_without_remove':
            r['notifications'].append(['remove','stale'])
        elif action == 'merge_listing':
            r['listing'].extend(r['staged'])
        elif action == 'refresh_lease':
            frames.append(['REFRESH','stale'])
        elif action == 'expire_silent':
            r['listing']=[]
        elif action == 'publish_remove':
            r['notifications'].append(['remove','svc'+str(seed)])
        elif action == 'continue':
            r['notifications'].append(['add','svc'+str(seed)])
        else:
            if action not in ("wait", "ignore", "keep", "retain", "keep_peer", "keep_offer", "keep_hold", "keep_batch", "retain_staging", "keep_staging", "keep_local_owner", "keep_old_session", "keep_address", "keep_old_expiry", "ignore_conflict", "keep_service", "keep_subscription", "retain_shadow", "keep_live", "keep_suspicion", "keep_roles", "keep_staged"):
                frames.append([action, payload[:]])
        observed.append(json.loads(json.dumps({"phase": phase, "registers": r, "frames": frames})))
    return observed
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {1: [('idle/start_watch', 'idle', ['start_watch'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 1]]}]), ('snapshotting/listed_service', 'snapshotting', ['listed_service'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first', 'svc1'], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('snapshotting/listing_end', 'snapshotting', ['listing_end'], [{'phase': 'live', 'registers': {'listing': ['first'], 'staged': ['first'], 'tombstones': [], 'notifications': [['first']], 'watch_owned': True}, 'frames': []}]), ('live/service_added', 'live', ['service_added'], [{'phase': 'live', 'registers': {'listing': ['stale', 'svc1'], 'staged': [], 'tombstones': [], 'notifications': [['add', 'svc1']], 'watch_owned': True}, 'frames': []}]), ('live/service_removed', 'live', ['service_removed'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['remove', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/watch_gap', 'live', ['watch_gap'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 1]]}]), ('live/lease_expired', 'live', ['lease_expired'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/stop_watch', 'live', ['stop_watch'], [{'phase': 'closed', 'registers': {'listing': ['stale'], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': []}]), ('closed/service_added', 'closed', ['service_added'], [{'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 1]]}]), ('snapshotting/service_removed', 'snapshotting', ['service_removed'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first'], 'tombstones': ['svc1'], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('ordered trace 0', 'idle', ['start_watch', 'listing_end', 'watch_gap', 'listed_service'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 1]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 1]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 1], ['LIST', 1]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['svc1'], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 1], ['LIST', 1]]}]), ('ordered trace 3', 'live', ['lease_expired', 'stop_watch', 'service_added', 'service_added'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 1]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 1], ['IGNORED_CLOSED', 1]]}]), ('empty input', 'idle', [], []), ('unknown event', 'idle', ['unsupported'], [{'phase': 'idle', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['reject', [1, 2, 3]]]}])], 2: [('idle/start_watch', 'idle', ['start_watch'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 2]]}]), ('snapshotting/listed_service', 'snapshotting', ['listed_service'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first', 'svc2'], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('snapshotting/listing_end', 'snapshotting', ['listing_end'], [{'phase': 'live', 'registers': {'listing': ['first'], 'staged': ['first'], 'tombstones': [], 'notifications': [['first']], 'watch_owned': True}, 'frames': []}]), ('live/service_added', 'live', ['service_added'], [{'phase': 'live', 'registers': {'listing': ['stale', 'svc2'], 'staged': [], 'tombstones': [], 'notifications': [['add', 'svc2']], 'watch_owned': True}, 'frames': []}]), ('live/service_removed', 'live', ['service_removed'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['remove', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/watch_gap', 'live', ['watch_gap'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 2]]}]), ('live/lease_expired', 'live', ['lease_expired'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/stop_watch', 'live', ['stop_watch'], [{'phase': 'closed', 'registers': {'listing': ['stale'], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': []}]), ('closed/service_added', 'closed', ['service_added'], [{'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 2]]}]), ('snapshotting/service_removed', 'snapshotting', ['service_removed'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first'], 'tombstones': ['svc2'], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('ordered trace 0', 'idle', ['start_watch', 'listing_end', 'watch_gap', 'listed_service', 'listing_end'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 2]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 2]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 2], ['LIST', 2]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['svc2'], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 2], ['LIST', 2]]}, {'phase': 'live', 'registers': {'listing': ['svc2'], 'staged': ['svc2'], 'tombstones': [], 'notifications': [[], ['svc2']], 'watch_owned': True}, 'frames': [['LIST', 2], ['LIST', 2]]}]), ('ordered trace 3', 'live', ['lease_expired', 'stop_watch', 'service_added', 'service_added', 'service_added'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 2]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 2], ['IGNORED_CLOSED', 2]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 2], ['IGNORED_CLOSED', 2], ['IGNORED_CLOSED', 2]]}]), ('empty input', 'idle', [], []), ('unknown event', 'idle', ['unsupported'], [{'phase': 'idle', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['reject', [2, 3, 4, 5]]]}])], 3: [('idle/start_watch', 'idle', ['start_watch'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 3]]}]), ('snapshotting/listed_service', 'snapshotting', ['listed_service'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first', 'svc3'], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('snapshotting/listing_end', 'snapshotting', ['listing_end'], [{'phase': 'live', 'registers': {'listing': ['first'], 'staged': ['first'], 'tombstones': [], 'notifications': [['first']], 'watch_owned': True}, 'frames': []}]), ('live/service_added', 'live', ['service_added'], [{'phase': 'live', 'registers': {'listing': ['stale', 'svc3'], 'staged': [], 'tombstones': [], 'notifications': [['add', 'svc3']], 'watch_owned': True}, 'frames': []}]), ('live/service_removed', 'live', ['service_removed'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['remove', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/watch_gap', 'live', ['watch_gap'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 3]]}]), ('live/lease_expired', 'live', ['lease_expired'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/stop_watch', 'live', ['stop_watch'], [{'phase': 'closed', 'registers': {'listing': ['stale'], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': []}]), ('closed/service_added', 'closed', ['service_added'], [{'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 3]]}]), ('snapshotting/service_removed', 'snapshotting', ['service_removed'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first'], 'tombstones': ['svc3'], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('ordered trace 0', 'idle', ['start_watch', 'listing_end', 'watch_gap', 'listed_service', 'listing_end', 'service_added'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 3]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 3]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 3], ['LIST', 3]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['svc3'], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 3], ['LIST', 3]]}, {'phase': 'live', 'registers': {'listing': ['svc3'], 'staged': ['svc3'], 'tombstones': [], 'notifications': [[], ['svc3']], 'watch_owned': True}, 'frames': [['LIST', 3], ['LIST', 3]]}, {'phase': 'live', 'registers': {'listing': ['svc3', 'svc3'], 'staged': ['svc3'], 'tombstones': [], 'notifications': [[], ['svc3'], ['add', 'svc3']], 'watch_owned': True}, 'frames': [['LIST', 3], ['LIST', 3]]}]), ('ordered trace 3', 'live', ['lease_expired', 'stop_watch', 'service_added', 'service_added', 'service_added', 'service_added'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 3]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 3], ['IGNORED_CLOSED', 3]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 3], ['IGNORED_CLOSED', 3], ['IGNORED_CLOSED', 3]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 3], ['IGNORED_CLOSED', 3], ['IGNORED_CLOSED', 3], ['IGNORED_CLOSED', 3]]}]), ('empty input', 'idle', [], []), ('unknown event', 'idle', ['unsupported'], [{'phase': 'idle', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['reject', [3, 4, 5, 6, 7]]]}])], 4: [('idle/start_watch', 'idle', ['start_watch'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 4]]}]), ('snapshotting/listed_service', 'snapshotting', ['listed_service'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first', 'svc4'], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('snapshotting/listing_end', 'snapshotting', ['listing_end'], [{'phase': 'live', 'registers': {'listing': ['first'], 'staged': ['first'], 'tombstones': [], 'notifications': [['first']], 'watch_owned': True}, 'frames': []}]), ('live/service_added', 'live', ['service_added'], [{'phase': 'live', 'registers': {'listing': ['stale', 'svc4'], 'staged': [], 'tombstones': [], 'notifications': [['add', 'svc4']], 'watch_owned': True}, 'frames': []}]), ('live/service_removed', 'live', ['service_removed'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['remove', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/watch_gap', 'live', ['watch_gap'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 4]]}]), ('live/lease_expired', 'live', ['lease_expired'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/stop_watch', 'live', ['stop_watch'], [{'phase': 'closed', 'registers': {'listing': ['stale'], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': []}]), ('closed/service_added', 'closed', ['service_added'], [{'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 4]]}]), ('snapshotting/service_removed', 'snapshotting', ['service_removed'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first'], 'tombstones': ['svc4'], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('ordered trace 0', 'idle', ['start_watch', 'listing_end', 'watch_gap', 'listed_service', 'listing_end', 'service_added', 'service_removed'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 4]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 4]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 4], ['LIST', 4]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['svc4'], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 4], ['LIST', 4]]}, {'phase': 'live', 'registers': {'listing': ['svc4'], 'staged': ['svc4'], 'tombstones': [], 'notifications': [[], ['svc4']], 'watch_owned': True}, 'frames': [['LIST', 4], ['LIST', 4]]}, {'phase': 'live', 'registers': {'listing': ['svc4', 'svc4'], 'staged': ['svc4'], 'tombstones': [], 'notifications': [[], ['svc4'], ['add', 'svc4']], 'watch_owned': True}, 'frames': [['LIST', 4], ['LIST', 4]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': ['svc4'], 'tombstones': [], 'notifications': [[], ['svc4'], ['add', 'svc4'], ['remove', 'stale']], 'watch_owned': True}, 'frames': [['LIST', 4], ['LIST', 4]]}]), ('ordered trace 3', 'live', ['lease_expired', 'stop_watch', 'service_added', 'service_added', 'service_added', 'service_added', 'service_added'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 4]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4], ['IGNORED_CLOSED', 4]]}]), ('empty input', 'idle', [], []), ('unknown event', 'idle', ['unsupported'], [{'phase': 'idle', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['reject', [4, 5, 6, 7, 8, 9]]]}])], 5: [('idle/start_watch', 'idle', ['start_watch'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 5]]}]), ('snapshotting/listed_service', 'snapshotting', ['listed_service'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first', 'svc5'], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('snapshotting/listing_end', 'snapshotting', ['listing_end'], [{'phase': 'live', 'registers': {'listing': ['first'], 'staged': ['first'], 'tombstones': [], 'notifications': [['first']], 'watch_owned': True}, 'frames': []}]), ('live/service_added', 'live', ['service_added'], [{'phase': 'live', 'registers': {'listing': ['stale', 'svc5'], 'staged': [], 'tombstones': [], 'notifications': [['add', 'svc5']], 'watch_owned': True}, 'frames': []}]), ('live/service_removed', 'live', ['service_removed'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['remove', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/watch_gap', 'live', ['watch_gap'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 5]]}]), ('live/lease_expired', 'live', ['lease_expired'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}]), ('live/stop_watch', 'live', ['stop_watch'], [{'phase': 'closed', 'registers': {'listing': ['stale'], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': []}]), ('closed/service_added', 'closed', ['service_added'], [{'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5]]}]), ('snapshotting/service_removed', 'snapshotting', ['service_removed'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['first'], 'tombstones': ['svc5'], 'notifications': [], 'watch_owned': True}, 'frames': []}]), ('ordered trace 0', 'idle', ['start_watch', 'listing_end', 'watch_gap', 'listed_service', 'listing_end', 'service_added', 'service_removed', 'watch_gap'], [{'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['LIST', 5]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 5]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 5], ['LIST', 5]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': ['svc5'], 'tombstones': [], 'notifications': [[]], 'watch_owned': True}, 'frames': [['LIST', 5], ['LIST', 5]]}, {'phase': 'live', 'registers': {'listing': ['svc5'], 'staged': ['svc5'], 'tombstones': [], 'notifications': [[], ['svc5']], 'watch_owned': True}, 'frames': [['LIST', 5], ['LIST', 5]]}, {'phase': 'live', 'registers': {'listing': ['svc5', 'svc5'], 'staged': ['svc5'], 'tombstones': [], 'notifications': [[], ['svc5'], ['add', 'svc5']], 'watch_owned': True}, 'frames': [['LIST', 5], ['LIST', 5]]}, {'phase': 'live', 'registers': {'listing': [], 'staged': ['svc5'], 'tombstones': [], 'notifications': [[], ['svc5'], ['add', 'svc5'], ['remove', 'stale']], 'watch_owned': True}, 'frames': [['LIST', 5], ['LIST', 5]]}, {'phase': 'snapshotting', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [[], ['svc5'], ['add', 'svc5'], ['remove', 'stale']], 'watch_owned': True}, 'frames': [['LIST', 5], ['LIST', 5], ['LIST', 5]]}]), ('ordered trace 3', 'live', ['lease_expired', 'stop_watch', 'service_added', 'service_added', 'service_added', 'service_added', 'service_added', 'service_added'], [{'phase': 'live', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': True}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': []}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5]]}, {'phase': 'closed', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [['expired', 'stale']], 'watch_owned': False}, 'frames': [['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5], ['IGNORED_CLOSED', 5]]}]), ('empty input', 'idle', [], []), ('unknown event', 'idle', ['unsupported'], [{'phase': 'idle', 'registers': {'listing': [], 'staged': [], 'tombstones': [], 'notifications': [], 'watch_owned': True}, 'frames': [['reject', [5, 6, 7, 8, 9, 10, 11]]]}])]}
for label, phase, events, expected in cases[N]:
    check(label, solve(phase, events, N), 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:
While live, event service_removed produces live/keep_service instead of live/remove_notify.

For the stipulated Service-discovery watch lifecycle controller, initialize the explicitly shown resource registers from seed (1..5), then consume classified events sequentially. After each event return a deep snapshot of phase, resource registers, and cumulative emitted control frames. The explicit ten phase/event rules and their resource effects define admission and response semantics. Unlisted events preserve phase and emit rejection. Payload is the consecutive integers seed through 2*seed+1. Empty event input returns no observations.

split: open-access