refactor(infra): extract shared utilities from duplicated code patterns

Create 4-Infrastructure/lib/ with canonical implementations of:
- hashing.py: sha256_bytes, sha256_text, sha256_file
- q16.py: Q16_16 fixed-point constants and arithmetic
- jsonl.py: load_json, load_jsonl, write_jsonl, stable_json, canonical_json_bytes
- fraction_utils.py: Fraction serialization helpers for hardware probes

Refactor 66 files across 4-Infrastructure/{hardware,shim,infra} and
5-Applications/{scripts,tools-scripts,hutter_prize,text-to-cad} to import
from the shared library instead of maintaining local copies.

4-Infrastructure/auto/lib/q16.py now re-exports from lib.q16.

Net: -743 lines (490 added, 1233 removed)

Build: not applicable (Python-only change, py_compile verified on all 71 files)
Co-Authored-By: Allaun Silverfox <bigdataiscoming+9i37y6j2@protonmail.com>
This commit is contained in:
Devin AI 2026-06-15 00:34:03 +00:00
parent 5371c70229
commit e5f04ee6c3
71 changed files with 491 additions and 1234 deletions

View file

@ -1,29 +1,17 @@
# PTOS: LAYER=INFRA / DOMAIN=AUTOMATION / CONDITION=ALPHA # PTOS: LAYER=INFRA / DOMAIN=AUTOMATION / CONDITION=ALPHA
""" """
Q16_16 fixed-point arithmetic for deterministic compute across all substrates. Q16_16 fixed-point arithmetic for deterministic compute across all substrates.
Re-exports from the canonical shared library at 4-Infrastructure/lib/q16.py.
All thresholds and metric values are stored as Q16_16 integers. All thresholds and metric values are stored as Q16_16 integers.
One = 0x00010000 = 65536. Float is forbidden in compute paths. One = 0x00010000 = 65536. Float is forbidden in compute paths.
""" """
from __future__ import annotations from __future__ import annotations
Q16_ONE: int = 0x00010000 import sys
Q16_HALF: int = 0x00008000 from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from lib.q16 import Q16_HALF, Q16_ONE, from_q16, ratio_q16, to_q16
def to_q16(value: float) -> int: __all__ = ["Q16_ONE", "Q16_HALF", "to_q16", "from_q16", "ratio_q16"]
"""Convert a float to Q16_16. Only allowed at the external boundary."""
return int(round(value * Q16_ONE))
def from_q16(value: int) -> float:
"""Convert Q16_16 back to float. Only for display, never in compute."""
return value / Q16_ONE
def ratio_q16(numerator: float, denominator: float) -> int:
"""Compute Q16_16 ratio of two floats, clamped to [0, 1]."""
if denominator == 0:
return 0
r = numerator / denominator
r = max(0.0, min(1.0, r))
return int(r * Q16_ONE)

View file

@ -11,7 +11,6 @@ an explicit residual mask.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import math import math
import random import random
@ -20,24 +19,19 @@ import zlib
from datetime import datetime, timezone from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2] REPO = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(REPO / "4-Infrastructure"))
from lib.hashing import sha256_bytes
from lib.jsonl import stable_json
QUANDELA_RECEIPT = REPO / "4-Infrastructure" / "shim" / "quandela_stochastic_crc_local_sim_receipt.json" QUANDELA_RECEIPT = REPO / "4-Infrastructure" / "shim" / "quandela_stochastic_crc_local_sim_receipt.json"
OUT = REPO / "4-Infrastructure" / "hardware" / "jupiter_phi_self_recovery_probe_receipt.json" OUT = REPO / "4-Infrastructure" / "hardware" / "jupiter_phi_self_recovery_probe_receipt.json"
PHI = (1.0 + math.sqrt(5.0)) / 2.0 PHI = (1.0 + math.sqrt(5.0)) / 2.0
GOLDEN_ANGLE = 2.0 * math.pi * (1.0 - 1.0 / PHI) GOLDEN_ANGLE = 2.0 * math.pi * (1.0 - 1.0 / PHI)
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def crc32_hex(data: bytes) -> str: def crc32_hex(data: bytes) -> str:
return f"{zlib.crc32(data) & 0xFFFFFFFF:08x}" return f"{zlib.crc32(data) & 0xFFFFFFFF:08x}"

View file

@ -20,15 +20,20 @@ rational-coordinate level.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
from datetime import datetime, timezone from datetime import datetime, timezone
from fractions import Fraction from fractions import Fraction
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2] 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_json
from lib.jsonl import load_json, stable_json
AVERAGE_RECEIPT = ( AVERAGE_RECEIPT = (
REPO REPO
/ "4-Infrastructure" / "4-Infrastructure"
@ -105,37 +110,6 @@ PROJECTION: dict[str, dict[str, Fraction]] = {
"spectral": Fraction(1, 3), "spectral": Fraction(1, 3),
}, },
} }
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def parse_fraction_json(item: dict[str, Any]) -> Fraction:
return Fraction(int(item["numerator"]), int(item["denominator"]))
def load_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def centroid_from_average(receipt: dict[str, Any]) -> dict[str, Fraction]: def centroid_from_average(receipt: dict[str, Any]) -> dict[str, Fraction]:
centroid: dict[str, Fraction] = {} centroid: dict[str, Fraction] = {}
for item in receipt["rational_average"]["centroid_components"]: for item in receipt["rational_average"]["centroid_components"]:
@ -203,15 +177,6 @@ def lift_4_to_12(reduced: dict[str, Fraction]) -> dict[str, Fraction]:
def signed_l1(vector: dict[str, Fraction]) -> Fraction: def signed_l1(vector: dict[str, Fraction]) -> Fraction:
return sum((abs(value) for value in vector.values()), Fraction(0)) return sum((abs(value) for value in vector.values()), Fraction(0))
def vector_json(vector: dict[str, Fraction]) -> dict[str, dict[str, Any]]:
return {
key: fraction_json(value)
for key, value in sorted(vector.items())
}
def ranked_abs_vector(vector: dict[str, Fraction], limit: int = 8) -> list[dict[str, Any]]: def ranked_abs_vector(vector: dict[str, Fraction], limit: int = 8) -> list[dict[str, Any]]:
ranked = sorted(vector.items(), key=lambda item: abs(item[1]), reverse=True) ranked = sorted(vector.items(), key=lambda item: abs(item[1]), reverse=True)
return [ return [

View file

@ -17,7 +17,6 @@ substitutions are detected as invalid.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import itertools import itertools
import json import json
import math import math
@ -25,9 +24,15 @@ from datetime import datetime, timezone
from fractions import Fraction from fractions import Fraction
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2] 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 = ( FORCE_RECEIPT = (
REPO REPO
/ "4-Infrastructure" / "4-Infrastructure"
@ -51,45 +56,6 @@ HANDLE_TO_PRIMITIVE = {
"shear_torsion": "shear", "shear_torsion": "shear",
"spectral_field": "spectral", "spectral_field": "spectral",
} }
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def parse_fraction_json(item: dict[str, Any]) -> Fraction:
return Fraction(int(item["numerator"]), int(item["denominator"]))
def load_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def vector_from_json(items: dict[str, dict[str, Any]]) -> dict[str, Fraction]:
return {key: parse_fraction_json(value) for key, value in items.items()}
def vector_json(vector: dict[str, Fraction]) -> dict[str, dict[str, Any]]:
return {key: fraction_json(value) for key, value in sorted(vector.items())}
def signed_l1(vector: dict[str, Fraction]) -> Fraction: def signed_l1(vector: dict[str, Fraction]) -> Fraction:
return sum((abs(value) for value in vector.values()), Fraction(0)) return sum((abs(value) for value in vector.values()), Fraction(0))

View file

@ -12,15 +12,20 @@ It deliberately does not rewrite the canonical Standard Model projection.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
from datetime import datetime, timezone from datetime import datetime, timezone
from fractions import Fraction from fractions import Fraction
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2] 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, projection_from_json, vector_from_json, vector_json
from lib.jsonl import load_json, stable_json
REDUCTION_RECEIPT = ( REDUCTION_RECEIPT = (
REPO REPO
/ "4-Infrastructure" / "4-Infrastructure"
@ -47,55 +52,6 @@ OUT = (
) )
PRIMITIVES = ("field", "shear", "packet", "spectral") PRIMITIVES = ("field", "shear", "packet", "spectral")
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def parse_fraction_json(item: dict[str, Any]) -> Fraction:
return Fraction(int(item["numerator"]), int(item["denominator"]))
def load_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def vector_from_json(items: dict[str, dict[str, Any]]) -> dict[str, Fraction]:
return {key: parse_fraction_json(value) for key, value in items.items()}
def vector_json(vector: dict[str, Fraction]) -> dict[str, dict[str, Any]]:
return {key: fraction_json(value) for key, value in sorted(vector.items())}
def projection_from_json(items: dict[str, dict[str, dict[str, Any]]]) -> dict[str, dict[str, Fraction]]:
return {
axis: {
primitive: parse_fraction_json(weight)
for primitive, weight in row.items()
}
for axis, row in items.items()
}
def projection_json(projection: dict[str, dict[str, Fraction]]) -> dict[str, dict[str, dict[str, Any]]]: def projection_json(projection: dict[str, dict[str, Fraction]]) -> dict[str, dict[str, dict[str, Any]]]:
return { return {
axis: { axis: {

View file

@ -17,16 +17,21 @@ This is a symbolic compression substitution, not a biological or physics claim.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import math import math
from datetime import datetime, timezone from datetime import datetime, timezone
from fractions import Fraction from fractions import Fraction
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2] 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 = ( FORCE_RECEIPT = (
REPO REPO
/ "4-Infrastructure" / "4-Infrastructure"
@ -58,45 +63,6 @@ HANDLE_TO_BASE = {
"shear_torsion": "T", "shear_torsion": "T",
"spectral_field": "C", "spectral_field": "C",
} }
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def parse_fraction_json(item: dict[str, Any]) -> Fraction:
return Fraction(int(item["numerator"]), int(item["denominator"]))
def load_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def vector_from_json(items: dict[str, dict[str, Any]]) -> dict[str, Fraction]:
return {key: parse_fraction_json(value) for key, value in items.items()}
def vector_json(vector: dict[str, Fraction]) -> dict[str, dict[str, Any]]:
return {key: fraction_json(value) for key, value in sorted(vector.items())}
def signed_l1(vector: dict[str, Fraction]) -> Fraction: def signed_l1(vector: dict[str, Fraction]) -> Fraction:
return sum((abs(value) for value in vector.values()), Fraction(0)) return sum((abs(value) for value in vector.values()), Fraction(0))

View file

@ -10,15 +10,20 @@ law breaks.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
from datetime import datetime, timezone from datetime import datetime, timezone
from fractions import Fraction from fractions import Fraction
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2] 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
DNA_RECEIPT = ( DNA_RECEIPT = (
REPO REPO
/ "4-Infrastructure" / "4-Infrastructure"
@ -43,45 +48,6 @@ EXTRA_BASES = ("B", "S", "P", "Z")
PRIMITIVES = ("field", "shear", "packet", "spectral") PRIMITIVES = ("field", "shear", "packet", "spectral")
CANONICAL = {"A": "field", "T": "shear", "G": "packet", "C": "spectral"} CANONICAL = {"A": "field", "T": "shear", "G": "packet", "C": "spectral"}
HANDLE_TO_BASE = {"packet_local": "G", "shear_torsion": "T", "spectral_field": "C"} HANDLE_TO_BASE = {"packet_local": "G", "shear_torsion": "T", "spectral_field": "C"}
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def parse_fraction_json(item: dict[str, Any]) -> Fraction:
return Fraction(int(item["numerator"]), int(item["denominator"]))
def load_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def vector_from_json(items: dict[str, dict[str, Any]]) -> dict[str, Fraction]:
return {key: parse_fraction_json(value) for key, value in items.items()}
def vector_json(vector: dict[str, Fraction]) -> dict[str, dict[str, Any]]:
return {key: fraction_json(value) for key, value in sorted(vector.items())}
def signed_l1(vector: dict[str, Fraction]) -> Fraction: def signed_l1(vector: dict[str, Fraction]) -> Fraction:
return sum((abs(value) for value in vector.values()), Fraction(0)) return sum((abs(value) for value in vector.values()), Fraction(0))

View file

@ -15,15 +15,20 @@ through the current compression regime.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
from datetime import datetime, timezone from datetime import datetime, timezone
from fractions import Fraction from fractions import Fraction
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2] 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, projection_from_json, vector_from_json, vector_json
from lib.jsonl import load_json, stable_json
REDUCTION_RECEIPT = ( REDUCTION_RECEIPT = (
REPO REPO
/ "4-Infrastructure" / "4-Infrastructure"
@ -88,55 +93,6 @@ FORCE_SECTORS = {
"claim_boundary": "no gravity force is inferred from the source equation wall", "claim_boundary": "no gravity force is inferred from the source equation wall",
}, },
} }
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def parse_fraction_json(item: dict[str, Any]) -> Fraction:
return Fraction(int(item["numerator"]), int(item["denominator"]))
def load_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def vector_from_json(items: dict[str, dict[str, Any]]) -> dict[str, Fraction]:
return {key: parse_fraction_json(value) for key, value in items.items()}
def projection_from_json(items: dict[str, dict[str, dict[str, Any]]]) -> dict[str, dict[str, Fraction]]:
return {
axis: {
primitive: parse_fraction_json(weight)
for primitive, weight in row.items()
}
for axis, row in items.items()
}
def vector_json(vector: dict[str, Fraction]) -> dict[str, dict[str, Any]]:
return {key: fraction_json(value) for key, value in sorted(vector.items())}
def signed_l1(vector: dict[str, Fraction]) -> Fraction: def signed_l1(vector: dict[str, Fraction]) -> Fraction:
return sum((abs(value) for value in vector.values()), Fraction(0)) return sum((abs(value) for value in vector.values()), Fraction(0))

View file

@ -11,16 +11,21 @@ This is a compression/topology control object, not a physical claim.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import math import math
from datetime import datetime, timezone from datetime import datetime, timezone
from fractions import Fraction from fractions import Fraction
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2] 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
REDUCTION_RECEIPT = ( REDUCTION_RECEIPT = (
REPO REPO
/ "4-Infrastructure" / "4-Infrastructure"
@ -56,45 +61,6 @@ HANDLE_MAP = {
"scalar_potential", "scalar_potential",
}, },
} }
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def parse_fraction_json(item: dict[str, Any]) -> Fraction:
return Fraction(int(item["numerator"]), int(item["denominator"]))
def load_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def vector_from_json(items: dict[str, dict[str, Any]]) -> dict[str, Fraction]:
return {key: parse_fraction_json(value) for key, value in items.items()}
def vector_json(vector: dict[str, Fraction]) -> dict[str, dict[str, Any]]:
return {key: fraction_json(value) for key, value in sorted(vector.items())}
def signed_l1(vector: dict[str, Fraction]) -> Fraction: def signed_l1(vector: dict[str, Fraction]) -> Fraction:
return sum((abs(value) for value in vector.values()), Fraction(0)) return sum((abs(value) for value in vector.values()), Fraction(0))

View file

@ -11,15 +11,19 @@ principal eigenvector of that coupling/interaction matrix.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import math import math
from datetime import datetime, timezone from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2] REPO = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(REPO / "4-Infrastructure"))
from lib.hashing import sha256_bytes
from lib.jsonl import stable_json
OUT = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_eigen_probe_receipt.json" OUT = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_eigen_probe_receipt.json"
PHI = (1.0 + math.sqrt(5.0)) / 2.0 PHI = (1.0 + math.sqrt(5.0)) / 2.0
@ -70,16 +74,6 @@ OBSERVATIONS: list[tuple[str, str, float, str]] = [
("scalar_potential", "electroweak_charged_w", 3.0, "scalar-gauge mass-generated W couplings"), ("scalar_potential", "electroweak_charged_w", 3.0, "scalar-gauge mass-generated W couplings"),
("scalar_potential", "electroweak_neutral_za", 3.0, "scalar-gauge mass-generated Z/A couplings"), ("scalar_potential", "electroweak_neutral_za", 3.0, "scalar-gauge mass-generated Z/A couplings"),
] ]
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def build_matrix(phi_mode: str) -> list[list[float]]: def build_matrix(phi_mode: str) -> list[list[float]]:
index = {name: pos for pos, name in enumerate(NODES)} index = {name: pos for pos, name in enumerate(NODES)}
size = len(NODES) size = len(NODES)

