feat(vm): add deterministic gameplay math and domain coverage gates
Intrepid/Prometeu/Runtime/pipeline/pr-master There was a failure building this commit

This commit is contained in:
2026-10-09 10:25:11 +01:00
parent 2e041266f1
commit 9318cf7cd3
47 changed files with 8825 additions and 281 deletions
+161
View File
@@ -0,0 +1,161 @@
#!/usr/bin/env python3
"""Audit exclusive source ownership and nonempty LLVM coverage domain evidence."""
import argparse
import json
import math
import sys
from pathlib import Path
class AuditError(ValueError):
pass
def load_json(path):
try:
return json.loads(path.read_text())
except (OSError, ValueError) as error:
raise AuditError(f"cannot read {path}: {error}") from error
def matches(name, domain):
return any(name.startswith(p) for p in domain["paths"]) and not any(
name.startswith(p) for p in domain.get("exclude_paths", [])
)
def owners(name, domains):
return [key for key, domain in domains.items() if matches(name, domain)]
def source_name(filename, root):
"""Normalize absolute or remapped relative report paths into repository paths."""
if not isinstance(filename, str):
raise AuditError("report filename must be a string")
path = Path(filename)
if not path.is_absolute():
path = root / path
try:
name = path.resolve().relative_to(root).as_posix()
except ValueError:
return None
parts = Path(name).parts
return name if parts and parts[0] == "crates" and "src" in parts and name.endswith(".rs") else None
def audit(root, config_path, report_path=None):
root = root.resolve()
config = load_json(config_path)
if not isinstance(config, dict) or config.get("version") != 1:
raise AuditError("unsupported coverage configuration version (expected 1)")
domains = config.get("domains")
if not isinstance(domains, dict) or not domains:
raise AuditError("configuration must contain nonempty domains")
sources = sorted(p.relative_to(root).as_posix() for p in (root / "crates").glob("**/src/**/*.rs"))
if not sources:
raise AuditError("no crate src Rust files found")
for key, domain in domains.items():
if not key or not isinstance(domain, dict):
raise AuditError(f"invalid domain {key!r}")
baseline = domain.get("baseline_percent")
if isinstance(baseline, bool) or not isinstance(baseline, (float, int)) or not math.isfinite(baseline) or not 0 <= baseline <= 100:
raise AuditError(f"{key}: baseline_percent must be finite in [0,100]")
paths = domain.get("paths")
exclusions = domain.get("exclude_paths", [])
if not isinstance(paths, list) or not paths or not isinstance(exclusions, list):
raise AuditError(f"{key}: paths must be nonempty and exclude_paths must be a list")
for prefix in paths + exclusions:
if not isinstance(prefix, str) or not prefix.startswith("crates/") or ".." in Path(prefix).parts or not any(s.startswith(prefix) for s in sources):
raise AuditError(f"{key}: prefix does not match crate sources: {prefix!r}")
if any(not any(p.startswith(include) for include in paths) for p in exclusions):
raise AuditError(f"{key}: exclusions must be within inclusive paths")
source_owners = {}
for name in sources:
assigned = owners(name, domains)
if len(assigned) != 1:
raise AuditError(f"{name}: expected exactly one domain, got {assigned}")
source_owners[name] = assigned[0]
result = {
"source_files": len(sources),
"domains": {
key: {"source_files": sum(owner == key for owner in source_owners.values()),
"report_files": 0, "files": [], "lines_count": 0, "lines_covered": 0,
"functions": {"count": 0, "covered": 0},
"regions": {"count": 0, "covered": 0},
"baseline_percent": domain["baseline_percent"]}
for key, domain in domains.items()
},
}
if report_path is None:
return result
report = load_json(report_path)
if not isinstance(report, dict) or not isinstance(report.get("data"), list) or not report["data"]:
raise AuditError("coverage report must contain nonempty data")
seen = set()
for data in report["data"]:
if not isinstance(data, dict) or not isinstance(data.get("files"), list):
raise AuditError("coverage report data must contain files")
for record in data["files"]:
if not isinstance(record, dict):
raise AuditError("coverage report file must be an object")
name = source_name(record.get("filename"), root)
if name is None:
continue
if name not in source_owners:
raise AuditError(f"report source not present in repository: {name}")
if name in seen:
raise AuditError(f"duplicate source in coverage report: {name}")
seen.add(name)
try:
lines = record["summary"]["lines"]
count, covered = lines["count"], lines["covered"]
except (KeyError, TypeError) as error:
raise AuditError(f"{name}: missing line summary") from error
if any(isinstance(v, bool) or not isinstance(v, int) for v in (count, covered)) or not 0 <= covered <= count:
raise AuditError(f"{name}: invalid line counts")
domain = result["domains"][source_owners[name]]
domain["report_files"] += 1
domain["files"].append(name)
domain["lines_count"] += count
domain["lines_covered"] += covered
for metric in ("functions", "regions"):
item = record["summary"].get(metric, {"count": 0, "covered": 0})
if not isinstance(item, dict):
raise AuditError(f"{name}: invalid {metric} summary")
metric_count, metric_covered = item.get("count"), item.get("covered")
if any(isinstance(v, bool) or not isinstance(v, int) for v in (metric_count, metric_covered)) or not 0 <= metric_covered <= metric_count:
raise AuditError(f"{name}: invalid {metric} counts")
domain[metric]["count"] += metric_count
domain[metric]["covered"] += metric_covered
for key, domain in result["domains"].items():
if domain["lines_count"] <= 0:
raise AuditError(f"{key}: no instrumented lines in report")
domain["line_percent"] = (domain["lines_covered"] * 10000 // domain["lines_count"]) / 100
domain["gate_passed"] = domain["line_percent"] >= domain["baseline_percent"]
return result
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parent.parent)
parser.add_argument("--config", type=Path)
parser.add_argument("--report", type=Path)
parser.add_argument("--json", action="store_true")
parser.add_argument("--quiet", action="store_true")
args = parser.parse_args()
try:
result = audit(args.root, args.config or args.root / "files/config/runtime-edge-coverage-domains.json", args.report)
except AuditError as error:
print(f"Coverage domain audit failed: {error}", file=sys.stderr)
return 1
if args.json:
print(json.dumps(result, indent=2))
elif not args.quiet:
print(f"Coverage ownership: {result['source_files']} source files, {len(result['domains'])} exclusive domains")
if args.report:
print("Coverage evidence: all domains contain instrumented lines; no source orphans or duplicates")
return 0
if __name__ == "__main__":
sys.exit(main())
+28 -49
View File
@@ -10,29 +10,14 @@ CYAN=$'\033[36m'
RESET=$'\033[0m'
line_percent() {
jq -r --arg domain "${1}" --arg root "${ROOT}" '
.domains[$domain].paths as $paths
| def matches_domain:
. as $file
| any($paths[]; . as $p | ($file.filename | startswith($root + "/" + $p) or startswith("./" + $p) or startswith($p)));
def pct($covered; $count):
if $count > 0 then (($covered * 10000 / $count | floor) / 100) else 0 end;
reduce (
input.data[]?.files[]?
| select(matches_domain)
) as $file (
{lines: {count: 0, covered: 0}};
.lines.count += ($file.summary.lines.count // 0)
| .lines.covered += ($file.summary.lines.covered // 0)
)
| pct(.lines.covered; .lines.count)
' "${CONFIG_PATH}" "${2}"
jq -r --arg domain "${1}" '.domains[$domain].line_percent' <<< "${AUDIT_RESULT}"
}
usage() {
cat <<'EOF'
Usage:
coverage-domain-evidence.sh --list
coverage-domain-evidence.sh --audit [report.json]
coverage-domain-evidence.sh [--check] [--show-summary-details] [--show-paths] [--show-tracked-files] [--show-matched-files] [--verbose] <domain> [report.json]
coverage-domain-evidence.sh [--show-summary-details] [--show-paths] [--show-tracked-files] [--show-matched-files] [--verbose] --check-all [report.json]
@@ -56,6 +41,11 @@ if [[ "$1" == "--list" ]]; then
exit 0
fi
if [[ "$1" == "--audit" ]]; then
python3 "${ROOT}/scripts/audit-coverage-domains.py" --report "${2:-${DEFAULT_REPORT}}"
exit
fi
CHECK_MODE=0
CHECK_ALL=0
SHOW_PATHS=0
@@ -114,6 +104,7 @@ done
if [[ ${CHECK_ALL} -eq 1 ]]; then
REPORT_PATH="${1:-${DEFAULT_REPORT}}"
python3 "${ROOT}/scripts/audit-coverage-domains.py" --report "${REPORT_PATH}" --quiet
FAIL=0
while IFS= read -r domain; do
@@ -151,17 +142,24 @@ if [[ ${SHOW_PATHS} -eq 1 ]]; then
jq -r --arg domain "${DOMAIN}" '.domains[$domain].paths[]' "${CONFIG_PATH}" | while IFS= read -r prefix; do
echo " - ${prefix}"
done
echo "Excluded repository paths:"
jq -r --arg domain "${DOMAIN}" '.domains[$domain].exclude_paths[]?' "${CONFIG_PATH}" | while IFS= read -r prefix; do
echo " - ${prefix}"
done
echo
fi
if [[ ${SHOW_TRACKED_FILES} -eq 1 ]]; then
echo "Tracked files present in repository:"
while IFS= read -r path; do
(
cd "${ROOT}"
rg --files . | sed 's#^\./##'
) | rg "^${path//\//\\/}" || true
done < <(jq -r --arg domain "${DOMAIN}" '.domains[$domain].paths[]' "${CONFIG_PATH}")
(
cd "${ROOT}"
rg --files crates
) | jq -Rr --arg domain "${DOMAIN}" --slurpfile config "${CONFIG_PATH}" '
$config[0].domains[$domain] as $mapping
| . as $name
| select(any($mapping.paths[]; . as $p | $name | startswith($p))
and (any($mapping.exclude_paths[]?; . as $p | $name | startswith($p)) | not))
'
echo
fi
@@ -174,32 +172,13 @@ if [[ ! -f "${REPORT_PATH}" ]]; then
exit 0
fi
jq -r --arg domain "${DOMAIN}" --arg root "${ROOT}" --argjson baseline "${BASELINE}" --argjson show_matched_files "${SHOW_MATCHED_FILES}" --argjson show_summary_details "${SHOW_SUMMARY_DETAILS}" '
.domains[$domain].paths as $paths
| def matches_domain:
. as $file
| any($paths[]; . as $p | ($file.filename | startswith($root + "/" + $p) or startswith("./" + $p) or startswith($p)));
AUDIT_RESULT="$(python3 "${ROOT}/scripts/audit-coverage-domains.py" --report "${REPORT_PATH}" --json)"
jq -r --arg domain "${DOMAIN}" --argjson show_matched_files "${SHOW_MATCHED_FILES}" --argjson show_summary_details "${SHOW_SUMMARY_DETAILS}" '
def pct($covered; $count):
if $count > 0 then (($covered * 10000 / $count | floor) / 100) else 0 end;
reduce (
input.data[]?.files[]?
| select(matches_domain)
) as $file (
{
files: [],
lines: {count: 0, covered: 0},
functions: {count: 0, covered: 0},
regions: {count: 0, covered: 0}
};
.files += [$file.filename]
| .lines.count += ($file.summary.lines.count // 0)
| .lines.covered += ($file.summary.lines.covered // 0)
| .functions.count += ($file.summary.functions.count // 0)
| .functions.covered += ($file.summary.functions.covered // 0)
| .regions.count += ($file.summary.regions.count // 0)
| .regions.covered += ($file.summary.regions.covered // 0)
)
| . + {line_percent: pct(.lines.covered; .lines.count)}
.domains[$domain]
| . + {lines: {count: .lines_count, covered: .lines_covered}}
| (if $show_summary_details == 1 then "lines: " + (.line_percent | tostring) + "% (" + (.lines.covered|tostring) + "/" + (.lines.count|tostring) + ")" else empty end),
(if $show_summary_details == 1 then " files_in_report: " + (.files | length | tostring) else empty end),
(if $show_summary_details == 1 then
@@ -211,9 +190,9 @@ jq -r --arg domain "${DOMAIN}" --arg root "${ROOT}" --argjson baseline "${BASELI
(if $show_matched_files == 1 then "" else empty end),
(if $show_matched_files == 1 then "Matched report files:" else empty end),
(if $show_matched_files == 1 then (.files[]? | " - " + .) else empty end)
' "${CONFIG_PATH}" "${REPORT_PATH}"
' <<< "${AUDIT_RESULT}"
LINE_PERCENT="$(line_percent "${DOMAIN}" "${REPORT_PATH}")"
LINE_PERCENT="$(line_percent "${DOMAIN}")"
if awk "BEGIN { exit !(${LINE_PERCENT} >= ${BASELINE}) }"; then
GATE_STATUS="${GREEN}PASS${RESET}"
+1
View File
@@ -0,0 +1 @@
gmpy2==2.3.0
+228
View File
@@ -0,0 +1,228 @@
#!/usr/bin/env python3
"""Independent MPFR gate. Requires gmpy2==2.3.0 in tooling, never in the VM.
Build numeric_probe in debug/release, run against each with identical seed/count,
then compare the emitted corpora. Directed MPFR intervals adapt precision until
binary64 rounding and strict trig error classification can be certified.
"""
import argparse
import hashlib
import json
import platform
import random
import struct
import subprocess
from pathlib import Path
import gmpy2 as g
def fbits(value):
return struct.unpack(">Q", struct.pack(">d", value))[0]
def host(bits):
return struct.unpack(">d", struct.pack(">Q", bits))[0]
def exact(bits):
"""Import through the exact rational representation, including subnormals."""
n, d = host(bits).as_integer_ratio()
return g.mpq(n, d)
def evaluate(op, args, precision, rounding):
with g.context(precision=precision, round=rounding):
xs = [g.mpfr("-0") if int(x[2:],16)==1<<63 else g.mpfr(exact(int(x[2:], 16))) for x in args if x.startswith("f:")]
if op == "3010":
return g.sqrt(xs[0])
if op == "3011":
return g.sin(xs[0])
if op == "3012":
return g.cos(xs[0])
if op == "3013":
return g.atan2(xs[0], xs[1])
if op in ("3016", "3017"):
return g.mpfr(int(args[0][2:]))
if op == "ADD":
return xs[0] + xs[1]
if op == "SUB":
return xs[0] - xs[1]
if op == "MUL":
return xs[0] * xs[1]
if op == "DIV":
return xs[0] / xs[1]
raise ValueError(op)
def rounded_bits(value):
# MPFR IEEE context includes gradual underflow and binary64 exponent bounds.
with g.context(g.ieee(64), round=g.RoundToNearest):
rounded = g.mpfr(value)
return fbits(float(rounded))
def certify(op, args, result):
if op in ("300c", "300d", "300e", "300f", "3018", "3019"):
bits = int(args[0][2:], 16)
n, d = host(bits).as_integer_ratio()
integer = abs(n) // d
if op == "300c":
integer = n // d
elif op == "300d":
integer = -((-n) // d)
elif op == "300f":
integer, remainder = divmod(abs(n), d)
if 2*remainder > d or (2*remainder == d and integer % 2):
integer += 1
if n < 0:
integer = -integer
elif n < 0:
integer = -integer
if op in ("3018", "3019"):
width = 32 if op == "3018" else 64
expected = "trap:13" if not -(1 << (width-1)) <= integer < (1 << (width-1)) else ("i:" if width == 32 else "l:")+str(integer)
else:
with g.context(precision=160, round=g.RoundToNearest):
rounded = rounded_bits(g.mpfr(integer))
if integer == 0:
rounded = bits & (1 << 63)
expected = f"f:{rounded:016x}"
if result != expected:
raise AssertionError(f"{op} {args}: {result}, exact {expected}")
return 0.0, 160
if op in ("1000", "1001"):
with g.context(g.ieee(64), round=g.RoundToNearest):
xs = [g.mpfr("-0") if int(x[2:],16)==1<<63 else g.mpfr(exact(int(x[2:],16))) for x in args]
if op == "1000":
p1 = xs[0]*xs[2]
p2 = xs[1]*xs[3]
value = p1+p2
if not g.is_finite(p1) or not g.is_finite(p2):
value = g.mpfr("inf")
else:
m, n = max(abs(xs[0]),abs(xs[1])), min(abs(xs[0]),abs(xs[1]))
if m == 0:
value = g.mpfr(0)
else:
r = n/m
q = r*r
s = g.mpfr(1)+q
t = g.sqrt(s)
value = m*t
expected = "trap:13" if not g.is_finite(value) else f"f:{fbits(float(value)):016x}"
if result != expected:
raise AssertionError(f"{op} {args}: {result}, MPFR sequence {expected}")
return 0.0, 53
trig = op in ("3011", "3012", "3013")
for precision in (160, 320, 640, 1280, 2560, 5120, 10240):
lo = evaluate(op, args, precision, g.RoundDown)
hi = evaluate(op, args, precision, g.RoundUp)
lb, hb = rounded_bits(lo), rounded_bits(hi)
if not trig:
if lo == hi == 0:
lb=hb=rounded_bits(evaluate(op,args,precision,g.RoundToNearest))
if lb != hb:
continue
expected = "trap:13" if lb & 0x7ff0000000000000 == 0x7ff0000000000000 else f"f:{lb:016x}"
if result != expected:
raise AssertionError(f"{op} {args}: {result}, MPFR {expected}")
return 0.0, precision
if not result.startswith("f:"):
raise AssertionError(f"unexpected trig fault: {op} {args}: {result}")
out_bits = int(result[2:], 16)
with g.context(precision=precision, round=g.RoundUp):
out = g.mpfr(exact(out_bits))
# Use a conservative spacing if interval crosses a power of two.
magnitude_lo = min(abs(lo), abs(hi))
if lo <= 0 <= hi:
magnitude_lo = g.mpfr(0)
exponent = max(-1074, int(g.get_exp(magnitude_lo)) - 53) if magnitude_lo else -1074
ulp = g.mul_2exp(g.mpfr(1), exponent)
# Each positive endpoint distance rounds upward. Taking abs after
# an upward-rounded negative subtraction would underestimate it.
error_hi = max(out - lo, hi - out, g.mpfr(0)) / ulp
if error_hi < 1:
return float(error_hi), precision
with g.context(precision=precision, round=g.RoundDown):
error_lo = (lo-out if out < lo else out-hi if out > hi else g.mpfr(0)) / ulp
if error_lo >= 1:
raise AssertionError(f"{op} {args}: {result}, error >= {float(error_lo):.12g} ULP")
raise AssertionError(f"unresolved MPFR interval: {op} {args}")
def corpus(count, seed):
rng = random.Random(seed)
# Both sides of binades, subnormal transitions, pi/2 reductions, huge angles.
critical = [0, 1, 2, 0xfffffffffffff, 0x10000000000000,
0x3fdfffffffffffff, 0x3fe0000000000000, 0x3fe0000000000001,
0x3ff0000000000000, 0x4000000000000000,
0x3ff921fb54442d18, 0x400921fb54442d18,
0x401921fb54442d18, 0x7fefffffffffffff]
critical += [v + delta for v in critical[4:] for delta in (-2, -1, 1, 2)]
critical = [v for v in critical if v & 0x7ff0000000000000 != 0x7ff0000000000000]
bits = critical + [rng.getrandbits(64) & 0x7fefffffffffffff for _ in range(count)]
# Masked samples omit half the exponent binades; add unmasked finite samples.
while len(bits) < 2*count + len(critical):
v = rng.getrandbits(64)
if v & 0x7ff0000000000000 != 0x7ff0000000000000:
bits.append(v)
rows = []
for v in bits:
positive = v & 0x7fffffffffffffff
rows.append(("3010", [f"f:{positive:016x}"]))
for op in ("300c", "300d", "300e", "300f", "3018", "3019"):
rows.append((op, [f"f:{v:016x}"]))
for signed in (positive, positive | (1 << 63)):
rows.append(("3011", [f"f:{signed:016x}"]))
rows.append(("3012", [f"f:{signed:016x}"]))
other = rng.choice(bits)
rows.append(("1001", [f"f:{v:016x}", f"f:{other:016x}"]))
rows.append(("1000", [f"f:{v:016x}", f"f:{other:016x}", f"f:{rng.choice(bits):016x}", f"f:{rng.choice(bits):016x}"]))
if v & 0x7fffffffffffffff or other & 0x7fffffffffffffff:
rows.append(("3013", [f"f:{v:016x}", f"f:{other:016x}"]))
for op in ("ADD", "SUB", "MUL", "DIV"):
if op != "DIV" or other & 0x7fffffffffffffff:
rows.append((op, [f"f:{v:016x}", f"f:{other:016x}"]))
for n in [-(1 << 63), (1 << 63)-1, 0, 1, -1, (1 << 53)-1, (1 << 53)+1] + [rng.randrange(-(1<<63), 1<<63) for _ in range(count)]:
rows.append(("3017", [f"l:{n}"]))
for n in [-(1<<31), (1<<31)-1, 0, 1, -1] + [rng.randrange(-(1<<31), 1<<31) for _ in range(count)]:
rows.append(("3016", [f"i:{n}"]))
return rows
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--probe", required=True)
parser.add_argument("--output", required=True)
parser.add_argument("--count", type=int, default=1000)
parser.add_argument("--seed", type=int, default=560053)
args = parser.parse_args()
rows = corpus(args.count, args.seed)
wire = "".join(" ".join([op] + values) + "\n" for op, values in rows)
process = subprocess.run([args.probe], input=wire, text=True, capture_output=True, check=True)
results = process.stdout.splitlines()
assert len(rows) == len(results)
maxima = {}
max_precision = 0
output = []
for (op, values), line in zip(rows, results):
result, cycles = line.split()
error, precision = certify(op, values, result)
maxima[op] = max(maxima.get(op, 0), error)
max_precision = max(max_precision, precision)
output.append(" ".join([op] + values + [result, cycles]) + "\n")
content = "".join(output).encode()
Path(args.output).parent.mkdir(parents=True, exist_ok=True)
Path(args.output).write_bytes(content)
print(json.dumps({
"rows": len(rows), "count": args.count, "seed": args.seed,
"corpus_sha256": hashlib.sha256(content).hexdigest(),
"max_error_ulp_upper_bound": maxima, "max_mpfr_precision": max_precision,
"gmpy2": g.version(), "mpfr": g.mpfr_version(),
"oracle_architecture": platform.machine(),
"probe": args.probe, "cross_host_execution": "pending",
}, indent=2))
if __name__ == "__main__":
main()