Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: read a run's checkpoint state in the history listing only when the answer depends on it (#16323)

Daniel Han9ea088608b39
diff --git a/studio/backend/core/training/resume.py b/studio/backend/core/training/resume.py
index ba6caf51ad9..ffa02e72f63 100644
--- a/studio/backend/core/training/resume.py
+++ b/studio/backend/core/training/resume.py
@@ -10,6 +10,7 @@ from pathlib import Path, PurePosixPath, PureWindowsPath
from typing import Optional
from utils.paths import outputs_root, resolve_output_dir
+from utils.scan_memo import memoized
def _is_foreign_absolute_path(path_value: str) -> bool:
@@ -33,7 +34,11 @@ def _is_under_outputs(path: Path) -> bool:
def has_resume_state(path_value: Optional[str]) -> bool:
if not path_value:
return False
- return get_resume_checkpoint_path(path_value) is not None
+ # can_resume_run and the blocked reason both ask per listing row.
+ return memoized(
+ ("has_resume_state", path_value),
+ lambda: get_resume_checkpoint_path(path_value) is not None,
+ )
def _checkpoint_step(path: Path) -> int:
@@ -165,7 +170,8 @@ def is_resume_checkpoint_valid(
backend: Optional[str] = None,
) -> bool:
step = _checkpoint_state(path) if path.is_dir() else None
- step_valid = step is not None and (expected_step is None or step == expected_step)
+ if step is None or (expected_step is not None and step != expected_step):
+ return False
if backend == "mlx":
valid_bundle = _valid_state_file(path / "adapters.safetensors") and _valid_state_file(
path / "optimizer_state.safetensors"
@@ -181,7 +187,7 @@ def is_resume_checkpoint_valid(
valid_bundle = _valid_state_file(path / "adapters.safetensors") and _valid_state_file(
path / "optimizer_state.safetensors"
)
- return step_valid and valid_bundle
+ return valid_bundle
def artifacts_present(path_value: Optional[str]) -> bool:
@@ -272,6 +278,7 @@ def _resource_resume_cache_key(config: dict) -> Optional[str]:
"actual_model_repo_id": config.get("actual_model_repo_id"),
"model_snapshot_path": config.get("model_snapshot_path"),
"load_in_4bit": config.get("load_in_4bit"),
+ "resume_model_load_mode": config.get("resume_model_load_mode"),
"hf_dataset": config.get("hf_dataset"),
"dataset_snapshot_path": config.get("dataset_snapshot_path"),
}
@@ -286,6 +293,16 @@ def _resource_resume_cache_key(config: dict) -> Optional[str]:
return None
+def resource_resume_blocker(config: dict) -> Optional[str]:
+ """resource_provenance_resume_blocker, answered once per scan_memo scope for runs with the same inputs."""
+ from core.training.provenance import resource_provenance_resume_blocker
+
+ key = _resource_resume_cache_key(config)
+ if key is None:
+ return resource_provenance_resume_blocker(config)
+ return memoized(("resume_blocker", key), lambda: resource_provenance_resume_blocker(config))
+
+
def can_resume_run(run: dict, *, resource_cache: Optional[dict[str, bool]] = None) -> bool:
if run.get("resumed_later"):
return False
diff --git a/studio/backend/routes/training_history.py b/studio/backend/routes/training_history.py
index 20d2203d05e..2f1c92216ed 100644
--- a/studio/backend/routes/training_history.py
+++ b/studio/backend/routes/training_history.py
@@ -72,9 +72,11 @@ def _canonical_output_dir(output_dir: Optional[str]) -> Optional[Path]:
def _preview_fields(output_dir: Optional[str], sharing_on: bool) -> dict:
"""The signed /p ref is a capability, omitted when public sharing is off so the UI hides the link."""
- ref = preview_ref(output_dir)
+ previewable = has_preview_model(output_dir)
+ # preview_ref is None without a preview model.
+ ref = preview_ref(output_dir) if previewable else None
return {
- "has_preview_model": has_preview_model(output_dir),
+ "has_preview_model": previewable,
"preview_ref": ref,
"preview_sig": sign_preview_ref(ref) if (ref and sharing_on) else None,
}
@@ -84,14 +86,14 @@ def _resume_blocked_reason(row: dict) -> Optional[str]:
"""The provenance gate's own explanation, for runs it refuses. Only consulted for a row that is
already known unresumable, so the extra work is bounded to those. Returns None when the
checkpoint is what is missing, leaving the client's existing wording in place for that case."""
- from core.training.provenance import resource_provenance_resume_blocker
- from core.training.resume import has_resume_state, training_run_config
-
+ from core.training.resume import has_resume_state, resource_resume_blocker, training_run_config
try:
+ # Gate first: the checkpoint read only changes the answer for refused runs.
+ reason = resource_resume_blocker(training_run_config(row))
# A missing checkpoint is refused for that reason, not provenance.
- if not has_resume_state(row.get("output_dir")):
+ if reason is None or not has_resume_state(row.get("output_dir")):
return None
- return resource_provenance_resume_blocker(training_run_config(row))
+ return reason
except Exception:
return None
diff --git a/studio/backend/tests/test_training_history_resume_reads.py b/studio/backend/tests/test_training_history_resume_reads.py
new file mode 100644
index 00000000000..20a08a6fcab
--- /dev/null
+++ b/studio/backend/tests/test_training_history_resume_reads.py
@@ -0,0 +1,159 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The history listing reads a run's checkpoint state only when the answer can depend on it."""
+
+import json
+from pathlib import Path
+
+import pytest
+
+from core.training import provenance as provenance_mod
+from core.training import resume as resume_mod
+from routes import training_history
+
+_REASON = "The exact model snapshot for this run is no longer available."
+
+
+def _row(output_dir, **overrides):
+ row = {
+ "id": "run-1",
+ "status": "completed",
+ "model_name": "m",
+ "dataset_name": "d",
+ "started_at": "t",
+ "output_dir": str(output_dir),
+ "config_json": json.dumps({}),
+ "final_step": 100,
+ "total_steps": 100,
+ }
+ row.update(overrides)
+ return row
+
+
+def reads(monkeypatch, tmp_path):
+ monkeypatch.setattr(resume_mod, "outputs_root", lambda: tmp_path)
+ monkeypatch.setattr(resume_mod, "resolve_output_dir", lambda value: Path(value))
+ calls: list = []
+ real = resume_mod.get_resume_checkpoint_path
+
+ def counted(path_value, expected_step = None):
+ calls.append(path_value)
+ return real(path_value, expected_step)
+
+ monkeypatch.setattr(resume_mod, "get_resume_checkpoint_path", counted)
+ return calls
+
+
+def test_a_completed_run_the_gate_does_not_refuse_reads_no_checkpoint(reads, tmp_path, monkeypatch):
+ monkeypatch.setattr(provenance_mod, "resource_provenance_resume_blocker", lambda config: None)
+ (tmp_path / "run-1").mkdir()
+
+ summary = training_history._summaries_from_rows([_row(tmp_path / "run-1")], False)[0]
+
+ assert summary.can_resume is False
+ assert summary.resume_blocked_reason is None
+ assert reads == []
+
+
[email protected]("has_state", [True, False])
+def test_a_refused_run_reads_its_checkpoint_once(reads, tmp_path, monkeypatch, has_state):
+ monkeypatch.setattr(
+ provenance_mod, "resource_provenance_resume_blocker", lambda config: _REASON
+ )
+ run = tmp_path / "run-1"
+ run.mkdir()
+ monkeypatch.setattr(
+ resume_mod, "is_resume_checkpoint_valid", lambda path, step = None, backend = None: has_state
+ )
+
+ summary = training_history._summaries_from_rows(
+ [_row(run, status = "stopped", final_step = 50)], False
+ )[0]
+
+ assert summary.can_resume is False
+ assert summary.resume_blocked_reason == (_REASON if has_state else None)
+ assert reads == [str(run)]
+
+
+def test_state_files_are_not_parsed_without_a_valid_trainer_step(tmp_path, monkeypatch):
+ parsed: list = []
+ monkeypatch.setattr(
+ resume_mod, "_valid_state_file", lambda path, require_tensor = True: parsed.append(path)
+ )
+ monkeypatch.setattr(resume_mod, "_has_model_state", lambda path: parsed.append(path) or True)
+ checkpoint = tmp_path / "checkpoint-30"
+ checkpoint.mkdir()
+
+ assert resume_mod.is_resume_checkpoint_valid(checkpoint) is False
+ (checkpoint / "trainer_state.json").write_text(json.dumps({"global_step": 31}))
+ assert resume_mod.is_resume_checkpoint_valid(checkpoint) is False
+ (checkpoint / "trainer_state.json").write_text(json.dumps({"global_step": 30}))
+ assert resume_mod.is_resume_checkpoint_valid(checkpoint, expected_step = 29) is False
+ assert parsed == []
+
+
+def test_no_preview_model_skips_the_preview_ref_lookup(monkeypatch, tmp_path):
+ monkeypatch.setattr(training_history, "has_preview_model", lambda output_dir: False)
+
+ def unexpected(output_dir):
+ raise AssertionError("preview_ref is None whenever there is no preview model")
+
+ monkeypatch.setattr(training_history, "preview_ref", unexpected)
+
+ assert training_history._preview_fields(str(tmp_path), True) == {
+ "has_preview_model": False,
+ "preview_ref": None,
+ "preview_sig": None,
+ }
+
+
+def test_the_provenance_gate_answers_once_per_listing_for_runs_that_share_its_inputs(
+ reads, tmp_path, monkeypatch
+):
+ asked: list = []
+
+ def blocker(config):
+ asked.append(config.get("resume_model_load_mode"))
+ return None
+
+ monkeypatch.setattr(provenance_mod, "resource_provenance_resume_blocker", blocker)
+ rows = [_row(tmp_path / f"run-{i}", id = f"run-{i}") for i in range(3)]
+ rows.append(
+ _row(
+ tmp_path / "run-3",
+ id = "run-3",
+ config_json = json.dumps({"resume_model_load_mode": "runtime_4bit"}),
+ )
+ )
+
+ summaries = training_history._summaries_from_rows(rows, False)
+
+ assert [s.resume_blocked_reason for s in summaries] == [None] * 4
+ assert asked == [None, "runtime_4bit"]
+ training_history._summary_from_row(rows[0], False)
+ training_history._summary_from_row(rows[0], False)
+ assert asked == [None, "runtime_4bit", None, None]
+
+
+def test_runs_differing_only_in_resume_load_mode_get_their_own_resume_answer(monkeypatch):
+ monkeypatch.setattr(resume_mod, "has_resume_state", lambda output_dir: True)
+ monkeypatch.setattr(
+ provenance_mod,
+ "resource_provenance_resume_blocker",
+ lambda config: _REASON if config.get("resume_model_load_mode") == "runtime_4bit" else None,
+ )
+ rows = [
+ _row(
+ f"/tmp/run-{mode}",
+ id = f"run-{mode}",
+ status = "stopped",
+ final_step = 50,
+ config_json = json.dumps({"resume_model_load_mode": mode}),
+ )
+ for mode in ("unquantized", "runtime_4bit")
+ ]
+ cache: dict = {}
+
+ assert [resume_mod.can_resume_run(row, resource_cache = cache) for row in rows] == [True, False]