Skip to content

fix(ml): set last_train_time from model's embedded training_timestamp on load#4077

Merged
springfall2008 merged 3 commits into
mainfrom
fix/last_train_time
Jun 16, 2026
Merged

fix(ml): set last_train_time from model's embedded training_timestamp on load#4077
springfall2008 merged 3 commits into
mainfrom
fix/last_train_time

Conversation

@springfall2008

Copy link
Copy Markdown
Owner

Summary

  • Fixes the same startup-retraining bug as Fix unnecessary ML retraining on startup by setting last_train_time #4072, using the correct data source
  • After LoadPredictor.load() succeeds, predictor.training_timestamp is already populated from the .npz metadata — use that directly instead of os.path.getmtime()
  • If training_timestamp is None (model predates that field), last_train_time stays None and a retrain correctly triggers as a safe fallback

Why not the approach in #4072

PR #4072 reads os.path.getmtime() to approximate the training time, but filesystem mtime is unreliable — it resets on file copy, backup restore, touch, or a filesystem with lower mtime resolution. The actual training timestamp is already serialised inside the .npz file and is loaded by LoadPredictor.load() on every startup, so there is no need to fall back to the filesystem.

The fix is a single line with no try/except needed.

Test plan

  • Start Predbat with an existing cached predbat_ml_model.npz and confirm no immediate retraining occurs in the logs
  • Delete the cached model and confirm retraining is still triggered on first startup
  • Confirm last_trained in the component status reflects the model's embedded training time, not the file's mtime

🤖 Generated with Claude Code

… on load

When a cached model is loaded at startup, last_train_time was left None,
causing the training-age check to treat the model as infinitely old and
force an immediate retrain. Use predictor.training_timestamp (already
restored from the .npz metadata by LoadPredictor.load) rather than the
filesystem mtime, which can be wrong after file copies or backup restores.

If training_timestamp is None (pre-dating that field), last_train_time
stays None and a retrain correctly triggers as a safe fallback.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 16, 2026 19:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes unnecessary ML retraining on startup by setting LoadMLComponent.last_train_time from the model’s embedded training_timestamp immediately after a successful load and validation, rather than leaving it unset (which makes the component assume the model is “old” on the next loop).

Changes:

  • Set last_train_time to self.predictor.training_timestamp when a cached model loads successfully and passes validation.

@springfall2008 springfall2008 merged commit 3890f80 into main Jun 16, 2026
1 check passed
@springfall2008 springfall2008 deleted the fix/last_train_time branch June 16, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants