Recompute validation from the saved model, bind the decision to a policy and keep failed candidates available for inspection.
Problem
When you need this. The flow produced a model with 29 correct validation predictions. A release decision also needs a rule: which population, how many cases, what minimum result and which comparison? Choosing those requirements after inspecting each candidate makes the gate easy to move.
This local analyst-preview exercise declares a 0.5 decision threshold, at least 0.60 validation accuracy, two more correct predictions than the training-derived majority rule and exactly 48 validation observations. These are teaching requirements for this exercise, not acceptance criteria that a business has approved.
Concepts
A release gate applies a policy to an identified artifact. The gate recomputes predictions from the saved model and the selected cohort instead of trusting a separate score report. The decision records the model, cohort and policy digests.
The candidate passes this policy with 29/48 correct; the majority rule gets 27/48. A stricter alternative that requires 0.99 accuracy fails. Both results use the same validation set, so neither is a new held-out estimate.
The gate also reports the result from the cost workshop: at threshold 0.5 the model has 41 illustrative decision-cost units. The gate does not enforce a cost budget. An analyst would need to approve a budget from a measured retention experiment before release. The final test set stays reserved.
Architecture
Working code
Download the shared application and tests, unzip it and enter mlops-return-workflow/. Keep the same directory throughout the course. Create the environment once. Install the pinned platform dependencies for this workshop.
python3.12 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements-platform.txtOn PowerShell, activate with .venv\Scripts\Activate.ps1.
Continue from Test pipeline changes before producing a candidate; keep the same lab directory and outputs.
Recompute the evaluation and apply each requirement
Before it predicts, the code checks the model format and confirms that the model belongs to this cohort. Each requirement gets its own result in checks, so a failed gate shows which requirement failed.
def gate(cohort_path,model_path,policy=None):
policy=dict(POLICY if policy is None else policy)
if set(policy)!=set(POLICY) or policy['format']!=POLICY['format']:
raise ValueError('unsupported policy schema')
for field in ['threshold','minimum_accuracy']:
value=policy[field]
if type(value) not in (float,int) or not math.isfinite(value) or not 0<=value<=1:
raise ValueError('invalid policy probability')
if policy['threshold']!=0.5 or policy['final_test_allowed'] is not False:
raise ValueError('this release requires the declared threshold and untouched final test')
for key in ['minimum_gain_over_majority','required_validation_rows']:
if type(policy[key]) is not int or policy[key]<0:raise ValueError('invalid policy count')
model=w.load_model(model_path);report=w.evaluate(cohort_path,model_path)
checks={'row_count':report['rows']==policy['required_validation_rows'],
'minimum_accuracy':report['accuracy']>=policy['minimum_accuracy'],
'majority_gain':report['correct']-report['majority_correct']>=policy['minimum_gain_over_majority'],
'saved_threshold':model['settings']['threshold']==policy['threshold']}
return {'policy':policy,'policy_sha256':f.digest(policy),'model_sha256':w.sha(model_path),
'cohort_sha256':w.sha(cohort_path),'checks':checks,'passed':all(checks.values()),
'validation':report,'cost_comparison':f.cost_report(cohort_path,model_path),
'final_test_used':False,'permission':'local rollout exercise only'}The policy’s saved threshold must match the model threshold. The gate raises ValueError if the policy allows the final test, so the final test cannot become an ordinary candidate-selection set.
Retain a passing decision and a failing alternative
The command saves the declared policy’s decision to decision.json and reports the stricter comparison as a separate field. It does not change a failed candidate to make it pass.
def gate_report(root):
need(root,'ci-checks.json');result=training(root);_,source=release.candidate_files(root/'pipeline-state.json')
normal=release.gate(source/'outputs/cohort.json',source/'outputs/candidate.json')
stricter=release.gate(source/'outputs/cohort.json',source/'outputs/candidate.json',dict(release.POLICY,minimum_accuracy=0.99))
w.save(root/'decision.json',normal)
return {'declared_policy':normal['policy'],'checks':normal['checks'],'passed':normal['passed'],
'validation_correct':normal['validation']['correct'],'majority_correct':normal['validation']['majority_correct'],
'strict_alternative_passed':stricter['passed'],'cost_at_threshold_0_5':normal['cost_comparison']['candidates'][1]['total_cost'],
'permission':normal['permission'],'final_test_used':False}python course.py candidate-gate --workspace work/course{
"declared_policy": {
"format": "return-release-policy-v1",
"purpose": "local analyst-preview exercise",
"threshold": 0.5,
"minimum_accuracy": 0.6,
"minimum_gain_over_majority": 2,
"required_validation_rows": 48,
"final_test_allowed": false
},
"checks": {
"row_count": true,
"minimum_accuracy": true,
"majority_gain": true,
"saved_threshold": true
},
"passed": true,
"validation_correct": 29,
"majority_correct": 27,
"strict_alternative_passed": false,
"cost_at_threshold_0_5": 41.0,
"permission": "local rollout exercise only",
"final_test_used": false
}Every check passes under the declared policy, and strict_alternative_passed is false. The permission field limits the result to a local rollout exercise.
The output above is from a saved run in recorded-platform/; your commands write to work/. Read the actual command records. Use a fresh work directory for another attempt.
Failure cases
test_gate_recomputes_validation_and_retains_failed_candidate builds a constant-probability model. The gate marks it as failed, and its model file stays on disk.
test_policy_rejects_nonfinite_or_final_test_permission checks that the gate raises ValueError for a NaN accuracy minimum, a boolean threshold and permission to use the final test. Later, load_bundle runs the gate again when it loads a selected release.
python -m unittest -v test_platform.pyRunning and scaling the workshop
A policy file is versioned decision logic. Treat a policy change as a reviewed change with its own identity, not a retry of the same gate.
This fixture is small and synthetic. Before a real release, choose representative independent evaluation cases, decide how to treat uncertainty and set acceptance criteria for the product. A local pass does not show that the model is ready for production.
Key takeaways
- Bind a release decision to the actual model and evaluation data.
- A failed gate retains the candidate but blocks selection.
- Business costs and technical minimums must be stated separately.
Best practices
Declare the policy before evaluating a new candidate.
Reserve the final test for the intended final assessment.
Report each requirement as its own check, so a failed gate names the rule that blocked the candidate.
Keep a failed candidate and its decision on disk; block its selection instead of deleting it.
Check your understanding
Why does the gate recompute predictions instead of reading validation.json alone?
Show the explanation
The report could belong to a different model or have changed. Recomputing with bound model and cohort files checks the actual candidate being considered.
Read the explanation
A failed gate preserves the candidate and its report while preventing selection under that policy. Evaluation failure is not a reason to erase the attempt.