#!/usr/bin/env python3 """ Somatic Ledger v0.5.1-draft — Substrate-Aware Validator (v2) Handles dual-track validation: Silicon vs Biological memristor substrates. Rejects "Substrate Illusion" by routing metrics based on substrate_type field. Usage: python3 substrate_validator_v2.txt """ import json, csv, sys # Threshold specs per substrate SPECS = { "silicon": { "required_fields": ["ts_utc_ns", "power_mw", "temp_celsius"], "acoustic_kurtosis_limit": 3.5, "thermal_hard_limit": 43.0, "thermal_abort_delta": 4.0, "power_sag_fail_pct": 5.0, "sampling_min_khz": 3 }, "biological": { "required_fields": ["ts_utc_ns", "power_mw", "temp_celsius"], "impedance_drift_limit_pct": 15.0, "humidity_warning_pct": 65.0, "sampling_min_khz": 12 }, "mycelium": { "required_fields": ["ts_utc_ns", "power_mw", "temp_celsius"], "impedance_drift_limit_pct": 15.0, "humidity_warning_pct": 65.0, "sampling_min_khz": 12 } } def validate_record(row, substrate): errs, warns = [], [] if substrate not in SPECS: return [f"ERROR: Unknown substrate_type '{substrate}'"], [] spec = SPECS[substrate] # Check required fields for f in spec["required_fields"]: if f not in row or row[f] == "": errs.append(f"MISSING_FIELD: {f}") # Silicon-specific validation if substrate == "silicon": if "acoustic_kurtosis_120hz" in row and row["acoustic_kurtosis_120hz"]: try: k = float(row["acoustic_kurtosis_120hz"]) if k > spec["acoustic_kurtosis_limit"]: errs.append(f"HIGH_ENTROPY: acoustic_kurtosis_120hz={k} > {spec['acoustic_kurtosis_limit']}") except ValueError: warns.append("INVALID_NUMERIC: acoustic_kurtosis_120hz") if "temp_celsius" in row and row["temp_celsius"]: try: t = float(row["temp_celsius"]) if t > spec["thermal_hard_limit"]: errs.append(f"THERMAL_ABORT: temp_celsius={t} > {spec['thermal_hard_limit']}°C") except ValueError: warns.append("INVALID_NUMERIC: temp_celsius") if "power_sag_pct" in row and row["power_sag_pct"]: try: p = float(row["power_sag_pct"]) if p > spec["power_sag_fail_pct"]: errs.append(f"POWER_SAG_FAIL: {p}% > {spec['power_sag_fail_pct']}%") except ValueError: warns.append("INVALID_NUMERIC: power_sag_pct") # Biological/mycelium-specific validation elif substrate in ("biological", "mycelium"): if "impedance_drift_ohm" in row and row["impedance_drift_ohm"]: try: i = float(row["impedance_drift_ohm"]) if i > spec["impedance_drift_limit_pct"]: errs.append(f"IMPEDANCE_DRIFT: {i}% > {spec['impedance_drift_limit_pct']}%") except ValueError: warns.append("INVALID_NUMERIC: impedance_drift_ohm") if "relative_humidity_pct" in row and row["relative_humidity_pct"]: try: h = float(row["relative_humidity_pct"]) if h < spec["humidity_warning_pct"]: warns.append(f"DEHYDRATION_RISK: humidity={h}% < {spec['humidity_warning_pct']}%") except ValueError: warns.append("INVALID_NUMERIC: relative_humidity_pct") return errs, warns def main(): if len(sys.argv) < 2: print("Usage: python3 substrate_validator_v2.txt ") sys.exit(1) input_file = sys.argv[1] total_records = 0 failed_records = 0 try: with open(input_file, 'r') as f: reader = csv.DictReader(f) for row in reader: total_records += 1 substrate = row.get("substrate_type", "silicon") errs, warns = validate_record(row, substrate) if errs: failed_records += 1 print(f"RECORD {total_records} [SUBSTRATE:{substrate}] FAILED:") for e in errs: print(f" - {e}") if warns: print(" WARNINGS:") for w in warns: print(f" - {w}") elif warns: print(f"RECORD {total_records} [SUBSTRATE:{substrate}] PASSED WITH WARNINGS:") for w in warns: print(f" - {w}") except FileNotFoundError: print(f"ERROR: File '{input_file}' not found.") sys.exit(1) except Exception as e: print(f"ERROR: {e}") sys.exit(1) print(f" === SUMMARY ===") print(f"Total records: {total_records}") print(f"Failed: {failed_records}") if total_records > 0: print(f"Pass rate: {(1 - failed_records/total_records)*100:.1f}%") if __name__ == "__main__": main()