Research-Stack/4-Infrastructure/shim/build_corpus278.py
Brandon Schneider 81a61b6944 feat(rrc): 278-equation corpus — AVM sole output boundary, RRC classifier feeds it
Architecture:
  RRC.Corpus278  — raw features only (Python supplies, Lean owns gate)
  RRC.Emit       — alignment classifier; emitCorpus generic entry point
  AVMIsa.Emit    — sole output boundary; imports Corpus278, stamps bundle

Changes:
- RRC/Emit.lean: extend FixtureRow + RrcRow with 5 generator fields
    (operatorTokens, invariantsDeclared, boundaryConds, templateKey, templateParams)
  Add emitCorpus (schema, corpus) generic emitter; emitFixture is now a thin wrapper
  jRrcRow JSON serializer emits all generator fields
- RRC/Corpus278.lean: auto-generated 278-row FixtureRow list
  Source: archive/experimental-shim-probes/rrc_equation_classifier_receipt.json
  Python extracts raw features; all gating in Lean (alignment gate fires missingPrediction
  for all 278 rows currently — correct, no PIST labels present yet)
- AVMIsa/Emit.lean: import Corpus278; add §7 emitRrcCorpus278 — AVM canaries must
  pass for bundle receipt to be valid; stamped by AVM authority (avm.rrc_corpus278.bundle)
  §8 eval: corpus summary fires (278, 0, 278) — all held, 0 promoted, gate honest
- lakefile.toml: add Semantics.RRC.Corpus278 to Compiler blessed roots; update comment
- 4-Infrastructure/shim/build_corpus278.py: corpus builder script

Build: 3567 jobs, 0 errors (lake build)

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-05-26 22:23:56 -05:00

161 lines
7.2 KiB
Python

