The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Workers: find model files under ComfyUI's older folder names
Adarshfd9fa1b2ba2a
diff --git a/bin/lib/worker/comfy.mjs b/bin/lib/worker/comfy.mjsindex baa54069..4e774f33 100644--- a/bin/lib/worker/comfy.mjs+++ b/bin/lib/worker/comfy.mjs@@ -16,6 +16,7 @@ import fs from "node:fs";import net from "node:net";import path from "node:path";import { fileURLToPath } from "node:url";+import { twinFolders } from "./inventory.mjs";const here = path.dirname(fileURLToPath(import.meta.url));@@ -72,14 +73,19 @@ export const folders = (workDir) => ({* extra_model_paths.yaml: the model folders, for reading. With `o.first` (a ComfyUI that knows `is_default`), its search* looks in them before anywhere else, in the order given: before its own models folder and the folders that its own* extra_model_paths.yaml adds. ComfyUI puts each `is_default` folder at the front of its list, so they are written in- * reverse. The worker checks the copy that this search finds first (inventory.mjs).+ * reverse. A folder name with an older twin (text_encoders and clip, diffusion_models and unet: ComfyUI reads both as+ * one) gets both subfolders, the name's own first (twinFolders), so a file under clip/ or unet/ is found too. The worker+ * checks the copy that this search finds first (inventory.mjs).*/export function modelPathsYaml(modelDirs, folderNames, o = {}) {const lines = [];const entries = modelDirs.map((dir, i) => [dir, i]);+ // Each subfolder once, under its own name (ComfyUI maps an older name to the folder it stands for).+ const subs = [...new Set(folderNames.flatMap((f) => twinFolders(f)))];for (const [dir, i] of o.first ? entries.reverse() : entries) {lines.push(`fuobe_worker_${i}:`, ` base_path: ${JSON.stringify(dir)}`, ...(o.first ? [" is_default: true"] : []));- for (const f of folderNames) lines.push(` ${f}: ${f}`);+ // is_default puts each one at the front: the twin is written before the name's own, so the name's own comes first.+ for (const f of o.first ? [...subs].reverse() : subs) lines.push(` ${f}: ${f}`);}return `${lines.join("\n")}\n`;}diff --git a/bin/lib/worker/inventory.mjs b/bin/lib/worker/inventory.mjsindex 7149682b..1cef7c1e 100644--- a/bin/lib/worker/inventory.mjs+++ b/bin/lib/worker/inventory.mjs@@ -77,6 +77,16 @@ const LEGACY = { unet: "diffusion_models", clip: "text_encoders" };const OWN_SUBFOLDERS = { diffusion_models: ["unet", "diffusion_models"], text_encoders: ["text_encoders", "clip"] };const canonical = (folder) => LEGACY[folder] ?? folder;+/**+ * The subfolders of one of the worker's model folders where its ComfyUI looks for a folder name's files, in the order it+ * looks: the name itself, then its older twin (text_encoders also reads clip, diffusion_models also reads unet), as the+ * worker's extra_model_paths.yaml lists them (comfy.mjs modelPathsYaml).+ */+export function twinFolders(folder) {+ const name = canonical(folder);+ return [name, ...Object.keys(LEGACY).filter((old) => LEGACY[old] === name)];+}+/** A path from an extra_model_paths.yaml with ~ and environment variables ($X, ${X}, and %X% on Windows) filled in. */function expand(p, env, home) {const vars = (name, whole) => env[name] ?? env[Object.keys(env).find((k) => k.toUpperCase() === name.toUpperCase()) ?? ""] ?? whole;@@ -187,7 +197,8 @@ const samePath = (a, b) => (process.platform === "win32" ? path.resolve(a).toLow* The verified files: for every file the catalog names with a pinned SHA-256 (`pins`, else the index's own), the copy* that ComfyUI finds first, with the exact size and that hash. ComfyUI looks in the model folders in order and takes the* first one that has the file, so only that copy counts: one of the wrong size or with another hash isn't reported,- * even when a later folder has a good copy. Every copy where ComfyUI itself also looks (`o.comfyDir`: its own models+ * even when a later folder has a good copy. In each model folder it looks under the folder's name and then under its+ * older twin (twinFolders: text_encoders/x and then clip/x). Every copy where ComfyUI itself also looks (`o.comfyDir`: its own models* folder and its extra_model_paths.yaml) must be the same file too, or it isn't reported. A file with no pin isn't* reported at all, so no job that needs it comes here.*/@@ -223,12 +234,15 @@ export async function modelFiles(modelDirs, index, cacheFile, pins = filePins(),if (typeof want !== "string" || !/^[0-9a-f]{64}$/.test(want)) continue;let first = null;let firstPath = null;- for (const root of modelDirs ?? []) {- const p = path.join(root, f.folder, f.file);- first = await look(p, f, want);- if (first) {- firstPath = p;- break;+ // In each model folder, the folder's own name first, then its older twin (as the worker's ComfyUI looks).+ search: for (const root of modelDirs ?? []) {+ for (const sub of twinFolders(f.folder)) {+ const p = path.join(root, sub, f.file);+ first = await look(p, f, want);+ if (first) {+ firstPath = p;+ break search;+ }}}if (!first?.ok) continue;diff --git a/docs/gpu-workers.md b/docs/gpu-workers.mdindex 0c763bc1..8f067a54 100644--- a/docs/gpu-workers.md+++ b/docs/gpu-workers.md@@ -554,6 +554,9 @@ The worker checks the copy that its ComfyUI loads:worker's `extra_model_paths.yaml`), and takes the first folder that has the file. So the worker checks only thatfirst copy. If that copy has the wrong size or hash, the file is not reported, even when a later folder has a goodcopy.+- ComfyUI reads an older folder name as the same folder: `clip` for `text_encoders`, and `unet` for+ `diffusion_models`. In each model folder, the worker looks under the folder's own name first and then under its older+ name, and its `extra_model_paths.yaml` lists both in that order. So a text encoder in `models/clip` is found.- Some node packs read ComfyUI's own `models` folder directly. And an older ComfyUI that does not know `is_default`looks in its own folders first. So a copy with the same name in ComfyUI's own `models` folder (`unet` and `clip`count for `diffusion_models` and `text_encoders`), or in a folder that ComfyUI's own `extra_model_paths.yaml` adds,diff --git a/tests/worker-environment.test.ts b/tests/worker-environment.test.tsindex 5327cef7..12ac1956 100644--- a/tests/worker-environment.test.ts+++ b/tests/worker-environment.test.ts@@ -5,7 +5,7 @@ import path from "node:path";import { describe, expect, it } from "vitest";import { inputRules } from "../bin/lib/worker/checks.mjs";import { comfyArgs, comfyEnv, defaultPathsFlag, modelPathsYaml, OFFLINE_ENV, OwnComfy } from "../bin/lib/worker/comfy.mjs";-import { modelFiles, nodePacks, packFolders, yamlModelDirs } from "../bin/lib/worker/inventory.mjs";+import { modelFiles, nodePacks, packFolders, twinFolders, yamlModelDirs } from "../bin/lib/worker/inventory.mjs";import { minVramBytesFor, needsOf, VRAM_TOLERANCE } from "@/lib/lanes";import type { RequirementIndex } from "@/lib/gpu/types";import { tempDir } from "./helpers/temp-dir";@@ -112,6 +112,35 @@ describe("the worker's model files", () => {expect((await modelFiles([b, a], index, cache, pins)).map((f) => f.name).sort()).toEqual(["x.safetensors", "y.safetensors"]);});+ it("finds a file under the folder's older twin (clip for text_encoders, unet for diffusion_models), the name's own first", async () => {+ const a = tempDir("fuobe-worker-twins-a-");+ const b = tempDir("fuobe-worker-twins-b-");+ for (const d of ["clip", "unet", "text_encoders"]) fs.mkdirSync(path.join(a, d));+ fs.mkdirSync(path.join(b, "text_encoders"));+ // Only under the twin: found there.+ fs.writeFileSync(path.join(a, "clip", "t.safetensors"), "GOOD!");+ fs.writeFileSync(path.join(a, "unet", "u.safetensors"), "GOOD!");+ // Under both names in one folder: the name's own copy is the one ComfyUI finds first, and it decides.+ fs.writeFileSync(path.join(a, "text_encoders", "w.safetensors"), "EVIL!");+ fs.writeFileSync(path.join(a, "clip", "w.safetensors"), "GOOD!");+ // The twin of an earlier folder comes before a later folder: its wrong copy isn't made up for by the good one.+ fs.writeFileSync(path.join(a, "clip", "v.safetensors"), "EVIL!");+ fs.writeFileSync(path.join(b, "text_encoders", "v.safetensors"), "GOOD!");+ type F = { folder: string; file: string; sizeBytes: number };+ const f = (folder: string, file: string): F => ({ folder, file, sizeBytes: 5 });+ const index = { files: { t: f("text_encoders", "t.safetensors"), u: f("diffusion_models", "u.safetensors"), w: f("text_encoders", "w.safetensors"), v: f("text_encoders", "v.safetensors") } };+ const pins = Object.fromEntries(Object.values(index.files).map((x) => [`${x.folder}/${x.file}`, sha("GOOD!")]));+ const cache = path.join(tempDir("fuobe-worker-cache-"), "hash-cache.json");+ const found = await modelFiles([a, b], index, cache, pins);+ expect(found).toEqual([+ { folder: "text_encoders", name: "t.safetensors", size: 5, sha256: sha("GOOD!") },+ { folder: "diffusion_models", name: "u.safetensors", size: 5, sha256: sha("GOOD!") },+ ]);+ expect(twinFolders("text_encoders")).toEqual(["text_encoders", "clip"]);+ expect(twinFolders("diffusion_models")).toEqual(["diffusion_models", "unet"]);+ expect(twinFolders("loras")).toEqual(["loras"]);+ });+it("refuses a file that ComfyUI's own models folder or its own extra_model_paths.yaml has with other content", async () => {const models = tempDir("fuobe-worker-models-");const comfyDir = tempDir("fuobe-worker-comfy-");@@ -186,6 +215,36 @@ describe("the worker's model files", () => {fs.writeFileSync(path.join(comfyDir, "utils", "extra_config.py"), 'is_default = False\nif "is_default" in conf:\n is_default = conf.pop("is_default")\n');expect(defaultPathsFlag(comfyDir)).toBe(true);});++ it("lists each folder's older twin too, so its ComfyUI looks where the inventory looks, in the same order", () => {+ // ComfyUI's extra_config.py and folder_paths.add_model_folder_path: each line's name through map_legacy, an+ // is_default path put at the front, any other one at the end.+ const legacy: Record<string, string> = { clip: "text_encoders", unet: "diffusion_models" };+ const searchOf = (yaml: string) => {+ const lists: Record<string, string[]> = { text_encoders: ["<own>/text_encoders", "<own>/clip"], diffusion_models: ["<own>/unet", "<own>/diffusion_models"], loras: ["<own>/loras"] };+ for (const block of yaml.split(/^(?=\S)/m).filter(Boolean)) {+ const base = /base_path: "(.+)"/.exec(block)![1];+ const first = /is_default: true/.test(block);+ for (const m of block.matchAll(/^ {2}(\w+): (\w+)$/gm)) {+ if (m[1] === "is_default") continue;+ const list = (lists[legacy[m[1]] ?? m[1]] ??= []);+ const p = `${base}/${m[2]}`;+ if (first) list.unshift(p);+ else list.push(p);+ }+ }+ return lists;+ };+ const folders = ["diffusion_models", "loras", "text_encoders"];+ const first = searchOf(modelPathsYaml(["D:/a", "E:/b"], folders, { first: true }));+ expect(first.text_encoders).toEqual(["D:/a/text_encoders", "D:/a/clip", "E:/b/text_encoders", "E:/b/clip", "<own>/text_encoders", "<own>/clip"]);+ expect(first.diffusion_models).toEqual(["D:/a/diffusion_models", "D:/a/unet", "E:/b/diffusion_models", "E:/b/unet", "<own>/unet", "<own>/diffusion_models"]);+ expect(first.loras).toEqual(["D:/a/loras", "E:/b/loras", "<own>/loras"]);+ // An older ComfyUI: after its own folders, each model folder's name and then its twin.+ const older = searchOf(modelPathsYaml(["D:/a", "E:/b"], folders));+ expect(older.text_encoders).toEqual(["<own>/text_encoders", "<own>/clip", "D:/a/text_encoders", "D:/a/clip", "E:/b/text_encoders", "E:/b/clip"]);+ expect(older.diffusion_models.slice(2)).toEqual(["D:/a/diffusion_models", "D:/a/unet", "E:/b/diffusion_models", "E:/b/unet"]);+ });});describe("its own ComfyUI's environment", () => {