# shizheng-rlfresh/mlflow-monitor architecture

> Architecture diagram of the GitHub repository shizheng-rlfresh/mlflow-monitor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shizheng-rlfresh/mlflow-monitor
- Repository: https://github.com/shizheng-rlfresh/mlflow-monitor

## Overview

#### 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: [`src/mlflow_monitor/orchestration.py`](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/orchestration.py) (Lifecycle orchestration), [`src/mlflow_monitor/workflow/analyze.py`](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/workflow/analyze.py) (Analyze findings), [`src/mlflow_monitor/workflow/prepare.py`](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/workflow/prepare.py) (Prepare baseline), [`src/mlflow_monitor/workflow/check.py`](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/workflow/check.py) (Check comparability)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Public interface"]
  node_cli["CLI<br/>command interface<br/>[cli.py]"]
  node_monitor["Monitor facade<br/>public API<br/>[monitor.py]"]
  node_result_contract["Result contract<br/>public result<br/>[result_contract.py]"]
end

subgraph group_workflow["Monitoring workflow"]
  node_orchestration["Lifecycle orchestration<br/>orchestrator<br/>[orchestration.py]"]
  node_prepare["Prepare baseline<br/>workflow stage<br/>[prepare.py]"]
  node_prepared_context["Prepared context<br/>validated context"]
  node_check["Check comparability<br/>workflow stage<br/>[check.py]"]
  node_analyze["Analyze findings<br/>workflow stage<br/>[analyze.py]"]
end

subgraph group_domain["Domain and policy"]
  node_lifecycle["Lifecycle state<br/>domain model<br/>[lifecycle.py]"]
  node_reference["Baseline reference<br/>domain model<br/>[reference.py]"]
  node_contracts["Run contracts<br/>contract layer<br/>[contract.py]"]
  node_contract_checker["Contract checker<br/>comparison policy"]
  node_diff["Diff model<br/>domain model<br/>[diff.py]"]
  node_recipes["Recipe compiler<br/>policy extension<br/>[recipe_compiler.py]"]
  node_finding_policy["Finding policy<br/>[finding_policy.py]"]
  node_finding["Finding model<br/>domain model<br/>[finding.py]"]
end

subgraph group_persistence["Persistence boundary"]
  node_gateway_protocol["Gateway protocol<br/>persistence port<br/>[protocol.py]"]
  node_mlflow_gateway["MLflow gateway<br/>gateway adapter<br/>[mlflow.py]"]
  node_memory_gateway["In-memory gateway<br/>gateway adapter<br/>[memory.py]"]
end

node_mlflow_service[("MLflow tracking and artifacts<br/>external service")]

node_cli -->|"invokes"| node_monitor
node_monitor -->|"delegates lifecycle"| node_orchestration
node_orchestration -->|"transitions"| node_lifecycle
node_orchestration -->|"prepares"| node_prepare
node_prepare -->|"materializes"| node_prepared_context
node_prepare -->|"resolves pinned baseline"| node_reference
node_prepare -->|"loads state and runs"| node_gateway_protocol
node_orchestration -->|"checks"| node_check
node_prepared_context -->|"supplies context"| node_check
node_check -->|"builds"| node_contracts
node_check -->|"evaluates with"| node_contract_checker
node_contract_checker -->|"produces"| node_diff
node_orchestration -->|"analyzes diffs"| node_analyze
node_diff -->|"feeds"| node_analyze
node_analyze -->|"uses configured rules"| node_recipes
node_analyze -->|"applies"| node_finding_policy
node_analyze -->|"materializes"| node_finding
node_orchestration -->|"returns"| node_result_contract
node_orchestration -->|"persists monitor state/results"| node_gateway_protocol
node_gateway_protocol -.->|"implemented by"| node_mlflow_gateway
node_gateway_protocol -.->|"implemented by"| node_memory_gateway
node_mlflow_gateway -->|"reads runs; writes monitor artifacts"| node_mlflow_service

