feat(scanner): report and re-admit model roots that are unavailable (#1108)

`Config._dedupe_existing_paths()` drops roots whose directory does not exist
when the root list is built, so a drive that is switched off while LM starts was
not "an offline root" — it was not a root at all. The Refresh ▾ menu had no row
for it in either state, a full refresh reported nothing (its cached entries
survived only because they fall outside every configured prefix), and plugging
the drive back in changed nothing until the next restart.

* `Config` now records the configured (existence-unfiltered) paths per model
  type — from ComfyUI's list in plugin mode, from the active library snapshot in
  standalone mode, where the host mock filters non-existent paths itself — and
  exposes them through `configured_roots_for()`.
* `ModelScanner.describe_model_roots()` appends configured-but-unavailable roots
  with a live `reachable`, an `available` flag and their cached entry count, so
  `/roots` (and therefore the menu) offers a row in both states: greyed while the
  directory is missing, normal and clickable once it is back.
* `_reconcile_cache()` reports them as `skipped_roots` / `unavailable_paths` with
  reason `root_unavailable` and counts their cached entries in `kept_unreachable`,
  which is what makes the "N models kept" toast appear in the startup-offline
  case. Report-only: they stay outside the scan scope, so nothing about pruning
  changes. A root-scoped scan stays quiet about roots it was not asked about, and
  a folder-scoped scan only mentions a missing root that holds cached entries of
  that folder.
* `Config.admit_configured_roots()` re-runs the per-type prepare helpers against
  the configured paths and APPENDS what is readable now (plus the
  checkpoint/unet/other side maps), then refreshes the preview allowlist, so a
  drive plugged in mid-session can be scanned without restarting. Append-only and
  order-preserving by design: removing a root mid-session would let a later
  settings save persist the loss, and re-sorting would move `*_roots[0]`, which
  derives the recipes directory and the usage-stats file location. `_roots[0]`
  never moves.
* `/roots` and `/scan` admit first, so `describe_model_roots()` stays a pure read
  and a caller that never opens the menu (the browser extension) still gets a
  drive that came back.

Verified on the sandbox with drive-Z switched off before startup: `/roots`
reports `drive-Z reachable=false available=false models=60`, a full refresh
returns `skipped_roots=[drive-Z root_unavailable]`,
`unavailable_paths=[{... kept: 60}]`, `kept_unreachable=60`, and all 432 models
stay cached. Renaming the drive back while the server runs admits it
(`available=true`) and `GET /scan?roots=<drive-Z>` walks it with no restart.
3714 passed, 7 skipped; frontend 1495 passed (149 files); vue widgets 96 passed.
This commit is contained in:
Will Miao
2026-10-07 16:38:38 +08:00
parent 08a8e54d87
commit f4d0228d6b
8 changed files with 576 additions and 12 deletions
+132
View File
@@ -0,0 +1,132 @@
"""Wave 6: configured roots that are unavailable must be reportable and
re-admitted append-only, never dropped from the live lists."""
import os
import pytest
from py import config as config_module
def _make_config(**overrides) -> config_module.Config:
config = config_module.Config.__new__(config_module.Config)
config._path_mappings = {}
config._preview_root_paths = set()
config._cached_fingerprint = None
config.loras_roots = []
config.extra_loras_roots = []
config.base_models_roots = []
config.checkpoints_roots = []
config.unet_roots = []
config.embeddings_roots = []
config.extra_checkpoints_roots = []
config.extra_unet_roots = []
config.extra_embeddings_roots = []
config.other_roots = []
config.recipes_path = ""
config.other_root_subtypes = {}
config.other_folder_roots = {}
config._configured_root_paths = {}
for key, value in overrides.items():
setattr(config, key, value)
return config
def _normalize(path) -> str:
return os.path.normpath(str(path)).replace(os.sep, "/")
class TestAppendNewPaths:
def test_appends_without_reordering_or_duplicating(self):
merged = config_module._append_new_paths(
["/b/loras", "/a/loras"], ["/a/loras", "/c/loras", "", None]
)
# Existing order is preserved (it derives *_roots[0] consumers) and the
# new root lands at the end.
assert merged == ["/b/loras", "/a/loras", "/c/loras"]
def test_matches_case_insensitively(self):
merged = config_module._append_new_paths(["C:/Loras"], ["c:/loras"])
assert merged == ["C:/Loras"]
class TestAdmitConfiguredRoots:
def test_admits_a_root_that_became_readable(self, tmp_path):
primary = tmp_path / "primary"
primary.mkdir()
drive = tmp_path / "drive-Z" / "loras"
config = _make_config(
loras_roots=[str(primary)],
_configured_root_paths={"lora": [str(primary), str(drive)]},
)
assert config.configured_roots_for("lora") == [str(primary), str(drive)]
# Still switched off: nothing to admit.
assert config.admit_configured_roots() == []
assert config.loras_roots == [str(primary)]
drive.mkdir(parents=True)
admitted = config.admit_configured_roots()
assert admitted == [str(drive)]
assert config.loras_roots == [str(primary), str(drive)]
# Idempotent: a second call adds nothing.
assert config.admit_configured_roots() == []
def test_never_removes_a_root_that_disappeared(self, tmp_path):
primary = tmp_path / "primary"
primary.mkdir()
drive = tmp_path / "drive-Z" / "loras"
drive.mkdir(parents=True)
config = _make_config(
loras_roots=[str(primary), str(drive)],
_configured_root_paths={"lora": [str(drive), str(primary)]},
)
# The drive is switched off after the lists were built: the live list
# keeps it (reporting and pruning decide separately) and admission must
# not "clean it up" — a later settings save would persist the loss.
drive.rmdir()
config.admit_configured_roots()
assert config.loras_roots == [str(primary), str(drive)]
def test_admits_every_model_type_and_its_side_maps(self, tmp_path):
checkpoint_dir = tmp_path / "checkpoints"
unet_dir = tmp_path / "unet"
embedding_dir = tmp_path / "embeddings"
other_dir = tmp_path / "vae"
for directory in (checkpoint_dir, unet_dir, embedding_dir, other_dir):
directory.mkdir()
config = _make_config(
_configured_root_paths={
"checkpoint": [str(checkpoint_dir)],
"unet": [str(unet_dir)],
"embedding": [str(embedding_dir)],
"other": [str(other_dir)],
},
)
config._get_enabled_other_folder_keys = lambda: ["vae"]
config._configured_other_paths_for_key = lambda key: [str(other_dir)]
admitted = config.admit_configured_roots()
assert set(admitted) == {
_normalize(checkpoint_dir),
_normalize(unet_dir),
_normalize(embedding_dir),
_normalize(other_dir),
}
assert config.base_models_roots == [
_normalize(checkpoint_dir),
_normalize(unet_dir),
]
assert config.checkpoints_roots == [_normalize(checkpoint_dir)]
assert config.unet_roots == [_normalize(unet_dir)]
assert config.embeddings_roots == [_normalize(embedding_dir)]
assert config.other_roots == [_normalize(other_dir)]
assert config.other_root_subtypes[_normalize(other_dir)] == "vae"
assert config.other_folder_roots["vae"] == [_normalize(other_dir)]
+48 -2
View File
@@ -213,21 +213,43 @@ class ScopedScanService:
model_type = "lora"
def __init__(self, roots=None, summary=None, cancelled=False):
def __init__(self, roots=None, summary=None, cancelled=False, unavailable=None):
self._roots = list(roots or [])
self._unavailable = list(unavailable or [])
self._summary = summary
self.cancelled = cancelled
self.scanner = SimpleNamespace(is_cancelled=lambda: self.cancelled)
self.received_scope = "not-called"
self.received_rebuild = None
self.root_refreshes = 0
def get_model_roots(self):
return list(self._roots)
def refresh_model_roots(self):
"""Mimic the scanner admitting roots that became readable again."""
self.root_refreshes += 1
return []
def describe_model_roots(self):
return [
{"path": root, "label": root.rsplit("/", 1)[-1], "reachable": True, "models": 7}
{
"path": root,
"label": root.rsplit("/", 1)[-1],
"reachable": True,
"available": True,
"models": 7,
}
for root in self._roots
] + [
{
"path": root,
"label": root.rsplit("/", 1)[-1],
"reachable": False,
"available": False,
"models": 3,
}
for root in self._unavailable
]
async def scan_models(self, force_refresh=False, rebuild_cache=False, scope=None):
@@ -265,6 +287,8 @@ async def test_scan_models_accepts_roots_param():
assert service.received_scope is not None
assert service.received_scope.roots == ("/mnt/a",)
assert service.received_rebuild is False
# A drive plugged in after startup is admitted before the roots are validated.
assert service.root_refreshes == 1
@pytest.mark.asyncio
@@ -307,6 +331,28 @@ async def test_scan_models_rejects_roots_with_full_rebuild():
assert service.received_scope == "not-called"
@pytest.mark.asyncio
async def test_get_model_roots_reports_configured_but_unavailable_roots():
"""Wave 6: a root whose directory was missing at startup is still reported."""
service = ScopedScanService(
roots=["/mnt/a"], unavailable=["/mnt/drive-Z/loras"]
)
handler = ModelQueryHandler(service=service, logger=logging.getLogger(__name__))
response = await handler.get_model_roots(SimpleNamespace(query=QueryParams({})))
payload = json.loads(response.text)
assert response.status == 200
assert payload["roots"] == ["/mnt/a"]
details = {entry["path"]: entry for entry in payload["root_details"]}
assert details["/mnt/a"]["available"] is True
assert details["/mnt/drive-Z/loras"]["available"] is False
assert details["/mnt/drive-Z/loras"]["reachable"] is False
assert details["/mnt/drive-Z/loras"]["models"] == 3
# Describing the roots admits anything that became readable again.
assert service.root_refreshes == 1
@pytest.mark.asyncio
async def test_scan_models_accepts_folder_param():
service = ScopedScanService(roots=["/mnt/a", "/mnt/b"], summary=SUMMARY)
+106
View File
@@ -2649,6 +2649,112 @@ async def test_reconcile_folder_scope_covers_every_root_holding_it(tmp_path: Pat
assert _normalize_path(primary / "only-primary" / "c.txt") in paths
async def _startup_filtered_scanner(tmp_path: Path, monkeypatch, configured_missing: Path):
"""Scanner whose live root list lacks a root that the config still holds.
Models the startup case: the directory did not exist when the live list was
built (drive switched off), so `Config` filtered it out while its cached
entries are still there.
"""
primary = tmp_path / "primary"
(primary / "pack").mkdir(parents=True)
(primary / "pack" / "a.txt").write_text("a", encoding="utf-8")
configured_missing.mkdir(parents=True)
(configured_missing / "pack").mkdir()
(configured_missing / "pack" / "b.txt").write_text("b", encoding="utf-8")
scanner = MultiRootDummyScanner([primary, configured_missing])
await scanner._initialize_cache()
# The drive goes away before the process starts, as far as the live list is
# concerned: keep the roots the filter would have kept.
shutil.rmtree(configured_missing)
scanner._roots = [str(primary)]
monkeypatch.setattr(
scanner,
"get_configured_model_roots",
lambda: [str(primary), str(configured_missing)],
)
return scanner, primary, configured_missing
@pytest.mark.asyncio
async def test_describe_model_roots_reports_unavailable_configured_root(
tmp_path: Path, monkeypatch
):
configured_missing = tmp_path / "drive-Z" / "loras"
scanner, primary, missing = await _startup_filtered_scanner(
tmp_path, monkeypatch, configured_missing
)
described = scanner.describe_model_roots()
assert [entry["path"] for entry in described] == [
_normalize_path(primary),
_normalize_path(missing),
]
available, unavailable = described
assert available["reachable"] is True
assert available["available"] is True
# The missing root is reported with its cached count so the refresh menu can
# offer it as an offline row instead of hiding it until a restart.
assert unavailable["reachable"] is False
assert unavailable["available"] is False
assert unavailable["models"] == 1
@pytest.mark.asyncio
async def test_reconcile_reports_root_filtered_out_at_startup(tmp_path: Path, monkeypatch):
configured_missing = tmp_path / "drive-Z" / "loras"
scanner, _primary, missing = await _startup_filtered_scanner(
tmp_path, monkeypatch, configured_missing
)
kept_path = _normalize_path(missing / "pack" / "b.txt")
summary = await scanner._reconcile_cache()
assert summary is not None
# Nothing is added or removed...
assert summary["added"] == 0
assert summary["removed"] == 0
# ...but the switched-off root is no longer silent: it is reported with the
# entries that were kept for it.
assert summary["skipped_roots"] == [
{
"path": _normalize_path(missing),
"label": missing.name,
"reason": "root_unavailable",
}
]
assert summary["kept_unreachable"] == 1
assert summary["unavailable_paths"] == [
{"path": _normalize_path(missing), "reason": "root_unavailable", "kept": 1}
]
assert kept_path in {item["file_path"] for item in scanner._cache.raw_data}
@pytest.mark.asyncio
async def test_scoped_scan_stays_quiet_about_filtered_out_roots(tmp_path: Path, monkeypatch):
configured_missing = tmp_path / "drive-Z" / "loras"
scanner, primary, missing = await _startup_filtered_scanner(
tmp_path, monkeypatch, configured_missing
)
summary = await scanner._reconcile_cache(
scope=ReconcileScope(roots=(str(primary),))
)
assert summary is not None
# The caller asked for one root: a root it did not ask about is not its
# business, but its entries are still kept.
assert summary["scanned_roots"] == [primary.name]
assert "root_unavailable" not in {
entry["reason"] for entry in summary["skipped_roots"]
}
assert _normalize_path(missing / "pack" / "b.txt") in {
item["file_path"] for item in scanner._cache.raw_data
}
@pytest.mark.asyncio
async def test_reconcile_keeps_entries_under_unreadable_dir(tmp_path: Path, monkeypatch):
"""A directory os.walk cannot enter (permissions, I/O error, offline