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
+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)