"""A fixed seed is not a reset contract
Deterministic, standard-library experiment. RESULTS contains the full data.
"""
import hashlib
import json

def fresh(seed):
    return dict(seed=seed, target=seed % 2, cookies={}, visits=0, cache=None)

def fingerprint(state):
    raw = json.dumps(state, sort_keys=True, separators=(',', ':')).encode()
    return hashlib.sha256(raw).hexdigest()

def run(state):
    choice = state['cache'] if state['cache'] is not None else state['target']
    state['cache'] = choice
    state['visits'] += 1
    return choice == state['target'] and state['visits'] == 1

for mode in ('partial', 'complete'):
    identical = successes = 0
    for seed in range(10):
        state = fresh(seed)
        initial = fingerprint(state)
        assert run(state)
        state['cookies']['previous_run'] = True
        state['visits'] += 7
        if seed % 2:
            state['cache'] = 1 - state['target']
        if mode == 'complete':
            state = fresh(seed)
        else:
            state['cookies'] = {}
            state['visits'] = 0
        identical += fingerprint(state) == initial
        successes += run(state)
    print(mode, 'identical_initial_states', identical, 'repeat_successes', successes)
    assert (identical, successes) == ((10, 10) if mode == 'complete' else (0, 5))


records=[]
for mode in ('partial','complete'):
    for seed in range(10):
        state=fresh(seed); initial=fingerprint(state); run(state)
        state['cookies']['previous_run']=True; state['visits']+=7
        if seed%2: state['cache']=1-state['target']
        contaminated=json.loads(json.dumps(state))
        if mode=='complete': state=fresh(seed)
        else: state['cookies']={}; state['visits']=0
        before=json.loads(json.dumps(state)); repeated=fingerprint(state)
        changes=[key for key in state if state[key]!=fresh(seed)[key]]
        success=run(state)
        records.append(dict(mode=mode,seed=seed,initial_hash=initial,repeat_hash=repeated,identical=initial==repeated,contaminated=contaminated,after_reset=before,different_fields=changes,success=success))
RESULTS=dict(repeats=records,contract_fields=sorted(fresh(0)),contract_version=1)
assert all(r['different_fields']==['cache'] for r in records if r['mode']=='partial')
print('All partial-reset mismatches isolated to: cache')
