Apply stage transitions in a model registry

HardMLOpsPro

Why it matters Registries such as the ones in MLflow-style platforms enforce these rules so deploy jobs always know which version to serve.

A model registry tracks each model version's stage. Only some moves are allowed, and only one version may serve in Production at a time.

Write apply_transitions(events). Each event is ("register", version) or ("move", version, stage). Stages are "None", "Staging", "Production", "Archived".
- register adds a new version in stage "None"; registering an existing version is rejected.
- Allowed moves: None to Staging, Staging to Production, Staging to Archived, Production to Archived, Archived to Staging. Anything else, or a move of an unknown version, is rejected.
- When a version moves to Production, the version currently in Production (if any) moves to Archived.
Rejected events change nothing; record their indices. Return (dict of version to stage, the version in Production or None, list of rejected event indices).

Signaturedef apply_transitions(events)

Example

apply_transitions([("register", "v1"), ("move", "v1", "Staging"), ("move", "v1", "Production"), ("register", "v2"), ("move", "v2", "Production")]) gives ({'v1': 'Production', 'v2': 'None'}, 'v1', [4]).

Understand the idea

A model registry is the source of truth for which model versions exist and where each one is in its life. A typical life is: registered, tested in Staging, promoted to Production, and eventually Archived. Rules on the allowed moves stop accidents, such as pushing an untested version straight to Production.

Example: v1 goes None, Staging, Production. v2 is registered and someone tries to move it straight to Production. That move (None to Production) is not allowed, so it is rejected and v1 keeps serving. If v2 first went to Staging, promoting it would automatically archive v1.

Rules: allowed = {None to Staging, Staging to Production, Staging to Archived, Production to Archived, Archived to Staging}; at most one Production version; promotion archives the previous one.

Deployment pipelines read the Production pointer to decide what to serve, so keeping it unique is essential.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: exampleapply_transitions([('register', 'v1'), ('move', 'v1', 'Staging'), ('move', 'v1', 'Production'), ('register'...
  • Sample 2: promote replacesapply_transitions([('register', 'a'), ('register', 'b'), ('move', 'a', 'Staging'), ('move', 'a', 'Productio...