Research-Stack/4-Infrastructure/hardware/standard_model_dna_substitution_alignment.py
Devin AI 0c9efac330 chore(consolidation): integrate E8Sidon stack (PRs #79 #80 #81 #89) into one PR
Squash the four overlapping feature branches into a single change set against
main, eliminating cross-PR merge conflicts and the duplicated CI-fix scripts.

What this brings in (merge order #79 -> #80 -> #81 -> #89):
- #79 refactor(infra): shared utilities (4-Infrastructure/lib/*: q16, hashing,
  jsonl, fraction_utils) + the scripts/math-first/* validators that the
  math-check CI requires.
- #80 feat(lean): Semantics.E8Sidon (1025 lines) -- Eisenstein coefficient
  identity E4^2 = E8 and the Sidon framework. E4_sq_eq_E8_coeff is fully proved
  (all Fourier-coefficient extraction machine-checked); the single residual gap
  is pinned to E4_sq_eq_E8_qExpansion (Mathlib lacks the valence formula /
  dim M8 = 1). 4 sorries + 1 axiom (e8_additive_completeness), all TODO(lean-port).
- #81 refactor(lean): Float-free FixedPoint core (integer-only sqrt/log2/expNeg).
  E8Sidon.lean kept at #80's final 1025-line version (the #81 intermediate
  438-line copy was overridden by merge order).
- #89 feat(lean): Semantics.RRC.PolyFactorIdentity -- short-sleeve polynomial
  detection at the zerocopy limb boundary; now imports Semantics.E8Sidon for
  sigma3/sigma7/convolutionLHS (single source of truth) instead of inlining them.

Conflict resolution:
- flake.nix -> canonical rs-surface removal (Garnix shutdown).
- scripts/math-first/* -> byte-identical across branches, clean.
- .cursorrules / AGENTS.md -> unified; baselines + sorry inventory refreshed.

Verification:
- lake build (default aggregator): 3573 jobs, 0 errors.
- lake build Semantics.RRC.PolyFactorIdentity (E8Sidon + FixedPoint + PolyFactor):
  3655 jobs, 0 errors. Witnesses verified (sigma7 4 = 16513, convolutionLHS 6 = 2350).
- Python tests: 68/68 pass.

Note: the "Workers Builds: researchstack" check is a preexisting external
Cloudflare build unrelated to this change (no branch touches 4-Infrastructure/cloudflare/).

Build: 3573 jobs (default), 3655 jobs (narrow), 0 errors
Co-Authored-By: Allaun Silverfox <bigdataiscoming+9i37y6j2@protonmail.com>
2026-06-16 02:01:31 +00:00

245 lines
9.1 KiB
Python

#!/usr/bin/env python3
"""DNA substitution alignment for the force-regime compression model.
This substitutes the four local primitives with the A/T/G/C genetic event
alphabet used elsewhere in the hardware probes:
* A -> field
* T -> shear
* G -> packet
* C -> spectral
The goal is modest: check whether the existing 4D primitive keel and genus-3
residual boat can be represented as a DNA-like alphabet without losing closure.
This is a symbolic compression substitution, not a biological or physics claim.
"""
from __future__ import annotations
import argparse
import json
import math
from datetime import datetime, timezone
from fractions import Fraction
from pathlib import Path
from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(REPO / "4-Infrastructure"))
from lib.hashing import sha256_bytes
from lib.fraction_utils import fraction_json, fraction_str, parse_fraction_json, vector_from_json, vector_json
from lib.jsonl import load_json, stable_json
FORCE_RECEIPT = (
REPO
/ "4-Infrastructure"
/ "hardware"
/ "standard_model_force_regime_model_receipt.json"
)
OUT = (
REPO
/ "4-Infrastructure"
/ "hardware"
/ "standard_model_dna_substitution_alignment_receipt.json"
)
BASE_TO_PRIMITIVE = {
"A": "field",
"T": "shear",
"G": "packet",
"C": "spectral",
}
PRIMITIVE_TO_BASE = {primitive: base for base, primitive in BASE_TO_PRIMITIVE.items()}
BASE_PHASE_DEGREES = {
"A": 0,
"T": 90,
"G": 180,
"C": 270,
}
HANDLE_TO_BASE = {
"packet_local": "G",
"shear_torsion": "T",
"spectral_field": "C",
}
def signed_l1(vector: dict[str, Fraction]) -> Fraction:
return sum((abs(value) for value in vector.values()), Fraction(0))
def base_vector_from_primitive(primitive_vector: dict[str, Fraction]) -> dict[str, Fraction]:
return {
base: primitive_vector[primitive]
for base, primitive in BASE_TO_PRIMITIVE.items()
}
def primitive_vector_from_base(base_vector: dict[str, Fraction]) -> dict[str, Fraction]:
return {
primitive: base_vector[base]
for base, primitive in BASE_TO_PRIMITIVE.items()
}
def phase_centroid(base_vector: dict[str, Fraction]) -> dict[str, Any]:
real = 0.0
imag = 0.0
for base, value in base_vector.items():
radians = math.radians(BASE_PHASE_DEGREES[base])
real += float(value) * math.cos(radians)
imag += float(value) * math.sin(radians)
magnitude = math.hypot(real, imag)
angle = math.degrees(math.atan2(imag, real)) % 360.0 if magnitude else 0.0
return {
"real": real,
"imag": imag,
"magnitude": magnitude,
"angle_degrees": angle,
}
def dominant_base(base_vector: dict[str, Fraction]) -> dict[str, Any]:
base, value = max(base_vector.items(), key=lambda item: abs(item[1]))
return {
"base": base,
"primitive": BASE_TO_PRIMITIVE[base],
"value": fraction_json(value),
"phase_degrees": BASE_PHASE_DEGREES[base],
}
def sector_dna_signatures(force: dict[str, Any]) -> dict[str, Any]:
sectors = {}
for sector, data in force["force_like_sectors"].items():
primitive_vector = vector_from_json(data["primitive_vector"])
base_vector = base_vector_from_primitive(primitive_vector)
handle_vector = vector_from_json(data["residual_handle_vector"])
residual_bases = {base: Fraction(0) for base in BASE_TO_PRIMITIVE}
for handle, value in handle_vector.items():
residual_bases[HANDLE_TO_BASE[handle]] += value
sectors[sector] = {
"base_vector": vector_json(base_vector),
"dominant_base": dominant_base(base_vector),
"phase_centroid": phase_centroid(base_vector),
"residual_base_vector": vector_json(residual_bases),
"dominant_residual_base": dominant_base(residual_bases),
"codon_hint": "".join(
sorted(
BASE_TO_PRIMITIVE,
key=lambda base: abs(base_vector[base]),
reverse=True,
)[:3]
),
"source_dominant_primitive": data["dominant_primitive"],
"source_dominant_residual_handle": data["dominant_residual_handle"],
}
return sectors
def build_receipt() -> dict[str, Any]:
force = load_json(FORCE_RECEIPT)
primitive_keel = vector_from_json(force["closure"]["primitive_target"])
dna_keel = base_vector_from_primitive(primitive_keel)
roundtrip_primitive = primitive_vector_from_base(dna_keel)
primitive_delta = {
primitive: roundtrip_primitive[primitive] - primitive_keel[primitive]
for primitive in primitive_keel
}
handle_signed_sum = vector_from_json(force["closure"]["handle_signed_sum_target"])
residual_base_signed = {base: Fraction(0) for base in BASE_TO_PRIMITIVE}
for handle, value in handle_signed_sum.items():
residual_base_signed[HANDLE_TO_BASE[handle]] += value
sectors = sector_dna_signatures(force)
receipt = {
"schema": "standard_model_dna_substitution_alignment_receipt_v1",
"generated_utc": datetime.now(timezone.utc).isoformat(),
"surface_id": "standard_model_dna_substitution_alignment",
"source": {
"force_regime_receipt": str(FORCE_RECEIPT.relative_to(REPO)),
"force_regime_stable_hash_sha256": force.get("stable_force_regime_hash_sha256"),
},
"substitution": {
"base_to_primitive": BASE_TO_PRIMITIVE,
"primitive_to_base": PRIMITIVE_TO_BASE,
"base_phase_degrees": BASE_PHASE_DEGREES,
"handle_to_base": HANDLE_TO_BASE,
"meaning": (
"DNA bases are used as a four-symbol control alphabet over the "
"existing primitive coordinates."
),
},
"dna_keel": {
"base_vector": vector_json(dna_keel),
"base_total": fraction_json(sum(dna_keel.values(), Fraction(0))),
"dominant_base": dominant_base(dna_keel),
"phase_centroid": phase_centroid(dna_keel),
"roundtrip_primitive_delta": vector_json(primitive_delta),
"roundtrip_l1_error": fraction_json(signed_l1(primitive_delta)),
},
"dna_residual_boat": {
"residual_base_signed_vector": vector_json(residual_base_signed),
"residual_base_signed_total": fraction_json(sum(residual_base_signed.values(), Fraction(0))),
"zero_drift": sum(residual_base_signed.values(), Fraction(0)) == 0,
"dominant_residual_base": dominant_base(residual_base_signed),
},
"force_sector_dna_signatures": sectors,
"alignment": {
"primitive_roundtrip_exact": all(value == 0 for value in primitive_delta.values()),
"keel_total_is_one": sum(dna_keel.values(), Fraction(0)) == 1,
"residual_zero_drift": sum(residual_base_signed.values(), Fraction(0)) == 0,
"force_regime_closed": force["closure"]["closed"],
"aligned": (
all(value == 0 for value in primitive_delta.values())
and sum(dna_keel.values(), Fraction(0)) == 1
and sum(residual_base_signed.values(), Fraction(0)) == 0
and force["closure"]["closed"]
),
},
"claim_boundary": (
"This substitutes DNA bases as a symbolic four-letter control "
"alphabet. It does not imply biological DNA implements the Standard "
"Model, validate genomic physics, or make a synthetic-biology claim."
),
"lawful": True,
}
stable_preimage = stable_json({
"schema": receipt["schema"],
"surface_id": receipt["surface_id"],
"source": receipt["source"],
"substitution": receipt["substitution"],
"dna_keel": receipt["dna_keel"],
"dna_residual_boat": receipt["dna_residual_boat"],
"force_sector_dna_signatures": receipt["force_sector_dna_signatures"],
"alignment": receipt["alignment"],
"claim_boundary": receipt["claim_boundary"],
"lawful": receipt["lawful"],
}).encode("utf-8")
receipt["stable_dna_alignment_hash_sha256"] = sha256_bytes(stable_preimage)
receipt["receipt_hash_preimage_sha256"] = sha256_bytes(stable_json(receipt).encode("utf-8"))
return receipt
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--out", type=Path, default=OUT)
args = parser.parse_args()
receipt = build_receipt()
args.out.parent.mkdir(parents=True, exist_ok=True)
args.out.write_text(json.dumps(receipt, indent=2, sort_keys=True), encoding="utf-8")
print(json.dumps({
"lawful": receipt["lawful"],
"aligned": receipt["alignment"]["aligned"],
"stable_dna_alignment_hash_sha256": receipt["stable_dna_alignment_hash_sha256"],
"receipt_hash_preimage_sha256": receipt["receipt_hash_preimage_sha256"],
"dna_keel": receipt["dna_keel"],
"dna_residual_boat": receipt["dna_residual_boat"],
}, indent=2, sort_keys=True))
return 0
if __name__ == "__main__":
raise SystemExit(main())