"""Exact counterexamples to reviewer extensions, not an empirical auditor test."""
from fractions import Fraction as F
from pathlib import Path
import json

def mean(w):
    return [sum(c)/len(w) for c in zip(*w)]

def mul(a,b):
    return [[sum(x*y for x,y in zip(r,c)) for c in zip(*b)] for r in a]

def cycle(w,q,beta,t):
    n=len(w)
    joined=[[(1-beta)*x for x in r]+[beta] for r in w]+[[(1-beta)*x for x in q]+[beta]]
    mixed=mul(t,joined)
    return [[x/sum(r[:n]) for x in r[:n]] for r in mixed[:n]]

def l1(v): return sum(map(abs,v))
def sub(a,b): return [x-y for x,y in zip(a,b)]
def serial(x):
    if isinstance(x,F): return str(x)
    if isinstance(x,dict): return {k:serial(v) for k,v in x.items()}
    if isinstance(x,list): return [serial(v) for v in x]
    return x

w=[[F(9,10),F(1,10)],[F(1,10),F(9,10)]]
q=[F(4,5),F(1,5)]
t=[[F(1,3)]*3 for _ in range(3)]
u=cycle(w,q,F(1,2),t)
assert mean(u)!=mean(w)  # No resets, but a membership residue remains.
p=[[F(1,2)]*2 for _ in range(2)]
v=[F(4,5),F(1,5)]
pre=[x/2 for x in sub(v,w[0])]
post=[x/2 for x in sub(v,mul(p,w)[0])]
assert l1(post)>l1(pre)  # Post-mixing intervention can be larger.
assert sub(post,pre)==[x/2 for x in sub(w[0],mul(p,w)[0])]
# In a generic row-stochastic mixing map, one survivor never hears entrant.
# Even as incumbent retained masses vanish, its mean share remains 1/2.
deference_limit_mean=[(w[0][i]+q[i])/2 for i in range(2)]
assert deference_limit_mean!=q
# Successive prescribed single-newcomer cycles: order can matter.
q2=[F(1,5),F(4,5)]
ab=mean(cycle(cycle(w,q,F(1,2),t),q2,F(1,2),t))
ba=mean(cycle(cycle(w,q2,F(1,2),t),q,F(1,2),t))
assert ab!=ba
out={'status':'PASS','scope':'Exact finite mathematical diagnostics, not general proof or real-auditor validation.',
     'no_resets':{'initial_mean':mean(w),'returned_mean':mean(u)},
     'reset_timing':{'before':pre,'after':post,'before_l1':l1(pre),'after_l1':l1(post),
       'formula':'post minus pre = (w_i - (PW)_i)/n; no universal magnitude ordering'},
     'deference':{'T_survivors':[['1','0','0'],['0','0','1']],
       'theta_newcomer':'1/2','returned_mean':deference_limit_mean,'q':q,
       'conclusion':'beta tending to 1 need not yield theta_newcomer tending to 1 without influence in every survivor row'},
     'two_successive_cycles':{'order_ab':ab,'order_ba':ba,
       'limit':'One exact sequential example, not simultaneous multi-newcomer characterization or indefinite-churn convergence'}}
Path(__file__).with_name('REVIEW04_CHECKS.json').write_text(json.dumps(serial(out),indent=2)+'\n',encoding='utf-8')
print('PASS: no-reset residue, timing counterexample, partial influence, two-cycle order.')
