feat(sidebar): manage folders that live under several model roots

The sidebar's folder tree merges every model root into one relative-path
namespace, but folder operations turned a node into a path by prefixing
default_*_root. A folder living under another root failed to delete with
"Folder no longer exists" (recipes under the primary lora root while
default_lora_root is the extra one), and where the same relative folder
exists in both roots the operation silently hit the other copy — 14 of the
16 top-level folders in the reporting library are shared, so guessing a root
was never safe.

Backend:
* ModelMoveService.resolve_folder() and GET /api/lm/{prefix}/resolve-folder
  answer which directories a library-relative folder maps to
  (folder_path/root/is_symlink), in scanner root order, skipping directories
  no root holds and refusing absolute or climbing paths.
* delete_folder/rename_folder tag a vanished directory with code "missing"
  so the sidebar can tell "this node is stale, refresh" from a failed
  operation.

Frontend:
* _resolveFolderCandidates() is the single place that turns a node into
  absolute paths: default root first, the old root-prefix fallback only
  while a single root is configured, and an explicit unresolved error for a
  multi-root library — nothing is guessed silently any more.
* One copy keeps the single-target modal, which now names the resolved
  absolute path. Several copies render one checkbox row per copy, each
  dry-run against the delete guard ("no models" / "contains N model
  file(s)..." / a deletion is still pending / no longer exists / symbolic
  link): a blocked copy is unticked, disabled and explained, the button
  reads "Delete N folders", every ticked copy is deleted and guarded on its
  own, and a partial failure is reported without discarding the successes.
* Rows are built once per open and only their status text is updated, so
  ticking a box no longer rebuilds the list, steals focus or resizes the
  modal mid-click; the action row keeps a fixed button width.
* Rename offers a root picker in its inline row, create inherits the
  parent's root when the parent resolves to exactly one directory, and the
  undo restores every copy a delete removed.

i18n: 26 new keys (sidebar.deleteFolderModal.*, .deleteFolderResult.*,
.renameFolderResult.*, .folderRoot.*, .folderResult.*) translated in all 9
locales, with the en wording normalized to the established "model root" noun
(it had said "library root") and the new terminology recorded in
docs/i18n-translation-guidelines.md.

Verified: pytest 3686 passed, vitest 1473 passed (86 in the folder-management
suite), pytest tests/i18n 20 passed, sync_translation_keys.py --dry-run
clean. A sandboxed standalone instance with two roots confirmed that deleting
one copy leaves the node in place, that the twin's model card survives the
purge, and that deleting both copies and undoing restores both directories.
This commit is contained in:
Will Miao
2026-10-06 17:14:07 +08:00
parent 5ebf5aeccc
commit 2dcaf6a30e
23 changed files with 2220 additions and 180 deletions
+114 -1
View File
@@ -15,6 +15,7 @@ class FakeScanner:
self._roots = [str(root) for root in roots]
self.known_folders: List[str] = []
self.removed_folders: List[str] = []
self.removed_folder_paths: List[str | None] = []
self.renamed_folders: List[tuple] = []
self._excluded = list(excluded or [])
@@ -27,8 +28,9 @@ class FakeScanner:
async def add_known_folder(self, folder: str) -> None:
self.known_folders.append(folder)
async def remove_known_folder(self, folder: str) -> None:
async def remove_known_folder(self, folder: str, absolute_path: str | None = None) -> None:
self.removed_folders.append(folder)
self.removed_folder_paths.append(absolute_path)
async def rename_known_folder(self, previous: str, current: str, **kwargs) -> None:
self.renamed_folders.append((previous, current, kwargs))
@@ -107,6 +109,114 @@ async def test_create_folder_requires_path(tmp_path: Path):
assert result["success"] is False
def test_resolve_folder_finds_the_directory_under_the_root_that_holds_it(tmp_path: Path):
# The reported bug: the tree node lives under the primary root while the
# configured default root is the extra one. Resolution must answer with the
# directory that exists, not with a path fabricated from the default root.
primary = tmp_path / "primary"
extra = tmp_path / "extra"
primary.mkdir()
extra.mkdir()
(primary / "recipes").mkdir()
service = ModelMoveService(FakeScanner([primary, extra]), "lora")
result = service.resolve_folder("recipes")
assert result["success"] is True
assert result["folder"] == "recipes"
assert [Path(candidate["folder_path"]) for candidate in result["candidates"]] == [
primary / "recipes"
]
assert result["candidates"][0]["root"] == primary.as_posix()
assert result["candidates"][0]["is_symlink"] is False
def test_resolve_folder_returns_a_candidate_per_root_holding_the_folder(tmp_path: Path):
primary = tmp_path / "primary"
extra = tmp_path / "extra"
(primary / "characters" / "anime").mkdir(parents=True)
(extra / "characters" / "anime").mkdir(parents=True)
service = ModelMoveService(FakeScanner([primary, extra]), "lora")
result = service.resolve_folder("characters/anime")
# Order follows the scanner's root order so the caller can prefer the
# default root without losing the rest.
assert [Path(candidate["folder_path"]) for candidate in result["candidates"]] == [
primary / "characters" / "anime",
extra / "characters" / "anime",
]
assert [candidate["root"] for candidate in result["candidates"]] == [
primary.as_posix(),
extra.as_posix(),
]
def test_resolve_folder_reports_no_candidate_for_a_stale_tree_node(tmp_path: Path):
root = tmp_path / "library"
root.mkdir()
service = ModelMoveService(FakeScanner([root]), "lora")
result = service.resolve_folder("gone")
assert result["success"] is True
assert result["candidates"] == []
def test_resolve_folder_flags_symlinked_candidates(tmp_path: Path):
root = tmp_path / "library"
root.mkdir()
real = tmp_path / "real"
real.mkdir()
try:
(root / "linked").symlink_to(real, target_is_directory=True)
except (OSError, NotImplementedError): # pragma: no cover - platform guard
pytest.skip("symlinks are not supported on this platform")
service = ModelMoveService(FakeScanner([root]), "lora")
result = service.resolve_folder("linked")
assert result["success"] is True
assert result["candidates"][0]["is_symlink"] is True
def test_resolve_folder_normalizes_the_relative_path(tmp_path: Path):
root = tmp_path / "library"
(root / "characters" / "anime").mkdir(parents=True)
service = ModelMoveService(FakeScanner([root]), "lora")
result = service.resolve_folder(" characters\\anime/ ")
assert result["success"] is True
assert result["folder"] == "characters/anime"
assert len(result["candidates"]) == 1
@pytest.mark.parametrize(
"folder",
["", " ", "/library/recipes", "C:/library/recipes", "..", "../outside", "a/../../b"],
)
def test_resolve_folder_rejects_paths_outside_the_relative_namespace(
tmp_path: Path, folder: str
):
root = tmp_path / "library"
root.mkdir()
outside = tmp_path / "outside"
outside.mkdir()
service = ModelMoveService(FakeScanner([root]), "lora")
result = service.resolve_folder(folder)
assert result["success"] is False
assert "candidates" not in result
def _make_nested(root: Path) -> Path:
target = root / "characters" / "anime"
target.mkdir(parents=True)
@@ -127,6 +237,9 @@ async def test_delete_folder_removes_empty_directory_and_forgets_it(tmp_path: Pa
assert result["restorable"] is True
assert not target.exists()
assert scanner.removed_folders == ["characters/anime"]
# The scanner purges by the removed directory: the same relative folder may
# exist under another root, whose model cards must survive.
assert scanner.removed_folder_paths == [str(target)]
@pytest.mark.asyncio