click node_cli "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/cli.py"
click node_monitor "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/monitor.py"
click node_orchestration "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/orchestration.py"
click node_lifecycle "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/domain/lifecycle.py"
click node_prepare "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/workflow/prepare.py"
click node_prepared_context "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/workflow/prepared_context.py"
click node_reference "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/domain/reference.py"
click node_check "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/workflow/check.py"
click node_contracts "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/contract.py"
click node_contract_checker "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/contract_checker.py"
click node_diff "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/domain/diff.py"
click node_analyze "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/workflow/analyze.py"
click node_recipes "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/recipe_compiler.py"
click node_finding_policy "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/finding_policy.py"
click node_finding "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/domain/finding.py"
click node_result_contract "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/result_contract.py"
click node_gateway_protocol "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/gateway/protocol.py"
click node_mlflow_gateway "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/gateway/mlflow.py"
click node_memory_gateway "https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/gateway/memory.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_monitor,node_result_contract toneBlue
class node_orchestration,node_prepare,node_prepared_context,node_check,node_analyze toneAmber
class node_lifecycle,node_reference,node_contracts,node_contract_checker,node_diff,node_recipes,node_finding_policy,node_finding toneMint
class node_gateway_protocol,node_mlflow_gateway,node_memory_gateway toneRose
class node_mlflow_service toneNeutral
```

## Components

### Public interface

- [CLI](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/cli.py): command interface · `src/mlflow_monitor/cli.py`
- [Monitor facade](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/monitor.py): public API · Exposes monitor.run(...). · `src/mlflow_monitor/monitor.py`
- [Result contract](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/result_contract.py): public result · Stable result returned to callers. · `src/mlflow_monitor/result_contract.py`

### Monitoring workflow

- [Lifecycle orchestration](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/orchestration.py): orchestrator · `src/mlflow_monitor/orchestration.py`
- [Prepare baseline](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/workflow/prepare.py): workflow stage · `src/mlflow_monitor/workflow/prepare.py`
- [Prepared context](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/workflow/prepared_context.py): validated context · Separates later stages from raw MLflow access. · `src/mlflow_monitor/workflow/prepared_context.py`
- [Check comparability](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/workflow/check.py): workflow stage · Produces pass, warn, or fail outcomes. · `src/mlflow_monitor/workflow/check.py`
- [Analyze findings](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/workflow/analyze.py): workflow stage · Materializes findings from diffs. · `src/mlflow_monitor/workflow/analyze.py`

### Domain and policy

- [Lifecycle state](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/domain/lifecycle.py): domain model · `src/mlflow_monitor/domain/lifecycle.py`
- [Baseline reference](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/domain/reference.py): domain model · `src/mlflow_monitor/domain/reference.py`
- [Run contracts](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/contract.py): contract layer · `src/mlflow_monitor/contract.py`
- [Contract checker](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/contract_checker.py): comparison policy · `src/mlflow_monitor/contract_checker.py`
- [Diff model](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/domain/diff.py): domain model · `src/mlflow_monitor/domain/diff.py`
- [Recipe compiler](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/recipe_compiler.py): policy extension · Parses, validates, and compiles configurable monitoring recipes. · `src/mlflow_monitor/recipe_compiler.py`
- [Finding policy](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/finding_policy.py): policy · `src/mlflow_monitor/finding_policy.py`
- [Finding model](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/domain/finding.py): domain model · `src/mlflow_monitor/domain/finding.py`

### Persistence boundary

- [Gateway protocol](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/gateway/protocol.py): persistence port · Decouples workflows from MLflow implementations. · `src/mlflow_monitor/gateway/protocol.py`
- [MLflow gateway](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/gateway/mlflow.py): gateway adapter · Reads training runs and writes monitor-owned state and result artifacts. · `src/mlflow_monitor/gateway/mlflow.py`
- [In-memory gateway](https://github.com/shizheng-rlfresh/mlflow-monitor/blob/main/src/mlflow_monitor/gateway/memory.py): gateway adapter · Local and test implementation. · `src/mlflow_monitor/gateway/memory.py`

### Other components

- **MLflow tracking and artifacts**: external service

## Connections

- 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
