"""Independent Decimal reconstruction of every Codex projected request.
Executed within verify.py after the measured-receipt verification.
"""
from fractions import Fraction
X=read(ROOT/'artifacts/figures/codex-scenario.json');ids=['agnt-codex','codex-r2','openclaw-codex-r2','omp-codex','hermes-codex']
check(X['commonContent']['c1']==21845 and X['commonContent']['m']==4455,'Codex common parameters')
check(X['prices']['longThresholdExclusive']==272000,'Astra threshold')
# Independent loop uses exact rational context sizes and Decimal prices.
def recompute(n,policy='retained',o=Fraction(0)):
 c=Fraction(21845);g=Fraction(4455)+o;U=Fraction(0);C=Fraction(0);cost=Decimal(0);longs=0;rr=[]
 for t in range(1,n+1):
  I=c+(t-1)*g
  R=I-g if t>1 and policy!='uncached' and not (policy=='break-loss' and (t-1)%4==0) else Fraction(0)
  fresh=I-R;scale=2 if I>272000 else 1
  d=lambda f:Decimal(f.numerator)/Decimal(f.denominator)
  usd=(d(fresh)*10+d(R))*scale/Decimal(1000000)
  U+=fresh;C+=R;cost+=usd;longs+=scale==2;rr.append((t,float(I),float(R),float(fresh),scale,float(usd)))
 return float(U),float(C),float(cost),longs,rr
for i in ids:
 o=max(Fraction(0),Fraction(S[i]['turns'][4]['input']-S[i]['turns'][0]['input'],4)-4455)
 u,c,cost,longs,rr=recompute(20,'retained',o);p=X['oneHourRetained'][i]
 check(abs(u-p['uncached'])<1e-8 and abs(c-p['reads'])<1e-8 and abs(cost-p['usd'])<1e-10,'Codex one-hour '+i)
 for li,n in enumerate([5,10,20,40,80]):
  result=recompute(n,'retained',o);den=recompute(n,'uncached')[2]
  check(abs(result[2]/den-X['multipliers'][i][li])<1e-10,'Codex multiplier '+i)
 for j,n in enumerate([20,40,80]):check(abs(44*recompute(n,'retained',o)[2]-X['monthlyHarnessRetained'][i][j])<1e-8,'Codex harness monthly '+i)
for policy in ['retained','break-loss','uncached']:
 for li,n in enumerate([5,10,20,40,80]):
  u,c,cost,longs,rr=recompute(n,policy);entry=X['policyByLength'][policy][li]
  check(abs(cost-entry['usd'])<1e-10 and entry['longContextRequests']==longs,'Codex policy cost/tier')
  for (t,I,R,U,scale,usd),r in zip(rr,entry['requests']):
   check(t==r['turn'] and I==r['input'] and R==r['reads'] and U==r['uncached'] and scale==r['priceScale'] and abs(usd-r['usd'])<1e-12,'Codex per-request model')
 for j,n in enumerate([20,40,80]):check(abs(44*recompute(n,policy)[2]-X['monthlyPolicies'][policy][j])<1e-8,'Codex monthly direct sum')
 check(abs(X['annualPolicyHeavy'][policy]-12*X['monthlyPolicies'][policy][2])<1e-8,'Codex annual')
 check(abs(X['teamFiveAnnualHeavy'][policy]-5*X['annualPolicyHeavy'][policy])<1e-8,'Codex team')
for r in X['measured']:
 ts=S[r['id']]['turns'];check(r['input']==sum(t['input'] for t in ts) and r['reads']==sum(t['cacheRead'] for t in ts),'Codex actual totals')
 check(abs(r['fiveRequestUSD']-sum(t['apiEquivalentUSD'] for t in ts))<1e-10,'Codex actual session USD')
 check(abs(r['weightedReadShare']-r['reads']/r['input'])<1e-12,'Codex actual weighted share')
check(X['amplifier']['incrementalMonthUSD']==(10-1)*100000/1e6*30*22,'Codex tool-loss increment')
coverage=read(ROOT/'artifacts/data/section-coverage.json');check(len(coverage['originalMainSections'])==11,'reference main-section coverage');check(len(coverage['codexCoverage'])==11,'Codex counterpart coverage')
for n in range(1,7):
 check(f'id="figC{n}"' in html and f'Figure C{n}.' in html and f'Figure C{n}.' in tex,'Codex chart in both versions')
for n in range(1,9):check(f'Table C{n}.' in html and f'Table C{n}.' in tex,'Codex table in both versions')
for term in ['Codex caching semantics and explicit price list','Codex-specific controls','Codex — per-turn telemetry','Codex — context growth','Codex cost model and one-hour projection','Codex monthly and annual extrapolation','Codex cache-loss tax and team scale','Codex agentic amplifier','Codex subscription versus metered OpenAI access']:
 check(term in prose,'Codex required section '+term)
check('Codex is excluded from dollar projections' not in prose,'Codex not excluded')
for word in ['footer','natural-use','96.33','96.43','fix/cache','worktree','ledger']:
 check(word not in prose.lower(),'internal commentary absent '+word)
check('subscription allowance' in prose,'no claimed token-to-quota conversion')
