mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2026-07-11 00:47:14 +08:00
* feat(assets): add namespaced model type tags * fix(assets): mark path-derived upload tags automatic * fix(assets): merge duplicate scan specs * test(assets): make duplicate path normalization portable * feat(assets): add loader_path as the authoritative loader locator (#14796) * fix(assets): filter model_type tags by bucket extension sets Buckets sharing a base directory (e.g. diffusion_models and a custom unet_gguf) tagged every file in the directory regardless of whether the bucket could load it, so .safetensors files were tagged model_type:unet_gguf and vice versa. Carry each bucket's registered extension set through get_comfy_models_folders and only emit a model_type tag when the file extension matches, keeping the empty-set match-all convention from folder_paths.filter_files_extensions. Files under a model base matching no bucket now keep only the models tag instead of every directory-matching model_type tag. * feat(assets): replace response file_path with persisted loader_path The old file_path response field was a namespaced storage locator (models/checkpoints/foo.safetensors): not an absolute path, not unique identity, and not the value a loader consumes. Nothing needs that shape on the wire (hash/ID-based locating is the long-term direction), so it is dropped rather than renamed; the storage-root matching stays internal, powering display_name. What loaders DO need is the in-root loader path (category dropped: models/checkpoints/foo/bar.safetensors -> foo/bar.safetensors). Serve it as a first-class loader_path field, persisted on asset_references (migration 0006) and written by every ingest pipeline at insert, so responses read the column verbatim. Like the model_type tags, loader_path is a seed-time derivative of the model folder registry, maintained by the same scan lifecycle (new files seed fresh values, pruning retires rows whose bucket disappeared). Rows predating the column serve a null loader_path; databases from before this stack already need recreating for the base branch's tag changes. loader_path resolves every registered base including extra_model_paths entries; display_name only the canonical storage roots. A file can therefore be loadable with no display name (extra-path models) or the reverse (unregistered files under the models root), and loader_path is null exactly when no loader can resolve the file. * test(assets): lock loader_path matrix (asymmetry, null, persist/read) Cover the behaviour that has no production change but is easy to regress: the extra-path asymmetry (loadable but no storage namespace), null loader_path persistence for orphan files, and the response reading the stored column with a compute fallback for un-backfilled rows. * fix(assets): persist subfolder-qualified loader_path for ingested outputs ingest_existing_file built its seed spec with the file's basename, so outputs saved into a subfolder persisted loader_path (and the user_metadata filename that preview URLs split for their subfolder param) as just the basename: the served locator pointed at a file that does not exist at that path. Scanner and seeder specs already derive fname via compute_loader_path; use the same derivation here. * fix(assets): only extension-matching buckets contribute a loader_path The model-base match in get_asset_category_and_relative_path ignored each bucket's extension set, so a file inside a registered base whose extension the bucket cannot load (e.g. a .txt uploaded into model_type:checkpoints) advertised a loader_path that no loader list would ever resolve, while the tag side of the same stack already excluded it. Apply the extension check used for backend tags (empty set accepts any extension), keeping loader_path null exactly when no loader can resolve the file. * fix(assets): refresh loader_path when re-ingesting an existing reference upsert_reference only wrote loader_path on the INSERT branch, so re-ingesting an existing reference (an output overwritten in place, or a file re-registered after its loader_path derivation changed) kept the stale or NULL value forever. Write it on the UPDATE branch too, with a null-safe change guard so a loader_path difference alone is enough to trigger the update, and identical values stay a no-op. * fix(assets): repair semantic merge breakage from #14796 and master Two textually-clean but semantically-broken merges: - routes.py lost its folder_paths import when #14796's import block superseded the base's, while the content-type hardening added via the base's master merge still calls folder_paths.is_dangerous_content_type. - master's SVG download-hardening test uploads with the pre-namespacing bare checkpoints tag, which this branch's destination validation rejects; use model_type:checkpoints. --------- Co-authored-by: guill <jacob.e.segal@gmail.com>
144 lines
4.3 KiB
Python
144 lines
4.3 KiB
Python
import uuid
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import requests
|
|
from helpers import get_asset_filename, trigger_sync_seed_assets
|
|
|
|
|
|
@pytest.fixture
|
|
def create_seed_file(comfy_tmp_base_dir: Path):
|
|
"""Create a file on disk that will become a seed asset after sync."""
|
|
created: list[Path] = []
|
|
|
|
def _create(root: str, scope: str, name: str | None = None, data: bytes = b"TEST") -> Path:
|
|
name = name or f"seed_{uuid.uuid4().hex[:8]}.bin"
|
|
path = comfy_tmp_base_dir / root / "unit-tests" / scope / name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(data)
|
|
created.append(path)
|
|
return path
|
|
|
|
yield _create
|
|
|
|
for p in created:
|
|
p.unlink(missing_ok=True)
|
|
|
|
|
|
@pytest.fixture
|
|
def find_asset(http: requests.Session, api_base: str):
|
|
"""Query API for assets matching scope and optional name."""
|
|
def _find(scope: str, name: str | None = None) -> list[dict]:
|
|
params = {"limit": "500"}
|
|
if name:
|
|
params["name_contains"] = name
|
|
r = http.get(f"{api_base}/api/assets", params=params, timeout=120)
|
|
assert r.status_code == 200
|
|
assets = r.json().get("assets", [])
|
|
if name:
|
|
return [a for a in assets if a.get("name") == name]
|
|
return assets
|
|
|
|
return _find
|
|
|
|
|
|
@pytest.mark.parametrize("root", ["input", "output"])
|
|
def test_orphaned_seed_asset_is_pruned(
|
|
root: str,
|
|
create_seed_file,
|
|
find_asset,
|
|
http: requests.Session,
|
|
api_base: str,
|
|
):
|
|
"""Seed asset with deleted file is removed; with file present, it survives."""
|
|
scope = f"prune-{uuid.uuid4().hex[:6]}"
|
|
fp = create_seed_file(root, scope)
|
|
name = fp.name
|
|
|
|
trigger_sync_seed_assets(http, api_base)
|
|
assert find_asset(scope, name), "Seed asset should exist"
|
|
|
|
fp.unlink()
|
|
trigger_sync_seed_assets(http, api_base)
|
|
assert not find_asset(scope, name), "Orphaned seed should be pruned"
|
|
|
|
|
|
def test_seed_asset_with_file_survives_prune(
|
|
create_seed_file,
|
|
find_asset,
|
|
http: requests.Session,
|
|
api_base: str,
|
|
):
|
|
"""Seed asset with file still on disk is NOT pruned."""
|
|
scope = f"keep-{uuid.uuid4().hex[:6]}"
|
|
fp = create_seed_file("input", scope)
|
|
|
|
trigger_sync_seed_assets(http, api_base)
|
|
trigger_sync_seed_assets(http, api_base)
|
|
|
|
assert find_asset(scope, fp.name), "Seed with valid file should survive"
|
|
|
|
|
|
def test_hashed_asset_not_pruned_when_file_missing(
|
|
http: requests.Session,
|
|
api_base: str,
|
|
comfy_tmp_base_dir: Path,
|
|
asset_factory,
|
|
make_asset_bytes,
|
|
):
|
|
"""Hashed assets are never deleted by prune, even without file."""
|
|
scope = f"hashed-{uuid.uuid4().hex[:6]}"
|
|
data = make_asset_bytes("test", 2048)
|
|
a = asset_factory("test.bin", ["input", "unit-tests", scope], {}, data)
|
|
|
|
path = comfy_tmp_base_dir / "input" / get_asset_filename(a["asset_hash"], ".bin")
|
|
path.unlink()
|
|
|
|
trigger_sync_seed_assets(http, api_base)
|
|
|
|
r = http.get(f"{api_base}/api/assets/{a['id']}", timeout=120)
|
|
assert r.status_code == 200, "Hashed asset should NOT be pruned"
|
|
|
|
|
|
def test_prune_across_multiple_roots(
|
|
create_seed_file,
|
|
find_asset,
|
|
http: requests.Session,
|
|
api_base: str,
|
|
):
|
|
"""Prune correctly handles assets across input and output roots."""
|
|
scope = f"multi-{uuid.uuid4().hex[:6]}"
|
|
input_name = f"{scope}-input.bin"
|
|
output_name = f"{scope}-output.bin"
|
|
input_fp = create_seed_file("input", scope, input_name)
|
|
create_seed_file("output", scope, output_name)
|
|
|
|
trigger_sync_seed_assets(http, api_base)
|
|
assert find_asset(scope, input_name)
|
|
assert find_asset(scope, output_name)
|
|
|
|
input_fp.unlink()
|
|
trigger_sync_seed_assets(http, api_base)
|
|
|
|
assert not find_asset(scope, input_name)
|
|
assert find_asset(scope, output_name)
|
|
|
|
|
|
@pytest.mark.parametrize("dirname", ["100%_done", "my_folder_name", "has spaces"])
|
|
def test_special_chars_in_path_escaped_correctly(
|
|
dirname: str,
|
|
create_seed_file,
|
|
find_asset,
|
|
http: requests.Session,
|
|
api_base: str,
|
|
comfy_tmp_base_dir: Path,
|
|
):
|
|
"""SQL LIKE wildcards (%, _) and spaces in paths don't cause false matches."""
|
|
scope = f"special-{uuid.uuid4().hex[:6]}/{dirname}"
|
|
fp = create_seed_file("input", scope)
|
|
|
|
trigger_sync_seed_assets(http, api_base)
|
|
trigger_sync_seed_assets(http, api_base)
|
|
|
|
assert find_asset(scope.split("/")[0], fp.name), "Asset with special chars should survive"
|