"""Deterministic interleaving regression: stale expected revision must not overwrite."""
def buggy_apply(state, expected_revision, value):
    return {'revision':state['revision']+1, 'value':value}, 'ok'
def guarded_apply(state, expected_revision, value):
    if state['revision'] != expected_revision:
        return dict(state), 'conflict'
    return {'revision':state['revision']+1, 'value':value}, 'ok'
def scenario(apply, *, first='A'):
    state = {'revision':1,'value':0}
    values = {'A':10,'B':20}
    state,status = apply(state,1,values[first])
    assert status == 'ok'
    expected = dict(state)
    state,status = apply(state,1,values['B' if first=='A' else 'A'])
    return state == expected and status == 'conflict'
assert not scenario(buggy_apply)  # Reproduces lost-update behavior.
assert scenario(guarded_apply) and scenario(guarded_apply, first='B')
state,status = guarded_apply({'revision':2,'value':10},2,20)
assert state == {'revision':3,'value':20} and status == 'ok'  # Retry after reread.
print('PASS: old code reproduces failure; guard protects both orders; fresh-revision retry works')