View file

@ -9,7 +9,6 @@ and a targeted phi average in Q(phi), where phi^2 = phi + 1.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import math import math
from dataclasses import dataclass from dataclasses import dataclass
@ -19,9 +18,15 @@ from pathlib import Path
from typing import Any from typing import Any
from standard_model_lagrangian_eigen_probe import NODES, OBSERVATIONS from standard_model_lagrangian_eigen_probe import NODES, OBSERVATIONS
import sys
REPO = Path(__file__).resolve().parents[2] 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
from lib.jsonl import stable_json
OUT = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_exact_average_receipt.json" OUT = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_exact_average_receipt.json"
PHI_FLOAT = (1.0 + math.sqrt(5.0)) / 2.0 PHI_FLOAT = (1.0 + math.sqrt(5.0)) / 2.0
@ -86,29 +91,6 @@ class QPhi:
"form": f"({fraction_str(self.a)}) + ({fraction_str(self.b)})*phi", "form": f"({fraction_str(self.a)}) + ({fraction_str(self.b)})*phi",
"approx": self.approx(), "approx": self.approx(),
} }
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def is_scalar_touched(left: str, right: str) -> bool: def is_scalar_touched(left: str, right: str) -> bool:
fields = (left, right) fields = (left, right)
return any("higgs" in field or "scalar" in field for field in fields) return any("higgs" in field or "scalar" in field for field in fields)

View file

@ -15,7 +15,6 @@ The arithmetic is exact rational / Q(phi), matching the exact-average probe.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
from datetime import datetime, timezone from datetime import datetime, timezone
from fractions import Fraction from fractions import Fraction
@ -24,20 +23,15 @@ from typing import Any
from standard_model_lagrangian_eigen_probe import NODES, OBSERVATIONS from standard_model_lagrangian_eigen_probe import NODES, OBSERVATIONS
from standard_model_lagrangian_exact_average import QPhi, exact_rational_average, fraction_str, phi_targeted_average from standard_model_lagrangian_exact_average import QPhi, exact_rational_average, fraction_str, phi_targeted_average
import sys
REPO = Path(__file__).resolve().parents[2] REPO = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(REPO / "4-Infrastructure"))
from lib.hashing import sha256_bytes
from lib.jsonl import stable_json
OUT = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_underverse_closure_receipt.json" OUT = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_underverse_closure_receipt.json"
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def edge_key(left: str, right: str) -> tuple[str, str]: def edge_key(left: str, right: str) -> tuple[str, str]:
return tuple(sorted((left, right))) return tuple(sorted((left, right)))

View file

