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 the run history once per checkpoint listing (#15873)

Daniel Han0edfda6e1e01
diff --git a/studio/backend/tests/test_checkpoints_scan.py b/studio/backend/tests/test_checkpoints_scan.py
index 331d05340ad..bb7156271de 100644
--- a/studio/backend/tests/test_checkpoints_scan.py
+++ b/studio/backend/tests/test_checkpoints_scan.py
@@ -391,3 +391,95 @@ def test_scan_checkpoints_reads_training_loss_past_an_eval_entry(tmp_path, monke
models = checkpoints_module.scan_checkpoints(outputs_dir = str(outputs_dir))
assert models[0][1] == [("checkpoint-50", str(checkpoint_dir), 0.25)]
+
+
+def test_scan_checkpoints_reads_history_once_for_many_fallback_runs(tmp_path, monkeypatch):
+ # Full fine-tunes save config.json without _name_or_path, so every such run falls back to history.
+ outputs_dir = _make_outputs_dir(tmp_path, monkeypatch)
+ db_path = tmp_path / "studio.db"
+ _setup_training_runs_table(db_path)
+ expected = {}
+ conn = _make_history_connection(db_path)
+ try:
+ for index in range(12):
+ model = f"org/model-{index % 3}"
+ project = "Support" if index % 2 else None
+ run_name = build_default_output_dir_name(model, project, timestamp = 1771227800 + index)
+ run_dir = outputs_dir / run_name
+ run_dir.mkdir()
+ (run_dir / "config.json").write_text("{}")
+ expected[run_name] = model
+ conn.execute(
+ "INSERT INTO training_runs (id, model_name, config_json, output_dir, started_at)"
+ " VALUES (?, ?, ?, ?, ?)",
+ (
+ f"run-{index}",
+ model,
+ json.dumps({"project_name": project}),
+ str(run_dir.resolve()) if index % 3 == 0 else None,
+ f"2026-04-09T00:00:{index:02d}Z",
+ ),
+ )
+ conn.commit()
+ finally:
+ conn.close()
+
+ opened = []
+
+ def connect():
+ conn = _make_history_connection(db_path)
+ opened.append(conn)
+ return conn
+
+ parsed = []
+ real_loads = json.loads
+
+ class _CountingJson:
+ JSONDecodeError = json.JSONDecodeError
+ load = staticmethod(json.load)
+
+ @staticmethod
+ def loads(*args, **kwargs):
+ parsed.append(args[0] if args else None)
+ return real_loads(*args, **kwargs)
+
+ monkeypatch.setattr(checkpoints_module, "get_connection", connect)
+ monkeypatch.setattr(checkpoints_module, "json", _CountingJson)
+
+ models = checkpoints_module.scan_checkpoints(outputs_dir = str(outputs_dir))
+
+ assert {name: metadata["base_model"] for name, _, metadata in models} == expected
+ assert len(opened) == 1
+ assert len(parsed) <= 12 + len(models)
+
+
+def test_scan_checkpoints_tied_exact_match_follows_sqlite_order(tmp_path, monkeypatch):
+ outputs_dir = _make_outputs_dir(tmp_path, monkeypatch)
+ run_dir = outputs_dir / "custom-run"
+ run_dir.mkdir()
+ (run_dir / "config.json").write_text("{}")
+ db_path = tmp_path / "studio.db"
+ _setup_training_runs_table(db_path)
+ conn = _make_history_connection(db_path)
+ try:
+ for run_id, model in (("a", "first/base"), ("b", ""), ("c", "second/base")):
+ conn.execute(
+ "INSERT INTO training_runs (id, model_name, config_json, output_dir, started_at)"
+ " VALUES (?, ?, ?, ?, ?)",
+ (run_id, model, "{}", str(run_dir.resolve()), "2026-04-09T00:00:00Z"),
+ )
+ conn.commit()
+ want = conn.execute(
+ "SELECT model_name FROM training_runs WHERE output_dir IN (?, ?) AND model_name != ''"
+ " ORDER BY started_at DESC",
+ (str(run_dir.resolve()), str(run_dir)),
+ ).fetchone()["model_name"]
+ finally:
+ conn.close()
+ monkeypatch.setattr(
+ checkpoints_module, "get_connection", lambda: _make_history_connection(db_path)
+ )
+
+ models = checkpoints_module.scan_checkpoints(outputs_dir = str(outputs_dir))
+
+ assert models[0][2]["base_model"] == want
diff --git a/studio/backend/utils/models/checkpoints.py b/studio/backend/utils/models/checkpoints.py
index 66a32a0b2a3..add1f19a898 100644
--- a/studio/backend/utils/models/checkpoints.py
+++ b/studio/backend/utils/models/checkpoints.py
@@ -41,90 +41,100 @@ def _checkpoint_sort_key(checkpoint_path: Path) -> tuple[int, int, str]:
return (1, 0, str(checkpoint_path))
-def _infer_base_model_from_history(checkpoint_dir: Path) -> Optional[str]:
- """Best-effort base-model lookup using persisted Unsloth run metadata."""
- checkpoint_name = checkpoint_dir.name
- resolved_checkpoint_dir = str(checkpoint_dir.resolve())
+class _History:
+ def __init__(self) -> None:
+ self._conn = None
+ self._dirs: Optional[list] = None
+ self._pairs: Optional[list] = None
+
+ def _fetch(self, sql: str) -> list:
+ # A failed open or query is retried by the next run, as when each run opened its own connection.
+ if self._conn is None:
+ self._conn = get_connection()
+ return self._conn.execute(sql).fetchall()
+
+ def dir_rows(self) -> list:
+ if self._dirs is None:
+ self._dirs = [
+ (row["model_name"], row["output_dir"])
+ for row in self._fetch(
+ "SELECT model_name, output_dir FROM training_runs"
+ " WHERE output_dir IS NOT NULL ORDER BY started_at DESC"
+ )
+ ]
+ return self._dirs
+
+ def model_projects(self) -> list:
+ """Distinct (model_name, project_name), in order of their newest run."""
+ if self._pairs is None:
+ pairs = {}
+ for row in self._fetch(
+ "SELECT model_name, config_json FROM training_runs ORDER BY started_at DESC"
+ ):
+ if not row["model_name"]:
+ continue
+ project_name = None
+ if row["config_json"]:
+ try:
+ project_name = extract_project_name(json.loads(row["config_json"]))
+ except Exception:
+ project_name = None
+ pairs.setdefault((row["model_name"], project_name), None)
+ self._pairs = list(pairs)
+ return self._pairs
+
+ def close(self) -> None:
+ conn, self._conn = self._conn, None
+ if conn is not None:
+ try:
+ conn.close()
+ except Exception:
+ pass
+
+def _infer_base_model_from_history(
+ checkpoint_dir: Path, history: Optional[_History] = None
+) -> Optional[str]:
+ """Best-effort base-model lookup using persisted Unsloth run metadata."""
+ own_history = history is None
+ if own_history:
+ history = _History()
try:
- conn = get_connection()
+ return _lookup_base_model(checkpoint_dir, history)
except Exception:
return None
+ finally:
+ if own_history:
+ history.close()
- try:
- exact_rows = conn.execute(
- """
- SELECT model_name
- FROM training_runs
- WHERE output_dir IN (?, ?)
- ORDER BY started_at DESC
- """,
- (
- resolved_checkpoint_dir,
- str(checkpoint_dir),
- ),
- ).fetchall()
- for row in exact_rows:
- model_name = row["model_name"]
- if model_name:
- return model_name
-
- suffix_rows = conn.execute(
- """
- SELECT model_name, output_dir
- FROM training_runs
- WHERE output_dir IS NOT NULL
- ORDER BY started_at DESC
- """
- ).fetchall()
- for row in suffix_rows:
- output_dir = str(row["output_dir"] or "").rstrip("/\\")
- if not (
- output_dir.endswith(f"/{checkpoint_name}")
- or output_dir.endswith(f"\\{checkpoint_name}")
- ):
- continue
- model_name = row["model_name"]
- if model_name:
- return model_name
-
- parts = checkpoint_name.rsplit("_", 1)
- if len(parts) != 2 or not parts[1].isdigit():
- return None
-
- timestamp = int(parts[1])
- generated_rows = conn.execute(
- """
- SELECT model_name, config_json
- FROM training_runs
- ORDER BY started_at DESC
- """
- ).fetchall()
- for row in generated_rows:
- model_name = row["model_name"]
- if not model_name:
- continue
- project_name = None
- config_json = row["config_json"]
- if config_json:
- try:
- project_name = extract_project_name(json.loads(config_json))
- except (TypeError, json.JSONDecodeError):
- project_name = None
-
- expected_dir_name = build_default_output_dir_name(
- model_name,
- project_name,
- timestamp = timestamp,
- )
- if expected_dir_name == checkpoint_name:
- return model_name
- except Exception:
+def _lookup_base_model(checkpoint_dir: Path, history: _History) -> Optional[str]:
+ checkpoint_name = checkpoint_dir.name
+ exact_dirs = (str(checkpoint_dir.resolve()), str(checkpoint_dir))
+ dir_rows = history.dir_rows()
+ for model_name, output_dir in dir_rows:
+ if model_name and output_dir in exact_dirs:
+ return model_name
+
+ for model_name, output_dir in dir_rows:
+ output_dir = str(output_dir).rstrip("/\\")
+ if model_name and (
+ output_dir.endswith(f"/{checkpoint_name}")
+ or output_dir.endswith(f"\\{checkpoint_name}")
+ ):
+ return model_name
+
+ parts = checkpoint_name.rsplit("_", 1)
+ if len(parts) != 2 or not parts[1].isdigit():
return None
- finally:
- conn.close()
+ timestamp = int(parts[1])
+ for model_name, project_name in history.model_projects():
+ if (
+ build_default_output_dir_name(model_name, project_name, timestamp = timestamp)
+ == checkpoint_name
+ ):
+ return model_name
return None
@@ -257,6 +267,16 @@ def scan_checkpoints(
"""
if outputs_dir is None:
outputs_dir = str(outputs_root())
+ history = _History()
+ try:
+ return _scan_checkpoints(outputs_dir, include_decision, history)
+ finally:
+ history.close()
+
+
+def _scan_checkpoints(
+ outputs_dir: str, include_decision: bool, history: _History
+) -> List[Tuple[str, List[Tuple[str, str, Optional[float]]], dict]]:
models = []
outputs_path = resolve_output_dir(outputs_dir)
@@ -319,7 +339,7 @@ def scan_checkpoints(
# Fallback: "unsloth_Llama-3.2-3B-Instruct_1771227800" -> "unsloth/Llama-3.2-3B-Instruct".
if not metadata.get("base_model"):
- metadata["base_model"] = _infer_base_model_from_history(item)
+ metadata["base_model"] = _infer_base_model_from_history(item, history)
if not metadata.get("base_model"):
name_part = model_segment_from_default_output_dir_name(item.name)