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

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

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

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

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

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

176 lines
6.7 KiB
Python

#!/usr/bin/env python3
# ==============================================================================
# COPYRIGHT NO ONE EVERYWHERE LLC (WYOMING HOLDING COMPANY)
# PROJECT: SOVEREIGN STACK
# This artifact is entirely proprietary and cryptographically proven.
# Open-Source usage requires explicit permission from Brandon Scott Schneider.
# ==============================================================================
import argparse
import json
import sqlite3
import sys
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, List
sys.path.insert(0, str(Path(__file__).parent))
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
def read_json(path: Path) -> Any:
return json.loads(path.read_text(encoding="utf-8"))
def csv_preview(path: Path, max_lines: int = 10) -> List[str]:
lines = path.read_text(encoding="utf-8").splitlines()
return lines[:max_lines]
def line_count(path: Path) -> int:
with path.open("r", encoding="utf-8") as handle:
return sum(1 for _ in handle)
def sqlite_query(conn: sqlite3.Connection, query: str) -> List[Dict[str, Any]]:
rows = conn.execute(query).fetchall()
return [dict(row) for row in rows]
def build_attestation(db_path: Path, clusters_csv: Path, timeline_json: Path, template_csv: Path, session_files: List[Path] | None = None) -> Dict[str, Any]:
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
try:
counts = {
"observables": sqlite_query(conn, "SELECT COUNT(*) AS value FROM observables")[0]["value"],
"events": sqlite_query(conn, "SELECT COUNT(*) AS value FROM events")[0]["value"],
"relationships": sqlite_query(conn, "SELECT COUNT(*) AS value FROM relationships")[0]["value"],
}
events = sqlite_query(
conn,
"SELECT id AS event_id, source, observed_at, notes FROM events ORDER BY id",
)
observables = sqlite_query(
conn,
"SELECT id, observable_type, value, normalized_value FROM observables ORDER BY id",
)
relationships = sqlite_query(
conn,
"""
SELECT r.id,
r.event_id,
os.observable_type AS source_type,
os.value AS source_value,
r.relation_type,
ot.observable_type AS target_type,
ot.value AS target_value,
r.confidence
FROM relationships r
JOIN observables os ON os.id = r.source_observable_id
JOIN observables ot ON ot.id = r.target_observable_id
ORDER BY r.id
""",
)
finally:
conn.close()
timeline = read_json(timeline_json)
sessions_artifacts = {}
for sf in (session_files or []):
sessions_artifacts[sf.name] = {
"path": str(sf),
"sha256": sha256_file(sf),
"size_bytes": sf.stat().st_size,
}
attestation = {
"attested_at_utc": datetime.now(timezone.utc).replace(microsecond=0).isoformat(),
"generator": "5-Applications/scripts/generate_trace_attestation.py",
"workspace_root": str(PROJECT_ROOT),
"artifacts": {
"db": {
"path": str(db_path),
"sha256": sha256_file(db_path),
},
"clusters_csv": {
"path": str(clusters_csv),
"sha256": sha256_file(clusters_csv),
"line_count": line_count(clusters_csv),
"preview": csv_preview(clusters_csv),
},
"timeline_json": {
"path": str(timeline_json),
"sha256": sha256_file(timeline_json),
"event_count": len(timeline),
},
"repo_list_template": {
"path": str(template_csv),
"sha256": sha256_file(template_csv),
"line_count": line_count(template_csv),
"preview": csv_preview(template_csv),
},
**({"sessions": sessions_artifacts} if sessions_artifacts else {}),
},
"database_snapshot": {
"counts": counts,
"events": events,
"observables": observables,
"relationships": relationships,
},
}
return attestation
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Generate machine-checkable attestation for trace outputs.")
parser.add_argument("--db", default=str(PROJECT_ROOT / "out" / "demo_trace.sqlite3"), help="Path to trace SQLite DB.")
parser.add_argument("--clusters", default=str(PROJECT_ROOT / "out" / "clusters.csv"), help="Path to cluster CSV.")
parser.add_argument("--timeline", default=str(PROJECT_ROOT / "out" / "timeline.json"), help="Path to timeline JSON.")
parser.add_argument("--template", default=str(PROJECT_ROOT / "data_baselines" / "suspicious_repo_list_template.csv"), help="Path to repo list CSV template.")
parser.add_argument("--out", default=str(PROJECT_ROOT / "out" / "trace_attestation.json"), help="Output JSON path.")
parser.add_argument("--session", action="append", default=[], metavar="FILE", help="Session transcript file(s) to include as hashed artifacts. Repeatable.")
return parser.parse_args()
def main() -> int:
args = parse_args()
db_path = Path(args.db)
clusters_path = Path(args.clusters)
timeline_path = Path(args.timeline)
template_path = Path(args.template)
out_path = Path(args.out)
missing = [
str(path)
for path in (db_path, clusters_path, timeline_path, template_path)
if not path.exists()
]
if missing:
print(json.dumps({"error": "missing_input_files", "paths": missing}, indent=2))
return 2
attestation = build_attestation(db_path, clusters_path, timeline_path, template_path, [Path(s) for s in args.session])
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(attestation, indent=2) + "\n", encoding="utf-8")
out_sha256 = sha256_file(out_path)
try:
ts_info = rfc3161_stamp(out_path)
except Exception as exc:
ts_info = {"error": str(exc)}
session_stamps = {}
for sf in [Path(s) for s in args.session]:
try:
session_stamps[sf.name] = rfc3161_stamp(sf)
except Exception as exc:
session_stamps[sf.name] = {"error": str(exc)}
print(json.dumps({"wrote": str(out_path), "sha256": out_sha256, "rfc3161": ts_info, "session_stamps": session_stamps}, indent=2))
return 0
if __name__ == "__main__":
raise SystemExit(main())