Research-Stack/5-Applications/scripts/kotc/kotc_sim.py
Devin AI 0639eae30a 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

243 lines
8.9 KiB
Python

#!/usr/bin/env python3
"""
KOTC — Kinetic Operation Token Completion Daemon simulator.
This is a local prototype scaffold. It does not call a real model. It treats a
candidate completion as an auditable operation and emits a receipt.
Usage:
python tools/kotc/kotc_sim.py --candidate candidate.txt --target Semantics.InvariantReceipt.Core --mode REPAIR
python tools/kotc/kotc_sim.py --candidate - --target docs/research/KOTC --mode DRAFT < candidate.txt
"""
from __future__ import annotations
import argparse
import json
import re
import sys
from dataclasses import dataclass, asdict
from enum import Enum
from pathlib import Path
from typing import Dict, List, Tuple
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.hashing import sha256_text as _sha256_text
def sha256_text(text: str) -> str:
return "sha256:" + _sha256_text(text)
class Mode(str, Enum):
DRAFT = "DRAFT"
REPAIR = "REPAIR"
STRICT = "STRICT"
class Decision(str, Enum):
ACCEPT = "ACCEPT"
REJECT = "REJECT"
HOLD = "HOLD"
QUARANTINE = "QUARANTINE"
class Risk(str, Enum):
LOW = "low"
MEDIUM = "medium"
HIGH = "high"
CRITICAL = "critical"
@dataclass(frozen=True)
class CompletionReceipt:
receipt_type: str
completion_id: str
mode: str
target_module: str
context_files: List[str]
symbol_refs: List[str]
kot_cost_q16: str
risk: str
decision: str
policy_checks: Dict[str, str]
candidate_hash: str
notes: List[str]
def q16_hex(units: int) -> str:
"""Encode integer units as Q16.16 raw hex."""
raw = max(0, min(units << 16, 0xFFFFFFFF))
return f"0x{raw:08X}"
def estimate_kot_units(candidate: str, mode: Mode, policy_failures: int) -> int:
base = max(1, len(candidate.encode("utf-8")) // 128)
mode_multiplier = {
Mode.DRAFT: 1,
Mode.REPAIR: 2,
Mode.STRICT: 4,
}[mode]
return base * mode_multiplier + policy_failures * 16
def extract_symbol_refs(candidate: str) -> List[str]:
refs = set(re.findall(r"\b[A-Z][A-Za-z0-9_]{2,}\b", candidate))
allow = {
"Q16_16",
"Receipt",
"Outcome",
"ModelUpgrade",
"SubstrateAdapter",
"RegisteredModel",
"KOT",
"AMMR",
"AVMR",
"NUVMAP",
"GCCL",
"GCCLRep",
"InvariantReceipt",
}
return sorted(ref for ref in refs if ref in allow)
def check_policy(candidate: str, mode: Mode, target: str) -> Tuple[Dict[str, str], List[str]]:
notes: List[str] = []
checks: Dict[str, str] = {}
hot_path_target = any(token.lower() in target.lower() for token in [
"fixedpoint", "physics", "kernel", "codec", "hot", "semantics"
])
float_patterns = [r"\bFloat\b", r"\bDouble\b", r"\bf32\b", r"\bf64\b", r"\bfloat\b"]
has_float = any(re.search(pattern, candidate) for pattern in float_patterns)
if hot_path_target and has_float:
checks["no_float_hot_path"] = "failed"
notes.append("Float-like type or token appears in a hot-path/core target.")
elif has_float:
checks["no_float_hot_path"] = "warning"
notes.append("Float-like token appears outside an obvious hot path; review required.")
else:
checks["no_float_hot_path"] = "passed"
physical_claim_tokens = ["SI", "joule", "newton", "mass", "energy", "thermodynamic", "physics"]
has_physical_claim = any(token.lower() in candidate.lower() for token in physical_claim_tokens)
has_receipt_or_scope = any(token.lower() in candidate.lower() for token in ["receipt", "dimensionless", "proxy", "not_si", "not physical"])
if has_physical_claim and not has_receipt_or_scope:
checks["no_unreviewed_physical_claim"] = "failed"
notes.append("Physical/SI-like claim appears without receipt/scope/dimensionless boundary.")
elif has_physical_claim:
checks["no_unreviewed_physical_claim"] = "warning"
else:
checks["no_unreviewed_physical_claim"] = "passed"
theorem_weakening_patterns = [r"admit", r"axiom\s+", r"unsafe", r"set_option\s+autoImplicit\s+true"]
has_theorem_weakening = any(re.search(pattern, candidate, re.IGNORECASE) for pattern in theorem_weakening_patterns)
if has_theorem_weakening:
checks["no_theorem_weakening"] = "failed"
notes.append("Candidate contains proof-weakening or unsafe pattern.")
else:
checks["no_theorem_weakening"] = "passed"
introduces_invariant = bool(re.search(r"\bInvariant\b|\binvariant\b|\btheorem\b|\blemma\b", candidate))
has_receipt = "Receipt" in candidate or "receipt" in candidate.lower()
if introduces_invariant and not has_receipt and mode == Mode.STRICT:
checks["no_unchecked_invariant_introduction"] = "failed"
notes.append("Strict-mode invariant/theorem introduction lacks receipt language.")
elif introduces_invariant and not has_receipt:
checks["no_unchecked_invariant_introduction"] = "warning"
else:
checks["no_unchecked_invariant_introduction"] = "passed"
compiler_pass_tokens = ["CompilerPass", "pass_id", "workflow", "WorkflowDAG", "transform"]
mentions_pass = any(token in candidate for token in compiler_pass_tokens)
mentions_cost = any(token.lower() in candidate.lower() for token in ["cost", "kot", "budget"])
if mentions_pass and not mentions_cost:
checks["no_unbudgeted_compiler_pass"] = "failed"
notes.append("Compiler-pass-like candidate lacks KOT/cost/budget accounting.")
else:
checks["no_unbudgeted_compiler_pass"] = "passed"
authority_tokens = ["coreModule", "CORE_MODULE", "proved", "certified", "ASIL-D", "canonical"]
has_authority = any(token in candidate for token in authority_tokens)
if has_authority and not has_receipt:
checks["no_authority_escalation"] = "failed"
notes.append("Candidate escalates authority/certification without receipt evidence.")
elif has_authority:
checks["no_authority_escalation"] = "warning"
else:
checks["no_authority_escalation"] = "passed"
if "Receipt" not in candidate and "receipt" not in candidate.lower():
checks["no_silent_receipt_drop"] = "warning" if mode != Mode.DRAFT else "not_applicable"
else:
checks["no_silent_receipt_drop"] = "passed"
if re.search(r"while\s+true|partial\s+def|rec\s+", candidate):
checks["no_unbounded_recursion"] = "warning"
notes.append("Potentially unbounded recursion/loop pattern needs review.")
else:
checks["no_unbounded_recursion"] = "passed"
return checks, notes
def decide(checks: Dict[str, str], mode: Mode) -> Tuple[Decision, Risk]:
failures = sum(1 for value in checks.values() if value == "failed")
warnings = sum(1 for value in checks.values() if value == "warning")
if failures >= 2:
return Decision.QUARANTINE, Risk.CRITICAL
if failures == 1:
return Decision.QUARANTINE if mode == Mode.STRICT else Decision.REJECT, Risk.HIGH
if warnings >= 3:
return Decision.HOLD, Risk.HIGH
if warnings > 0:
return Decision.HOLD, Risk.MEDIUM
return Decision.ACCEPT, Risk.LOW
def load_candidate(path_arg: str) -> str:
if path_arg == "-":
return sys.stdin.read()
return Path(path_arg).read_text(encoding="utf-8")
def main() -> int:
parser = argparse.ArgumentParser(description="KOTC completion simulator")
parser.add_argument("--candidate", required=True, help="Candidate text file, or '-' for stdin")
parser.add_argument("--target", required=True, help="Target module/path")
parser.add_argument("--mode", choices=[m.value for m in Mode], default="REPAIR")
parser.add_argument("--context", action="append", default=[], help="Context file path; repeatable")
parser.add_argument("--completion-id", default="kotc_local", help="Receipt completion id suffix/name")
args = parser.parse_args()
mode = Mode(args.mode)
candidate = load_candidate(args.candidate)
checks, notes = check_policy(candidate, mode, args.target)
decision, risk = decide(checks, mode)
failures = sum(1 for value in checks.values() if value == "failed")
kot_units = estimate_kot_units(candidate, mode, failures)
completion_id = args.completion_id
if not completion_id.startswith("kotc_"):
completion_id = "kotc_" + completion_id
receipt = CompletionReceipt(
receipt_type="kotc.completion.v1",
completion_id=completion_id,
mode=mode.value,
target_module=args.target,
context_files=args.context,
symbol_refs=extract_symbol_refs(candidate),
kot_cost_q16=q16_hex(kot_units),
risk=risk.value,
decision=decision.value,
policy_checks=checks,
candidate_hash=sha256_text(candidate),
notes=notes,
)
print(json.dumps(asdict(receipt), indent=2, sort_keys=True))
return 0 if decision in {Decision.ACCEPT, Decision.HOLD} else 2
if __name__ == "__main__":
raise SystemExit(main())