diff --git a/browser_tests/fixtures/assetApiFixture.ts b/browser_tests/fixtures/assetApiFixture.ts index 0242aacbb2d..15d08c87241 100644 --- a/browser_tests/fixtures/assetApiFixture.ts +++ b/browser_tests/fixtures/assetApiFixture.ts @@ -35,8 +35,8 @@ export function countAssetRequestsByTag( } type ModelLibraryOptions = { - operators?: AssetOperator[] folders?: ModelFolderInfo[] + operators?: AssetOperator[] } export const assetApiFixture = base.extend<{ 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 3ef38c86010..ad667b1968c 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, @@ -52,8 +54,8 @@ test.use({ test.describe('Model library sidebar - asset mode', () => { test.use({ modelLibraryOptions: { - operators: [withModels(WALK_ASSETS)], - folders: REGISTERED_FOLDERS + folders: REGISTERED_FOLDERS, + operators: [withModels(WALK_ASSETS)] } }) @@ -296,8 +298,8 @@ test.describe('Model library sidebar - asset mode when the walk fails', () => { test.describe('Model library sidebar - asset mode before the loader_path cutover', () => { test.use({ modelLibraryOptions: { - operators: [withModels([MODEL_TYPE_CHECKPOINT_PRE_CUTOVER])], - folders: REGISTERED_FOLDERS + folders: REGISTERED_FOLDERS, + operators: [withModels([MODEL_TYPE_CHECKPOINT_PRE_CUTOVER])] } }) @@ -342,20 +344,120 @@ test.describe('Model library sidebar - asset mode before the loader_path cutover }) }) -test.describe('Model library sidebar - asset mode on bare-tag backends', () => { +test.describe('Model library sidebar - asset mode with a legacy bare tag', () => { test.use({ modelLibraryOptions: { - operators: [withModels([STABLE_CHECKPOINT])], folders: [ { name: 'checkpoints', folders: ['/models/checkpoints'], extensions: ['.safetensors'] } + ], + operators: [ + withModels([ + MODEL_TYPE_CHECKPOINT_ROOT, + MODEL_TYPE_CHECKPOINT_LEGACY_TAG + ]) + ] + } + }) + + test.beforeEach(async ({ assetApi: _, comfyPage }) => { + await comfyPage.featureFlags.setServerFlagsPersistent({ + supports_model_type_tags: true + }) + }) + + 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.use({ + modelLibraryOptions: { + folders: [ + { + name: 'checkpoints', + folders: ['/models/checkpoints'], + extensions: ['.safetensors'] + }, + { + name: 'loras', + folders: ['/models/loras'], + extensions: ['.safetensors'] + } + ], + operators: [ + withModels([ + MODEL_TYPE_CHECKPOINT_ROOT, + MODEL_TYPE_CHECKPOINT_MID_RETAG, + MODEL_TYPE_LORA + ]) ] } }) + test.beforeEach(async ({ assetApi: _, comfyPage }) => { + await comfyPage.featureFlags.setServerFlagsPersistent({ + supports_model_type_tags: true + }) + await comfyPage.menu.modelLibraryTab.open() + }) + + 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.use({ + modelLibraryOptions: { + folders: [ + { + name: 'checkpoints', + folders: ['/models/checkpoints'], + extensions: ['.safetensors'] + } + ], + operators: [withModels([STABLE_CHECKPOINT])] + } + }) + test.beforeEach(async ({ assetApi: _, page }) => { await new FeatureFlagHelper(page).mockServerFeatures({}) }) diff --git a/src/platform/assets/services/assetService.ts b/src/platform/assets/services/assetService.ts index 2ae17cfe200..936bbbe88fd 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' @@ -223,26 +223,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 @@ -412,11 +392,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 @@ -427,9 +409,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(