"""Check the unchanged harmonic identities of T626 after a line-wrap repair.

Download generate.py from https://numberdb.org/files/T626/generate.py?raw=1
beside this file, then run: sage -python repair_checks.py
This is an exact consistency check of the existing generator, not an
independent recomputation of the stored values or an argument-review verdict.
The canonical instructions used for this repair are archived in
agents/runs/20261030T132806Z-repair-skills/manifest.json; both local and live
skill SHA-256 values are 28ecbeeccb0c5e5d6eb541b5a5b0f88aec47b154374d909085116d34f5629199.
"""
import importlib.util
import json
from pathlib import Path

folder=Path(__file__).resolve().parent
source=folder/'generate.py'
if not source.exists():
    source=folder/'T626-before-generate.py'
spec=importlib.util.spec_from_file_location('t626_generator',source)
g=importlib.util.module_from_spec(spec)
spec.loader.exec_module(g)
t=g.triangular

def ha(m,n):
    return 3*t(m,n)

def hb(m,n):
    return t(m,n)+t(m+1,n)+t(m+1,n+1)

# An exact known-value control precedes the residual checks.
assert 3*t(1,0)==1
assert t(0,0)==0
counts={'triangular A':0,'honeycomb A':0,'honeycomb B':0}
for params in g.Resistance().enumerate():
    m,n=int(params['m']),int(params['n'])
    kind=params['lattice']+' '+params['sublattice']
    if kind=='triangular A':
        assert (m,n)!=(0,0)
        residual=6*t(m,n)-sum(t(m+dm,n+dn) for dm,dn in [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1)])
    elif kind=='honeycomb A':
        assert (m,n)!=(0,0)
        residual=3*ha(m,n)-hb(m,n)-hb(m-1,n)-hb(m-1,n-1)
    else:
        residual=3*hb(m,n)-ha(m,n)-ha(m+1,n)-ha(m+1,n+1)
    assert residual==0,(params,str(residual))
    counts[kind]+=1
assert counts=={'triangular A':48,'honeycomb A':15,'honeycomb B':21}
print(json.dumps({'table':'T626','exact_harmonic_residuals':counts,'all_zero':True,
 'known_value_controls':True,'arithmetic':'exact polynomials over QQ; no floating arguments',
 'scope':'Consistency of existing generator with unchanged harmonic formulas; not independent numerical or argument review',
 'skill_manifest':'agents/runs/20261030T132806Z-repair-skills/manifest.json',
 'skill_sha256_local':'28ecbeeccb0c5e5d6eb541b5a5b0f88aec47b154374d909085116d34f5629199',
 'skill_sha256_live':'28ecbeeccb0c5e5d6eb541b5a5b0f88aec47b154374d909085116d34f5629199'},indent=2))
