fix: reject unprovenanced ML artifacts
This commit is contained in:
+23
-3
@@ -5,6 +5,7 @@ import os
|
||||
import logging
|
||||
|
||||
from scoring.policy import SCORE_VERSION, assessment_for_score
|
||||
from ml.artifacts import validate_ml_artifact
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -580,16 +581,30 @@ _ML_KEY_MAP = {
|
||||
}
|
||||
|
||||
|
||||
_ml_artifact_status = {"valid": False, "errors": ["not_loaded"]}
|
||||
|
||||
|
||||
def load_ml_weights():
|
||||
"""Load ML-optimized weights from config."""
|
||||
"""Load weights only when their schema and training provenance are valid."""
|
||||
global _ml_artifact_status
|
||||
try:
|
||||
with open(ML_WEIGHTS_PATH) as f:
|
||||
data = json.load(f)
|
||||
_ml_artifact_status = validate_ml_artifact(data)
|
||||
if not _ml_artifact_status["valid"]:
|
||||
log.error("Rejected invalid ML artifact: %s", ", ".join(_ml_artifact_status["errors"]))
|
||||
return {}
|
||||
return data.get("weights", {})
|
||||
except Exception:
|
||||
except Exception as exc:
|
||||
_ml_artifact_status = {"valid": False, "errors": [f"load_error:{exc}"]}
|
||||
return {}
|
||||
|
||||
|
||||
def get_ml_artifact_status():
|
||||
"""Return the status from the most recent artifact load attempt."""
|
||||
return dict(_ml_artifact_status)
|
||||
|
||||
|
||||
def score_all_ml(metrics):
|
||||
"""Score all metrics using ML-optimized weights.
|
||||
|
||||
@@ -604,7 +619,12 @@ def score_all_ml(metrics):
|
||||
if not ml_weights:
|
||||
# Fallback to classic if no ML weights available
|
||||
classic["ml_mode"] = False
|
||||
classic["ml_error"] = "ML weights not found — run ml/optimizer.py"
|
||||
status = get_ml_artifact_status()
|
||||
if status.get("errors") and status["errors"] != ["not_loaded"]:
|
||||
classic["ml_error"] = "ML artifact invalid: " + ", ".join(status["errors"])
|
||||
else:
|
||||
classic["ml_error"] = "ML weights not found — run ml/optimizer.py"
|
||||
classic["ml_artifact"] = status
|
||||
return classic
|
||||
|
||||
results = classic["metrics"]
|
||||
|
||||
Reference in New Issue
Block a user