#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = []
# ///
"""
Build Semantics/RRC/Corpus278.lean from rrc_equation_classifier_receipt.json.
Python's role: read raw fields, map to stable IDs, derive generator tokens.
Lean's role: alignment gate, receipt stamping, all output decisions.
"""
from __future__ import annotations
import hashlib, json, re, sys
from pathlib import Path
ROOT = Path("/home/allaun/Research Stack")
RECEIPT_JSON = ROOT / "archive/experimental-shim-probes/rrc_equation_classifier_receipt.json"
OUT_LEAN = ROOT / "0-Core-Formalism/lean/Semantics/Semantics/RRC/Corpus278.lean"
# ── shape mapping (classifier JSON → Lean RRCShape constructor) ──────────────
SHAPE_MAP = {
"CognitiveLoadField": ".cognitiveLoadField",
"SignalShapedRouteCompiler": ".signalShapedRouteCompiler",
"ProjectableGeometryTopology": ".projectableGeometryTopology",
"CadForceProbeReceipt": ".cadForceProbeReceipt",
"LogogramProjection": ".logogramProjection",
"HoldForUnlawfulOrUnderspecifiedShape": ".holdForUnlawfulOrUnderspecifiedShape",
}
# ── template_key logic (rrc_kind → template) ─────────────────────────────────
def template_key(rrc_kind: str, status: str) -> str:
if status == "HOLD":
return "hold"
kind_map = {
"cognitive_field_receipt": "definition",
"compression_route_prior": "master_equation",
"geometry_topology_receipt":"definition",
"cad_force_receipt": "gate",
"logogram_projection": "receipt",
"negative_control": "hold",
}
return kind_map.get(rrc_kind, "definition")
# ── operator token derivation (route_hint + rrc_kind + equation text) ─────────
def operator_tokens(er: dict) -> list[str]:
tokens = []
rh = (er.get("route_hint_non_authoritative") or "").strip()
rk = (er.get("rrc_kind") or "").strip()
if rh and rh != "unclassified_equation":
tokens.append(rh)
if rk:
tokens.append(rk)
# Extract simple operator keywords from equation text
eq_text = (er.get("equation") or "").lower()
for op in ["exp(", "log(", "max(", "min(", "sum(", "integral", "derivative",
"laplacian", "nabla", "div(", "curl(", "sigmoid", "softmax",
"tanh(", "relu(", "norm(", "dot(", "cross("]:
if op in eq_text:
tokens.append(op.rstrip("("))
return list(dict.fromkeys(tokens)) # deduplicate, preserve order
# ── string escaping for Lean ──────────────────────────────────────────────────
def lean_str(s: str) -> str:
s = s.replace("\\", "\\\\").replace('"', '\\"')
return f'"{s}"'
def lean_opt(s: str | None) -> str:
if s is None:
return "none"
return f"some {lean_str(s)}"
def lean_str_list(xs: list[str]) -> str:
return "[" + ", ".join(lean_str(x) for x in xs) + "]"
# ── stable equation ID ────────────────────────────────────────────────────────
def stable_id(raw_id: str) -> str:
h = hashlib.sha256(raw_id.encode()).hexdigest()[:16]
return f"rrc_eq_{h}"
# ── main ──────────────────────────────────────────────────────────────────────
def main() -> None:
d = json.loads(RECEIPT_JSON.read_text())
eqs = d["compiled_equations"]
print(f"Loaded {len(eqs)} equations", file=sys.stderr)
lines: list[str] = []
lines.append("-- Semantics.RRC.Corpus278 — AUTO-GENERATED by build_corpus278.py")
lines.append("-- DO NOT EDIT BY HAND. Regenerate with:")
lines.append("-- python3 4-Infrastructure/shim/build_corpus278.py")
lines.append("--")
lines.append("-- Python role: raw feature extraction only.")
lines.append("-- Lean role: alignment gate, receipt stamping, all output decisions.")
lines.append("--")
lines.append("-- Source: archive/experimental-shim-probes/rrc_equation_classifier_receipt.json")
lines.append(f"-- Equation count: {len(eqs)}")
lines.append("")
lines.append("import Semantics.RRC.Emit")
lines.append("")
lines.append("namespace Semantics.RRC.Corpus278")
lines.append("")
lines.append("open Semantics.RRC.Emit")
lines.append("open Semantics.RRCLogogramProjection")
lines.append("open Semantics.ReceiptCore")
lines.append("")
lines.append("/-- Full 278-equation corpus from rrc_equation_classifier_receipt.json.")
lines.append(" Each row carries raw features only; the alignment gate in")
lines.append(" Semantics.RRC.Emit.emitCorpus makes all admissibility decisions. -/")
lines.append("def corpus278 : List FixtureRow := [")
row_strs: list[str] = []
for eq in eqs:
er = eq["equation_record"]
ir = eq["invariant_receipt"]
tw = eq["type_witness"]
raw_id = er["equation_id"]
eq_id = stable_id(raw_id)
name = er["name"]
shape_str = ir["shape"]
lean_shape = SHAPE_MAP.get(shape_str, ".holdForUnlawfulOrUnderspecifiedShape")
status_str = ir["status"]
lean_status = ".candidate" if status_str == "CANDIDATE" else ".hold"
rrc_kind = er.get("rrc_kind", "")
weak_cnt = len(tw.get("missing_or_weak_axes") or [])
# Generator fields
op_tokens = operator_tokens(er)
inv_declared = (er.get("domain_type") or "unknown").strip() or "unknown"
bound_conds = (er.get("bind_class") or "unknown").strip() or "unknown"
t_key = template_key(rrc_kind, status_str)
t_params = f"route={er.get('route_hint_non_authoritative','unclassified_equation') or 'unclassified_equation'};shape={shape_str}"
row = (
f" {{ equationId := {lean_str(eq_id)}\n"
f" name := {lean_str(name)}\n"
f" shape := {lean_shape}\n"
f" status := {lean_status}\n"
f" rrcKind := {lean_str(rrc_kind)}\n"
f" weakAxesCnt := {weak_cnt}\n"
f" pistProxyLabel := none\n"
f" pistExactLabel := none\n"
f" operatorTokens := {lean_str_list(op_tokens)}\n"
f" invariantsDeclared := {lean_str(inv_declared)}\n"
f" boundaryConds := {lean_str(bound_conds)}\n"
f" templateKey := {lean_str(t_key)}\n"
f" templateParams := {lean_str(t_params)} }}"
)
row_strs.append(row)
lines.append(",\n".join(row_strs))
lines.append("]")
lines.append("")
lines.append("end Semantics.RRC.Corpus278")
lines.append("")
OUT_LEAN.write_text("\n".join(lines))
print(f"Wrote {OUT_LEAN}", file=sys.stderr)
print(f"Total rows: {len(row_strs)}", file=sys.stderr)
if __name__ == "__main__":
main()