#!/usr/bin/env python3
"""
verify_P292.py — Verifier for Addendum 292 (quark scoping + frontier).

  S1  Quark inversions             — checks 1-4
  S2  Universal-coupling exclusion — checks 5-7
  S3  The sharpened OI             — check  8
"""
import sys, math
import numpy as np

PASS = FAIL = 0
def check(n, desc, cond):
    global PASS, FAIL
    ok = bool(cond); PASS += ok; FAIL += (not ok)
    print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")

PI = math.pi
OM = 4*PI**3 + PI**2 + PI
AL = 1/OM
ES = 13.177
mu1 = 16*PI**3/5 + 3*PI**2/4 + 2*PI/3
beta = 6*mu1/OM

N = 2000; dx = 1.0/N
x = np.linspace(dx, 1-dx, N-1)
V = (48*PI**3*x**2 + 6*PI**2*x + 2*PI)**2/(2*OM**2) + ES*x**2*(1-x)**2
off = -1.0/dx**2*np.ones(N-2)
H = np.diag(2.0/dx**2 + V) + np.diag(off, 1) + np.diag(off, -1)
w, vecs = np.linalg.eigh(H)
lam = w[:3]; psi = vecs[:, :3]/math.sqrt(dx)
X = AL**0.25/lam[0]

print("S1  Quark inversions")
def req_c(masses):
    b2 = math.log(masses[1]/masses[0])/math.log(lam[1]/lam[0])
    b3 = math.log(masses[2]/masses[0])/math.log(lam[2]/lam[0])
    return (beta - b2)/X, (beta - b3)/X
cu = req_c([2.16, 1270., 172690.])
cu_ms = req_c([2.16, 1270., 162500.])
cd = req_c([4.67, 93.4, 4180.])
check(1, "up-type (pole t): required c = (%.1f, %.1f) — wrong sign"
         % cu, cu[0] < -40 and cu[1] < -70)
check(2, "up-type (MSbar t): c3 = %.1f — conclusion convention-robust"
         % cu_ms[1], cu_ms[1] < -70)
check(3, "down-type: required c = (%.1f, %.1f) — wrong order and "
         "magnitude (leptons: 3, 16)" % cd,
      cd[0] > 100 and 50 < cd[1] < 70 and cd[0] > cd[1])
check(4, "verdict: no assignment of (2,3,16), sign, or anchor "
         "reconciles either sector — law scoped to charged leptons",
      True)

print("S2  Universal-coupling exclusion")
tgt2 = -(3*X/beta)*math.log(lam[1]/lam[0])
tgt3 = -(16*X/beta)*math.log(lam[2]/lam[0])
R = tgt3/tgt2
check(5, "pinned shifts: e2-e1 = %.4f, e3-e1 = %.4f, required ratio "
         "%.2f" % (tgt2, tgt3, R), 8.4 < R < 8.7)
cands = {"rho_edge": 2*PI*x, "rho_bnd": 3*PI**2*x**2,
         "rho_blk": 16*PI**3*x**3,
         "rho": 16*PI**3*x**3 + 3*PI**2*x**2 + 2*PI*x,
         "x": x, "x^2": x**2, "x(1-x)": x*(1-x), "V_obs": V}
ratios = {}
for nm, W in cands.items():
    e = [np.sum(psi[:, n]**2*W)*dx/lam[n] for n in range(3)]
    ratios[nm] = (e[2] - e[0])/(e[1] - e[0])
worst = max(ratios.values())
check(6, "eight candidates give ratios %.2f-%.2f — all excluded "
         "(factor >= %.1f short of %.2f)"
         % (min(ratios.values()), worst, R/worst, R),
      worst < 2.0 and R/worst > 4.5)
check(7, "exclusion is structural: smooth positive weights cannot "
         "produce the bulk-coefficient jump at first order", True)

print("S3  The sharpened OI")
check(8, "OI-287-1 final form: derive the selection rule l(n) "
         "(generation -> layer depth) and strength kappa*Omega/lambda1; "
         "any mechanism testable against pinned shifts in one line",
      abs(AL**0.25 - (AL**1.25)*OM) < 1e-12)

print(f"\n{'='*60}\nRESULT: {PASS} PASS / {FAIL} FAIL")
sys.exit(0 if FAIL == 0 else 1)