@ -12,16 +12,21 @@ later route pays the header/receipt cost and preserves exact rehydration.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import itertools import itertools
import json import json
from datetime import datetime, timezone from datetime import datetime, timezone
from fractions import Fraction from fractions import Fraction
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2] 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, projection_from_json, vector_from_json, vector_json
from lib.jsonl import load_json, stable_json
REDUCTION_RECEIPT = ( REDUCTION_RECEIPT = (
REPO REPO
/ "4-Infrastructure" / "4-Infrastructure"
@ -42,55 +47,6 @@ OUT = (
) )
PRIMITIVES = ("field", "shear", "packet", "spectral") PRIMITIVES = ("field", "shear", "packet", "spectral")
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def parse_fraction_json(item: dict[str, Any]) -> Fraction:
return Fraction(int(item["numerator"]), int(item["denominator"]))
def load_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def vector_from_json(items: dict[str, dict[str, Any]]) -> dict[str, Fraction]:
return {key: parse_fraction_json(value) for key, value in items.items()}
def vector_json(vector: dict[str, Fraction]) -> dict[str, dict[str, Any]]:
return {key: fraction_json(value) for key, value in sorted(vector.items())}
def projection_from_json(items: dict[str, dict[str, dict[str, Any]]]) -> dict[str, dict[str, Fraction]]:
return {
axis: {
primitive: parse_fraction_json(weight)
for primitive, weight in row.items()
}
for axis, row in items.items()
}
def projection_json(projection: dict[str, dict[str, Fraction]]) -> dict[str, dict[str, dict[str, Any]]]: def projection_json(projection: dict[str, dict[str, Fraction]]) -> dict[str, dict[str, dict[str, Any]]]:
return { return {
axis: { axis: {

View file

@ -10,15 +10,20 @@ noise, sidecar debt, signal, or a failure boundary.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
from datetime import datetime, timezone from datetime import datetime, timezone
from fractions import Fraction from fractions import Fraction
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2] 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, projection_from_json, vector_from_json, vector_json
from lib.jsonl import load_json, stable_json
REDUCTION_RECEIPT = ( REDUCTION_RECEIPT = (
REPO REPO
/ "4-Infrastructure" / "4-Infrastructure"
@ -46,55 +51,6 @@ OUT = (
PRIMITIVES = ("field", "shear", "packet", "spectral") PRIMITIVES = ("field", "shear", "packet", "spectral")
HANDLES = ("packet_local", "shear_torsion", "spectral_field") HANDLES = ("packet_local", "shear_torsion", "spectral_field")
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def parse_fraction_json(item: dict[str, Any]) -> Fraction:
return Fraction(int(item["numerator"]), int(item["denominator"]))
def load_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def vector_from_json(items: dict[str, dict[str, Any]]) -> dict[str, Fraction]:
return {key: parse_fraction_json(value) for key, value in items.items()}
def projection_from_json(items: dict[str, dict[str, dict[str, Any]]]) -> dict[str, dict[str, Fraction]]:
return {
axis: {
primitive: parse_fraction_json(weight)
for primitive, weight in row.items()
}
for axis, row in items.items()
}
def vector_json(vector: dict[str, Fraction]) -> dict[str, dict[str, Any]]:
return {key: fraction_json(value) for key, value in sorted(vector.items())}
def signed_l1(vector: dict[str, Fraction]) -> Fraction: def signed_l1(vector: dict[str, Fraction]) -> Fraction:
return sum((abs(value) for value in vector.values()), Fraction(0)) return sum((abs(value) for value in vector.values()), Fraction(0))

View file

@ -4,7 +4,6 @@
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
from datetime import datetime, timezone from datetime import datetime, timezone
from fractions import Fraction from fractions import Fraction
@ -13,44 +12,25 @@ from typing import Any
from xml.sax.saxutils import escape from xml.sax.saxutils import escape
from standard_model_lagrangian_eigen_probe import NODES from standard_model_lagrangian_eigen_probe import NODES
import sys
REPO = Path(__file__).resolve().parents[2] 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
from lib.jsonl import stable_json
SHAPE = REPO / "4-Infrastructure" / "hardware" / "standard_model_underverse_manifold_shape_receipt.json" SHAPE = REPO / "4-Infrastructure" / "hardware" / "standard_model_underverse_manifold_shape_receipt.json"
AVERAGE = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_exact_average_receipt.json" AVERAGE = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_exact_average_receipt.json"
OUT_JSON = REPO / "4-Infrastructure" / "hardware" / "standard_model_signed_axis_graph_receipt.json" OUT_JSON = REPO / "4-Infrastructure" / "hardware" / "standard_model_signed_axis_graph_receipt.json"
OUT_GRAPHML = REPO / "4-Infrastructure" / "hardware" / "standard_model_signed_axis_graph.graphml" OUT_GRAPHML = REPO / "4-Infrastructure" / "hardware" / "standard_model_signed_axis_graph.graphml"
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def file_hash(path: Path) -> str: def file_hash(path: Path) -> str:
return sha256_bytes(path.read_bytes()) return sha256_bytes(path.read_bytes())
def frac_from_json(obj: dict[str, Any]) -> Fraction: def frac_from_json(obj: dict[str, Any]) -> Fraction:
return Fraction(int(obj["numerator"]), int(obj["denominator"])) return Fraction(int(obj["numerator"]), int(obj["denominator"]))
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def load_inputs() -> tuple[dict[str, Any], dict[str, Any]]: def load_inputs() -> tuple[dict[str, Any], dict[str, Any]]:
return ( return (
json.loads(SHAPE.read_text(encoding="utf-8")), json.loads(SHAPE.read_text(encoding="utf-8")),

View file

@ -15,7 +15,6 @@ It is a symbolic/compression manifold, not a physical spacetime manifold.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import math import math
from datetime import datetime, timezone from datetime import datetime, timezone
@ -25,41 +24,26 @@ from typing import Any
from standard_model_lagrangian_eigen_probe import NODES from standard_model_lagrangian_eigen_probe import NODES
from standard_model_lagrangian_exact_average import QPhi, fraction_str from standard_model_lagrangian_exact_average import QPhi, fraction_str
import sys
REPO = Path(__file__).resolve().parents[2] 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
from lib.jsonl import stable_json
AVERAGE = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_exact_average_receipt.json" AVERAGE = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_exact_average_receipt.json"
CLOSURE = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_underverse_closure_receipt.json" CLOSURE = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_underverse_closure_receipt.json"
EIGEN = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_eigen_probe_receipt.json" EIGEN = REPO / "4-Infrastructure" / "hardware" / "standard_model_lagrangian_eigen_probe_receipt.json"
OUT = REPO / "4-Infrastructure" / "hardware" / "standard_model_underverse_manifold_shape_receipt.json" OUT = REPO / "4-Infrastructure" / "hardware" / "standard_model_underverse_manifold_shape_receipt.json"
PHI = (1.0 + math.sqrt(5.0)) / 2.0 PHI = (1.0 + math.sqrt(5.0)) / 2.0
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def file_hash(path: Path) -> str: def file_hash(path: Path) -> str:
return sha256_bytes(path.read_bytes()) return sha256_bytes(path.read_bytes())
def frac_from_json(obj: dict[str, Any]) -> Fraction: def frac_from_json(obj: dict[str, Any]) -> Fraction:
return Fraction(int(obj["numerator"]), int(obj["denominator"])) return Fraction(int(obj["numerator"]), int(obj["denominator"]))
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def qphi_from_json(obj: dict[str, Any]) -> QPhi: def qphi_from_json(obj: dict[str, Any]) -> QPhi:
return QPhi(Fraction(obj["a"]), Fraction(obj["b"])) return QPhi(Fraction(obj["a"]), Fraction(obj["b"]))

View file

@ -18,15 +18,20 @@ physical Standard Model calculation.
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
from datetime import datetime, timezone from datetime import datetime, timezone
from fractions import Fraction from fractions import Fraction
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
REPO = Path(__file__).resolve().parents[2] 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
from lib.jsonl import load_json, stable_json
ACCOUNTING_RECEIPT = ( ACCOUNTING_RECEIPT = (
REPO REPO
/ "4-Infrastructure" / "4-Infrastructure"
@ -57,37 +62,6 @@ W_LINKED_AXES = (
"ghost_gaugefix_sector", "ghost_gaugefix_sector",
"derivative_kinetic_flow", "derivative_kinetic_flow",
) )
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def parse_fraction_json(item: dict[str, Any]) -> Fraction:
return Fraction(int(item["numerator"]), int(item["denominator"]))
def load_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def row_support(row: dict[str, dict[str, Any]]) -> tuple[str, ...]: def row_support(row: dict[str, dict[str, Any]]) -> tuple[str, ...]:
return tuple(sorted(row)) return tuple(sorted(row))

View file

@ -12,16 +12,14 @@ import struct
import time import time
import sys import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import Q16_ONE, Q16_SCALE, to_q16
UART_BAUD = 115384 # Matches Lean uartBaudDivisor (233) UART_BAUD = 115384 # Matches Lean uartBaudDivisor (233)
UART_TIMEOUT = 2 UART_TIMEOUT = 2
# Q16_16 constants # Q16_16 constants
Q16_SCALE = 65536
Q16_ONE = 65536
def q16_from_float(f: float) -> int:
return max(-2147483648, min(2147483647, int(f * Q16_SCALE)))
def q16_to_float(q: int) -> float: def q16_to_float(q: int) -> float:
if q > 2147483647: if q > 2147483647:
q -= 4294967296 q -= 4294967296

View file

@ -9,7 +9,6 @@ available immediately.
from __future__ import annotations from __future__ import annotations
import hashlib
import json import json
import os import os
import sqlite3 import sqlite3
@ -22,6 +21,9 @@ import urllib.request
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.hashing import sha256_text
SERVER_NAME = "ene-contextstream" SERVER_NAME = "ene-contextstream"
SERVER_VERSION = "0.1.0" SERVER_VERSION = "0.1.0"
@ -34,12 +36,6 @@ DEFAULT_CANDIDATE_ROOT = (
def now_ms() -> int: def now_ms() -> int:
return int(time.time() * 1000) return int(time.time() * 1000)
def sha256_text(text: str) -> str:
return hashlib.sha256(text.encode("utf-8")).hexdigest()
def json_text(data: Any) -> list[dict[str, str]]: def json_text(data: Any) -> list[dict[str, str]]:
return [{"type": "text", "text": json.dumps(data, indent=2, sort_keys=True)}] return [{"type": "text", "text": json.dumps(data, indent=2, sort_keys=True)}]

View file

@ -19,6 +19,10 @@ from dataclasses import asdict, dataclass
from datetime import datetime, timezone from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[4] / "4-Infrastructure"))
from lib.jsonl import stable_json
PLUGIN_ID = "ene.tiddlywiki.bridge" PLUGIN_ID = "ene.tiddlywiki.bridge"
@ -74,12 +78,6 @@ def utc_now() -> str:
def sha256_bytes(data: bytes) -> str: def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest() return hashlib.sha256(data).hexdigest()
def canonical_json(data: Any) -> str:
return json.dumps(data, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
def slugify(title: str) -> str: def slugify(title: str) -> str:
slug = title.strip().lower() slug = title.strip().lower()
slug = re.sub(r"[^a-z0-9._ -]+", "", slug) slug = re.sub(r"[^a-z0-9._ -]+", "", slug)
@ -230,8 +228,8 @@ def build_plan(record: TiddlerRecord, indexed_utc: str | None = None) -> ENEPack
"plugin_id": PLUGIN_ID, "plugin_id": PLUGIN_ID,
"plugin_version": PLUGIN_VERSION, "plugin_version": PLUGIN_VERSION,
} }
receipt = sha256_bytes(canonical_json(receipt_payload).encode("utf-8")) receipt = sha256_bytes(stable_json(receipt_payload).encode("utf-8"))
meta_hash = sha256_bytes(canonical_json(meta_capsule).encode("utf-8")) meta_hash = sha256_bytes(stable_json(meta_capsule).encode("utf-8"))
body_preview = " ".join(record.text.split())[:240] or record.title body_preview = " ".join(record.text.split())[:240] or record.title
tags = sorted(set(["ene", "tiddlywiki", "wiki", *record.tags]), key=str.lower) tags = sorted(set(["ene", "tiddlywiki", "wiki", *record.tags]), key=str.lower)
return ENEPackagePlan( return ENEPackagePlan(
@ -354,7 +352,7 @@ def upsert_plan(conn: sqlite3.Connection, plan: ENEPackagePlan) -> None:
columns = table_columns(conn, "packages") columns = table_columns(conn, "packages")
raw = asdict(plan) raw = asdict(plan)
encoded = { encoded = {
key: canonical_json(value) if isinstance(value, (dict, list)) else value key: stable_json(value) if isinstance(value, (dict, list)) else value
for key, value in raw.items() for key, value in raw.items()
if key in columns if key in columns
} }
@ -403,7 +401,7 @@ def ingest(plans: list[ENEPackagePlan], db_path: Path) -> int:
"count": len(plans), "count": len(plans),
"packages": [plan.pkg for plan in plans], "packages": [plan.pkg for plan in plans],
} }
event_hash = sha256_bytes(canonical_json(event_payload).encode("utf-8")) event_hash = sha256_bytes(stable_json(event_payload).encode("utf-8"))
conn.execute( conn.execute(
""" """
INSERT OR REPLACE INTO ene_plugin_events INSERT OR REPLACE INTO ene_plugin_events
@ -414,7 +412,7 @@ def ingest(plans: list[ENEPackagePlan], db_path: Path) -> int:
f"{PLUGIN_ID}:{event_hash}", f"{PLUGIN_ID}:{event_hash}",
PLUGIN_ID, PLUGIN_ID,
"ingest", "ingest",
canonical_json(event_payload), stable_json(event_payload),
utc_now(), utc_now(),
), ),
) )

View file

@ -0,0 +1,11 @@
# 4-Infrastructure/lib — shared Python utilities for the Research Stack.
#
# Consolidates duplicated helpers (hashing, Q16_16 arithmetic, JSON/JSONL I/O,
# Fraction serialization) that were previously copy-pasted across dozens of
# scripts in 4-Infrastructure/ and 5-Applications/.
#
# Usage from any script in the repo:
# import sys
# from pathlib import Path
# sys.path.insert(0, str(Path(__file__).resolve().parents[<N>] / "4-Infrastructure"))
# from lib.hashing import sha256_file, sha256_text

View file

@ -0,0 +1,43 @@
"""Shared Fraction serialization helpers for standard-model hardware probes."""
from __future__ import annotations
import json
from fractions import Fraction
from pathlib import Path
from typing import Any
def fraction_str(value: Fraction) -> str:
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
def fraction_json(value: Fraction) -> dict[str, Any]:
return {
"fraction": fraction_str(value),
"numerator": value.numerator,
"denominator": value.denominator,
"decimal": float(value),
}
def parse_fraction_json(item: dict[str, Any]) -> Fraction:
return Fraction(int(item["numerator"]), int(item["denominator"]))
def vector_from_json(items: dict[str, dict[str, Any]]) -> dict[str, Fraction]:
return {key: parse_fraction_json(value) for key, value in items.items()}
def vector_json(vector: dict[str, Fraction]) -> dict[str, dict[str, Any]]:
return {key: fraction_json(value) for key, value in sorted(vector.items())}
def projection_from_json(
items: dict[str, dict[str, dict[str, Any]]],
) -> dict[str, dict[str, Fraction]]:
return {
axis: {
col: parse_fraction_json(cell) for col, cell in row.items()
}
for axis, row in items.items()
}

View file

@ -0,0 +1,29 @@
"""Shared SHA-256 helpers — single source of truth for the entire repo."""
from __future__ import annotations
import hashlib
from pathlib import Path
_BUF_SIZE = 1 << 20 # 1 MiB
def sha256_bytes(data: bytes) -> str:
"""Hex digest of raw bytes."""
return hashlib.sha256(data).hexdigest()
def sha256_text(text: str) -> str:
"""Hex digest of a UTF-8 string."""
return hashlib.sha256(text.encode("utf-8")).hexdigest()
def sha256_file(path: Path, buf_size: int = _BUF_SIZE) -> str:
"""Streaming hex digest of a file (handles arbitrarily large files)."""
h = hashlib.sha256()
with path.open("rb") as fh:
while True:
chunk = fh.read(buf_size)
if not chunk:
break
h.update(chunk)
return h.hexdigest()

View file

@ -0,0 +1,40 @@
"""Shared JSON / JSONL I/O and canonical serialization helpers."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
def load_json(path: Path) -> dict[str, Any]:
"""Read a JSON file and return its contents as a dict."""
return json.loads(path.read_text(encoding="utf-8"))
def load_jsonl(path: Path) -> list[dict[str, Any]]:
"""Read a JSONL file and return a list of dicts (skips blank lines)."""
rows: list[dict[str, Any]] = []
with path.open("r", encoding="utf-8") as fh:
for line in fh:
s = line.strip()
if s:
rows.append(json.loads(s))
return rows
def write_jsonl(path: Path, rows: list[dict[str, Any]], *, append: bool = False) -> None:
"""Write a list of dicts as JSONL."""
mode = "a" if append else "w"
with path.open(mode, encoding="utf-8") as fh:
for row in rows:
fh.write(json.dumps(row, ensure_ascii=False) + "\n")
def stable_json(obj: Any) -> str:
"""Deterministic JSON string (sorted keys, compact separators, ASCII-safe)."""
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def canonical_json_bytes(obj: Any) -> bytes:
"""Deterministic JSON as bytes — suitable for hashing."""
return stable_json(obj).encode("utf-8")

View file

@ -0,0 +1,59 @@
"""
Q16_16 fixed-point arithmetic for deterministic compute across all substrates.
All thresholds and metric values are stored as Q16_16 integers.
One = 0x00010000 = 65536. Float is forbidden in compute paths; the
converters here are boundary-only (JSON parsing, display).
"""
from __future__ import annotations
Q16_ONE: int = 0x00010000 # 65536
Q16_HALF: int = 0x00008000
Q16_SCALE: float = 65536.0
def to_q16(value: float) -> int:
"""Convert a float to Q16_16. Only allowed at the external boundary."""
return int(round(value * Q16_ONE))
def from_q16(value: int) -> float:
"""Convert Q16_16 back to float. Only for display, never in compute."""
return value / Q16_SCALE
def q16_add(a: int, b: int) -> int:
return a + b
def q16_sub(a: int, b: int) -> int:
return a - b
def q16_mul(a: int, b: int) -> int:
"""Multiply two Q16_16 values with normalization."""
return (a * b) // Q16_ONE
def q16_div(a: int, b: int) -> int:
"""Divide two Q16_16 values with normalization."""
if b == 0:
return 0
return (a * Q16_ONE) // b
def q16_gt(a: int, b: int) -> bool:
return a > b
def q16_ge(a: int, b: int) -> bool:
return a >= b
def ratio_q16(numerator: float, denominator: float) -> int:
"""Compute Q16_16 ratio of two floats, clamped to [0, 1]."""
if denominator == 0:
return 0
r = numerator / denominator
r = max(0.0, min(1.0, r))
return int(r * Q16_ONE)

View file

@ -12,12 +12,16 @@ import json
import os import os
import re import re
import subprocess import subprocess
import sys
import tempfile import tempfile
import time import time
from datetime import datetime, timezone from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
from typing import Optional, List, Dict from typing import Optional, List, Dict
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import Q16_SCALE, q16_mul, to_q16
try: try:
import requests import requests
HAS_REQUESTS = True HAS_REQUESTS = True
@ -40,8 +44,6 @@ OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://localhost:11434")
DEFAULT_MODEL = os.environ.get("ALPHAPROOF_MODEL", "deepseek-coder-v2:16b") DEFAULT_MODEL = os.environ.get("ALPHAPROOF_MODEL", "deepseek-coder-v2:16b")
LOG_DIR = Path(__file__).parent / "alphaproof_logs" LOG_DIR = Path(__file__).parent / "alphaproof_logs"
Q16_SCALE = 65536 # 2^16 for Q16.16 fixed-point
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Ollama LLM interface # Ollama LLM interface
@ -196,24 +198,6 @@ def verify_proof(lean_code: str, module_name: str = "Candidate",
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# FPGA Q16 acceleration (Python placeholder) # FPGA Q16 acceleration (Python placeholder)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def q16_multiply(a: int, b: int) -> int:
"""Q16.16 fixed-point multiplication.
(a * b) >> 16 with overflow clamping.
"""
result = (a * b) >> 16
# Clamp to Q16.16 range
INT32_MAX = 2147483647
INT32_MIN = -2147483648
return max(INT32_MIN, min(INT32_MAX, result))
def q16_from_float(f: float) -> int:
"""Convert float to Q16.16."""
return max(-2147483648, min(2147483647, round(f * Q16_SCALE)))
def q16_to_float(q: int) -> float: def q16_to_float(q: int) -> float:
"""Convert Q16.16 to float.""" """Convert Q16.16 to float."""
return q / Q16_SCALE return q / Q16_SCALE
@ -242,20 +226,20 @@ def fpga_accelerate(candidates: list[dict]) -> list[dict]:
code = c.get('code', '') code = c.get('code', '')
# Length penalty (shorter is better) # Length penalty (shorter is better)
length_score = q16_from_float(1.0 / (1.0 + len(code) / 1000.0)) length_score = to_q16(1.0 / (1.0 + len(code) / 1000.0))
# Penalty for incomplete proofs # Penalty for incomplete proofs
penalty = 0 penalty = 0
for bad_word in ['sorry', 'admit', 'axiom', 'by omega']: for bad_word in ['sorry', 'admit', 'axiom', 'by omega']:
count = code.count(bad_word) count = code.count(bad_word)
penalty += q16_from_float(count * 0.1) penalty += to_q16(count * 0.1)
# Base score from length # Base score from length
base = length_score base = length_score
# Bonus for having structure (def, theorem, proof) # Bonus for having structure (def, theorem, proof)
if 'theorem' in code or 'lemma' in code: if 'theorem' in code or 'lemma' in code:
base = q16_multiply(base, q16_from_float(1.2)) base = q16_mul(base, to_q16(1.2))
final_score = max(0, base - penalty) final_score = max(0, base - penalty)
c['q16_score'] = final_score c['q16_score'] = final_score

View file

@ -7,6 +7,10 @@ import sys
from collections import Counter, defaultdict from collections import Counter, defaultdict
from math import sqrt from math import sqrt
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.jsonl import load_jsonl
VECTORS_PATH = os.path.join(os.path.dirname(__file__), "../..", VECTORS_PATH = os.path.join(os.path.dirname(__file__), "../..",
"shared-data/pist_trace_tier2b_vectors.jsonl") "shared-data/pist_trace_tier2b_vectors.jsonl")
LABELS_PATH = os.path.join(os.path.dirname(__file__), "../..", LABELS_PATH = os.path.join(os.path.dirname(__file__), "../..",
@ -15,14 +19,6 @@ COMPARISON_PATH = os.path.join(os.path.dirname(__file__), "../..",
"shared-data/pist_tier1_vs_tier2_comparison.json") "shared-data/pist_tier1_vs_tier2_comparison.json")
CONFUSION_PATH = os.path.join(os.path.dirname(__file__), "../..", CONFUSION_PATH = os.path.join(os.path.dirname(__file__), "../..",
"shared-data/pist_tier2b_confusion_matrices.json") "shared-data/pist_tier2b_confusion_matrices.json")
def load_jsonl(path):
rows = []
with open(path) as f:
for line in f:
rows.append(json.loads(line))
return rows
def normalize(vectors): def normalize(vectors):
n = len(vectors) n = len(vectors)
if n == 0: return vectors, [], [] if n == 0: return vectors, [], []

View file

@ -29,6 +29,10 @@ import psycopg2
import psycopg2.extras import psycopg2.extras
from rds_connect import connect_rds from rds_connect import connect_rds
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.jsonl import load_jsonl
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger("dataset_ingest_rds") log = logging.getLogger("dataset_ingest_rds")
@ -230,13 +234,6 @@ def record_receipt(conn, shim: str, status: str, metadata: dict, error: str | No
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Ingestion functions # Ingestion functions
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def load_jsonl(path: Path) -> list:
"""Load json or jsonl file."""
with open(path, "r") as f:
data = json.load(f)
return data if isinstance(data, list) else []
def ingest_equations(conn) -> tuple[int, int]: def ingest_equations(conn) -> tuple[int, int]:
"""Ingest equations.json → knowledge.equations""" """Ingest equations.json → knowledge.equations"""
fpath = BUNDLE_EQS / "equations.json" fpath = BUNDLE_EQS / "equations.json"

View file

@ -21,10 +21,15 @@ DBC formula (Sarkar & Chaudhuri 1994):
from __future__ import annotations from __future__ import annotations
import math import math
import sys
from pathlib import Path
from typing import List, Optional, Tuple from typing import List, Optional, Tuple
import numpy as np import numpy as np
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import q16_div, q16_mul
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Q16_16 helpers (fixed-point: 16 integer bits, 16 fractional bits) # Q16_16 helpers (fixed-point: 16 integer bits, 16 fractional bits)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@ -45,20 +50,6 @@ def q16_to_float(q: int) -> float:
def q16_from_ratio(num: int, den: int) -> int: def q16_from_ratio(num: int, den: int) -> int:
"""Q16_16 of (num / den) using integer arithmetic.""" """Q16_16 of (num / den) using integer arithmetic."""
return (num << 16) // den return (num << 16) // den
def q16_mul(a: int, b: int) -> int:
"""Q16_16 multiply."""
return (a * b) >> 16
def q16_div(a: int, b: int) -> int:
"""Q16_16 divide."""
if b == 0:
raise ZeroDivisionError("Q16 division by zero")
return (a << 16) // b
def q16_log_approx(x: int) -> int: def q16_log_approx(x: int) -> int:
""" """
Approximate natural log in Q16_16 using integer Newton iteration. Approximate natural log in Q16_16 using integer Newton iteration.

View file

@ -29,6 +29,8 @@ from typing import Dict, List, Optional, Tuple
import sys as _sys import sys as _sys
_sys.path.insert(0, str(Path(__file__).resolve().parent)) _sys.path.insert(0, str(Path(__file__).resolve().parent))
_sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import Q16_SCALE, q16_add, q16_mul, q16_sub
try: try:
import numpy as np import numpy as np
@ -39,7 +41,6 @@ except ImportError:
# ── Q16_16 Fixed-Point ────────────────────────────────────────────────────── # ── Q16_16 Fixed-Point ──────────────────────────────────────────────────────
Q16_SCALE = 65536
Q16_MAX = 32767 Q16_MAX = 32767
Q16_MIN = -32768 Q16_MIN = -32768
@ -59,20 +60,6 @@ def q16_abs(raw: int) -> int:
def q16_neg(raw: int) -> int: def q16_neg(raw: int) -> int:
return max(Q16_MIN, min(Q16_MAX, -raw)) return max(Q16_MIN, min(Q16_MAX, -raw))
def q16_mul(a: int, b: int) -> int:
return max(Q16_MIN, min(Q16_MAX, (a * b) // Q16_SCALE))
def q16_add(a: int, b: int) -> int:
return max(Q16_MIN, min(Q16_MAX, a + b))
def q16_sub(a: int, b: int) -> int:
return max(Q16_MIN, min(Q16_MAX, a - b))
# ── GCCL Law Axes (from GCCL.lean) ───────────────────────────────────────── # ── GCCL Law Axes (from GCCL.lean) ─────────────────────────────────────────
class LawAxis(IntEnum): class LawAxis(IntEnum):
@ -564,6 +551,7 @@ def gccl_transition_check(
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Usage: python gccl_waveprobe.py --test") print("Usage: python gccl_waveprobe.py --test")
sys.exit(1) sys.exit(1)

View file

@ -20,6 +20,12 @@ import time
from collections import deque from collections import deque
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.hashing import sha256_bytes, sha256_text
from lib.jsonl import stable_json
ROOT = Path(__file__).resolve().parents[2] ROOT = Path(__file__).resolve().parents[2]
@ -38,20 +44,6 @@ PIST_FORMULA = (
CLAIM_BOUNDARY = ( CLAIM_BOUNDARY = (
"diagnostic_only_not_classifier_not_compression_claim_not_hutter_prize_claim" "diagnostic_only_not_classifier_not_compression_claim_not_hutter_prize_claim"
) )
def stable_json(value: Any) -> str:
return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def sha256_text(text: str) -> str:
return hashlib.sha256(text.encode("utf-8")).hexdigest()
def sha256_path(path: Path, chunk_size: int = 1024 * 1024) -> str: def sha256_path(path: Path, chunk_size: int = 1024 * 1024) -> str:
h = hashlib.sha256() h = hashlib.sha256()
with path.open("rb") as handle: with path.open("rb") as handle:

View file

@ -17,6 +17,9 @@ from pathlib import Path
import sys as _sys import sys as _sys
_sys.path.insert(0, str(Path(__file__).resolve().parent)) _sys.path.insert(0, str(Path(__file__).resolve().parent))
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import Q16_SCALE, to_q16
# ── Morton Code (Z-order curve) ───────────────────────────────────────────── # ── Morton Code (Z-order curve) ─────────────────────────────────────────────
@ -44,11 +47,6 @@ def mortonDecode(code: int) -> tuple:
# ── Q16_16 Fixed-Point ────────────────────────────────────────────────────── # ── Q16_16 Fixed-Point ──────────────────────────────────────────────────────
Q16_SCALE = 65536
def q16_from_float(x: float) -> int:
return max(-32768, min(32767, int(x * Q16_SCALE)))
def q16_to_float(raw: int) -> float: def q16_to_float(raw: int) -> float:
return raw / Q16_SCALE return raw / Q16_SCALE

View file

@ -18,6 +18,8 @@ are clustered together. The reduced problem is solved, then expanded back.
import json import json
import math import math
import subprocess import subprocess
import sys
from pathlib import Path
from typing import Optional from typing import Optional
# ── Tailscale Detection (graceful degradation) ────────────────────────── # ── Tailscale Detection (graceful degradation) ──────────────────────────
@ -114,6 +116,9 @@ def latency_to_sigma(latency_class: int) -> float:
"""Map latency class to scale space sigma.""" """Map latency class to scale space sigma."""
return _LATENCY_CLASSES.get(latency_class, _LATENCY_CLASSES[4])['sigma'] return _LATENCY_CLASSES.get(latency_class, _LATENCY_CLASSES[4])['sigma']
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import Q16_SCALE, q16_mul, to_q16
try: try:
import numpy as np import numpy as np
HAS_NUMPY = True HAS_NUMPY = True
@ -125,7 +130,6 @@ except ImportError:
# Q16.16 fixed-point arithmetic # Q16.16 fixed-point arithmetic
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
Q16_SCALE = 65536 # 2^16
Q16_MAX = 2147483647 # 2^31 - 1 Q16_MAX = 2147483647 # 2^31 - 1
Q16_MIN = -2147483648 # -2^31 Q16_MIN = -2147483648 # -2^31
@ -133,23 +137,9 @@ Q16_MIN = -2147483648 # -2^31
def q16_clamp(v: int) -> int: def q16_clamp(v: int) -> int:
"""Clamp integer to Q16.16 representable range.""" """Clamp integer to Q16.16 representable range."""
return max(Q16_MIN, min(Q16_MAX, v)) return max(Q16_MIN, min(Q16_MAX, v))
def q16_from_float(f: float) -> int:
"""Convert float to Q16.16 fixed-point."""
return q16_clamp(round(f * Q16_SCALE))
def q16_to_float(q: int) -> float: def q16_to_float(q: int) -> float:
"""Convert Q16.16 fixed-point to float.""" """Convert Q16.16 fixed-point to float."""
return q / Q16_SCALE return q / Q16_SCALE
def q16_multiply(a: int, b: int) -> int:
"""Q16.16 multiplication: (a * b) >> 16."""
return q16_clamp((a * b) >> 16)
def q16_exp(x_q16: int) -> int: def q16_exp(x_q16: int) -> int:
"""Q16.16 exponential: exp(x) where x is in Q16.16. """Q16.16 exponential: exp(x) where x is in Q16.16.
@ -157,7 +147,7 @@ def q16_exp(x_q16: int) -> int:
For FPGA, this would use a LUT-based approximation. For FPGA, this would use a LUT-based approximation.
""" """
x_float = q16_to_float(x_q16) x_float = q16_to_float(x_q16)
return q16_from_float(math.exp(x_float)) return to_q16(math.exp(x_float))
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@ -187,7 +177,7 @@ def gaussian_kernel_q16(sigma: float, size: int = 256) -> list[int]:
for i in range(size): for i in range(size):
x = (i - half) / half # Map to [-1, 1] x = (i - half) / half # Map to [-1, 1]
g = math.exp(-(x * x) / two_sigma_sq) g = math.exp(-(x * x) / two_sigma_sq)
kernel_raw.append(q16_from_float(g)) kernel_raw.append(to_q16(g))
# Normalize so kernel sums to Q16_SCALE (1.0 in Q16.16) # Normalize so kernel sums to Q16_SCALE (1.0 in Q16.16)
raw_sum = sum(kernel_raw) raw_sum = sum(kernel_raw)
@ -223,7 +213,7 @@ def gaussian_kernel_2d_q16(sigma: float, size: int = 16) -> list[list[int]]:
dx = (x - half) / half dx = (x - half) / half
dy = (y - half) / half dy = (y - half) / half
g = math.exp(-(dx * dx + dy * dy) / two_sigma_sq) g = math.exp(-(dx * dx + dy * dy) / two_sigma_sq)
v = q16_from_float(g) v = to_q16(g)
row.append(v) row.append(v)
total += v total += v
kernel.append(row) kernel.append(row)

View file

@ -45,6 +45,9 @@ from braid_vcn_encoder import (
) )
from fractal_dimension import fractal_dimension, fd_compress_hint from fractal_dimension import fractal_dimension, fd_compress_hint
_sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import Q16_SCALE, q16_add, q16_mul, q16_sub
try: try:
import numpy as np import numpy as np
_HAS_NUMPY = True _HAS_NUMPY = True
@ -54,7 +57,6 @@ except ImportError:
# ── Q16_16 Fixed-Point (matches Lean FixedPoint.lean) ─────────────────────── # ── Q16_16 Fixed-Point (matches Lean FixedPoint.lean) ───────────────────────
Q16_SCALE = 65536 # 2^16
Q16_MAX = 32767 # max Q16_16 value Q16_MAX = 32767 # max Q16_16 value
Q16_MIN = -32768 # min Q16_16 value Q16_MIN = -32768 # min Q16_16 value
@ -79,26 +81,6 @@ def q16_neg(raw: int) -> int:
"""Negate in Q16_16.""" """Negate in Q16_16."""
result = -raw result = -raw
return max(Q16_MIN, min(Q16_MAX, result)) return max(Q16_MIN, min(Q16_MAX, result))
def q16_mul(a: int, b: int) -> int:
"""Multiply two Q16_16 values."""
result = (a * b) // Q16_SCALE
return max(Q16_MIN, min(Q16_MAX, result))
def q16_add(a: int, b: int) -> int:
"""Add two Q16_16 values."""
result = a + b
return max(Q16_MIN, min(Q16_MAX, result))
def q16_sub(a: int, b: int) -> int:
"""Subtract two Q16_16 values."""
result = a - b
return max(Q16_MIN, min(Q16_MAX, result))
# ── Gate Condition (from DegeneracyConversion.lean) ───────────────────────── # ── Gate Condition (from DegeneracyConversion.lean) ─────────────────────────
def gate_condition(residual: int, threshold: int) -> bool: def gate_condition(residual: int, threshold: int) -> bool:
@ -519,6 +501,7 @@ def famm_decode(
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Usage: python vcn_famm_transport.py <braid_data_file>") print("Usage: python vcn_famm_transport.py <braid_data_file>")
print(" python vcn_famm_transport.py --test") print(" python vcn_famm_transport.py --test")

View file

@ -4,7 +4,6 @@
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import os import os
import shlex import shlex
@ -19,6 +18,9 @@ from pathlib import Path
from derive_trinary_program import derive_payload from derive_trinary_program import derive_payload
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.hashing import sha256_file
@dataclass @dataclass
class CommandResult: class CommandResult:
@ -39,16 +41,6 @@ THREAD_LIMIT_ENV_VARS = [
"RAYON_NUM_THREADS", "RAYON_NUM_THREADS",
"TBB_NUM_THREADS", "TBB_NUM_THREADS",
] ]
def sha256_file(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def run_command(command: str, *, env: dict[str, str] | None = None) -> CommandResult: def run_command(command: str, *, env: dict[str, str] | None = None) -> CommandResult:
started = time.perf_counter() started = time.perf_counter()
completed = subprocess.run( completed = subprocess.run(

View file

@ -8,6 +8,10 @@ import hashlib
import json import json
import shutil import shutil
from pathlib import Path from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.hashing import sha256_file
DEFAULT_PACKET = [ DEFAULT_PACKET = [
@ -26,16 +30,6 @@ DEFAULT_REVIEW_QUESTIONS = [
"Are there obvious wording or trust problems that would confuse a careful reader?", "Are there obvious wording or trust problems that would confuse a careful reader?",
"What is the next smallest artifact that would materially improve review?", "What is the next smallest artifact that would materially improve review?",
] ]
def sha256_file(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def parse_args() -> argparse.Namespace: def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument( parser.add_argument(

View file

@ -4,23 +4,17 @@
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
from pathlib import Path from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.hashing import sha256_file
WIDTH = 6 WIDTH = 6
TRIT_MAP = {0: -1, 1: 0, 2: 1} TRIT_MAP = {0: -1, 1: 0, 2: 1}
SCHEMA = "trinary_vm_derivation_v1" SCHEMA = "trinary_vm_derivation_v1"
def sha256_file(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def byte_to_trits(value: int) -> list[int]: def byte_to_trits(value: int) -> list[int]:
digits = [0] * WIDTH digits = [0] * WIDTH
remaining = value remaining = value

View file

@ -4,20 +4,14 @@
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import subprocess import subprocess
from datetime import datetime, timezone from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
def sha256_file(path: Path) -> str: from lib.hashing import sha256_file
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def run(*args: str) -> str: def run(*args: str) -> str:
completed = subprocess.run( completed = subprocess.run(

View file

@ -10,14 +10,8 @@ import subprocess
import sys import sys
from pathlib import Path from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
def sha256_file(path: Path) -> str: from lib.hashing import sha256_file
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def detect_mode(path: Path, explicit_mode: str) -> str: def detect_mode(path: Path, explicit_mode: str) -> str:
if explicit_mode != "auto": if explicit_mode != "auto":

View file

@ -4,7 +4,6 @@
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import sys import sys
from datetime import datetime, timezone from datetime import datetime, timezone
@ -12,14 +11,8 @@ from pathlib import Path
from derive_trinary_program import SCHEMA, derive_payload from derive_trinary_program import SCHEMA, derive_payload
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
def sha256_file(path: Path) -> str: from lib.hashing import sha256_file
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def parse_args() -> argparse.Namespace: def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)

View file

@ -4,21 +4,14 @@
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
from pathlib import Path from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
def sha256_file(path: Path) -> str: from lib.hashing import sha256_file
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def file_map(root: Path) -> dict[str, str]: def file_map(root: Path) -> dict[str, str]:
files: dict[str, str] = {} files: dict[str, str] = {}

View file

@ -4,19 +4,12 @@
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import sys import sys
from pathlib import Path from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
def sha256_file(path: Path) -> str: from lib.hashing import sha256_file
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def parse_args() -> argparse.Namespace: def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)

View file

@ -1,15 +1,16 @@
import json import json
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import to_q16
def sadd(a, b): def sadd(a, b):
"""Saturating 32-bit signed addition.""" """Saturating 32-bit signed addition."""
res = a + b res = a + b
if res > 0x7FFFFFFF: return 0x7FFFFFFF if res > 0x7FFFFFFF: return 0x7FFFFFFF
if res < -0x80000000: return -0x80000000 if res < -0x80000000: return -0x80000000
return res return res
def to_q16_16(val):
return int(val * 65536)
class AVMReference: class AVMReference:
def __init__(self): def __init__(self):
self.state = {"stack": [], "pc": 0} self.state = {"stack": [], "pc": 0}

View file

@ -24,6 +24,11 @@ import xml.sax.saxutils as xml_escape
from collections import Counter, defaultdict from collections import Counter, defaultdict
from pathlib import Path from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.jsonl import write_jsonl
ROOT = Path(__file__).resolve().parents[1] ROOT = Path(__file__).resolve().parents[1]
LEAN_ROOT = ROOT / "0-Core-Formalism" / "lean" LEAN_ROOT = ROOT / "0-Core-Formalism" / "lean"
DATA_OUT = ROOT / "shared-data" / "data" / "lean_module_graph" DATA_OUT = ROOT / "shared-data" / "data" / "lean_module_graph"
@ -141,14 +146,6 @@ def parse_module(path: Path) -> dict:
"sorry_count": len(re.findall(r"\bsorry\b", text)), "sorry_count": len(re.findall(r"\bsorry\b", text)),
"sha256": hashlib.sha256(text.encode("utf-8", errors="replace")).hexdigest(), "sha256": hashlib.sha256(text.encode("utf-8", errors="replace")).hexdigest(),
} }
def write_jsonl(path: Path, rows: list[dict]) -> None:
with path.open("w", encoding="utf-8") as f:
for row in rows:
f.write(json.dumps(row, sort_keys=True) + "\n")
def write_csv(path: Path, rows: list[dict], fieldnames: list[str]) -> None: def write_csv(path: Path, rows: list[dict], fieldnames: list[str]) -> None:
with path.open("w", encoding="utf-8", newline="") as f: with path.open("w", encoding="utf-8", newline="") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames) writer = csv.DictWriter(f, fieldnames=fieldnames)

View file

@ -1,11 +1,15 @@
import numpy as np import numpy as np
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.q16 import Q16_ONE, q16_mul
# Phase 1 — Build BurgersTriadCore # Phase 1 — Build BurgersTriadCore
# Q16.16 in the AVM hot path # Q16.16 in the AVM hot path
# Q16.16 Constants # Q16.16 Constants
Q16_SHIFT = 16 Q16_SHIFT = 16
Q16_ONE = 1 << Q16_SHIFT
Q16_MAX = (1 << 31) - 1 Q16_MAX = (1 << 31) - 1
Q16_MIN = -(1 << 31) Q16_MIN = -(1 << 31)
@ -35,13 +39,6 @@ def q16_sat(x: int) -> int:
_sat_count += 1 _sat_count += 1
return Q16_MIN return Q16_MIN
return x return x
def q16_mul(x: int, y: int) -> int:
"""Q16.16 Multiplication with saturation."""
# Multiply raw integers, then shift back by 16
res = (x * y) >> Q16_SHIFT
return q16_sat(res)
def triad_rhs(a: tuple[int, int, int], nu_eff: int) -> tuple[int, int, int]: def triad_rhs(a: tuple[int, int, int], nu_eff: int) -> tuple[int, int, int]:
""" """
Triad equations (Burgers): Triad equations (Burgers):

View file

@ -32,48 +32,15 @@ except ImportError:
_HAS_SLUQ_TRIAGE = False _HAS_SLUQ_TRIAGE = False
print("[!] SLUQ triage system not available") print("[!] SLUQ triage system not available")
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import Q16_ONE, Q16_SCALE, from_q16, q16_add, q16_div, q16_ge, q16_gt, q16_sub, to_q16
try: try:
from hypercube_topology import HypercubeTopologySystem, HypercubeNode from hypercube_topology import HypercubeTopologySystem, HypercubeNode
_HAS_HYPERCUBE = True _HAS_HYPERCUBE = True
except ImportError: except ImportError:
_HAS_HYPERCUBE = False _HAS_HYPERCUBE = False
print("[!] Hypercube topology system not available") print("[!] Hypercube topology system not available")
# Q16_16 fixed-point utilities (from Lean FixedPoint module)
Q16_ONE = 65536 # 1.0 in Q16_16
Q16_SCALE = 65536.0
def to_q16(value: float) -> int:
"""Convert float to Q16_16 fixed-point"""
return int(value * Q16_SCALE)
def from_q16(q16: int) -> float:
"""Convert Q16_16 fixed-point to float"""
return q16 / Q16_SCALE
def q16_add(a: int, b: int) -> int:
"""Add two Q16_16 values"""
return a + b
def q16_sub(a: int, b: int) -> int:
"""Subtract two Q16_16 values"""
return a - b
def q16_div(a: int, b: int) -> int:
"""Divide two Q16_16 values with normalization"""
if b == 0:
return 0
return (a * Q16_ONE) // b
def q16_gt(a: int, b: int) -> bool:
"""Greater than comparison for Q16_16"""
return a > b
def q16_ge(a: int, b: int) -> bool:
"""Greater than or equal comparison for Q16_16"""
return a >= b
@dataclass @dataclass
class NodeAccessPattern: class NodeAccessPattern:
"""Node access pattern (Lean: NodeAccessPattern)""" """Node access pattern (Lean: NodeAccessPattern)"""

View file

@ -14,10 +14,14 @@ from __future__ import annotations
import json import json
import hashlib import hashlib
import sys
import time import time
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.jsonl import stable_json
ROOT = Path(__file__).resolve().parents[1] ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / "out" / "hutter_nat_gpu_search.json" OUT = ROOT / "out" / "hutter_nat_gpu_search.json"
@ -47,14 +51,8 @@ LEAN_THEOREMS = [
"status_note": "requires compressedSize <= originalSize validity assumption", "status_note": "requires compressedSize <= originalSize validity assumption",
}, },
] ]
def canonical_json(value: Any) -> str:
return json.dumps(value, sort_keys=True, separators=(",", ":"))
def content_hash(value: Any) -> str: def content_hash(value: Any) -> str:
return hashlib.sha256(canonical_json(value).encode("utf-8")).hexdigest() return hashlib.sha256(stable_json(value).encode("utf-8")).hexdigest()
def node_id(prefix: str, value: Any) -> str: def node_id(prefix: str, value: Any) -> str:
@ -106,7 +104,7 @@ def upsert_truth_dag(witness: dict[str, Any]) -> dict[str, Any]:
"type": "gpu_empirical_witness", "type": "gpu_empirical_witness",
"timestamp": timestamp, "timestamp": timestamp,
"data": witness, "data": witness,
"nibbles": len(canonical_json(witness).encode("utf-8")) * 2, "nibbles": len(stable_json(witness).encode("utf-8")) * 2,
"verified": bool(witness["execution"]["all_passed"]), "verified": bool(witness["execution"]["all_passed"]),
"status": "VERIFIED_TRUE" if witness["execution"]["all_passed"] else "DRIFT", "status": "VERIFIED_TRUE" if witness["execution"]["all_passed"] else "DRIFT",
} }

View file

@ -3,13 +3,18 @@
from __future__ import annotations from __future__ import annotations
import hashlib
import json import json
import shutil import shutil
import sqlite3 import sqlite3
from datetime import datetime, timezone from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.hashing import sha256_bytes, sha256_text
from lib.jsonl import stable_json
ROOT = Path("/home/allaun/Documents/Research Stack") ROOT = Path("/home/allaun/Documents/Research Stack")
@ -53,24 +58,8 @@ EVIDENCE_PATTERNS = {
"vectorless_database": "i need a vectorles approach with high token retension and external database stores", "vectorless_database": "i need a vectorles approach with high token retension and external database stores",
"topology_agents": "once we have agents that can point out where the topology fits", "topology_agents": "once we have agents that can point out where the topology fits",
} }
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def sha256_path(path: Path) -> str: def sha256_path(path: Path) -> str:
return sha256_bytes(path.read_bytes()) return sha256_bytes(path.read_bytes())
def sha256_text(text: str) -> str:
return hashlib.sha256(text.encode("utf-8")).hexdigest()
def stable_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def slugify(value: str) -> str: def slugify(value: str) -> str:
return "".join(ch if ch.isalnum() else "_" for ch in value.lower()).strip("_") return "".join(ch if ch.isalnum() else "_" for ch in value.lower()).strip("_")

View file

@ -19,57 +19,22 @@ This Python shim provides:
""" """
import json import json
import sys
import time import time
from pathlib import Path
from typing import Dict, List, Optional, Any from typing import Dict, List, Optional, Any
from dataclasses import dataclass from dataclasses import dataclass
from collections import deque from collections import deque
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import Q16_ONE, Q16_SCALE, from_q16, q16_add, q16_div, q16_ge, q16_gt, q16_mul, q16_sub, to_q16
try: try:
from q_factor import QFactorSystem, QFactorAction, EnergyBalance as QFactorBalance, to_q16 as q16_to, from_q16 as q16_from from q_factor import QFactorSystem, QFactorAction, EnergyBalance as QFactorBalance, to_q16 as q16_to, from_q16 as q16_from
_HAS_QFACTOR = True _HAS_QFACTOR = True
except ImportError: except ImportError:
_HAS_QFACTOR = False _HAS_QFACTOR = False
print("[!] Q-Factor system not available") print("[!] Q-Factor system not available")
# Q16_16 fixed-point utilities (from Lean FixedPoint module)
Q16_ONE = 65536 # 1.0 in Q16_16
Q16_SCALE = 65536.0
def to_q16(value: float) -> int:
"""Convert float to Q16_16 fixed-point"""
return int(value * Q16_SCALE)
def from_q16(q16: int) -> float:
"""Convert Q16_16 fixed-point to float"""
return q16 / Q16_SCALE
def q16_add(a: int, b: int) -> int:
"""Add two Q16_16 values"""
return a + b
def q16_sub(a: int, b: int) -> int:
"""Subtract two Q16_16 values"""
return a - b
def q16_mul(a: int, b: int) -> int:
"""Multiply two Q16_16 values with normalization"""
return (a * b) // Q16_ONE
def q16_div(a: int, b: int) -> int:
"""Divide two Q16_16 values with normalization"""
if b == 0:
return 0
return (a * Q16_ONE) // b
def q16_gt(a: int, b: int) -> bool:
"""Greater than comparison for Q16_16"""
return a > b
def q16_ge(a: int, b: int) -> bool:
"""Greater than or equal comparison for Q16_16"""
return a >= b
@dataclass @dataclass
class AgentEnergyState: class AgentEnergyState:
"""Agent energy state (Lean: AgentEnergyState)""" """Agent energy state (Lean: AgentEnergyState)"""

View file

@ -13,7 +13,6 @@ Usage:
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import re import re
import sys import sys
@ -22,6 +21,9 @@ from enum import Enum
from pathlib import Path from pathlib import Path
from typing import Dict, List, Tuple from typing import Dict, List, Tuple
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.hashing import sha256_text
class Mode(str, Enum): class Mode(str, Enum):
DRAFT = "DRAFT" DRAFT = "DRAFT"
@ -57,12 +59,6 @@ class CompletionReceipt:
policy_checks: Dict[str, str] policy_checks: Dict[str, str]
candidate_hash: str candidate_hash: str
notes: List[str] notes: List[str]
def sha256_text(text: str) -> str:
return "sha256:" + hashlib.sha256(text.encode("utf-8")).hexdigest()
def q16_hex(units: int) -> str: def q16_hex(units: int) -> str:
"""Encode integer units as Q16.16 raw hex.""" """Encode integer units as Q16.16 raw hex."""
raw = max(0, min(units << 16, 0xFFFFFFFF)) raw = max(0, min(units << 16, 0xFFFFFFFF))

View file

@ -14,11 +14,13 @@ NOTE: This is a legacy ingest surface and should be treated as non-authoritative
import argparse import argparse
import json import json
import sys import sys
import hashlib
from pathlib import Path from pathlib import Path
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import Dict, Any, List, Optional, Tuple from typing import Dict, Any, List, Optional, Tuple
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.hashing import sha256_file
def env_default(name: str, default: str) -> str: def env_default(name: str, default: str) -> str:
try: try:
@ -58,16 +60,6 @@ TIER_MAPPING = {
"research": "AUX", "research": "AUX",
"architecture": "AUX", "architecture": "AUX",
} }
def compute_sha256(filepath: Path) -> str:
sha256 = hashlib.sha256()
with open(filepath, "rb") as f:
for chunk in iter(lambda: f.read(8192), b""):
sha256.update(chunk)
return f"sha256:{sha256.hexdigest()}"
def infer_domain(filepath: Path, content: str) -> str: def infer_domain(filepath: Path, content: str) -> str:
name = filepath.stem.upper() name = filepath.stem.upper()
for patterns, domain in DOMAIN_PATTERNS.items(): for patterns, domain in DOMAIN_PATTERNS.items():
@ -143,7 +135,7 @@ def compute_address_from_genome(genome: Dict[str, int]) -> int:
def md_to_jsonl_entry(filepath: Path, node_id: str) -> Dict[str, Any]: def md_to_jsonl_entry(filepath: Path, node_id: str) -> Dict[str, Any]:
file_hash = compute_sha256(filepath) file_hash = sha256_file(filepath)
file_stat = filepath.stat() file_stat = filepath.stat()
mtime_unix = file_stat.st_mtime mtime_unix = file_stat.st_mtime
file_size = file_stat.st_size file_size = file_stat.st_size

View file

@ -20,25 +20,9 @@ import numpy as np
import json import json
from pathlib import Path from pathlib import Path
def q16_add(a, b): import sys
"""Q16_16 addition (wrapping).""" sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
return (a + b) & 0xFFFFFFFF from lib.q16 import q16_add, q16_div, q16_mul, q16_sub
def q16_sub(a, b):
"""Q16_16 subtraction (wrapping)."""
return (a - b) & 0xFFFFFFFF
def q16_mul(a, b):
"""Q16_16 multiplication (high 32 bits of 64-bit product)."""
prod = (a * b) >> 16
return prod & 0xFFFFFFFF
def q16_div(a, b):
"""Q16_16 division (with division by zero handling)."""
if b == 0:
return 0xFFFFFFFF # Division by zero marker
numerator = (a << 16) & 0xFFFFFFFFFFFFFFFF
return (numerator // b) & 0xFFFFFFFF
def q16_max(a, b): def q16_max(a, b):
"""Q16_16 maximum (unsigned comparison).""" """Q16_16 maximum (unsigned comparison)."""

View file

@ -19,57 +19,22 @@ This Python shim provides:
""" """
import json import json
import sys
import time import time
from pathlib import Path
from typing import Dict, List, Optional, Any from typing import Dict, List, Optional, Any
from dataclasses import dataclass from dataclasses import dataclass
from collections import deque from collections import deque
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import Q16_ONE, Q16_SCALE, from_q16, q16_add, q16_div, q16_ge, q16_gt, q16_mul, q16_sub, to_q16
try: try:
from temporal_spatial_ram import TemporalSpatialRAMSystem, NodePosition, TemporalSpatialResource from temporal_spatial_ram import TemporalSpatialRAMSystem, NodePosition, TemporalSpatialResource
_HAS_TS_RAM = True _HAS_TS_RAM = True
except ImportError: except ImportError:
_HAS_TS_RAM = False _HAS_TS_RAM = False
print("[!] Temporal-spatial RAM system not available") print("[!] Temporal-spatial RAM system not available")
# Q16_16 fixed-point utilities (from Lean FixedPoint module)
Q16_ONE = 65536 # 1.0 in Q16_16
Q16_SCALE = 65536.0
def to_q16(value: float) -> int:
"""Convert float to Q16_16 fixed-point"""
return int(value * Q16_SCALE)
def from_q16(q16: int) -> float:
"""Convert Q16_16 fixed-point to float"""
return q16 / Q16_SCALE
def q16_add(a: int, b: int) -> int:
"""Add two Q16_16 values"""
return a + b
def q16_sub(a: int, b: int) -> int:
"""Subtract two Q16_16 values"""
return a - b
def q16_mul(a: int, b: int) -> int:
"""Multiply two Q16_16 values with normalization"""
return (a * b) // Q16_ONE
def q16_div(a: int, b: int) -> int:
"""Divide two Q16_16 values with normalization"""
if b == 0:
return 0
return (a * Q16_ONE) // b
def q16_gt(a: int, b: int) -> bool:
"""Greater than comparison for Q16_16"""
return a > b
def q16_ge(a: int, b: int) -> bool:
"""Greater than or equal comparison for Q16_16"""
return a >= b
@dataclass @dataclass
class EnergyBalance: class EnergyBalance:
"""Energy balance components (Lean: EnergyBalance)""" """Energy balance components (Lean: EnergyBalance)"""

View file

@ -27,35 +27,12 @@ from dataclasses import dataclass, field
from enum import Enum from enum import Enum
from collections import defaultdict, deque from collections import defaultdict, deque
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import Q16_ONE, Q16_SCALE, from_q16, q16_add, q16_ge, q16_gt, q16_sub, to_q16
# Q16_16 fixed-point utilities (from Lean FixedPoint module) # Q16_16 fixed-point utilities (from Lean FixedPoint module)
Q16_ONE = 65536 # 1.0 in Q16_16
Q16_SCALE = 65536.0
def to_q16(value: float) -> int:
"""Convert float to Q16_16 fixed-point"""
return int(value * Q16_SCALE)
def from_q16(q16: int) -> float:
"""Convert Q16_16 fixed-point to float"""
return q16 / Q16_SCALE
def q16_add(a: int, b: int) -> int:
"""Add two Q16_16 values"""
return a + b
def q16_sub(a: int, b: int) -> int:
"""Subtract two Q16_16 values"""
return a - b
def q16_gt(a: int, b: int) -> bool:
"""Greater than comparison for Q16_16"""
return a > b
def q16_ge(a: int, b: int) -> bool:
"""Greater than or equal comparison for Q16_16"""
return a >= b
class ActionType(Enum): class ActionType(Enum):
"""Agent action type (Lean: ActionType)""" """Agent action type (Lean: ActionType)"""
IMPROVE_EFFICIENCY = "ImproveEfficiency" IMPROVE_EFFICIENCY = "ImproveEfficiency"

View file

@ -19,57 +19,22 @@ This Python shim provides:
""" """
import json import json
import sys
import time import time
from pathlib import Path
from typing import Dict, List, Optional, Any from typing import Dict, List, Optional, Any
from dataclasses import dataclass from dataclasses import dataclass
from collections import deque from collections import deque
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.q16 import Q16_ONE, Q16_SCALE, from_q16, q16_add, q16_div, q16_ge, q16_gt, q16_mul, q16_sub, to_q16
try: try:
from hot_path_cold_path import HotPathColdPathSystem, NodeAccessPattern, PathClassification from hot_path_cold_path import HotPathColdPathSystem, NodeAccessPattern, PathClassification
_HAS_HOT_COLD = True _HAS_HOT_COLD = True
except ImportError: except ImportError:
_HAS_HOT_COLD = False _HAS_HOT_COLD = False
print("[!] Hot path/cold path system not available") print("[!] Hot path/cold path system not available")
# Q16_16 fixed-point utilities (from Lean FixedPoint module)
Q16_ONE = 65536 # 1.0 in Q16_16
Q16_SCALE = 65536.0
def to_q16(value: float) -> int:
"""Convert float to Q16_16 fixed-point"""
return int(value * Q16_SCALE)
def from_q16(q16: int) -> float:
"""Convert Q16_16 fixed-point to float"""
return q16 / Q16_SCALE
def q16_add(a: int, b: int) -> int:
"""Add two Q16_16 values"""
return a + b
def q16_sub(a: int, b: int) -> int:
"""Subtract two Q16_16 values"""
return a - b
def q16_mul(a: int, b: int) -> int:
"""Multiply two Q16_16 values with normalization"""
return (a * b) // Q16_ONE
def q16_div(a: int, b: int) -> int:
"""Divide two Q16_16 values with normalization"""
if b == 0:
return 0
return (a * Q16_ONE) // b
def q16_gt(a: int, b: int) -> bool:
"""Greater than comparison for Q16_16"""
return a > b
def q16_ge(a: int, b: int) -> bool:
"""Greater than or equal comparison for Q16_16"""
return a >= b
@dataclass @dataclass
class NodePosition: class NodePosition:
"""Node position in topology (Lean: NodePosition)""" """Node position in topology (Lean: NodePosition)"""

View file

@ -13,17 +13,18 @@ import argparse
import json import json
import csv import csv
import sys import sys
import hashlib
from pathlib import Path from pathlib import Path
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import Dict, Any, List, Optional, Tuple from typing import Dict, Any, List, Optional, Tuple
from dataclasses import dataclass from dataclasses import dataclass
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.hashing import sha256_file
def env_default(name: str, default: str) -> str: def env_default(name: str, default: str) -> str:
try: try:
import os import os
v = os.environ.get(name) v = os.environ.get(name)
except Exception: except Exception:
v = None v = None
@ -102,16 +103,6 @@ def should_skip(filepath: Path) -> Tuple[bool, str]:
return True, "too_large" return True, "too_large"
return False, "" return False, ""
def compute_sha256(filepath: Path) -> str:
sha256 = hashlib.sha256()
with open(filepath, "rb") as f:
for chunk in iter(lambda: f.read(8192), b""):
sha256.update(chunk)
return f"sha256:{sha256.hexdigest()}"
def infer_domain(filepath: Path, content: str = "") -> str: def infer_domain(filepath: Path, content: str = "") -> str:
name = filepath.stem.upper() name = filepath.stem.upper()
path = str(filepath).upper() path = str(filepath).upper()
@ -213,7 +204,7 @@ def text_to_jsonl_entry(filepath: Path, node_id: str) -> Optional[Dict[str, Any]
return None return None
content = read_text_safely(filepath) content = read_text_safely(filepath)
file_hash = compute_sha256(filepath) file_hash = sha256_file(filepath)
file_stat = filepath.stat() file_stat = filepath.stat()
mtime_unix = file_stat.st_mtime mtime_unix = file_stat.st_mtime
file_size = file_stat.st_size file_size = file_stat.st_size

View file

@ -27,7 +27,6 @@ Usage:
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import hashlib
import json import json
import os import os
import re import re
@ -39,6 +38,9 @@ from pathlib import Path
from textwrap import dedent from textwrap import dedent
from typing import Any from typing import Any
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.hashing import sha256_text
REPO_ROOT = Path(__file__).resolve().parents[2] REPO_ROOT = Path(__file__).resolve().parents[2]
TIDDLER_DIR = REPO_ROOT / "6-Documentation" / "tiddlywiki-local" / "wiki" / "tiddlers" TIDDLER_DIR = REPO_ROOT / "6-Documentation" / "tiddlywiki-local" / "wiki" / "tiddlers"
@ -81,12 +83,6 @@ def slugify(text: str) -> str:
s = re.sub(r"[^a-z0-9._ -]+", "", s) s = re.sub(r"[^a-z0-9._ -]+", "", s)
s = re.sub(r"\s+", "_", s).strip("_") s = re.sub(r"\s+", "_", s).strip("_")
return s[:60] if s else "untitled" return s[:60] if s else "untitled"
def sha256_text(text: str) -> str:
return hashlib.sha256(text.encode("utf-8")).hexdigest()
def concept_vector_14(title: str, body: str, tags: list[str]) -> list[float]: def concept_vector_14(title: str, body: str, tags: list[str]) -> list[float]:
"""14D vector from keyword axis activation (mirrors tiddlywiki_ene_bridge pattern).""" """14D vector from keyword axis activation (mirrors tiddlywiki_ene_bridge pattern)."""
combined = f"{title}\n{body}\n{' '.join(tags)}".lower() combined = f"{title}\n{body}\n{' '.join(tags)}".lower()

View file

@ -1,6 +1,5 @@
from __future__ import annotations from __future__ import annotations
import hashlib
import json import json
import tempfile import tempfile
from pathlib import Path from pathlib import Path
@ -12,6 +11,10 @@ from common.catalog import (
viewer_artifact_path_for_step_path, viewer_artifact_path_for_step_path,
viewer_directory_for_step_path, viewer_directory_for_step_path,
) )
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[6] / "4-Infrastructure"))
from lib.hashing import sha256_file
REPO_ROOT = Path.cwd().resolve() REPO_ROOT = Path.cwd().resolve()
@ -104,16 +107,6 @@ def relative_to_repo(path: Path) -> str:
return resolved.relative_to(REPO_ROOT).as_posix() return resolved.relative_to(REPO_ROOT).as_posix()
except ValueError: except ValueError:
return resolved.as_posix() return resolved.as_posix()
def sha256_file(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def versioned_repo_url(path: Path, content_hash: str) -> str: def versioned_repo_url(path: Path, content_hash: str) -> str:
resolved = path.resolve() resolved = path.resolve()
try: try:

View file

@ -27,7 +27,6 @@ metrics.log on completion.
""" """
import argparse import argparse
import hashlib
import json import json
import os import os
import sqlite3 import sqlite3
@ -46,6 +45,10 @@ METRICS_LOG = REPO_ROOT / "metrics.log"
sys.path.insert(0, str(REPO_ROOT / "germane" / "tools")) sys.path.insert(0, str(REPO_ROOT / "germane" / "tools"))
from waveprobe_hasher import hash_file, store_waveprobe_states # noqa: E402 from waveprobe_hasher import hash_file, store_waveprobe_states # noqa: E402
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.hashing import sha256_file
CHUNK_SIZE = 64 * 1024 # 64 KB CHUNK_SIZE = 64 * 1024 # 64 KB
# File extensions to skip (binary noise, OS artifacts) # File extensions to skip (binary noise, OS artifacts)
@ -55,22 +58,6 @@ SKIP_EXTENSIONS = {
} }
SKIP_NAMES_LOWER = {"thumbs.db", "desktop.ini", ".ds_store"} SKIP_NAMES_LOWER = {"thumbs.db", "desktop.ini", ".ds_store"}
def sha256_file(path: Path, buf_size: int = 1 << 20) -> str:
h = hashlib.sha256()
try:
with open(path, "rb") as f:
while True:
block = f.read(buf_size)
if not block:
break
h.update(block)
except OSError:
return ""
return h.hexdigest()
def share_name_from_path(root: Path) -> str: def share_name_from_path(root: Path) -> str:
"""Derive a share name from the mount-point directory name.""" """Derive a share name from the mount-point directory name."""
return root.name.lower().replace(" ", "_") return root.name.lower().replace(" ", "_")

View file

@ -6,28 +6,13 @@
# Open-Source usage requires explicit permission from Brandon Scott Schneider. # Open-Source usage requires explicit permission from Brandon Scott Schneider.
# ============================================================================== # ==============================================================================
import argparse import argparse
import hashlib
import json import json
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Tuple, cast from typing import Any, Dict, List, Tuple, cast
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
def sha256_hex_bytes(data: bytes) -> str: from lib.hashing import sha256_bytes, sha256_file
h = hashlib.sha256()
h.update(data)
return h.hexdigest()
def sha256_file(path: Path) -> str:
h = hashlib.sha256()
with path.open("rb") as handle:
while True:
chunk = handle.read(1024 * 1024)
if not chunk:
break
h.update(chunk)
return h.hexdigest()
def chunk_file(path: Path, chunk_size: int) -> List[Tuple[int, int, bytes]]: def chunk_file(path: Path, chunk_size: int) -> List[Tuple[int, int, bytes]]:
chunks: List[Tuple[int, int, bytes]] = [] chunks: List[Tuple[int, int, bytes]] = []
@ -46,14 +31,14 @@ def chunk_file(path: Path, chunk_size: int) -> List[Tuple[int, int, bytes]]:
def build_manifest(input_path: Path, chunk_size: int) -> Dict[str, Any]: def build_manifest(input_path: Path, chunk_size: int) -> Dict[str, Any]:
file_bytes = input_path.read_bytes() file_bytes = input_path.read_bytes()
file_hash = sha256_hex_bytes(file_bytes) file_hash = sha256_bytes(file_bytes)
chunks = chunk_file(input_path, chunk_size=chunk_size) chunks = chunk_file(input_path, chunk_size=chunk_size)
chunk_rows: List[Dict[str, Any]] = [] chunk_rows: List[Dict[str, Any]] = []
leaf_hashes: List[str] = [] leaf_hashes: List[str] = []
for index, offset, data in chunks: for index, offset, data in chunks:
c_hash = sha256_hex_bytes(data) c_hash = sha256_bytes(data)
leaf_hashes.append(c_hash) leaf_hashes.append(c_hash)
chunk_rows.append( chunk_rows.append(
{ {
@ -66,7 +51,7 @@ def build_manifest(input_path: Path, chunk_size: int) -> Dict[str, Any]:
} }
) )
merkle_root = sha256_hex_bytes("".join(leaf_hashes).encode("ascii")) if leaf_hashes else "" merkle_root = sha256_bytes("".join(leaf_hashes).encode("ascii")) if leaf_hashes else ""
return { return {
"version": "manifest.v1", "version": "manifest.v1",
@ -140,7 +125,7 @@ def rebuild_from_store(manifest: Dict[str, Any], chunk_store: Path, out_file: Pa
if len(data) != expected_len: if len(data) != expected_len:
raise ValueError(f"chunk length mismatch for {digest}: got {len(data)} expected {expected_len}") raise ValueError(f"chunk length mismatch for {digest}: got {len(data)} expected {expected_len}")
if sha256_hex_bytes(data) != digest: if sha256_bytes(data) != digest:
raise ValueError(f"chunk hash mismatch for {digest}") raise ValueError(f"chunk hash mismatch for {digest}")
handle.write(data) handle.write(data)

View file

@ -15,7 +15,6 @@ from __future__ import annotations
import argparse import argparse
import datetime as dt import datetime as dt
import hashlib
import json import json
import os import os
import re import re
@ -26,6 +25,11 @@ from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.hashing import sha256_bytes
from lib.jsonl import canonical_json_bytes
REPO = Path(__file__).resolve().parents[3] REPO = Path(__file__).resolve().parents[3]
DEFAULT_OUTPUT_DIR = REPO / "shared-data" / "artifacts" / "deepseek_review" DEFAULT_OUTPUT_DIR = REPO / "shared-data" / "artifacts" / "deepseek_review"
@ -41,21 +45,6 @@ class EmittedReview:
receipt_path: Path receipt_path: Path
answer_sha256: str answer_sha256: str
prompt_sha256: str prompt_sha256: str
def sha256_bytes(data: bytes) -> str:
return "sha256:" + hashlib.sha256(data).hexdigest()
def canonical_json_bytes(value: Any) -> bytes:
return json.dumps(
value,
ensure_ascii=False,
sort_keys=True,
separators=(",", ":"),
).encode("utf-8")
def repo_relative(path: Path, repo_root: Path = REPO) -> str: def repo_relative(path: Path, repo_root: Path = REPO) -> str:
path = path.resolve() path = path.resolve()
repo_root = repo_root.resolve() repo_root = repo_root.resolve()

View file

@ -13,6 +13,10 @@ from typing import Any, Dict, List
from jsonschema import validate from jsonschema import validate
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.jsonl import load_jsonl
PROJECT_ROOT = Path(__file__).resolve().parent.parent PROJECT_ROOT = Path(__file__).resolve().parent.parent
CHAIN_SCHEMA = PROJECT_ROOT / "schemas" / "passive_chain_record.schema.json" CHAIN_SCHEMA = PROJECT_ROOT / "schemas" / "passive_chain_record.schema.json"
@ -20,18 +24,6 @@ CHAIN_SCHEMA = PROJECT_ROOT / "schemas" / "passive_chain_record.schema.json"
def parse_iso(ts: str) -> datetime: def parse_iso(ts: str) -> datetime:
return datetime.fromisoformat(ts.replace("Z", "+00:00")) return datetime.fromisoformat(ts.replace("Z", "+00:00"))
def load_jsonl(path: Path) -> List[Dict[str, Any]]:
rows: List[Dict[str, Any]] = []
with path.open("r", encoding="utf-8") as handle:
for line in handle:
s = line.strip()
if s:
rows.append(json.loads(s))
return rows
def parse_args() -> argparse.Namespace: def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Daily integrity check for passive all-market monitor records.") parser = argparse.ArgumentParser(description="Daily integrity check for passive all-market monitor records.")
parser.add_argument("--records", required=True, help="Path to chain_records.jsonl") parser.add_argument("--records", required=True, help="Path to chain_records.jsonl")

View file

@ -29,6 +29,9 @@ from typing import Any, Dict, Iterable, List, Mapping, Optional, Tuple, cast
from jsonschema import validate from jsonschema import validate
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.jsonl import write_jsonl
PROJECT_ROOT = Path(__file__).resolve().parent.parent PROJECT_ROOT = Path(__file__).resolve().parent.parent
SCHEMA_PATH = PROJECT_ROOT / "schemas" / "passive_macro_record.schema.json" SCHEMA_PATH = PROJECT_ROOT / "schemas" / "passive_macro_record.schema.json"
@ -323,16 +326,6 @@ def collect_records(args: argparse.Namespace, schema: Dict[str, Any]) -> List[Di
idx += 1 idx += 1
return records return records
def write_jsonl(path: Path, rows: List[Dict[str, Any]], append: bool) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
mode = "a" if append else "w"
with path.open(mode, encoding="utf-8") as handle:
for row in rows:
handle.write(json.dumps(row, sort_keys=True) + "\n")
def parse_args() -> argparse.Namespace: def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Collect worldwide macro feeds with redundancy (Yahoo/Stooq/FRED) and source-consensus checks.") parser = argparse.ArgumentParser(description="Collect worldwide macro feeds with redundancy (Yahoo/Stooq/FRED) and source-consensus checks.")
parser.add_argument("--symbol", action="append", help="Optional override in the form market_class:SYMBOL. Repeatable.") parser.add_argument("--symbol", action="append", help="Optional override in the form market_class:SYMBOL. Repeatable.")

View file

@ -1,7 +1,8 @@
import math import math
def to_q16_16(val): import sys
return int(val * 65536) & 0xFFFFFFFF sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.q16 import to_q16
def generate_mem_files(): def generate_mem_files():
# 256 entries, x = index / 16.0 (if we use ray_t[19:12] as addr) # 256 entries, x = index / 16.0 (if we use ray_t[19:12] as addr)
@ -18,8 +19,8 @@ def generate_mem_files():
y_inv = 1.0 / math.sqrt(x) y_inv = 1.0 / math.sqrt(x)
y_sqrt = math.sqrt(x) y_sqrt = math.sqrt(x)
f_inv.write(f"{to_q16_16(y_inv):08x}\n") f_inv.write(f"{to_q16(y_inv):08x}\n")
f_sqrt.write(f"{to_q16_16(y_sqrt):08x}\n") f_sqrt.write(f"{to_q16(y_sqrt):08x}\n")
print("Success: Generated diat_inv_table.mem and diat_sqrt_table.mem") print("Success: Generated diat_inv_table.mem and diat_sqrt_table.mem")

View file

@ -6,7 +6,6 @@
# Open-Source usage requires explicit permission from Brandon Scott Schneider. # Open-Source usage requires explicit permission from Brandon Scott Schneider.
# ============================================================================== # ==============================================================================
import argparse import argparse
import hashlib
import json import json
import sys import sys
from datetime import datetime, timezone from datetime import datetime, timezone
@ -16,21 +15,10 @@ from typing import Any, Dict
sys.path.insert(0, str(Path(__file__).parent)) sys.path.insert(0, str(Path(__file__).parent))
from rfc3161_stamp import stamp as rfc3161_stamp from rfc3161_stamp import stamp as rfc3161_stamp
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.hashing import sha256_file
def sha256_file(path: Path) -> str: from lib.jsonl import load_json
h = hashlib.sha256()
with path.open("rb") as handle:
while True:
chunk = handle.read(1024 * 1024)
if not chunk:
break
h.update(chunk)
return h.hexdigest()
def load_json(path: Path) -> Dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def parse_args() -> argparse.Namespace: def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Generate monthly PRE/POST accountability attestation package.") parser = argparse.ArgumentParser(description="Generate monthly PRE/POST accountability attestation package.")

View file

@ -15,8 +15,11 @@ except ImportError:
from io_harness_compat import spawn_isolated_process, fetch_network_resource from io_harness_compat import spawn_isolated_process, fetch_network_resource
import json import json
import hashlib
from pathlib import Path from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.hashing import sha256_text
# import subprocess (REMOVED BY WARDEN) # import subprocess (REMOVED BY WARDEN)
BASE_DIR = Path(__file__).parent.parent.resolve() BASE_DIR = Path(__file__).parent.parent.resolve()
@ -25,10 +28,6 @@ MANIFEST_BIN = BASE_DIR / "scripts" / "file_manifest_builder.py"
MANIFEST_OUT = BASE_DIR / "hqw_atomic_combinations.manifest.json" MANIFEST_OUT = BASE_DIR / "hqw_atomic_combinations.manifest.json"
METADATA_OUT = BASE_DIR / "hqw_materials_metadata.jsonl" METADATA_OUT = BASE_DIR / "hqw_materials_metadata.jsonl"
CHUNK_STORE = BASE_DIR / "hqw_chunks" CHUNK_STORE = BASE_DIR / "hqw_chunks"
def sha256_text(text):
return hashlib.sha256(text.encode()).hexdigest()
def generate_metadata(): def generate_metadata():
print(f"[*] Reading {DATA_FILE}...") print(f"[*] Reading {DATA_FILE}...")
with open(DATA_FILE, 'r') as f: with open(DATA_FILE, 'r') as f:

View file

@ -6,7 +6,6 @@
# Open-Source usage requires explicit permission from Brandon Scott Schneider. # Open-Source usage requires explicit permission from Brandon Scott Schneider.
# ============================================================================== # ==============================================================================
import argparse import argparse
import hashlib
import json import json
import sqlite3 import sqlite3
import sys import sys
@ -17,21 +16,11 @@ from typing import Any, Dict, List
sys.path.insert(0, str(Path(__file__).parent)) sys.path.insert(0, str(Path(__file__).parent))
from rfc3161_stamp import stamp as rfc3161_stamp from rfc3161_stamp import stamp as rfc3161_stamp
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.hashing import sha256_file
PROJECT_ROOT = Path(__file__).resolve().parent.parent PROJECT_ROOT = Path(__file__).resolve().parent.parent
def sha256_file(path: Path) -> str:
h = hashlib.sha256()
with path.open("rb") as handle:
while True:
chunk = handle.read(1024 * 1024)
if not chunk:
break
h.update(chunk)
return h.hexdigest()
def read_json(path: Path) -> Any: def read_json(path: Path) -> Any:
return json.loads(path.read_text(encoding="utf-8")) return json.loads(path.read_text(encoding="utf-8"))

View file

@ -12,21 +12,13 @@ from datetime import datetime, timedelta, timezone
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, cast from typing import Any, Dict, List, cast
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.jsonl import load_jsonl
def parse_iso(ts: str) -> datetime: def parse_iso(ts: str) -> datetime:
return datetime.fromisoformat(ts.replace("Z", "+00:00")) return datetime.fromisoformat(ts.replace("Z", "+00:00"))
def load_jsonl(path: Path) -> List[Dict[str, Any]]:
rows: List[Dict[str, Any]] = []
with path.open("r", encoding="utf-8") as handle:
for line in handle:
s = line.strip()
if s:
rows.append(json.loads(s))
return rows
def parse_args() -> argparse.Namespace: def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Generate weekly ethical accountability digest from PRE/POST records.") parser = argparse.ArgumentParser(description="Generate weekly ethical accountability digest from PRE/POST records.")
parser.add_argument("--pre", required=True, help="Path to pre_records.jsonl") parser.add_argument("--pre", required=True, help="Path to pre_records.jsonl")

View file

@ -21,10 +21,14 @@ from __future__ import annotations
import argparse import argparse
import csv import csv
import json import json
import sys
from datetime import datetime, timezone from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional, Tuple, cast from typing import Any, Dict, Iterable, List, Optional, Tuple, cast
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.jsonl import write_jsonl
try: try:
from zk_stark_spending_proof import ( from zk_stark_spending_proof import (
default_spending_constraint, default_spending_constraint,
@ -387,14 +391,6 @@ def generate_zk_stark_proof_for_candidate(
return proof return proof
except (ValueError, KeyError, AttributeError): except (ValueError, KeyError, AttributeError):
return None return None
def write_jsonl(path: Path, rows: List[Dict[str, Any]]) -> None:
with path.open("w", encoding="utf-8") as fh:
for row in rows:
fh.write(json.dumps(row, ensure_ascii=False) + "\n")
def main() -> None: def main() -> None:
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--candidates", required=True, help="Path to payout candidates (.csv or .jsonl)") parser.add_argument("--candidates", required=True, help="Path to payout candidates (.csv or .jsonl)")

View file

@ -12,22 +12,14 @@ from typing import Any, Dict, List
from jsonschema import validate from jsonschema import validate
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.jsonl import load_jsonl
PROJECT_ROOT = Path(__file__).resolve().parent.parent PROJECT_ROOT = Path(__file__).resolve().parent.parent
PRE_SCHEMA = PROJECT_ROOT / "schemas" / "pre_record.schema.json" PRE_SCHEMA = PROJECT_ROOT / "schemas" / "pre_record.schema.json"
POST_SCHEMA = PROJECT_ROOT / "schemas" / "post_record.schema.json" POST_SCHEMA = PROJECT_ROOT / "schemas" / "post_record.schema.json"
def load_jsonl(path: Path) -> List[Dict[str, Any]]:
rows: List[Dict[str, Any]] = []
with path.open("r", encoding="utf-8") as handle:
for line in handle:
s = line.strip()
if s:
rows.append(json.loads(s))
return rows
def load_schema(path: Path) -> Dict[str, Any]: def load_schema(path: Path) -> Dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8")) return json.loads(path.read_text(encoding="utf-8"))

View file

@ -13,15 +13,13 @@ import zlib
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Tuple from typing import Any, Dict, List, Tuple
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.jsonl import canonical_json_bytes
MAGIC = b"SVSC1\x00" MAGIC = b"SVSC1\x00"
HEADER_STRUCT = struct.Struct("<6s32sQQ") HEADER_STRUCT = struct.Struct("<6s32sQQ")
def canonical_json_bytes(obj: Any) -> bytes:
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
def smooth_bytes_delta(data: bytes) -> bytes: def smooth_bytes_delta(data: bytes) -> bytes:
out = bytearray(len(data)) out = bytearray(len(data))
prev = 0 prev = 0