diff --git a/browser_tests/fixtures/data/assetFixtures.ts b/browser_tests/fixtures/data/assetFixtures.ts index ee425bea6c6..745cb0eb57d 100644 --- a/browser_tests/fixtures/data/assetFixtures.ts +++ b/browser_tests/fixtures/data/assetFixtures.ts @@ -167,6 +167,40 @@ export const MODEL_TYPE_CHECKPOINT_PRE_CUTOVER: CoreModelAsset = updated_at: '2025-02-18T09:00:00Z' }) +/** + * A legacy bare-tagged asset (no `model_type:` prefix) that still carries a + * `loader_path`, mimicking a `model_type:`-capable backend that has not + * finished re-tagging every asset. Asset grouping must fall back to bare-tag + * grouping for it instead of dropping it from the sidebar. + */ +export const MODEL_TYPE_CHECKPOINT_LEGACY_TAG: CoreModelAsset = + createCoreModelAsset({ + id: 'mt-checkpoint-007', + name: 'legacy_tagged_checkpoint.safetensors', + tags: ['models', 'checkpoints'], + loader_path: 'legacy_tagged_checkpoint.safetensors', + created_at: '2025-02-22T09:00:00Z', + updated_at: '2025-02-22T09:00:00Z' + }) + +/** + * An asset caught mid-migration: it already carries the authoritative + * `model_type:checkpoints` tag alongside its pre-migration bare-tag twin + * (`checkpoints`), plus an unrelated leftover bare tag (`loras`) that happens + * to match another real folder. A `model_type:`-covered asset must group by + * its `model_type:` tags alone, so it lands in `checkpoints` exactly once and + * never cross-lists into `loras`. + */ +export const MODEL_TYPE_CHECKPOINT_MID_RETAG: CoreModelAsset = + createCoreModelAsset({ + id: 'mt-checkpoint-008', + name: 'mid_retag_checkpoint.safetensors', + tags: ['models', 'model_type:checkpoints', 'checkpoints', 'loras'], + loader_path: 'mid_retag_checkpoint.safetensors', + created_at: '2025-02-23T09:00:00Z', + updated_at: '2025-02-23T09:00:00Z' + }) + export const MODEL_TYPE_LORA: CoreModelAsset = createCoreModelAsset({ id: 'mt-lora-001', name: 'detail_enhancer_v1.2.safetensors', diff --git a/browser_tests/tests/sidebar/modelLibraryAssetMode.spec.ts b/browser_tests/tests/sidebar/modelLibraryAssetMode.spec.ts index 215055b16ff..91d0889cfd4 100644 --- a/browser_tests/tests/sidebar/modelLibraryAssetMode.spec.ts +++ b/browser_tests/tests/sidebar/modelLibraryAssetMode.spec.ts @@ -5,6 +5,8 @@ import { assetApiFixture } from '@e2e/fixtures/assetApiFixture' import { comfyPageFixture } from '@e2e/fixtures/ComfyPage' import { MODEL_TYPE_CHECKPOINT_GGUF, + MODEL_TYPE_CHECKPOINT_LEGACY_TAG, + MODEL_TYPE_CHECKPOINT_MID_RETAG, MODEL_TYPE_CHECKPOINT_NESTED, MODEL_TYPE_CHECKPOINT_ORPHAN, MODEL_TYPE_CHECKPOINT_PRE_CUTOVER, @@ -350,6 +352,114 @@ test.describe('Model library sidebar - asset mode before the loader_path cutover }) }) +test.describe('Model library sidebar - asset mode with a legacy bare tag', () => { + test.beforeEach(async ({ comfyPage, assetApi }) => { + assetApi.configure( + withModels([MODEL_TYPE_CHECKPOINT_ROOT, MODEL_TYPE_CHECKPOINT_LEGACY_TAG]) + ) + await assetApi.mock() + await comfyPage.modelLibrary.mockModelFolders([ + { + name: 'checkpoints', + folders: ['/models/checkpoints'], + extensions: ['.safetensors'] + } + ]) + await comfyPage.setup() + await comfyPage.featureFlags.setServerFlagsPersistent({ + supports_model_type_tags: true + }) + }) + + test.afterEach(async ({ comfyPage }) => { + await comfyPage.modelLibrary.clearMocks() + }) + + // Regression for the AC3 bug flagged in #13574's review: on a + // model_type-capable backend, a bare (non-namespaced) tag resolved to no + // category and buildModelBuckets dropped the asset with a console.warn + // instead of falling back to legacy bare-tag grouping. + test('Falls back to legacy bare-tag grouping instead of dropping the asset', async ({ + comfyPage + }) => { + await comfyPage.menu.modelLibraryTab.open() + const tab = comfyPage.menu.modelLibraryTab + + await tab.getFolderRowByLabel('checkpoints').click() + await expect(tab.getLeafByLabel('v1-5-pruned-emaonly')).toBeVisible() + await expect(tab.getLeafByLabel('legacy_tagged_checkpoint')).toBeVisible() + }) +}) + +test.describe('Model library sidebar - asset mode with a mid-retag twin tag', () => { + test.beforeEach(async ({ comfyPage, assetApi }) => { + assetApi.configure( + withModels([ + MODEL_TYPE_CHECKPOINT_ROOT, + MODEL_TYPE_CHECKPOINT_MID_RETAG, + MODEL_TYPE_LORA + ]) + ) + await assetApi.mock() + await comfyPage.modelLibrary.mockModelFolders([ + { + name: 'checkpoints', + folders: ['/models/checkpoints'], + extensions: ['.safetensors'] + }, + { + name: 'loras', + folders: ['/models/loras'], + extensions: ['.safetensors'] + } + ]) + await comfyPage.setup() + await comfyPage.featureFlags.setServerFlagsPersistent({ + supports_model_type_tags: true + }) + await comfyPage.menu.modelLibraryTab.open() + }) + + test.afterEach(async ({ comfyPage }) => { + await comfyPage.modelLibrary.clearMocks() + }) + + // Regression for the high-severity finding on #14217: an asset carrying + // both an authoritative `model_type:checkpoints` tag and a leftover bare + // `checkpoints` twin (a partial re-tagging artifact) must land in + // `checkpoints` exactly once, and its unrelated leftover `loras` bare tag + // must not cross-list it into that folder too. + test('Groups a model_type-covered asset once and ignores its bare-tag twins', async ({ + comfyPage + }) => { + const tab = comfyPage.menu.modelLibraryTab + + await tab.getFolderRowByLabel('checkpoints').click() + await expect( + tab.modelTree + .locator('.p-tree-node-leaf') + .filter({ hasText: 'mid_retag_checkpoint' }) + ).toHaveCount(1) + + // loras carries a real model_type:loras asset so the folder itself + // renders (asset mode hides folders that load with zero models) — + // otherwise an absent 'loras' row would be ambiguous between "correctly + // empty" and "never loaded". + await tab.getFolderRowByLabel('loras').click() + await expect(tab.getLeafByLabel('detail_enhancer_v1.2')).toBeVisible() + + // Scoped to the loras subtree, not tab.modelTree: checkpoints is still + // expanded from the assertion above, and its (correct) copy of the leaf + // would otherwise satisfy a tree-wide query on its own. + await expect( + tab + .getFolderByLabel('loras') + .locator('.p-tree-node-leaf') + .filter({ hasText: 'mid_retag_checkpoint' }) + ).toHaveCount(0) + }) +}) + test.describe('Model library sidebar - asset mode on bare-tag backends', () => { test.beforeEach(async ({ comfyPage, assetApi }) => { assetApi.configure(withModels([STABLE_CHECKPOINT])) diff --git a/src/platform/assets/services/assetService.ts b/src/platform/assets/services/assetService.ts index 7e59e48d026..e476716fe45 100644 --- a/src/platform/assets/services/assetService.ts +++ b/src/platform/assets/services/assetService.ts @@ -23,7 +23,7 @@ import type { TagsOperationResult } from '@/platform/assets/schemas/assetSchema' import { - MODEL_TYPE_TAG_PREFIX, + getAssetCategories, getAssetFilename } from '@/platform/assets/utils/assetMetadataUtils' import { isCloud } from '@/platform/distribution/types' @@ -222,26 +222,6 @@ function normalizeAssetTags(tags: string[]): string[] { return tags.map((tag) => tag.trim()).filter(Boolean) } -/** - * Resolves the model folder a tag represents, or undefined when the tag is not - * a folder category. `supports_model_type_tags` backends carry the category as - * a namespaced `model_type:` tag; older backends mint bare tags, which - * may carry subfolder paths (e.g. `Chatterbox/sub/model`) and group by their - * top-level segment, matching the asset browser's legacy grouping. - */ -function modelFolderFromTag( - tag: string, - modelTypeMode: boolean -): string | undefined { - if (modelTypeMode) { - return tag.startsWith(MODEL_TYPE_TAG_PREFIX) - ? tag.slice(MODEL_TYPE_TAG_PREFIX.length) - : undefined - } - if (tag === MODELS_TAG || tag.length === 0) return undefined - return tag.split('/')[0] -} - /** * Orders loader paths as subdirectories before files at every level, * alphabetical within each group. The asset API returns models in storage @@ -411,11 +391,13 @@ function createAssetService() { } /** * Walks every `models`-tagged asset once and buckets each into the folder - * categories carried by its `model_type:` tags. A single asset lands in every - * category it is tagged with (e.g. a shared-root model in both `checkpoints` - * and `diffusion_models`). Which folders are actually shown is decided by - * `/experiment/models`; models with no category tag are dropped with a warning - * rather than hidden silently. + * categories `getAssetCategories` resolves for it. A single asset lands in + * every category it is tagged with (e.g. a shared-root model in both + * `checkpoints` and `diffusion_models`); an asset covered by `model_type:` + * tags is grouped by those alone, so a legacy bare-tag twin left over from a + * partial re-tagging cannot also cross-list it into another folder. Which + * folders are actually shown is decided by `/experiment/models`; models with + * no category tag are dropped with a warning rather than hidden silently. */ async function buildModelBuckets( modelTypeMode: boolean @@ -426,9 +408,7 @@ function createAssetService() { const buckets = new Map() for (const asset of assets) { - const folders = asset.tags - .map((tag) => modelFolderFromTag(tag, modelTypeMode)) - .filter((folder): folder is string => folder !== undefined) + const folders = [...new Set(getAssetCategories(asset, modelTypeMode))] if (folders.length === 0) { console.warn(