training: every run records the code's git commit; PROD logs only models defined in a module

This commit is contained in:
2026-11-04 00:49:19 +00:00
parent 0489475b86
commit f08b249eae
3 changed files with 94 additions and 7 deletions
+63 -4
View File
@@ -1,20 +1,24 @@
"""What this module covers: logging a model in a training run (`TrainingRun.log_model`, `model_sha`) and
choosing the version to use (`registry.resolve`).
"""What this module covers: logging a model in a training run (`TrainingRun.log_model`, `model_sha`), the
code version every run records (`code_version`), and choosing the version to use (`registry.resolve`).
Fixtures: `mlflow_tracking` (from `conftest.py`), a SQLite-backed MLflow per test; `production_ready`
(from `conftest.py`), which registers a PROD version that passes every promotion check. `Constant` is a
pyfunc that returns one fixed probability per row; `_train` registers a dev version, with or without a
logged model.
logged model; `_git` asks git about this repository, to compare with what the run recorded.
"""
import subprocess
from pathlib import Path
import mlflow.pyfunc
import pandas as pd
import pytest
from core.governance.enums import Environment
from mlflow import MlflowClient
from models import training
from models.registry import MODEL_SHA_TAG, registry
from models.training import TrainingRun
from models.training import GIT_COMMIT_ENV, GIT_COMMIT_TAG, GIT_DIRTY_TAG, TrainingRun
class Constant(mlflow.pyfunc.PythonModel):
@@ -122,3 +126,58 @@ def test_an_explicit_version_is_returned_with_its_status(production_ready):
assert registry.resolve(domain, scenario, first).status == "produccion"
with pytest.raises(LookupError):
registry.resolve(domain, scenario, "99")
def _git(*args: str) -> str:
return subprocess.run(["git", "-C", str(Path(training.__file__).parent), *args], capture_output=True, text=True,
check=True).stdout.strip()
def test_every_run_records_the_commit_of_the_code(mlflow_tracking):
"""Pins down: a run is tagged with the git commit of the `models` repository (MLflow's own tag name) and
whether tracked files had uncommitted changes; untracked files (notebooks, data) don't count.
Fails if the tags are dropped or renamed, or the dirty flag counts untracked files.
"""
run = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV)
tags = MlflowClient().get_run(run.run_id).data.tags
assert tags[GIT_COMMIT_TAG] == _git("rev-parse", "HEAD")
assert tags[GIT_DIRTY_TAG] == str(bool(_git("status", "--porcelain", "--untracked-files=no"))).lower()
def test_without_a_repository_the_commit_comes_from_the_environment(mlflow_tracking, monkeypatch):
"""Pins down: where git or the repository is missing (a delivered image), the commit is read from
`MODELS_GIT_COMMIT`, with no dirty flag; with neither, the run records no commit rather than a guess.
Fails if a missing repository raises, or a commit is invented.
"""
def no_git(*args, **kwargs):
raise FileNotFoundError("git")
monkeypatch.setattr(training.subprocess, "run", no_git)
monkeypatch.setenv(GIT_COMMIT_ENV, "abc123")
tags = MlflowClient().get_run(TrainingRun(domain="cacu", scenario="tamizaje",
environment=Environment.DEV).run_id).data.tags
assert tags[GIT_COMMIT_TAG] == "abc123" and GIT_DIRTY_TAG not in tags
monkeypatch.delenv(GIT_COMMIT_ENV)
tags = MlflowClient().get_run(TrainingRun(domain="cacu", scenario="tamizaje",
environment=Environment.DEV).run_id).data.tags
assert GIT_COMMIT_TAG not in tags
def test_a_prod_run_refuses_a_model_defined_in_a_notebook(mlflow_tracking):
"""Pins down: a PROD run only logs a model whose class lives in a module; one defined in a notebook or
script (`__main__`) is refused, because its code has no commit to trace. A dev run accepts it.
Fails if the check is dropped, or starts refusing dev runs or module classes.
"""
InNotebook = type("InNotebook", (Constant,), {"__module__": "__main__"})
prod = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.PROD, cohorte_armado_id=1)
with pytest.raises(ValueError, match="defined in a notebook"):
prod.log_model(InNotebook(0.25))
prod.log_model(Constant(0.25))
TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV).log_model(InNotebook(0.25))