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

N = 1
observations = []
def solve(initial, events, generation, payload=None, observe=None):
    table = {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'archived', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}
    data = json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})) if payload is None else {**json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})), **json.loads(json.dumps(payload))}
    state = initial
    for delivery in events:
        event, event_generation = delivery[:2]
        arg = delivery[2] if len(delivery) > 2 else {}
        if event_generation == generation:
            previous = state
            if event == 'archive-click': data['saved'] = dict(data['row'])
            if event == 'new-revision-arrives':
                revision = arg.get('revision',data['row']['revision']+1)
                if revision <= data['row']['revision']: event = 'unknown-event'
                else: data['row'] = {'revision':revision,'text':arg.get('text','new')}
            state = table.get((state, event), state)
            if state in ('archived','optimistically-hidden','hidden-new-revision','restore-error','restoring'): data['visible'] = False
            if state in ('row-visible','restored-error','restored-new-revision-error'): data['visible'] = True
            if state == 'restored-error' and data['saved'] is not None: data['row'] = dict(data['saved'])
    return {'phase': state, 'value': data[observe]} if observe else state
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('row-visible / archive-click', solve('row-visible', [('archive-click', N)], N), 'optimistically-hidden')
check('optimistically-hidden / archive-success', solve('optimistically-hidden', [('archive-success', N)], N), 'archived')
check('optimistically-hidden / archive-failure', solve('optimistically-hidden', [('archive-failure', N)], N), 'restored-error')
check('restored-error / retry-archive', solve('restored-error', [('retry-archive', N)], N), 'optimistically-hidden')
check('archived / restore-click', solve('archived', [('restore-click', N)], N), 'restoring')
check('restoring / restore-success', solve('restoring', [('restore-success', N)], N), 'row-visible')
check('restoring / restore-failure', solve('restoring', [('restore-failure', N)], N), 'restore-error')
check('restore-error / retry-restore', solve('restore-error', [('retry-restore', N)], N), 'restoring')
check('optimistically-hidden / new-revision-arrives', solve('optimistically-hidden', [('new-revision-arrives', N)], N), 'hidden-new-revision')
check('hidden-new-revision / archive-failure', solve('hidden-new-revision', [('archive-failure', N)], N), 'restored-new-revision-error')
check('older notification incarnation', solve('row-visible', [('archive-click', N - 1)], N), 'row-visible')
check('future notification incarnation', solve('row-visible', [('archive-click', N + 1)], N), 'row-visible')
check('unknown event is inert', solve('row-visible', [('unknown-event', N)], N), 'row-visible')
check('empty delivery batch', solve('row-visible', [], N), 'row-visible')
check('N stale deliveries before current delivery', solve('row-visible', [('archive-click', N - 1)] * N + [('archive-click', N)], N), 'optimistically-hidden')
check('trace archive-click then archive-success', solve('row-visible', [('archive-click', N), ('archive-success', N)], N), 'archived')
check('trace archive-click then archive-failure', solve('row-visible', [('archive-click', N), ('archive-failure', N)], N), 'restored-error')
check('trace archive-click then new-revision-arrives', solve('row-visible', [('archive-click', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace archive-success then restore-click', solve('optimistically-hidden', [('archive-success', N), ('restore-click', N)], N), 'restoring')
check('trace archive-failure then retry-archive', solve('optimistically-hidden', [('archive-failure', N), ('retry-archive', N)], N), 'optimistically-hidden')
check('trace retry-archive then archive-success', solve('restored-error', [('retry-archive', N), ('archive-success', N)], N), 'archived')
check('trace retry-archive then archive-failure', solve('restored-error', [('retry-archive', N), ('archive-failure', N)], N), 'restored-error')
check('trace retry-archive then new-revision-arrives', solve('restored-error', [('retry-archive', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace restore-click then restore-success', solve('archived', [('restore-click', N), ('restore-success', N)], N), 'row-visible')
check('trace restore-click then restore-failure', solve('archived', [('restore-click', N), ('restore-failure', N)], N), 'restore-error')
check('trace restore-success then archive-click', solve('restoring', [('restore-success', N), ('archive-click', N)], N), 'optimistically-hidden')
check('trace restore-failure then retry-restore', solve('restoring', [('restore-failure', N), ('retry-restore', N)], N), 'restoring')
check('trace retry-restore then restore-success', solve('restore-error', [('retry-restore', N), ('restore-success', N)], N), 'row-visible')
check('trace retry-restore then restore-failure', solve('restore-error', [('retry-restore', N), ('restore-failure', N)], N), 'restore-error')
check('trace new-revision-arrives then archive-failure', solve('optimistically-hidden', [('new-revision-arrives', N), ('archive-failure', N)], N), 'restored-new-revision-error')
check('domain state regression 1: saved', solve('row-visible', [('archive-click', N, {})], N, {}, 'saved'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 2: row', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 1, 'text': 'stale'})], N, {}, 'row'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 3: visible', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 2, 'text': 'new'})], N, {}, 'visible'), {'phase': 'hidden-new-revision', 'value': False})
check('domain state regression 4: row', solve('hidden-new-revision', [('archive-failure', N, {})], N, {'row': {'revision': 2, 'text': 'new'}, 'saved': {'revision': 1, 'text': 'old'}}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
check('domain state regression 5: row', solve('optimistically-hidden', [('archive-failure', N, {})], N, {'saved': {'revision': 1, 'text': 'original'}}, 'row'), {'phase': 'restored-error', 'value': {'revision': 1, 'text': 'original'}})
check('domain state regression 6: visible', solve('restoring', [('restore-failure', N, {})], N, {}, 'visible'), {'phase': 'restore-error', 'value': False})
check('domain state regression 7: row', solve('row-visible', [('archive-click', N, {}), ('new-revision-arrives', N, {'revision': 2, 'text': 'new'}), ('archive-failure', N, {})], N, {}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
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(initial, events, generation, payload=None, observe=None):
    table = {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'restore-error', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}
    data = json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})) if payload is None else {**json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})), **json.loads(json.dumps(payload))}
    state = initial
    for delivery in events:
        event, event_generation = delivery[:2]
        arg = delivery[2] if len(delivery) > 2 else {}
        if event_generation == generation:
            previous = state
            if event == 'archive-click': data['saved'] = dict(data['row'])
            if event == 'new-revision-arrives':
                revision = arg.get('revision',data['row']['revision']+1)
                if revision <= data['row']['revision']: event = 'unknown-event'
                else: data['row'] = {'revision':revision,'text':arg.get('text','new')}
            state = table.get((state, event), state)
            if state in ('archived','optimistically-hidden','hidden-new-revision','restore-error','restoring'): data['visible'] = False
            if state in ('row-visible','restored-error','restored-new-revision-error'): data['visible'] = True
            if state == 'restored-error' and data['saved'] is not None: data['row'] = dict(data['saved'])
    return {'phase': state, 'value': data[observe]} if observe else state
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('row-visible / archive-click', solve('row-visible', [('archive-click', N)], N), 'optimistically-hidden')
check('optimistically-hidden / archive-success', solve('optimistically-hidden', [('archive-success', N)], N), 'archived')
check('optimistically-hidden / archive-failure', solve('optimistically-hidden', [('archive-failure', N)], N), 'restored-error')
check('restored-error / retry-archive', solve('restored-error', [('retry-archive', N)], N), 'optimistically-hidden')
check('archived / restore-click', solve('archived', [('restore-click', N)], N), 'restoring')
check('restoring / restore-success', solve('restoring', [('restore-success', N)], N), 'row-visible')
check('restoring / restore-failure', solve('restoring', [('restore-failure', N)], N), 'restore-error')
check('restore-error / retry-restore', solve('restore-error', [('retry-restore', N)], N), 'restoring')
check('optimistically-hidden / new-revision-arrives', solve('optimistically-hidden', [('new-revision-arrives', N)], N), 'hidden-new-revision')
check('hidden-new-revision / archive-failure', solve('hidden-new-revision', [('archive-failure', N)], N), 'restored-new-revision-error')
check('older notification incarnation', solve('row-visible', [('archive-click', N - 1)], N), 'row-visible')
check('future notification incarnation', solve('row-visible', [('archive-click', N + 1)], N), 'row-visible')
check('unknown event is inert', solve('row-visible', [('unknown-event', N)], N), 'row-visible')
check('empty delivery batch', solve('row-visible', [], N), 'row-visible')
check('N stale deliveries before current delivery', solve('row-visible', [('archive-click', N - 1)] * N + [('archive-click', N)], N), 'optimistically-hidden')
check('trace archive-click then archive-success', solve('row-visible', [('archive-click', N), ('archive-success', N)], N), 'archived')
check('trace archive-click then archive-failure', solve('row-visible', [('archive-click', N), ('archive-failure', N)], N), 'restored-error')
check('trace archive-click then new-revision-arrives', solve('row-visible', [('archive-click', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace archive-success then restore-click', solve('optimistically-hidden', [('archive-success', N), ('restore-click', N)], N), 'restoring')
check('trace archive-failure then retry-archive', solve('optimistically-hidden', [('archive-failure', N), ('retry-archive', N)], N), 'optimistically-hidden')
check('trace retry-archive then archive-success', solve('restored-error', [('retry-archive', N), ('archive-success', N)], N), 'archived')
check('trace retry-archive then archive-failure', solve('restored-error', [('retry-archive', N), ('archive-failure', N)], N), 'restored-error')
check('trace retry-archive then new-revision-arrives', solve('restored-error', [('retry-archive', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace restore-click then restore-success', solve('archived', [('restore-click', N), ('restore-success', N)], N), 'row-visible')
check('trace restore-click then restore-failure', solve('archived', [('restore-click', N), ('restore-failure', N)], N), 'restore-error')
check('trace restore-success then archive-click', solve('restoring', [('restore-success', N), ('archive-click', N)], N), 'optimistically-hidden')
check('trace restore-failure then retry-restore', solve('restoring', [('restore-failure', N), ('retry-restore', N)], N), 'restoring')
check('trace retry-restore then restore-success', solve('restore-error', [('retry-restore', N), ('restore-success', N)], N), 'row-visible')
check('trace retry-restore then restore-failure', solve('restore-error', [('retry-restore', N), ('restore-failure', N)], N), 'restore-error')
check('trace new-revision-arrives then archive-failure', solve('optimistically-hidden', [('new-revision-arrives', N), ('archive-failure', N)], N), 'restored-new-revision-error')
check('domain state regression 1: saved', solve('row-visible', [('archive-click', N, {})], N, {}, 'saved'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 2: row', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 1, 'text': 'stale'})], N, {}, 'row'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 3: visible', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 2, 'text': 'new'})], N, {}, 'visible'), {'phase': 'hidden-new-revision', 'value': False})
check('domain state regression 4: row', solve('hidden-new-revision', [('archive-failure', N, {})], N, {'row': {'revision': 2, 'text': 'new'}, 'saved': {'revision': 1, 'text': 'old'}}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
check('domain state regression 5: row', solve('optimistically-hidden', [('archive-failure', N, {})], N, {'saved': {'revision': 1, 'text': 'original'}}, 'row'), {'phase': 'restored-error', 'value': {'revision': 1, 'text': 'original'}})
check('domain state regression 6: visible', solve('restoring', [('restore-failure', N, {})], N, {}, 'visible'), {'phase': 'restore-error', 'value': False})
check('domain state regression 7: row', solve('row-visible', [('archive-click', N, {}), ('new-revision-arrives', N, {'revision': 2, 'text': 'new'}), ('archive-failure', N, {})], N, {}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
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:
The retry-archive event leaves a notification in archived instead of optimistically-hidden.

Inbox optimistic archive rollback is a bounded visual-notification workflow with mutable policy state {'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True}. Its default transition relation is {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}; domain inputs can suppress or redirect transitions and update policy fields, as specified in solve. Different-generation events are inert. Batches apply in order. Optional observe returns the selected policy field alongside the phase.

split: open-access


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

N = 1
observations = []
def solve(initial, events, generation, payload=None, observe=None):
    table = {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'row-visible', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}
    data = json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})) if payload is None else {**json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})), **json.loads(json.dumps(payload))}
    state = initial
    for delivery in events:
        event, event_generation = delivery[:2]
        arg = delivery[2] if len(delivery) > 2 else {}
        if event_generation == generation:
            previous = state
            if event == 'archive-click': data['saved'] = dict(data['row'])
            if event == 'new-revision-arrives':
                revision = arg.get('revision',data['row']['revision']+1)
                if revision <= data['row']['revision']: event = 'unknown-event'
                else: data['row'] = {'revision':revision,'text':arg.get('text','new')}
            state = table.get((state, event), state)
            if state in ('archived','optimistically-hidden','hidden-new-revision','restore-error','restoring'): data['visible'] = False
            if state in ('row-visible','restored-error','restored-new-revision-error'): data['visible'] = True
            if state == 'restored-error' and data['saved'] is not None: data['row'] = dict(data['saved'])
    return {'phase': state, 'value': data[observe]} if observe else state
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('row-visible / archive-click', solve('row-visible', [('archive-click', N)], N), 'optimistically-hidden')
check('optimistically-hidden / archive-success', solve('optimistically-hidden', [('archive-success', N)], N), 'archived')
check('optimistically-hidden / archive-failure', solve('optimistically-hidden', [('archive-failure', N)], N), 'restored-error')
check('restored-error / retry-archive', solve('restored-error', [('retry-archive', N)], N), 'optimistically-hidden')
check('archived / restore-click', solve('archived', [('restore-click', N)], N), 'restoring')
check('restoring / restore-success', solve('restoring', [('restore-success', N)], N), 'row-visible')
check('restoring / restore-failure', solve('restoring', [('restore-failure', N)], N), 'restore-error')
check('restore-error / retry-restore', solve('restore-error', [('retry-restore', N)], N), 'restoring')
check('optimistically-hidden / new-revision-arrives', solve('optimistically-hidden', [('new-revision-arrives', N)], N), 'hidden-new-revision')
check('hidden-new-revision / archive-failure', solve('hidden-new-revision', [('archive-failure', N)], N), 'restored-new-revision-error')
check('older notification incarnation', solve('row-visible', [('archive-click', N - 1)], N), 'row-visible')
check('future notification incarnation', solve('row-visible', [('archive-click', N + 1)], N), 'row-visible')
check('unknown event is inert', solve('row-visible', [('unknown-event', N)], N), 'row-visible')
check('empty delivery batch', solve('row-visible', [], N), 'row-visible')
check('N stale deliveries before current delivery', solve('row-visible', [('archive-click', N - 1)] * N + [('archive-click', N)], N), 'optimistically-hidden')
check('trace archive-click then archive-success', solve('row-visible', [('archive-click', N), ('archive-success', N)], N), 'archived')
check('trace archive-click then archive-failure', solve('row-visible', [('archive-click', N), ('archive-failure', N)], N), 'restored-error')
check('trace archive-click then new-revision-arrives', solve('row-visible', [('archive-click', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace archive-success then restore-click', solve('optimistically-hidden', [('archive-success', N), ('restore-click', N)], N), 'restoring')
check('trace archive-failure then retry-archive', solve('optimistically-hidden', [('archive-failure', N), ('retry-archive', N)], N), 'optimistically-hidden')
check('trace retry-archive then archive-success', solve('restored-error', [('retry-archive', N), ('archive-success', N)], N), 'archived')
check('trace retry-archive then archive-failure', solve('restored-error', [('retry-archive', N), ('archive-failure', N)], N), 'restored-error')
check('trace retry-archive then new-revision-arrives', solve('restored-error', [('retry-archive', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace restore-click then restore-success', solve('archived', [('restore-click', N), ('restore-success', N)], N), 'row-visible')
check('trace restore-click then restore-failure', solve('archived', [('restore-click', N), ('restore-failure', N)], N), 'restore-error')
check('trace restore-success then archive-click', solve('restoring', [('restore-success', N), ('archive-click', N)], N), 'optimistically-hidden')
check('trace restore-failure then retry-restore', solve('restoring', [('restore-failure', N), ('retry-restore', N)], N), 'restoring')
check('trace retry-restore then restore-success', solve('restore-error', [('retry-restore', N), ('restore-success', N)], N), 'row-visible')
check('trace retry-restore then restore-failure', solve('restore-error', [('retry-restore', N), ('restore-failure', N)], N), 'restore-error')
check('trace new-revision-arrives then archive-failure', solve('optimistically-hidden', [('new-revision-arrives', N), ('archive-failure', N)], N), 'restored-new-revision-error')
check('domain state regression 1: saved', solve('row-visible', [('archive-click', N, {})], N, {}, 'saved'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 2: row', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 1, 'text': 'stale'})], N, {}, 'row'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 3: visible', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 2, 'text': 'new'})], N, {}, 'visible'), {'phase': 'hidden-new-revision', 'value': False})
check('domain state regression 4: row', solve('hidden-new-revision', [('archive-failure', N, {})], N, {'row': {'revision': 2, 'text': 'new'}, 'saved': {'revision': 1, 'text': 'old'}}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
check('domain state regression 5: row', solve('optimistically-hidden', [('archive-failure', N, {})], N, {'saved': {'revision': 1, 'text': 'original'}}, 'row'), {'phase': 'restored-error', 'value': {'revision': 1, 'text': 'original'}})
check('domain state regression 6: visible', solve('restoring', [('restore-failure', N, {})], N, {}, 'visible'), {'phase': 'restore-error', 'value': False})
check('domain state regression 7: row', solve('row-visible', [('archive-click', N, {}), ('new-revision-arrives', N, {'revision': 2, 'text': 'new'}), ('archive-failure', N, {})], N, {}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
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(initial, events, generation, payload=None, observe=None):
    table = {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'optimistically-hidden', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}
    data = json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})) if payload is None else {**json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})), **json.loads(json.dumps(payload))}
    state = initial
    for delivery in events:
        event, event_generation = delivery[:2]
        arg = delivery[2] if len(delivery) > 2 else {}
        if event_generation == generation:
            previous = state
            if event == 'archive-click': data['saved'] = dict(data['row'])
            if event == 'new-revision-arrives':
                revision = arg.get('revision',data['row']['revision']+1)
                if revision <= data['row']['revision']: event = 'unknown-event'
                else: data['row'] = {'revision':revision,'text':arg.get('text','new')}
            state = table.get((state, event), state)
            if state in ('archived','optimistically-hidden','hidden-new-revision','restore-error','restoring'): data['visible'] = False
            if state in ('row-visible','restored-error','restored-new-revision-error'): data['visible'] = True
            if state == 'restored-error' and data['saved'] is not None: data['row'] = dict(data['saved'])
    return {'phase': state, 'value': data[observe]} if observe else state
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('row-visible / archive-click', solve('row-visible', [('archive-click', N)], N), 'optimistically-hidden')
check('optimistically-hidden / archive-success', solve('optimistically-hidden', [('archive-success', N)], N), 'archived')
check('optimistically-hidden / archive-failure', solve('optimistically-hidden', [('archive-failure', N)], N), 'restored-error')
check('restored-error / retry-archive', solve('restored-error', [('retry-archive', N)], N), 'optimistically-hidden')
check('archived / restore-click', solve('archived', [('restore-click', N)], N), 'restoring')
check('restoring / restore-success', solve('restoring', [('restore-success', N)], N), 'row-visible')
check('restoring / restore-failure', solve('restoring', [('restore-failure', N)], N), 'restore-error')
check('restore-error / retry-restore', solve('restore-error', [('retry-restore', N)], N), 'restoring')
check('optimistically-hidden / new-revision-arrives', solve('optimistically-hidden', [('new-revision-arrives', N)], N), 'hidden-new-revision')
check('hidden-new-revision / archive-failure', solve('hidden-new-revision', [('archive-failure', N)], N), 'restored-new-revision-error')
check('older notification incarnation', solve('row-visible', [('archive-click', N - 1)], N), 'row-visible')
check('future notification incarnation', solve('row-visible', [('archive-click', N + 1)], N), 'row-visible')
check('unknown event is inert', solve('row-visible', [('unknown-event', N)], N), 'row-visible')
check('empty delivery batch', solve('row-visible', [], N), 'row-visible')
check('N stale deliveries before current delivery', solve('row-visible', [('archive-click', N - 1)] * N + [('archive-click', N)], N), 'optimistically-hidden')
check('trace archive-click then archive-success', solve('row-visible', [('archive-click', N), ('archive-success', N)], N), 'archived')
check('trace archive-click then archive-failure', solve('row-visible', [('archive-click', N), ('archive-failure', N)], N), 'restored-error')
check('trace archive-click then new-revision-arrives', solve('row-visible', [('archive-click', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace archive-success then restore-click', solve('optimistically-hidden', [('archive-success', N), ('restore-click', N)], N), 'restoring')
check('trace archive-failure then retry-archive', solve('optimistically-hidden', [('archive-failure', N), ('retry-archive', N)], N), 'optimistically-hidden')
check('trace retry-archive then archive-success', solve('restored-error', [('retry-archive', N), ('archive-success', N)], N), 'archived')
check('trace retry-archive then archive-failure', solve('restored-error', [('retry-archive', N), ('archive-failure', N)], N), 'restored-error')
check('trace retry-archive then new-revision-arrives', solve('restored-error', [('retry-archive', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace restore-click then restore-success', solve('archived', [('restore-click', N), ('restore-success', N)], N), 'row-visible')
check('trace restore-click then restore-failure', solve('archived', [('restore-click', N), ('restore-failure', N)], N), 'restore-error')
check('trace restore-success then archive-click', solve('restoring', [('restore-success', N), ('archive-click', N)], N), 'optimistically-hidden')
check('trace restore-failure then retry-restore', solve('restoring', [('restore-failure', N), ('retry-restore', N)], N), 'restoring')
check('trace retry-restore then restore-success', solve('restore-error', [('retry-restore', N), ('restore-success', N)], N), 'row-visible')
check('trace retry-restore then restore-failure', solve('restore-error', [('retry-restore', N), ('restore-failure', N)], N), 'restore-error')
check('trace new-revision-arrives then archive-failure', solve('optimistically-hidden', [('new-revision-arrives', N), ('archive-failure', N)], N), 'restored-new-revision-error')
check('domain state regression 1: saved', solve('row-visible', [('archive-click', N, {})], N, {}, 'saved'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 2: row', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 1, 'text': 'stale'})], N, {}, 'row'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 3: visible', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 2, 'text': 'new'})], N, {}, 'visible'), {'phase': 'hidden-new-revision', 'value': False})
check('domain state regression 4: row', solve('hidden-new-revision', [('archive-failure', N, {})], N, {'row': {'revision': 2, 'text': 'new'}, 'saved': {'revision': 1, 'text': 'old'}}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
check('domain state regression 5: row', solve('optimistically-hidden', [('archive-failure', N, {})], N, {'saved': {'revision': 1, 'text': 'original'}}, 'row'), {'phase': 'restored-error', 'value': {'revision': 1, 'text': 'original'}})
check('domain state regression 6: visible', solve('restoring', [('restore-failure', N, {})], N, {}, 'visible'), {'phase': 'restore-error', 'value': False})
check('domain state regression 7: row', solve('row-visible', [('archive-click', N, {}), ('new-revision-arrives', N, {'revision': 2, 'text': 'new'}), ('archive-failure', N, {})], N, {}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
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:
The restore-click event leaves a notification in row-visible instead of restoring.

Inbox optimistic archive rollback is a bounded visual-notification workflow with mutable policy state {'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True}. Its default transition relation is {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}; domain inputs can suppress or redirect transitions and update policy fields, as specified in solve. Different-generation events are inert. Batches apply in order. Optional observe returns the selected policy field alongside the phase.

split: open-access


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

N = 1
observations = []
def solve(initial, events, generation, payload=None, observe=None):
    table = {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'archived', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}
    data = json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})) if payload is None else {**json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})), **json.loads(json.dumps(payload))}
    state = initial
    for delivery in events:
        event, event_generation = delivery[:2]
        arg = delivery[2] if len(delivery) > 2 else {}
        if event_generation == generation:
            previous = state
            if event == 'archive-click': data['saved'] = dict(data['row'])
            if event == 'new-revision-arrives':
                revision = arg.get('revision',data['row']['revision']+1)
                if revision <= data['row']['revision']: event = 'unknown-event'
                else: data['row'] = {'revision':revision,'text':arg.get('text','new')}
            state = table.get((state, event), state)
            if state in ('archived','optimistically-hidden','hidden-new-revision','restore-error','restoring'): data['visible'] = False
            if state in ('row-visible','restored-error','restored-new-revision-error'): data['visible'] = True
            if state == 'restored-error' and data['saved'] is not None: data['row'] = dict(data['saved'])
    return {'phase': state, 'value': data[observe]} if observe else state
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('row-visible / archive-click', solve('row-visible', [('archive-click', N)], N), 'optimistically-hidden')
check('optimistically-hidden / archive-success', solve('optimistically-hidden', [('archive-success', N)], N), 'archived')
check('optimistically-hidden / archive-failure', solve('optimistically-hidden', [('archive-failure', N)], N), 'restored-error')
check('restored-error / retry-archive', solve('restored-error', [('retry-archive', N)], N), 'optimistically-hidden')
check('archived / restore-click', solve('archived', [('restore-click', N)], N), 'restoring')
check('restoring / restore-success', solve('restoring', [('restore-success', N)], N), 'row-visible')
check('restoring / restore-failure', solve('restoring', [('restore-failure', N)], N), 'restore-error')
check('restore-error / retry-restore', solve('restore-error', [('retry-restore', N)], N), 'restoring')
check('optimistically-hidden / new-revision-arrives', solve('optimistically-hidden', [('new-revision-arrives', N)], N), 'hidden-new-revision')
check('hidden-new-revision / archive-failure', solve('hidden-new-revision', [('archive-failure', N)], N), 'restored-new-revision-error')
check('older notification incarnation', solve('row-visible', [('archive-click', N - 1)], N), 'row-visible')
check('future notification incarnation', solve('row-visible', [('archive-click', N + 1)], N), 'row-visible')
check('unknown event is inert', solve('row-visible', [('unknown-event', N)], N), 'row-visible')
check('empty delivery batch', solve('row-visible', [], N), 'row-visible')
check('N stale deliveries before current delivery', solve('row-visible', [('archive-click', N - 1)] * N + [('archive-click', N)], N), 'optimistically-hidden')
check('trace archive-click then archive-success', solve('row-visible', [('archive-click', N), ('archive-success', N)], N), 'archived')
check('trace archive-click then archive-failure', solve('row-visible', [('archive-click', N), ('archive-failure', N)], N), 'restored-error')
check('trace archive-click then new-revision-arrives', solve('row-visible', [('archive-click', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace archive-success then restore-click', solve('optimistically-hidden', [('archive-success', N), ('restore-click', N)], N), 'restoring')
check('trace archive-failure then retry-archive', solve('optimistically-hidden', [('archive-failure', N), ('retry-archive', N)], N), 'optimistically-hidden')
check('trace retry-archive then archive-success', solve('restored-error', [('retry-archive', N), ('archive-success', N)], N), 'archived')
check('trace retry-archive then archive-failure', solve('restored-error', [('retry-archive', N), ('archive-failure', N)], N), 'restored-error')
check('trace retry-archive then new-revision-arrives', solve('restored-error', [('retry-archive', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace restore-click then restore-success', solve('archived', [('restore-click', N), ('restore-success', N)], N), 'row-visible')
check('trace restore-click then restore-failure', solve('archived', [('restore-click', N), ('restore-failure', N)], N), 'restore-error')
check('trace restore-success then archive-click', solve('restoring', [('restore-success', N), ('archive-click', N)], N), 'optimistically-hidden')
check('trace restore-failure then retry-restore', solve('restoring', [('restore-failure', N), ('retry-restore', N)], N), 'restoring')
check('trace retry-restore then restore-success', solve('restore-error', [('retry-restore', N), ('restore-success', N)], N), 'row-visible')
check('trace retry-restore then restore-failure', solve('restore-error', [('retry-restore', N), ('restore-failure', N)], N), 'restore-error')
check('trace new-revision-arrives then archive-failure', solve('optimistically-hidden', [('new-revision-arrives', N), ('archive-failure', N)], N), 'restored-new-revision-error')
check('domain state regression 1: saved', solve('row-visible', [('archive-click', N, {})], N, {}, 'saved'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 2: row', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 1, 'text': 'stale'})], N, {}, 'row'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 3: visible', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 2, 'text': 'new'})], N, {}, 'visible'), {'phase': 'hidden-new-revision', 'value': False})
check('domain state regression 4: row', solve('hidden-new-revision', [('archive-failure', N, {})], N, {'row': {'revision': 2, 'text': 'new'}, 'saved': {'revision': 1, 'text': 'old'}}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
check('domain state regression 5: row', solve('optimistically-hidden', [('archive-failure', N, {})], N, {'saved': {'revision': 1, 'text': 'original'}}, 'row'), {'phase': 'restored-error', 'value': {'revision': 1, 'text': 'original'}})
check('domain state regression 6: visible', solve('restoring', [('restore-failure', N, {})], N, {}, 'visible'), {'phase': 'restore-error', 'value': False})
check('domain state regression 7: row', solve('row-visible', [('archive-click', N, {}), ('new-revision-arrives', N, {'revision': 2, 'text': 'new'}), ('archive-failure', N, {})], N, {}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
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(initial, events, generation, payload=None, observe=None):
    table = {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'restored-error', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}
    data = json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})) if payload is None else {**json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})), **json.loads(json.dumps(payload))}
    state = initial
    for delivery in events:
        event, event_generation = delivery[:2]
        arg = delivery[2] if len(delivery) > 2 else {}
        if event_generation == generation:
            previous = state
            if event == 'archive-click': data['saved'] = dict(data['row'])
            if event == 'new-revision-arrives':
                revision = arg.get('revision',data['row']['revision']+1)
                if revision <= data['row']['revision']: event = 'unknown-event'
                else: data['row'] = {'revision':revision,'text':arg.get('text','new')}
            state = table.get((state, event), state)
            if state in ('archived','optimistically-hidden','hidden-new-revision','restore-error','restoring'): data['visible'] = False
            if state in ('row-visible','restored-error','restored-new-revision-error'): data['visible'] = True
            if state == 'restored-error' and data['saved'] is not None: data['row'] = dict(data['saved'])
    return {'phase': state, 'value': data[observe]} if observe else state
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('row-visible / archive-click', solve('row-visible', [('archive-click', N)], N), 'optimistically-hidden')
check('optimistically-hidden / archive-success', solve('optimistically-hidden', [('archive-success', N)], N), 'archived')
check('optimistically-hidden / archive-failure', solve('optimistically-hidden', [('archive-failure', N)], N), 'restored-error')
check('restored-error / retry-archive', solve('restored-error', [('retry-archive', N)], N), 'optimistically-hidden')
check('archived / restore-click', solve('archived', [('restore-click', N)], N), 'restoring')
check('restoring / restore-success', solve('restoring', [('restore-success', N)], N), 'row-visible')
check('restoring / restore-failure', solve('restoring', [('restore-failure', N)], N), 'restore-error')
check('restore-error / retry-restore', solve('restore-error', [('retry-restore', N)], N), 'restoring')
check('optimistically-hidden / new-revision-arrives', solve('optimistically-hidden', [('new-revision-arrives', N)], N), 'hidden-new-revision')
check('hidden-new-revision / archive-failure', solve('hidden-new-revision', [('archive-failure', N)], N), 'restored-new-revision-error')
check('older notification incarnation', solve('row-visible', [('archive-click', N - 1)], N), 'row-visible')
check('future notification incarnation', solve('row-visible', [('archive-click', N + 1)], N), 'row-visible')
check('unknown event is inert', solve('row-visible', [('unknown-event', N)], N), 'row-visible')
check('empty delivery batch', solve('row-visible', [], N), 'row-visible')
check('N stale deliveries before current delivery', solve('row-visible', [('archive-click', N - 1)] * N + [('archive-click', N)], N), 'optimistically-hidden')
check('trace archive-click then archive-success', solve('row-visible', [('archive-click', N), ('archive-success', N)], N), 'archived')
check('trace archive-click then archive-failure', solve('row-visible', [('archive-click', N), ('archive-failure', N)], N), 'restored-error')
check('trace archive-click then new-revision-arrives', solve('row-visible', [('archive-click', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace archive-success then restore-click', solve('optimistically-hidden', [('archive-success', N), ('restore-click', N)], N), 'restoring')
check('trace archive-failure then retry-archive', solve('optimistically-hidden', [('archive-failure', N), ('retry-archive', N)], N), 'optimistically-hidden')
check('trace retry-archive then archive-success', solve('restored-error', [('retry-archive', N), ('archive-success', N)], N), 'archived')
check('trace retry-archive then archive-failure', solve('restored-error', [('retry-archive', N), ('archive-failure', N)], N), 'restored-error')
check('trace retry-archive then new-revision-arrives', solve('restored-error', [('retry-archive', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace restore-click then restore-success', solve('archived', [('restore-click', N), ('restore-success', N)], N), 'row-visible')
check('trace restore-click then restore-failure', solve('archived', [('restore-click', N), ('restore-failure', N)], N), 'restore-error')
check('trace restore-success then archive-click', solve('restoring', [('restore-success', N), ('archive-click', N)], N), 'optimistically-hidden')
check('trace restore-failure then retry-restore', solve('restoring', [('restore-failure', N), ('retry-restore', N)], N), 'restoring')
check('trace retry-restore then restore-success', solve('restore-error', [('retry-restore', N), ('restore-success', N)], N), 'row-visible')
check('trace retry-restore then restore-failure', solve('restore-error', [('retry-restore', N), ('restore-failure', N)], N), 'restore-error')
check('trace new-revision-arrives then archive-failure', solve('optimistically-hidden', [('new-revision-arrives', N), ('archive-failure', N)], N), 'restored-new-revision-error')
check('domain state regression 1: saved', solve('row-visible', [('archive-click', N, {})], N, {}, 'saved'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 2: row', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 1, 'text': 'stale'})], N, {}, 'row'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 3: visible', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 2, 'text': 'new'})], N, {}, 'visible'), {'phase': 'hidden-new-revision', 'value': False})
check('domain state regression 4: row', solve('hidden-new-revision', [('archive-failure', N, {})], N, {'row': {'revision': 2, 'text': 'new'}, 'saved': {'revision': 1, 'text': 'old'}}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
check('domain state regression 5: row', solve('optimistically-hidden', [('archive-failure', N, {})], N, {'saved': {'revision': 1, 'text': 'original'}}, 'row'), {'phase': 'restored-error', 'value': {'revision': 1, 'text': 'original'}})
check('domain state regression 6: visible', solve('restoring', [('restore-failure', N, {})], N, {}, 'visible'), {'phase': 'restore-error', 'value': False})
check('domain state regression 7: row', solve('row-visible', [('archive-click', N, {}), ('new-revision-arrives', N, {'revision': 2, 'text': 'new'}), ('archive-failure', N, {})], N, {}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
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:
The restore-success event leaves a notification in archived instead of row-visible.

Inbox optimistic archive rollback is a bounded visual-notification workflow with mutable policy state {'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True}. Its default transition relation is {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}; domain inputs can suppress or redirect transitions and update policy fields, as specified in solve. Different-generation events are inert. Batches apply in order. Optional observe returns the selected policy field alongside the phase.

split: open-access


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

N = 1
observations = []
def solve(initial, events, generation, payload=None, observe=None):
    table = {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'row-visible', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}
    data = json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})) if payload is None else {**json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})), **json.loads(json.dumps(payload))}
    state = initial
    for delivery in events:
        event, event_generation = delivery[:2]
        arg = delivery[2] if len(delivery) > 2 else {}
        if event_generation == generation:
            previous = state
            if event == 'archive-click': data['saved'] = dict(data['row'])
            if event == 'new-revision-arrives':
                revision = arg.get('revision',data['row']['revision']+1)
                if revision <= data['row']['revision']: event = 'unknown-event'
                else: data['row'] = {'revision':revision,'text':arg.get('text','new')}
            state = table.get((state, event), state)
            if state in ('archived','optimistically-hidden','hidden-new-revision','restore-error','restoring'): data['visible'] = False
            if state in ('row-visible','restored-error','restored-new-revision-error'): data['visible'] = True
            if state == 'restored-error' and data['saved'] is not None: data['row'] = dict(data['saved'])
    return {'phase': state, 'value': data[observe]} if observe else state
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('row-visible / archive-click', solve('row-visible', [('archive-click', N)], N), 'optimistically-hidden')
check('optimistically-hidden / archive-success', solve('optimistically-hidden', [('archive-success', N)], N), 'archived')
check('optimistically-hidden / archive-failure', solve('optimistically-hidden', [('archive-failure', N)], N), 'restored-error')
check('restored-error / retry-archive', solve('restored-error', [('retry-archive', N)], N), 'optimistically-hidden')
check('archived / restore-click', solve('archived', [('restore-click', N)], N), 'restoring')
check('restoring / restore-success', solve('restoring', [('restore-success', N)], N), 'row-visible')
check('restoring / restore-failure', solve('restoring', [('restore-failure', N)], N), 'restore-error')
check('restore-error / retry-restore', solve('restore-error', [('retry-restore', N)], N), 'restoring')
check('optimistically-hidden / new-revision-arrives', solve('optimistically-hidden', [('new-revision-arrives', N)], N), 'hidden-new-revision')
check('hidden-new-revision / archive-failure', solve('hidden-new-revision', [('archive-failure', N)], N), 'restored-new-revision-error')
check('older notification incarnation', solve('row-visible', [('archive-click', N - 1)], N), 'row-visible')
check('future notification incarnation', solve('row-visible', [('archive-click', N + 1)], N), 'row-visible')
check('unknown event is inert', solve('row-visible', [('unknown-event', N)], N), 'row-visible')
check('empty delivery batch', solve('row-visible', [], N), 'row-visible')
check('N stale deliveries before current delivery', solve('row-visible', [('archive-click', N - 1)] * N + [('archive-click', N)], N), 'optimistically-hidden')
check('trace archive-click then archive-success', solve('row-visible', [('archive-click', N), ('archive-success', N)], N), 'archived')
check('trace archive-click then archive-failure', solve('row-visible', [('archive-click', N), ('archive-failure', N)], N), 'restored-error')
check('trace archive-click then new-revision-arrives', solve('row-visible', [('archive-click', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace archive-success then restore-click', solve('optimistically-hidden', [('archive-success', N), ('restore-click', N)], N), 'restoring')
check('trace archive-failure then retry-archive', solve('optimistically-hidden', [('archive-failure', N), ('retry-archive', N)], N), 'optimistically-hidden')
check('trace retry-archive then archive-success', solve('restored-error', [('retry-archive', N), ('archive-success', N)], N), 'archived')
check('trace retry-archive then archive-failure', solve('restored-error', [('retry-archive', N), ('archive-failure', N)], N), 'restored-error')
check('trace retry-archive then new-revision-arrives', solve('restored-error', [('retry-archive', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace restore-click then restore-success', solve('archived', [('restore-click', N), ('restore-success', N)], N), 'row-visible')
check('trace restore-click then restore-failure', solve('archived', [('restore-click', N), ('restore-failure', N)], N), 'restore-error')
check('trace restore-success then archive-click', solve('restoring', [('restore-success', N), ('archive-click', N)], N), 'optimistically-hidden')
check('trace restore-failure then retry-restore', solve('restoring', [('restore-failure', N), ('retry-restore', N)], N), 'restoring')
check('trace retry-restore then restore-success', solve('restore-error', [('retry-restore', N), ('restore-success', N)], N), 'row-visible')
check('trace retry-restore then restore-failure', solve('restore-error', [('retry-restore', N), ('restore-failure', N)], N), 'restore-error')
check('trace new-revision-arrives then archive-failure', solve('optimistically-hidden', [('new-revision-arrives', N), ('archive-failure', N)], N), 'restored-new-revision-error')
check('domain state regression 1: saved', solve('row-visible', [('archive-click', N, {})], N, {}, 'saved'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 2: row', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 1, 'text': 'stale'})], N, {}, 'row'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 3: visible', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 2, 'text': 'new'})], N, {}, 'visible'), {'phase': 'hidden-new-revision', 'value': False})
check('domain state regression 4: row', solve('hidden-new-revision', [('archive-failure', N, {})], N, {'row': {'revision': 2, 'text': 'new'}, 'saved': {'revision': 1, 'text': 'old'}}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
check('domain state regression 5: row', solve('optimistically-hidden', [('archive-failure', N, {})], N, {'saved': {'revision': 1, 'text': 'original'}}, 'row'), {'phase': 'restored-error', 'value': {'revision': 1, 'text': 'original'}})
check('domain state regression 6: visible', solve('restoring', [('restore-failure', N, {})], N, {}, 'visible'), {'phase': 'restore-error', 'value': False})
check('domain state regression 7: row', solve('row-visible', [('archive-click', N, {}), ('new-revision-arrives', N, {'revision': 2, 'text': 'new'}), ('archive-failure', N, {})], N, {}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
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(initial, events, generation, payload=None, observe=None):
    table = {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'archived', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}
    data = json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})) if payload is None else {**json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})), **json.loads(json.dumps(payload))}
    state = initial
    for delivery in events:
        event, event_generation = delivery[:2]
        arg = delivery[2] if len(delivery) > 2 else {}
        if event_generation == generation:
            previous = state
            if event == 'archive-click': data['saved'] = dict(data['row'])
            if event == 'new-revision-arrives':
                revision = arg.get('revision',data['row']['revision']+1)
                if revision <= data['row']['revision']: event = 'unknown-event'
                else: data['row'] = {'revision':revision,'text':arg.get('text','new')}
            state = table.get((state, event), state)
            if state in ('archived','optimistically-hidden','hidden-new-revision','restore-error','restoring'): data['visible'] = False
            if state in ('row-visible','restored-error','restored-new-revision-error'): data['visible'] = True
            if state == 'restored-error' and data['saved'] is not None: data['row'] = dict(data['saved'])
    return {'phase': state, 'value': data[observe]} if observe else state
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('row-visible / archive-click', solve('row-visible', [('archive-click', N)], N), 'optimistically-hidden')
check('optimistically-hidden / archive-success', solve('optimistically-hidden', [('archive-success', N)], N), 'archived')
check('optimistically-hidden / archive-failure', solve('optimistically-hidden', [('archive-failure', N)], N), 'restored-error')
check('restored-error / retry-archive', solve('restored-error', [('retry-archive', N)], N), 'optimistically-hidden')
check('archived / restore-click', solve('archived', [('restore-click', N)], N), 'restoring')
check('restoring / restore-success', solve('restoring', [('restore-success', N)], N), 'row-visible')
check('restoring / restore-failure', solve('restoring', [('restore-failure', N)], N), 'restore-error')
check('restore-error / retry-restore', solve('restore-error', [('retry-restore', N)], N), 'restoring')
check('optimistically-hidden / new-revision-arrives', solve('optimistically-hidden', [('new-revision-arrives', N)], N), 'hidden-new-revision')
check('hidden-new-revision / archive-failure', solve('hidden-new-revision', [('archive-failure', N)], N), 'restored-new-revision-error')
check('older notification incarnation', solve('row-visible', [('archive-click', N - 1)], N), 'row-visible')
check('future notification incarnation', solve('row-visible', [('archive-click', N + 1)], N), 'row-visible')
check('unknown event is inert', solve('row-visible', [('unknown-event', N)], N), 'row-visible')
check('empty delivery batch', solve('row-visible', [], N), 'row-visible')
check('N stale deliveries before current delivery', solve('row-visible', [('archive-click', N - 1)] * N + [('archive-click', N)], N), 'optimistically-hidden')
check('trace archive-click then archive-success', solve('row-visible', [('archive-click', N), ('archive-success', N)], N), 'archived')
check('trace archive-click then archive-failure', solve('row-visible', [('archive-click', N), ('archive-failure', N)], N), 'restored-error')
check('trace archive-click then new-revision-arrives', solve('row-visible', [('archive-click', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace archive-success then restore-click', solve('optimistically-hidden', [('archive-success', N), ('restore-click', N)], N), 'restoring')
check('trace archive-failure then retry-archive', solve('optimistically-hidden', [('archive-failure', N), ('retry-archive', N)], N), 'optimistically-hidden')
check('trace retry-archive then archive-success', solve('restored-error', [('retry-archive', N), ('archive-success', N)], N), 'archived')
check('trace retry-archive then archive-failure', solve('restored-error', [('retry-archive', N), ('archive-failure', N)], N), 'restored-error')
check('trace retry-archive then new-revision-arrives', solve('restored-error', [('retry-archive', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace restore-click then restore-success', solve('archived', [('restore-click', N), ('restore-success', N)], N), 'row-visible')
check('trace restore-click then restore-failure', solve('archived', [('restore-click', N), ('restore-failure', N)], N), 'restore-error')
check('trace restore-success then archive-click', solve('restoring', [('restore-success', N), ('archive-click', N)], N), 'optimistically-hidden')
check('trace restore-failure then retry-restore', solve('restoring', [('restore-failure', N), ('retry-restore', N)], N), 'restoring')
check('trace retry-restore then restore-success', solve('restore-error', [('retry-restore', N), ('restore-success', N)], N), 'row-visible')
check('trace retry-restore then restore-failure', solve('restore-error', [('retry-restore', N), ('restore-failure', N)], N), 'restore-error')
check('trace new-revision-arrives then archive-failure', solve('optimistically-hidden', [('new-revision-arrives', N), ('archive-failure', N)], N), 'restored-new-revision-error')
check('domain state regression 1: saved', solve('row-visible', [('archive-click', N, {})], N, {}, 'saved'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 2: row', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 1, 'text': 'stale'})], N, {}, 'row'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 3: visible', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 2, 'text': 'new'})], N, {}, 'visible'), {'phase': 'hidden-new-revision', 'value': False})
check('domain state regression 4: row', solve('hidden-new-revision', [('archive-failure', N, {})], N, {'row': {'revision': 2, 'text': 'new'}, 'saved': {'revision': 1, 'text': 'old'}}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
check('domain state regression 5: row', solve('optimistically-hidden', [('archive-failure', N, {})], N, {'saved': {'revision': 1, 'text': 'original'}}, 'row'), {'phase': 'restored-error', 'value': {'revision': 1, 'text': 'original'}})
check('domain state regression 6: visible', solve('restoring', [('restore-failure', N, {})], N, {}, 'visible'), {'phase': 'restore-error', 'value': False})
check('domain state regression 7: row', solve('row-visible', [('archive-click', N, {}), ('new-revision-arrives', N, {'revision': 2, 'text': 'new'}), ('archive-failure', N, {})], N, {}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
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:
The restore-failure event leaves a notification in row-visible instead of restore-error.

Inbox optimistic archive rollback is a bounded visual-notification workflow with mutable policy state {'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True}. Its default transition relation is {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}; domain inputs can suppress or redirect transitions and update policy fields, as specified in solve. Different-generation events are inert. Batches apply in order. Optional observe returns the selected policy field alongside the phase.

split: open-access


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

N = 1
observations = []
def solve(initial, events, generation, payload=None, observe=None):
    table = {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'row-visible', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}
    data = json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})) if payload is None else {**json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})), **json.loads(json.dumps(payload))}
    state = initial
    for delivery in events:
        event, event_generation = delivery[:2]
        arg = delivery[2] if len(delivery) > 2 else {}
        if event_generation == generation:
            previous = state
            if event == 'archive-click': data['saved'] = dict(data['row'])
            if event == 'new-revision-arrives':
                revision = arg.get('revision',data['row']['revision']+1)
                if revision <= data['row']['revision']: event = 'unknown-event'
                else: data['row'] = {'revision':revision,'text':arg.get('text','new')}
            state = table.get((state, event), state)
            if state in ('archived','optimistically-hidden','hidden-new-revision','restore-error','restoring'): data['visible'] = False
            if state in ('row-visible','restored-error','restored-new-revision-error'): data['visible'] = True
            if state == 'restored-error' and data['saved'] is not None: data['row'] = dict(data['saved'])
    return {'phase': state, 'value': data[observe]} if observe else state
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('row-visible / archive-click', solve('row-visible', [('archive-click', N)], N), 'optimistically-hidden')
check('optimistically-hidden / archive-success', solve('optimistically-hidden', [('archive-success', N)], N), 'archived')
check('optimistically-hidden / archive-failure', solve('optimistically-hidden', [('archive-failure', N)], N), 'restored-error')
check('restored-error / retry-archive', solve('restored-error', [('retry-archive', N)], N), 'optimistically-hidden')
check('archived / restore-click', solve('archived', [('restore-click', N)], N), 'restoring')
check('restoring / restore-success', solve('restoring', [('restore-success', N)], N), 'row-visible')
check('restoring / restore-failure', solve('restoring', [('restore-failure', N)], N), 'restore-error')
check('restore-error / retry-restore', solve('restore-error', [('retry-restore', N)], N), 'restoring')
check('optimistically-hidden / new-revision-arrives', solve('optimistically-hidden', [('new-revision-arrives', N)], N), 'hidden-new-revision')
check('hidden-new-revision / archive-failure', solve('hidden-new-revision', [('archive-failure', N)], N), 'restored-new-revision-error')
check('older notification incarnation', solve('row-visible', [('archive-click', N - 1)], N), 'row-visible')
check('future notification incarnation', solve('row-visible', [('archive-click', N + 1)], N), 'row-visible')
check('unknown event is inert', solve('row-visible', [('unknown-event', N)], N), 'row-visible')
check('empty delivery batch', solve('row-visible', [], N), 'row-visible')
check('N stale deliveries before current delivery', solve('row-visible', [('archive-click', N - 1)] * N + [('archive-click', N)], N), 'optimistically-hidden')
check('trace archive-click then archive-success', solve('row-visible', [('archive-click', N), ('archive-success', N)], N), 'archived')
check('trace archive-click then archive-failure', solve('row-visible', [('archive-click', N), ('archive-failure', N)], N), 'restored-error')
check('trace archive-click then new-revision-arrives', solve('row-visible', [('archive-click', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace archive-success then restore-click', solve('optimistically-hidden', [('archive-success', N), ('restore-click', N)], N), 'restoring')
check('trace archive-failure then retry-archive', solve('optimistically-hidden', [('archive-failure', N), ('retry-archive', N)], N), 'optimistically-hidden')
check('trace retry-archive then archive-success', solve('restored-error', [('retry-archive', N), ('archive-success', N)], N), 'archived')
check('trace retry-archive then archive-failure', solve('restored-error', [('retry-archive', N), ('archive-failure', N)], N), 'restored-error')
check('trace retry-archive then new-revision-arrives', solve('restored-error', [('retry-archive', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace restore-click then restore-success', solve('archived', [('restore-click', N), ('restore-success', N)], N), 'row-visible')
check('trace restore-click then restore-failure', solve('archived', [('restore-click', N), ('restore-failure', N)], N), 'restore-error')
check('trace restore-success then archive-click', solve('restoring', [('restore-success', N), ('archive-click', N)], N), 'optimistically-hidden')
check('trace restore-failure then retry-restore', solve('restoring', [('restore-failure', N), ('retry-restore', N)], N), 'restoring')
check('trace retry-restore then restore-success', solve('restore-error', [('retry-restore', N), ('restore-success', N)], N), 'row-visible')
check('trace retry-restore then restore-failure', solve('restore-error', [('retry-restore', N), ('restore-failure', N)], N), 'restore-error')
check('trace new-revision-arrives then archive-failure', solve('optimistically-hidden', [('new-revision-arrives', N), ('archive-failure', N)], N), 'restored-new-revision-error')
check('domain state regression 1: saved', solve('row-visible', [('archive-click', N, {})], N, {}, 'saved'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 2: row', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 1, 'text': 'stale'})], N, {}, 'row'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 3: visible', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 2, 'text': 'new'})], N, {}, 'visible'), {'phase': 'hidden-new-revision', 'value': False})
check('domain state regression 4: row', solve('hidden-new-revision', [('archive-failure', N, {})], N, {'row': {'revision': 2, 'text': 'new'}, 'saved': {'revision': 1, 'text': 'old'}}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
check('domain state regression 5: row', solve('optimistically-hidden', [('archive-failure', N, {})], N, {'saved': {'revision': 1, 'text': 'original'}}, 'row'), {'phase': 'restored-error', 'value': {'revision': 1, 'text': 'original'}})
check('domain state regression 6: visible', solve('restoring', [('restore-failure', N, {})], N, {}, 'visible'), {'phase': 'restore-error', 'value': False})
check('domain state regression 7: row', solve('row-visible', [('archive-click', N, {}), ('new-revision-arrives', N, {'revision': 2, 'text': 'new'}), ('archive-failure', N, {})], N, {}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
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(initial, events, generation, payload=None, observe=None):
    table = {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'optimistically-hidden', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}
    data = json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})) if payload is None else {**json.loads(json.dumps({'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True})), **json.loads(json.dumps(payload))}
    state = initial
    for delivery in events:
        event, event_generation = delivery[:2]
        arg = delivery[2] if len(delivery) > 2 else {}
        if event_generation == generation:
            previous = state
            if event == 'archive-click': data['saved'] = dict(data['row'])
            if event == 'new-revision-arrives':
                revision = arg.get('revision',data['row']['revision']+1)
                if revision <= data['row']['revision']: event = 'unknown-event'
                else: data['row'] = {'revision':revision,'text':arg.get('text','new')}
            state = table.get((state, event), state)
            if state in ('archived','optimistically-hidden','hidden-new-revision','restore-error','restoring'): data['visible'] = False
            if state in ('row-visible','restored-error','restored-new-revision-error'): data['visible'] = True
            if state == 'restored-error' and data['saved'] is not None: data['row'] = dict(data['saved'])
    return {'phase': state, 'value': data[observe]} if observe else state
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('row-visible / archive-click', solve('row-visible', [('archive-click', N)], N), 'optimistically-hidden')
check('optimistically-hidden / archive-success', solve('optimistically-hidden', [('archive-success', N)], N), 'archived')
check('optimistically-hidden / archive-failure', solve('optimistically-hidden', [('archive-failure', N)], N), 'restored-error')
check('restored-error / retry-archive', solve('restored-error', [('retry-archive', N)], N), 'optimistically-hidden')
check('archived / restore-click', solve('archived', [('restore-click', N)], N), 'restoring')
check('restoring / restore-success', solve('restoring', [('restore-success', N)], N), 'row-visible')
check('restoring / restore-failure', solve('restoring', [('restore-failure', N)], N), 'restore-error')
check('restore-error / retry-restore', solve('restore-error', [('retry-restore', N)], N), 'restoring')
check('optimistically-hidden / new-revision-arrives', solve('optimistically-hidden', [('new-revision-arrives', N)], N), 'hidden-new-revision')
check('hidden-new-revision / archive-failure', solve('hidden-new-revision', [('archive-failure', N)], N), 'restored-new-revision-error')
check('older notification incarnation', solve('row-visible', [('archive-click', N - 1)], N), 'row-visible')
check('future notification incarnation', solve('row-visible', [('archive-click', N + 1)], N), 'row-visible')
check('unknown event is inert', solve('row-visible', [('unknown-event', N)], N), 'row-visible')
check('empty delivery batch', solve('row-visible', [], N), 'row-visible')
check('N stale deliveries before current delivery', solve('row-visible', [('archive-click', N - 1)] * N + [('archive-click', N)], N), 'optimistically-hidden')
check('trace archive-click then archive-success', solve('row-visible', [('archive-click', N), ('archive-success', N)], N), 'archived')
check('trace archive-click then archive-failure', solve('row-visible', [('archive-click', N), ('archive-failure', N)], N), 'restored-error')
check('trace archive-click then new-revision-arrives', solve('row-visible', [('archive-click', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace archive-success then restore-click', solve('optimistically-hidden', [('archive-success', N), ('restore-click', N)], N), 'restoring')
check('trace archive-failure then retry-archive', solve('optimistically-hidden', [('archive-failure', N), ('retry-archive', N)], N), 'optimistically-hidden')
check('trace retry-archive then archive-success', solve('restored-error', [('retry-archive', N), ('archive-success', N)], N), 'archived')
check('trace retry-archive then archive-failure', solve('restored-error', [('retry-archive', N), ('archive-failure', N)], N), 'restored-error')
check('trace retry-archive then new-revision-arrives', solve('restored-error', [('retry-archive', N), ('new-revision-arrives', N)], N), 'hidden-new-revision')
check('trace restore-click then restore-success', solve('archived', [('restore-click', N), ('restore-success', N)], N), 'row-visible')
check('trace restore-click then restore-failure', solve('archived', [('restore-click', N), ('restore-failure', N)], N), 'restore-error')
check('trace restore-success then archive-click', solve('restoring', [('restore-success', N), ('archive-click', N)], N), 'optimistically-hidden')
check('trace restore-failure then retry-restore', solve('restoring', [('restore-failure', N), ('retry-restore', N)], N), 'restoring')
check('trace retry-restore then restore-success', solve('restore-error', [('retry-restore', N), ('restore-success', N)], N), 'row-visible')
check('trace retry-restore then restore-failure', solve('restore-error', [('retry-restore', N), ('restore-failure', N)], N), 'restore-error')
check('trace new-revision-arrives then archive-failure', solve('optimistically-hidden', [('new-revision-arrives', N), ('archive-failure', N)], N), 'restored-new-revision-error')
check('domain state regression 1: saved', solve('row-visible', [('archive-click', N, {})], N, {}, 'saved'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 2: row', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 1, 'text': 'stale'})], N, {}, 'row'), {'phase': 'optimistically-hidden', 'value': {'revision': 1, 'text': 'old'}})
check('domain state regression 3: visible', solve('optimistically-hidden', [('new-revision-arrives', N, {'revision': 2, 'text': 'new'})], N, {}, 'visible'), {'phase': 'hidden-new-revision', 'value': False})
check('domain state regression 4: row', solve('hidden-new-revision', [('archive-failure', N, {})], N, {'row': {'revision': 2, 'text': 'new'}, 'saved': {'revision': 1, 'text': 'old'}}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
check('domain state regression 5: row', solve('optimistically-hidden', [('archive-failure', N, {})], N, {'saved': {'revision': 1, 'text': 'original'}}, 'row'), {'phase': 'restored-error', 'value': {'revision': 1, 'text': 'original'}})
check('domain state regression 6: visible', solve('restoring', [('restore-failure', N, {})], N, {}, 'visible'), {'phase': 'restore-error', 'value': False})
check('domain state regression 7: row', solve('row-visible', [('archive-click', N, {}), ('new-revision-arrives', N, {'revision': 2, 'text': 'new'}), ('archive-failure', N, {})], N, {}, 'row'), {'phase': 'restored-new-revision-error', 'value': {'revision': 2, 'text': 'new'}})
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:
The retry-restore event leaves a notification in row-visible instead of restoring.

Inbox optimistic archive rollback is a bounded visual-notification workflow with mutable policy state {'row': {'revision': 1, 'text': 'old'}, 'saved': None, 'visible': True}. Its default transition relation is {('row-visible', 'archive-click'): 'optimistically-hidden', ('optimistically-hidden', 'archive-success'): 'archived', ('optimistically-hidden', 'archive-failure'): 'restored-error', ('restored-error', 'retry-archive'): 'optimistically-hidden', ('archived', 'restore-click'): 'restoring', ('restoring', 'restore-success'): 'row-visible', ('restoring', 'restore-failure'): 'restore-error', ('restore-error', 'retry-restore'): 'restoring', ('optimistically-hidden', 'new-revision-arrives'): 'hidden-new-revision', ('hidden-new-revision', 'archive-failure'): 'restored-new-revision-error'}; domain inputs can suppress or redirect transitions and update policy fields, as specified in solve. Different-generation events are inert. Batches apply in order. Optional observe returns the selected policy field alongside the phase.

split: open-access