repair-checkpoints.py

back to table · edit · history · where entries came from · files · download

5635 bytes, as of the version from 2026-11-16 22:11 (current). Recorded here, not run.

"""T728 repair: verify unchanged entries by recomposing saved certified inputs.

Run under Sage with generate.py, hardy_arb.pyx, numerical-evidence.json,
endpoint-checkpoints.json, its source dependencies, before-snapshot.json,
before-document.json and after-document.json beside this file.
Configure NUMBERDB_API_KEY, or pipe the key on stdin with NUMBERDB_KEY_FROM_STDIN=1.
No roots or expensive special-function jets are regenerated. This is reuse of
verified, hash-pinned builder inputs with fresh gamma-envelope arithmetic; it
is NOT independent scientific validation. Nothing is uploaded by this program.
"""
import os,sys,json,hashlib,platform
from pathlib import Path
from decimal import Decimal
import numberdb.sage as numberdb
from sage.rings.real_arb import RealBallField
from sage.rings.rational_field import QQ
from sage.env import SAGE_VERSION
from generate import XiStrongLehmerCondition,PAIR_INDICES,ZERO_INDICES,endpoint_q
W=Path(__file__).resolve().parent
if os.environ.get('NUMBERDB_KEY_FROM_STDIN')=='1':
 key=sys.stdin.read().strip()
 if '=' in key and key.split('=',1)[0].isupper():key=key.split('=',1)[1].strip().strip('\'"')
 os.environ['NUMBERDB_API_KEY']=key
R=RealBallField(600)
def sha(p):return hashlib.sha256(p.read_bytes()).hexdigest()
def rational(text):
 d=Decimal(str(text));s,ds,e=d.as_tuple()
 return QQ(int(''.join(map(str,ds)) or '0')*(-1 if s else 1))*QQ(10)**e
def interval(text):
 d=Decimal(text);c=rational(text);u=QQ(10)**d.as_tuple().exponent
 return c-u,c+u
def inside(v,text):
 a,b=interval(text)
 return v.is_finite() and bool(a<v<b)
def ball(text):
 assert text.startswith('[') and text.endswith(']')
 return R(text[1:-1])
before=json.loads((W/'before-document.json').read_text());after=json.loads((W/'after-document.json').read_text())
assert {k:v for k,v in before.items() if k!='Programs'}=={k:v for k,v in after.items() if k!='Programs'}
assert before['Programs']['mpmath']==after['Programs']['example']
snapshot=json.loads((W/'before-snapshot.json').read_text())
ev=json.loads((W/'numerical-evidence.json').read_text())
hashes={}
for name,h in ev['source_sha256'].items():
 assert sha(W/name)==h==snapshot['files'][name]
 hashes[name]=h
for name in ['endpoint-checkpoints.json','numerical-evidence.json']:
 assert sha(W/name)==snapshot['files'][name];hashes[name]=sha(W/name)
endpoints=json.loads((W/'endpoint-checkpoints.json').read_text());assert len(endpoints)==107
endpoints={int(e['n']):e for e in endpoints};assert sorted(endpoints)==list(ZERO_INDICES)
rows={str(r['n']):r for r in ev['rows']}
assert list(after['Numbers'])==[str(n) for n in PAIR_INDICES]
assert len(after['Numbers'])==len(rows)==105
values={};endpoint_results=[]
for n in ZERO_INDICES:
 e=endpoints[n];t=ball(e['gamma']);ds=tuple(ball(s) for s in e['derivatives'])
 assert t.is_finite() and all(d.is_finite() for d in ds) and not ds[0].contains_zero()
 q=endpoint_q(t,ds);saved=ball(e['minus_Q_Xi'])
 assert q.is_finite() and saved.is_finite() and q.overlaps(saved)
 e['_t']=t;e['_q']=q
 endpoint_results.append({'n':n,'fresh_q_overlaps_saved_q':True,'fresh_q_accuracy_bits':int(q.accuracy())})
results=[]
for n in PAIR_INDICES:
 a=endpoints[n];b=endpoints[n+1];assert a['_t']<b['_t']
 value=(b['_t']-a['_t'])**2*(a['_q']+b['_q']);text=after['Numbers'][str(n)];row=rows[str(n)]
 assert text==row['number'] and inside(value,text)
 assert value.accuracy()>=490
 assert inside(ball(row['ball']),text) and inside(ball(row['high_ball']),text)
 assert inside(R(rational(row['mpmath_130dps'])),text) and inside(R(rational(row['mpmath_165dps'])),text)
 values[str(n)]=value
 results.append({'n':n,'stored':text,'recomposed_ball':str(value),'accuracy_bits':int(value.accuracy()),'both_saved_balls_inside_stored_interval':True,'both_saved_mpmath_values_inside_stored_interval':True})
# Controls exercise the actual interval predicate with finite and invalid data.
assert inside(R('3.14'),'3.14')
assert not inside(R('nan'),'3.14')
assert not inside(R('3.14 +/- 1'),'3.14')
assert not inside(values['1']+QQ(1)/10**90,after['Numbers']['1'])
assert not inside(values['2'],after['Numbers']['1'])
class CheckpointReplay(XiStrongLehmerCondition):
 def value(self,params,digits):
  assert digits==100
  return values[str(params['n'])]
g=CheckpointReplay()
assert list(g.enumerate())==[{'n':str(n)} for n in PAIR_INDICES]
report=g.verify(sample=None)
assert report.ok and report.checked==report.matched==105
live=numberdb.table('T728');assert live==after
result={'table':'T728','stage':'repair','checked_entries':105,'total_entries':105,'recomposed_endpoints':107,'failures':[],'known_answer_control':True,'negative_control_rejected':True,'nonfinite_control_rejected':True,'wide_ball_control_rejected':True,'wrong_index_control_rejected':True,'minimum_accuracy_bits':min(r['accuracy_bits'] for r in results),'verify_sample_none':str(report),'all_values_retained':True,'roots_and_hardy_jets_recomputed':False,'fresh_arithmetic':'Gamma-envelope ratios from saved full root and Hardy-jet enclosures, followed by pair arithmetic','independence':'Same generating formula; checkpoint reproducibility regression only. Fresh independent final-revision critique remains required.','source_sha256':hashes,'versions':{'sage':SAGE_VERSION,'python':platform.python_version(),'numberdb':numberdb.__version__},'skill_manifest':'/home/ubuntu/numberdb-campaign-20261116-sol/repo/agents/runs/20261116T220407Z-repair-skills/manifest.json','skill_manifest_sha256':'27ec6a391956d87a58148a74289700551986207fd04c93537b499a2711ae9cb0','endpoints':endpoint_results,'rows':results}
print('REPAIR_CHECKPOINT_RESULT_JSON '+json.dumps(result),flush=True)