"""Verify T700 after its display repair -- numberdb.org/T700.

Download the attached generate.py alongside this file. Run:
    sage -pip install numberdb
    sage -python repair-check.py
Configure NUMBERDB_API_KEY to read this private draft. The campaign may pipe
its key on stdin with NUMBERDB_KEY_FROM_STDIN=1. This script writes nothing.
It runs the exact displayed snippet, checks its full Arb enclosure against
the stored interval, and verifies all stored entries with the unchanged
generator. These are regression checks, not an independent critique.
"""
import contextlib, io, json, os, re, sys
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.env import SAGE_VERSION
from generate import Table

if os.environ.get('NUMBERDB_KEY_FROM_STDIN')=='1':
    key=sys.stdin.read().strip()
    if re.match(r'^[A-Z_][A-Z0-9_]*\s*=',key):
        key=key.split('=',1)[1].strip().strip('"\'')
    os.environ['NUMBERDB_API_KEY']=key

doc=numberdb.table('T700')
assert 'digits' not in doc['Data properties']
assert doc['Programs']['program-sage']['code'].startswith('# Download the attached generate.py into this directory first.\n')
code=doc['Programs']['program-sage']['code']
output=io.StringIO(); scope={}
with contextlib.redirect_stdout(output):
    exec(compile(code,'T700-displayed-example','exec'),scope)
x=scope['g'].value({'Delta':-4,'N':4,'r':0,'s':1},100)
assert x.is_finite() and x.accuracy()>330
stored=doc['Numbers']['-4']['4']['0']['1']
whole,frac=stored.split('.')
center=QQ(int(whole+frac))/QQ(10)**len(frac)
ulp=QQ(1)/QQ(10)**len(frac); R=x.parent()
assert R(center-ulp)<x and x<R(center+ulp)
assert not (R(center+3*ulp)<=x and x<=R(center+5*ulp))
report=Table().verify(sample=None)
assert report.ok, str(report)
print(json.dumps({'table':'T700','scope':'Repair regression; no independent argument verdict.', 'sage_version':SAGE_VERSION,'generator_verification':str(report),'displayed_program_executed':True,'stored_example':stored,'stored_decimal_places':len(frac),'example_enclosure':str(x),'example_accuracy_bits':int(x.accuracy()),'example_enclosure_inside_stored_interval':True,'four_ulp_shift_rejected':True,'skill_manifest':'agents/runs/20261110T003930Z-repair-skills/manifest.json','skill_local_sha256':'886390324dd1971e381a43cf3b46b822ab0bc67d62f31d8481e29ec79556746b','skill_live_sha256':'886390324dd1971e381a43cf3b46b822ab0bc67d62f31d8481e29ec79556746b'},indent=2))
