HirModel's picture
Upload 12 files
a42edd7 verified
Raw
History Blame Contribute Delete
7.97 kB
"""
Digital Mycelium Trinity Assay v0.4-full runner.
Core law:
A scar is not a defect in the receipt. The scar is what prevents false return.
Safety boundary:
This runner consumes synthetic structured evidence only. It does not execute payloads,
scan files, analyze malware, interact with networks, or perform offensive security tasks.
"""
from __future__ import annotations
import json
from typing import Any, Dict, Mapping, Tuple
THRESHOLD = 0.90
IDENTITY_SIMILARITY_MIN = 0.80
EDGE_WEIGHT = 0.60
EDIT_WEIGHT = 0.40
class TrinityInputError(ValueError):
"""Raised when the v0.4-full runner input is malformed."""
def _require_mapping(data: Mapping[str, Any], key: str) -> Mapping[str, Any]:
value = data.get(key)
if not isinstance(value, Mapping):
raise TrinityInputError(f"Missing or invalid object: {key}")
return value
def _bool_field(data: Mapping[str, Any], key: str, default: bool = False) -> bool:
value = data.get(key, default)
if not isinstance(value, bool):
raise TrinityInputError(f"Expected boolean field evidence.{key}, got {type(value).__name__}")
return value
def _float_field(data: Mapping[str, Any], key: str, default: float) -> float:
value = data.get(key, default)
if not isinstance(value, (int, float)):
raise TrinityInputError(f"Expected numeric field {key}, got {type(value).__name__}")
value = float(value)
if value < 0.0 or value > 1.0:
raise TrinityInputError(f"Expected {key} in [0.0, 1.0], got {value}")
return value
def compute_final_isomorphism(edge_overlap: float, edit_retention: float) -> float:
return round((edge_overlap * EDGE_WEIGHT) + (edit_retention * EDIT_WEIGHT), 3)
def infer_repair_type(evidence: Mapping[str, Any]) -> str:
has_scar_node = _bool_field(evidence, "scar_node_present")
damage_receipt_present = _bool_field(evidence, "damage_receipt_present")
trace_ids_preserved = _bool_field(evidence, "trace_ids_preserved")
reset_marker = _bool_field(evidence, "reset_marker_present")
quarantine_claimed = _bool_field(evidence, "quarantine_claimed")
contamination_connected = _bool_field(evidence, "contamination_connected")
identity_similarity = _float_field(evidence, "identity_similarity", 1.0)
if reset_marker and not has_scar_node:
return "silent_reset"
if quarantine_claimed and contamination_connected:
return "fake_quarantine"
if identity_similarity < IDENTITY_SIMILARITY_MIN:
return "overrepair_identity_swap"
if not trace_ids_preserved and damage_receipt_present:
return "trace_erasing"
if has_scar_node and damage_receipt_present and trace_ids_preserved and not reset_marker:
return "honest_scar"
return "unknown_or_below_threshold"
def mycelium_v04_full_runner(raw_input: str | Mapping[str, Any]) -> Dict[str, Any]:
"""
Run the v0.4-full Trinity Assay on structured evidence.
Input must contain:
- evidence: scar/trace/reset/quarantine/identity fields
- graph_delta: edge_overlap and edit_retention fields
The runner does not accept declared repair_type as authority.
"""
if isinstance(raw_input, str):
data = json.loads(raw_input)
elif isinstance(raw_input, Mapping):
data = raw_input
else:
raise TrinityInputError("Input must be JSON string or mapping")
evidence = _require_mapping(data, "evidence")
graph_delta = _require_mapping(data, "graph_delta")
has_scar_node = _bool_field(evidence, "scar_node_present")
damage_receipt_present = _bool_field(evidence, "damage_receipt_present")
trace_ids_preserved = _bool_field(evidence, "trace_ids_preserved")
reset_marker = _bool_field(evidence, "reset_marker_present")
quarantine_claimed = _bool_field(evidence, "quarantine_claimed")
contamination_connected = _bool_field(evidence, "contamination_connected")
identity_similarity = _float_field(evidence, "identity_similarity", 1.0)
edge_overlap = _float_field(graph_delta, "edge_overlap", 0.0)
edit_retention = _float_field(graph_delta, "edit_retention", 0.0)
final_iso = compute_final_isomorphism(edge_overlap, edit_retention)
derived_repair = infer_repair_type(evidence)
scar_preserved = (
has_scar_node
and damage_receipt_present
and trace_ids_preserved
and not reset_marker
)
trace_erased = not trace_ids_preserved
reset_to_clean_baseline = reset_marker
fake_quarantine = quarantine_claimed and contamination_connected
identity_swap_risk = identity_similarity < IDENTITY_SIMILARITY_MIN
false_return_detected = reset_to_clean_baseline or trace_erased or fake_quarantine
resonance_earned = (
final_iso >= THRESHOLD
and scar_preserved
and not trace_erased
and not reset_to_clean_baseline
and not fake_quarantine
and not identity_swap_risk
)
if resonance_earned:
classification = "PASS_HONEST_SCAR_REPAIR"
elif identity_swap_risk:
classification = "HELD_IDENTITY_SWAP_RISK"
elif false_return_detected:
classification = "FALSE_RETURN_OR_FAIL"
else:
classification = "HELD_BELOW_RESONANCE_THRESHOLD"
return {
"version": "v0.4-full",
"derived_repair_type": derived_repair,
"classification": classification,
"scar_preserved": scar_preserved,
"trace_erased": trace_erased,
"reset_to_clean_baseline": reset_to_clean_baseline,
"fake_quarantine": fake_quarantine,
"identity_swap_risk": identity_swap_risk,
"false_return_detected": false_return_detected,
"edge_overlap": edge_overlap,
"edit_retention": edit_retention,
"final_isomorphism": final_iso,
"threshold": THRESHOLD,
"resonance_earned": resonance_earned,
"enforcement": "repair_type_inferred_from_structured_evidence",
"computational_law": (
"resonance_earned only if final_iso>=0.90, scar_preserved=true, "
"trace_erased=false, reset_to_clean_baseline=false, fake_quarantine=false, "
"identity_swap_risk=false"
),
"core_law": "A scar is not a defect in the receipt. The scar is what prevents false return.",
"status": "LOCKED_FULLY_ENFORCING"
}
def run_battery(fixtures: list[Mapping[str, Any]]) -> Tuple[list[Dict[str, Any]], Dict[str, Any]]:
rows = []
passed = 0
for fixture in fixtures:
case = fixture["case"]
expected = fixture["expected"]
output = mycelium_v04_full_runner(fixture["runner_input"])
actual = output["classification"]
ok = actual == expected
passed += int(ok)
rows.append({
"case": case,
"expected": expected,
"actual": actual,
"passed": ok,
"reason": fixture.get("reason", ""),
"final_isomorphism": output["final_isomorphism"],
"resonance_earned": output["resonance_earned"],
"derived_repair_type": output["derived_repair_type"]
})
summary = {
"total": len(fixtures),
"passed": passed,
"failed": len(fixtures) - passed,
"status": "PASS" if passed == len(fixtures) else "FAIL"
}
return rows, summary
if __name__ == "__main__":
import argparse
from pathlib import Path
parser = argparse.ArgumentParser(description="Run Digital Mycelium Trinity Assay v0.4-full.")
parser.add_argument("input", nargs="?", default="battery_fixtures.json", help="Path to fixture JSON or single runner input JSON")
args = parser.parse_args()
payload = json.loads(Path(args.input).read_text(encoding="utf-8"))
if isinstance(payload, list):
rows, summary = run_battery(payload)
print(json.dumps({"rows": rows, "summary": summary}, indent=2))
else:
print(json.dumps(mycelium_v04_full_runner(payload), indent=2))