train.retrain¶
Retraining workflow: cadence scheduling, reproducible pipeline, regression gates.
Overview¶
The retrain module provides automated retraining with safety gates:
- Cadence checks determine when a global retrain or calibrator update is due.
- Dataset hashing ensures each model can be traced to its exact training data.
- Regression gates prevent deploying a model that is worse than the champion.
Configuration¶
Retrain configuration lives in configs/retrain.yaml (versioned in git):
| Field | Type | Default | Description |
|---|---|---|---|
global_retrain_cadence_days |
int | 7 | Days between global model retrains |
calibrator_update_cadence_days |
int | 7 | Days between calibrator updates |
data_lookback_days |
int | 30 | Days of data to include in training |
regression_tolerance |
float | 0.02 | Max allowed macro-F1 drop vs champion |
require_baseline_improvement |
bool | true | Challenger must beat rule baseline |
auto_promote |
bool | false | Auto-promote when all gates pass |
train_params.num_boost_round |
int | 100 | LightGBM boosting rounds |
train_params.class_weight |
str | balanced | Class weight strategy |
Promotion Gates¶
Both candidate gates and comparative gates must pass for promotion.
Candidate Gates (check_candidate_gates)¶
Hard gates evaluated on the challenger model in isolation (no champion needed):
- breakidle_precision — BreakIdle precision >= 0.95
- no_class_below_50_precision — every class precision >= 0.50
- challenger_acceptance — all acceptance checks from
docs/guide/acceptance.mdpass
Comparative Gates (check_regression_gates)¶
Regression check comparing challenger against the champion:
- macro_f1_no_regression — challenger macro-F1 within
regression_toleranceof champion
Comparative gates are only evaluated when a champion exists. If no champion is available, only candidate gates apply.
CLI¶
# Check if retraining is due
taskclf train check-retrain --models-dir models/
# Run full retrain pipeline
taskclf train retrain --config configs/retrain.yaml --force --synthetic
# Dry run (evaluate without promoting)
taskclf train retrain --config configs/retrain.yaml --dry-run --synthetic
taskclf.train.retrain
¶
Retraining workflow: cadence scheduling, reproducible pipeline, regression gates.
Implements TODO 11 from the labelling roadmap:
- Cadence — :func:
check_retrain_due/ :func:check_calibrator_update_duedetermine whether a global retrain or per-user calibrator update should run. - Reproducibility — :func:
compute_dataset_hashproduces a deterministic SHA-256 of the training data so each model bundle can be traced back to its exact dataset. :class:RetrainConfigis loadable from a versioned YAML file. - Regression gates — :func:
check_regression_gatescompares a challenger model against the current champion on key metrics (macro-F1, BreakIdle precision, per-class precision) and only promotes if no regression exceeds the configured tolerance. - Pipeline — :func:
run_retrain_pipelineorchestrates the full flow from data loading through training, evaluation, gate checking, and promotion.
TrainParams
¶
Bases: BaseModel
Hyperparameters forwarded to :func:~taskclf.train.lgbm.train_lgbm.
Source code in src/taskclf/train/retrain.py
RetrainConfig
¶
Bases: BaseModel
Cadence, gate thresholds, and training parameters for the retrain loop.
Source code in src/taskclf/train/retrain.py
DatasetSnapshot
¶
Bases: BaseModel
Immutable record of the dataset used for a training run.
Source code in src/taskclf/train/retrain.py
RegressionGate
¶
RegressionResult
¶
RetrainResult
¶
Bases: BaseModel
Output of :func:run_retrain_pipeline.
Source code in src/taskclf/train/retrain.py
load_retrain_config(path)
¶
Load a :class:RetrainConfig from a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to a YAML config file whose keys match
:class: |
required |
Returns:
| Type | Description |
|---|---|
RetrainConfig
|
A validated config instance. |
Source code in src/taskclf/train/retrain.py
compute_dataset_hash(features_df, labels)
¶
Compute a deterministic SHA-256 of the training data.
The hash is built from a canonical JSON representation of:
- Sorted feature column names
- Feature values serialized row-by-row (sorted by primary key)
- Label spans serialized in chronological order
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features_df
|
DataFrame
|
Feature DataFrame (pre-projection). |
required |
labels
|
Sequence[LabelSpan]
|
Label spans used for projection. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A hex-encoded SHA-256 digest (first 16 characters). |
Source code in src/taskclf/train/retrain.py
find_latest_model(models_dir)
¶
Return the path to the most recently created model bundle.
Scans models_dir for subdirectories containing metadata.json
and picks the one with the latest created_at timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models_dir
|
Path
|
Base directory containing model run folders. |
required |
Returns:
| Type | Description |
|---|---|
Path | None
|
Path to the latest run directory, or |
Source code in src/taskclf/train/retrain.py
check_retrain_due(models_dir, cadence_days=DEFAULT_RETRAIN_CADENCE_DAYS)
¶
Check whether a global retrain is due based on the last model's age.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models_dir
|
Path
|
Base directory containing model run folders. |
required |
cadence_days
|
int
|
Maximum age in days before a retrain is needed. |
DEFAULT_RETRAIN_CADENCE_DAYS
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/taskclf/train/retrain.py
check_calibrator_update_due(calibrator_store_dir, cadence_days=DEFAULT_CALIBRATOR_UPDATE_CADENCE_DAYS)
¶
Check whether a calibrator store update is due.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calibrator_store_dir
|
Path
|
Directory containing the calibrator store
(expected to have a |
required |
cadence_days
|
int
|
Maximum age in days before an update is needed. |
DEFAULT_CALIBRATOR_UPDATE_CADENCE_DAYS
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/taskclf/train/retrain.py
check_candidate_gates(challenger_report)
¶
Check candidate-only hard gates (no champion needed).
These gates evaluate the challenger model in isolation:
- breakidle_precision — BreakIdle precision >= 0.95.
- no_class_below_50_precision — no class may have precision < 0.50.
- challenger_acceptance — all of the challenger's own acceptance checks must pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
challenger_report
|
EvaluationReport
|
Evaluation report of the newly trained model. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
RegressionResult
|
class: |
Source code in src/taskclf/train/retrain.py
check_regression_gates(champion_report, challenger_report, config)
¶
Comparative regression check: challenger vs champion on macro-F1.
This function is purely comparative — it checks only whether the
challenger regresses relative to the champion. Candidate-only hard
gates (BreakIdle precision, per-class precision floor, acceptance
checks) are handled separately by :func:check_candidate_gates.
Gates:
- macro_f1_no_regression — challenger macro-F1 must be within
regression_toleranceof the champion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
champion_report
|
EvaluationReport
|
Evaluation report of the current deployed model. |
required |
challenger_report
|
EvaluationReport
|
Evaluation report of the newly trained model. |
required |
config
|
RetrainConfig
|
Retrain configuration with tolerance settings. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
RegressionResult
|
class: |
Source code in src/taskclf/train/retrain.py
run_retrain_pipeline(config, features_df, label_spans, *, models_dir=Path(DEFAULT_MODELS_DIR), out_dir=Path('artifacts'), force=False, dry_run=False, holdout_user_fraction=0.0, reject_threshold=DEFAULT_REJECT_THRESHOLD, data_provenance='real')
¶
Run the full retrain pipeline: train, evaluate, gate, promote.
Steps:
- Optionally check cadence (skipped if force).
- Compute dataset snapshot hash.
- Project labels, split, and train a challenger model.
- Evaluate the challenger.
- If a champion exists, run regression gates.
- Promote the challenger if all gates pass (skipped if dry_run).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
RetrainConfig
|
Retrain configuration. |
required |
features_df
|
DataFrame
|
Feature DataFrame covering the training range. |
required |
label_spans
|
Sequence[LabelSpan]
|
Label spans for projection. |
required |
models_dir
|
Path
|
Base directory for model bundles. |
Path(DEFAULT_MODELS_DIR)
|
out_dir
|
Path
|
Directory for evaluation artifacts. |
Path('artifacts')
|
force
|
bool
|
Skip cadence check. |
False
|
dry_run
|
bool
|
Evaluate without promoting. |
False
|
holdout_user_fraction
|
float
|
Fraction of users to hold out for test. |
0.0
|
reject_threshold
|
float
|
Reject threshold for evaluation. |
DEFAULT_REJECT_THRESHOLD
|
data_provenance
|
Literal['real', 'synthetic', 'mixed']
|
Origin of the training data. |
'real'
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
RetrainResult
|
class: |
Source code in src/taskclf/train/retrain.py
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 | |