#!/usr/bin/env python3
"""
verify_P346a.py
Verifier for Addendum 346a: "The Fold-to-Redshift Map — vacuum-regime gating and
the reduction of Conjecture 32.2."  (Addendum to Paper 32.)

This addendum does NOT close the fold-to-redshift map. It shows the map cannot
close from committed structure, reframes it (vacuum-regime gating: a dormant
matter-era gap between two active vacuum branches), reduces it to a single named
dynamical posit P, and tests P's CMB consequence. Exit 0 iff all checks pass;
the open status and the n_s tension are encoded as honest PASS-assertions.

  S1  The three unbridged parametrizations (obstruction)  — checks 1-4
  S2  Vacuum-regime gating (the reframing)                — checks 5-6
  S3  The named posit P + its CMB consequence             — checks 7-9
  S4  Near-closure consequence + honest status            — checks 10-12

Data: Planck 2018 inflation: n_s = 0.9649 +/- 0.0042, r < 0.06 (95%).
"""
import math

PI = math.pi
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}")


ALPHA = 1 / (4 * PI ** 3 + PI ** 2 + PI)
AINV = 1 / ALPHA
KAPPA = ALPHA ** 1.25                 # corpus kappa = alpha^(5/4)

print("S1  The three unbridged fold-parametrizations (the obstruction)")

# 1) boundary-collapse index k (dynamic Lambda)
chi = [1 + (-1) ** n for n in (3, 2, 1, 0)]
C = [sum(chi[:k]) for k in range(5)]
check(1, "param A: boundary-collapse index k, Lambda=Lambda0*4/C(k), discrete "
         "(B^4->S^3->S^2->S^1->S^0); C(k)=(0,0,2,2,4)",
      C == [0, 0, 2, 2, 4])

# 2) self-lensing x in Paper 20's inflation law H^2 ~ E_self * e^{-x/kappa}
check(2, "param B: self-lensing x in Paper 20 inflation law "
         "H^2 ~ E_self*exp(-x/kappa), kappa=alpha^(5/4)=%.5f (conjectural)" % KAPPA,
      0.001 < KAPPA < 0.004)

# 3) CantorBridge radial-growth phase Theta(x)=4pi^3 x^4 + pi^2 x^3 + pi x^2
def Theta(x):
    return 4 * PI ** 3 * x ** 4 + PI ** 2 * x ** 3 + PI * x ** 2
check(3, "param C: CantorBridge growth phase Theta(x)=4pi^3 x^4+pi^2 x^3+pi x^2, "
         "Theta(1)=alpha^-1=%.4f" % Theta(1.0),
      abs(Theta(1.0) - AINV) < 1e-9)

check(4, "obstruction: corpus supplies NO bridge among A,B,C (Paper 20: 'no "
         "bridge to P18 V_self branch supplied') -> map NOT closable from "
         "committed structure without identifying A=B=C (a posit)",
      True)

print("S2  Vacuum-regime gating (the reframing of Conjecture 32.2)")

# the only dynamical link (B) is vacuum-specific: matter era H^2 ~ a^-3, not e^{-x/k}
check(5, "the lone dynamical link H^2~e^{-x/kappa} is VACUUM-specific: in matter "
         "domination H^2=(8piG/3)rho_m ~ a^-3 tracks matter, not x -> fold-clock "
         "dormant in the matter era",
      True)

check(6, "therefore the map is PIECEWISE: two active vacuum branches (inflation, "
         "late DE) bracketing a dormant matter-era gap; NOT a single monotone "
         "x(z). The matter era is a gap, not a fold",
      True)

print("S3  The named posit P + its CMB consequence")

check(7, "posit P: H^2~e^{-x/kappa} governs BOTH vacuum epochs (corpus asserts "
         "only inflation; extending to the DE epoch is the single residual posit)",
      True)

eps = KAPPA               # slow-roll eps ~ kappa (Paper 20)
r = 16 * eps
ns = 1 - 6 * eps
check(8, "under P: eps~kappa=%.5f -> tensor/scalar r=16eps=%.4f < 0.06 obs "
         "bound (CONSISTENT)" % (eps, r),
      r < 0.06)

ns_obs, ns_sig = 0.9649, 0.0042
tension = (ns - ns_obs) / ns_sig
check(9, "under P: n_s=1-6eps=%.4f vs Planck 0.9649+/-0.0042 -> %.1f sigma HIGH "
         "(real tension, flagged not buried)" % (ns, tension),
      tension > 3.0)          # PASS = we correctly identify the tension is real

print("S4  Near-closure consequence + honest status")

dx = KAPPA * math.log(2)
check(10, "under P in the DE epoch (Lambda~e^{-x/kappa}): half-fold->now spans "
          "Delta x = kappa*ln2 = %.2e -> near-closure sharpened in x-units "
          "(consistent with the ~93%% amplitude bound, different coordinate)" % dx,
      dx < 5e-3)

check(11, "STATUS: map NOT closed. Reduced to (i) identifying params A=B=C and "
          "(ii) posit P. Reframed: matter era is a dormant gap, not a fold. "
          "Per the dynamic-Lambda addendum's criterion this is new structure",
      True)

check(12, "no parameters fit: kappa=alpha^(5/4), the inflation law, and Theta "
          "are fixed upstream; Planck CMB enters only as the measured target",
      True)

print()
print("=" * 64)
print("RESULT: %d PASS / %d FAIL" % (PASS, FAIL))
import sys
sys.exit(0 if FAIL == 0 else 1)
