How shizheng-rlfresh/mlflow-monitor works
Purpose and Entry Points
MLflow-Monitor is a Python 3.12+ library for monitoring existing MLflow training runs without mutating their experiments. It establishes a baseline per monitored subject, evaluates comparability for later source runs, and persists monitor-owned state/results separately. Primary API: monitor.run(...) in src/mlflow_monitor/monitor.py; CLI surface: src/mlflow_monitor/cli.py; package exports: src/mlflow_monitor/__init__.py.
Monitoring Lifecycle Orchestration
The public monitor facade delegates lifecycle sequencing and state transitions to orchestration. The flow is conceptually: create/locate monitor state for subject_id → prepare a comparison context → check compatibility → return a result contract. Lifecycle concepts and control flow are represented in:
• src/mlflow_monitor/monitor.py
• src/mlflow_monitor/orchestration.py
• src/mlflow_monitor/domain/lifecycle.py
Prepare Stage: Baseline Resolution and Context
Preparation determines whether this is the subject’s first monitor run. A first run requires baseline_source_run_id; subsequent runs recover the pinned baseline from monitor state. It materializes a validated PreparedContext that isolates downstream stages from raw MLflow access and prevents invalid workflow ordering.
• src/mlflow_monitor/workflow/prepare.py
• src/mlflow_monitor/workflow/prepared_context.py
• src/mlflow_monitor/domain/reference.py
Check Stage: Contracts and Comparability
The check workflow compares source and baseline run contracts and produces pass, warn, or fail comparability outcomes. Contract construction/checking and diff models separate declared run characteristics from evaluation policy, so compatibility rules can evolve independently of MLflow persistence.
• src/mlflow_monitor/workflow/check.py
• src/mlflow_monitor/contract.py
• src/mlflow_monitor/contract_checker.py
• src/mlflow_monitor/domain/contract.py
• src/mlflow_monitor/domain/diff.py
Analyze, Findings, and Policy Extension
The active development tree includes a later analysis stage for turning diffs into findings, with identity, invariants, and policies governing materialization. Built-in recipes and finding policy provide default behavior, while recipe parsing/validation/compiler paths support configurable monitoring logic.
• src/mlflow_monitor/workflow/analyze.py
• src/mlflow_monitor/finding_policy.py
• src/mlflow_monitor/recipe_compiler.py
• src/mlflow_monitor/recipe/parser.py
• src/mlflow_monitor/domain/finding.py
Persistence Boundary: MLflow Gateways
Training runs are read through an MLflow client/gateway boundary; monitoring state and outputs are written through monitor-owned gateway operations, including result artifacts such as outputs/result.json. Protocols decouple domain/workflow code from MLflow and allow an in-memory implementation for local/test execution.
• src/mlflow_monitor/client/mlflow.py
• src/mlflow_monitor/gateway/protocol.py
• src/mlflow_monitor/gateway/mlflow.py
• src/mlflow_monitor/gateway/memory.py
• src/mlflow_monitor/gateway/models.py
Results, Errors, and External Runtime
result_contract defines the stable result returned to callers; domain timeline and invariant logic enforce valid state/finding transitions. Typed error modules mark boundary failures—gateway, recipe, workflow, timeline, and allocation—rather than leaking infrastructure details. The material external service is an MLflow tracking server/artifact store; package execution is local Python, managed with uv.
• src/mlflow_monitor/result_contract.py
• src/mlflow_monitor/domain/timeline.py
• src/mlflow_monitor/invariant.py
• src/mlflow_monitor/errors/gateway.py
• src/mlflow_monitor/errors/workflow.py
Main entry points: orchestration.py Lifecycle orchestration · analyze.py Analyze findings · prepare.py Prepare baseline · check.py Check comparability
Components 20
Public interface
- CLI
src/mlflow_monitor/cli.py - Monitor facade · Exposes monitor.run(...).
src/mlflow_monitor/monitor.py - Result contract · Stable result returned to callers.
src/mlflow_monitor/result_contract.py
Monitoring workflow
- Lifecycle orchestration
src/mlflow_monitor/orchestration.py - Prepare baseline
src/mlflow_monitor/workflow/prepare.py - Prepared context · Separates later stages from raw MLflow access.
src/mlflow_monitor/workflow/prepared_context.py - Check comparability · Produces pass, warn, or fail outcomes.
src/mlflow_monitor/workflow/check.py - Analyze findings · Materializes findings from diffs.
src/mlflow_monitor/workflow/analyze.py
Domain and policy
- Lifecycle state
src/mlflow_monitor/domain/lifecycle.py - Baseline reference
src/mlflow_monitor/domain/reference.py - Run contracts
src/mlflow_monitor/contract.py - Contract checker
src/mlflow_monitor/contract_checker.py - Diff model
src/mlflow_monitor/domain/diff.py - Recipe compiler · Parses, validates, and compiles configurable monitoring recipes.
src/mlflow_monitor/recipe_compiler.py - Finding policy
src/mlflow_monitor/finding_policy.py - Finding model
src/mlflow_monitor/domain/finding.py
Persistence boundary
- Gateway protocol · Decouples workflows from MLflow implementations.
src/mlflow_monitor/gateway/protocol.py - MLflow gateway · Reads training runs and writes monitor-owned state and result artifacts.
src/mlflow_monitor/gateway/mlflow.py - In-memory gateway · Local and test implementation.
src/mlflow_monitor/gateway/memory.py
Other components
- MLflow tracking and artifacts
Connections 22
- CLI → Monitor facade: invokes
- Monitor facade → Lifecycle orchestration: delegates lifecycle
- Lifecycle orchestration → Lifecycle state: transitions
- Lifecycle orchestration → Prepare baseline: prepares
- Prepare baseline → Prepared context: materializes
- Prepare baseline → Baseline reference: resolves pinned baseline
- Prepare baseline → Gateway protocol: loads state and runs
- Lifecycle orchestration → Check comparability: checks
- Prepared context → Check comparability: supplies context
- Check comparability → Run contracts: builds
- Check comparability → Contract checker: evaluates with
- Contract checker → Diff model: produces
- Lifecycle orchestration → Analyze findings: analyzes diffs
- Diff model → Analyze findings: feeds
- Analyze findings → Recipe compiler: uses configured rules
- Analyze findings → Finding policy: applies
- Analyze findings → Finding model: materializes
- Lifecycle orchestration → Result contract: returns
- Lifecycle orchestration → Gateway protocol: persists monitor state/results
- Gateway protocol → MLflow gateway: implemented by
- Gateway protocol → In-memory gateway: implemented by
- MLflow gateway → MLflow tracking and artifacts: reads runs; writes monitor artifacts