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
@@ -901,3 +901,54 @@ def test_download_model_returns_skipped_success(mock_service, download_manager_s
await client.close()
asyncio.run(scenario())
def test_resolve_folder_answers_with_the_root_that_holds_the_directory(
mock_service, mock_scanner, tmp_path: Path
):
"""The route the sidebar needs to stop guessing a root for a tree node.
The unified folder tree merges every model root into one relative-path
namespace, so "recipes" below must resolve to the root that actually has it
even though another root is configured as the library default.
"""
primary = tmp_path / "primary"
extra = tmp_path / "extra"
(primary / "recipes").mkdir(parents=True)
extra.mkdir()
mock_scanner.get_model_roots = lambda: [str(primary), str(extra)]
async def scenario():
client = await create_test_client(mock_service)
try:
response = await client.get(
"/api/lm/test-models/resolve-folder", params={"folder": "recipes"}
)
payload = await response.json()
assert response.status == 200
assert payload["success"] is True
assert payload["folder"] == "recipes"
assert payload["candidates"] == [
{
"folder_path": str(primary / "recipes"),
"root": str(primary),
"is_symlink": False,
}
]
stale = await client.get(
"/api/lm/test-models/resolve-folder", params={"folder": "gone"}
)
assert (await stale.json())["candidates"] == []
refused = await client.get(
"/api/lm/test-models/resolve-folder",
params={"folder": str(primary / "recipes")},
)
assert refused.status == 400
finally:
await client.close()
asyncio.run(scenario())
+63
View File
@@ -12,6 +12,7 @@ class FakeMoveService:
self.received_path = None
self.received_dry_run = None
self.received_new_name = None
self.received_folder = None
async def create_folder(self, folder_path):
self.received_path = folder_path
@@ -27,6 +28,10 @@ class FakeMoveService:
self.received_new_name = new_name
return self._result
def resolve_folder(self, folder):
self.received_folder = folder
return self._result
class FakeRequest:
def __init__(self, payload):
@@ -36,6 +41,13 @@ class FakeRequest:
return self._payload
class FakeQueryRequest:
"""Request stand-in exposing only the query string."""
def __init__(self, query):
self.query = query
def _make_handler(result):
service = FakeMoveService(result)
handler = ModelMoveHandler(
@@ -141,6 +153,43 @@ async def test_delete_folder_forwards_dry_run():
assert json.loads(response.text)["dry_run"] is True
@pytest.mark.asyncio
async def test_resolve_folder_returns_the_matching_roots():
handler, service = _make_handler(
{
"success": True,
"folder": "recipes",
"candidates": [
{
"folder_path": "/library/recipes",
"root": "/library",
"is_symlink": False,
}
],
}
)
response = await handler.resolve_folder(FakeQueryRequest({"folder": "recipes"}))
assert response.status == 200
payload = json.loads(response.text)
assert payload["success"] is True
assert payload["candidates"][0]["folder_path"] == "/library/recipes"
assert service.received_folder == "recipes"
@pytest.mark.asyncio
async def test_resolve_folder_maps_a_refused_path_to_400():
handler, _service = _make_handler(
{"success": False, "error": "Folder path is required"}
)
response = await handler.resolve_folder(FakeQueryRequest({}))
assert response.status == 400
assert json.loads(response.text)["success"] is False
@pytest.mark.asyncio
async def test_delete_folder_missing_path():
handler, service = _make_handler({"success": True})
@@ -206,6 +255,20 @@ async def test_delete_folder_containment_failure_maps_to_400():
assert "outside configured library" in payload["error"]
@pytest.mark.asyncio
async def test_delete_folder_reports_a_vanished_directory_as_missing():
handler, _service = _make_handler(
{"success": False, "code": "missing", "error": "Folder no longer exists"}
)
response = await handler.delete_folder(
FakeRequest({"folder_path": "/library/gone"})
)
assert response.status == 400
assert json.loads(response.text)["code"] == "missing"
@pytest.mark.asyncio
async def test_delete_folder_invalid_json_body():
class BadJsonRequest: