comfyui-mcp
Version:
Local-first, agent-native control plane for ComfyUI — MCP server + autonomous sidebar agent that drives your live graph in natural language on ANY LLM: Claude/ChatGPT/Gemini on your subscription (no API key), free local models via Ollama (fully offline),
243 lines • 12.2 kB
JavaScript
/**
* Background registry for model downloads.
*
* `download_model` used to await the whole transfer before returning anything.
* On a multi-GB checkpoint that left the agent's tool call pending for minutes:
* the turn stayed alive burning tokens with nothing to say, and when the user
* forced a message to break the apparent hang, the pending call was cancelled —
* so the agent reported the download as not done. The stream kept writing to
* disk regardless, which is the worst version of the bug: the file arrives and
* the agent tells you it didn't. (Reported in Discord; issue #290.)
*
* This registry lets the tool hand back a handle instead of blocking forever,
* mirroring the move generation already made from a blocking `run_workflow` to
* `enqueue_workflow` + `get_job_status`.
*/
import { createHash } from "node:crypto";
import { downloadModel, resolveDownloadTarget, shouldDispatchDownloadToManager, } from "./model-resolver.js";
import { logger } from "../utils/logger.js";
// The in-flight registry is indexed under MULTIPLE keys per job so dedup is
// ROUTE-INDEPENDENT: a repeated request finds the in-flight job whether or not the
// Manager↔local route flipped between calls (#420 codex round 2). One Entry object
// is shared by all its keys.
const jobs = new Map();
/**
* Point `key` at `entry`, ENTRY-SCOPED (#420 codex round 3, rule 2/3): never clobber
* a row currently owned by a DIFFERENT, still-in-flight writer — that would orphan a
* live download's index. Records the key on the entry so it can be retired later.
*/
function registerKey(entry, key) {
const cur = jobs.get(key);
if (cur && cur !== entry && cur.job.status === "downloading")
return;
if (!entry.keys.includes(key))
entry.keys.push(key);
jobs.set(key, entry);
}
/**
* Retire a superseded (done/error) entry, ENTRY-SCOPED (#420 codex round 3, rule 2):
* only delete an index row that STILL points at THIS entry, so retiring an older job
* can never delete a key that has since been reassigned to a newer/live entry.
*/
function retireEntry(entry) {
for (const key of entry.keys) {
if (jobs.get(key) === entry)
jobs.delete(key);
}
}
/** The tray keys rows on a hash of the SOURCE URL; match it so both agree. */
export function downloadIdFor(url) {
return createHash("sha256").update(url).digest("hex").slice(0, 16);
}
/**
* The download's DISTINCT public id — a hash of the canonical resolved on-disk
* `targetPath` (from the shared resolveDownloadTarget), used as BOTH the registry
* key and `job.id`. Identity is the DESTINATION, not the URL: two requests that
* resolve to the SAME file are one job/one writer (even from different URLs);
* requests to different destinations are separately pollable via download_status.
*/
export function downloadJobIdFor(targetPath) {
return createHash("sha256").update(targetPath).digest("hex").slice(0, 16);
}
/**
* ROUTE-INDEPENDENT request key: a canonical, collision-safe JSON-encoded tuple of
* {url, trimmed subfolder, filename}. Derived ONLY from the request inputs — never
* from the resolved destination or the chosen route — so a repeated call for the
* SAME request adopts the in-flight job regardless of a Manager↔local reachability
* flip between calls (#420 codex round 2). Every job is indexed under this key; a
* locally-resolvable job is ALSO indexed under its destination key (below) so the
* "two different URLs → one local destination → one writer" dedup still holds.
*/
function requestDownloadKey(url, targetSubfolder, filename) {
const canonical = JSON.stringify([
url,
String(targetSubfolder ?? "").trim(),
filename ?? null,
]);
return createHash("sha256").update(canonical).digest("hex").slice(0, 16);
}
/**
* Start a download, or adopt one already running for the same on-disk destination.
*
* The adoption case matters: the visible symptom of the old bug was "the agent
* looks stuck", and the natural user response is to ask again. Without this,
* the second ask starts a SECOND stream onto the same target path — two writers,
* one file. Returning the in-flight job makes a repeated request harmless.
*
* Async because the destination is resolved by the SAME code the write uses
* (resolveDownloadTarget), so the job is keyed by the exact `targetPath` the file
* lands at — any two inputs resolving to one file are one job — and an INVALID
* input (blank / path-ful filename, escaping subfolder) is REJECTED here, up
* front, exactly as the download itself would reject it (never basename'd into a
* collision with a valid request). REMOTE mode still resolves a canonical
* server-side targetPath for identity even though the bytes are fetched by the
* Manager, so duplicate remote dispatches to one destination also dedupe.
*/
export async function startDownloadJob(url, targetSubfolder, filename, auth,
/** Post-download work (sidecars, type checks). Its lines land on `job.notes`
* so they reach the user even when the download outlives the tool call. */
onComplete) {
// Identity depends on mode. LOCAL: resolve the canonical on-disk destination
// with the SAME resolver the write uses (throws on an invalid filename/subfolder
// — surfaced immediately, matching downloadModel's own rejection — and keys by
// the exact targetPath so identity is the destination, not the URL). REMOTE:
// there is NO local filesystem — downloadModel short-circuits to the Manager
// dispatch, so resolving a local models dir would wrongly THROW (COMFYUI_PATH
// unset). Key by a canonical remote identity instead and let downloadModel take
// the Manager path. shouldDispatchDownloadToManager() also covers the #420
// reconnect case: a nominally-local session whose effective base was lost still
// routes through the connected Manager (and must NOT try to resolve a local
// targetPath, which would throw), keying by the same canonical remote identity.
//
// CRITICAL: evaluate the route EXACTLY ONCE and thread it into downloadModel
// below. The predicate awaits live /system_stats and reads mutable base config,
// so a reconnect/reachability flip between two evaluations would split the job —
// Manager-key + local-writer (or a duplicate job) for one request (#420 codex
// round 1). One decision keys the identity AND drives the writer.
const dispatchToManager = await shouldDispatchDownloadToManager();
// DEDUP INDEX (route-independent) vs WRITER ROUTE (the single decision above) are
// deliberately separated (#420 codex round 2). The in-flight lookup/registration
// is keyed by the REQUEST (url+subfolder+filename), which NEVER changes with the
// route — so two separate calls for one request with a Manager↔local flip between
// them still resolve to ONE job (no same-file double-write). When the request is
// locally resolvable we ALSO index the destination-path key, preserving the
// "two different URLs → same local destination → one writer" dedup (WS-4). An
// invalid filename/subfolder is REJECTED here (by resolveDownloadTarget), up
// front, exactly as the write would reject it.
const reqKey = requestDownloadKey(url, targetSubfolder, filename);
let destKey;
if (!dispatchToManager) {
const target = await resolveDownloadTarget(url, targetSubfolder, filename);
destKey = downloadJobIdFor(target.targetPath);
}
// The PUBLIC id (download_status handle): the destination key when we have one
// (so distinct destinations are separately pollable), else the request key.
const id = destKey ?? reqKey;
// This call's keys: the route-independent request key, plus the destination key
// when locally resolvable (two-URLs-one-dest dedup).
const lookupKeys = destKey ? [reqKey, destKey] : [reqKey];
const trayId = downloadIdFor(url);
// ADOPT ONLY AN IN-FLIGHT ENTRY (#420 codex round 3, rule 3): a FINISHED entry
// under any key is treated as absent — it must never shadow a currently-downloading
// writer reachable under another key, nor cause a retire that overwrites a live row.
// Scan every key and prefer an in-flight match.
let adopted;
for (const k of lookupKeys) {
const e = jobs.get(k);
if (e && e.job.status === "downloading") {
adopted = e;
break;
}
}
if (adopted) {
// Re-index THIS call's keys onto the adopted entry (#420 codex round 3, rule 1):
// when URL B adopts URL A's in-flight job by destination, B's request key must
// now point at the same entry too — otherwise a later repeat of B (especially
// after a local→Manager flip that drops the destination key) would miss it and
// start a second writer onto one file. Entry-scoped, so it can't steal a live row.
for (const k of lookupKeys)
registerKey(adopted, k);
logger.info(`Download already in flight, adopting it: ${adopted.job.id}`, {
url,
target_subfolder: targetSubfolder,
filename,
});
return adopted;
}
// No in-flight match. Retire any superseded (done/error) entries shadowing our keys
// — entry-scoped, so we only clear rows still pointing at that finished entry and
// never delete a row now owned by a different, live writer (rule 2).
const retired = new Set();
for (const k of lookupKeys) {
const e = jobs.get(k);
if (e && !retired.has(e)) {
retireEntry(e);
retired.add(e);
}
}
const job = {
id,
trayId,
url,
target_subfolder: targetSubfolder,
filename,
status: "downloading",
started_at: Date.now(),
};
// The promise is stored, never left dangling — an unhandled rejection here
// would take down the process on a simple 404.
const settled = downloadModel(url, targetSubfolder, filename, auth, dispatchToManager)
.then(async (path) => {
job.path = path;
if (onComplete) {
// Post-processing must not turn a landed file into a failed download —
// the bytes are on disk either way, and reporting "error" here would
// reproduce the very bug this registry exists to fix.
try {
job.notes = await onComplete(path);
}
catch (err) {
job.notes = [
`(post-download step failed: ${err instanceof Error ? err.message : String(err)} — the file itself downloaded fine)`,
];
}
}
job.status = "done";
job.finished_at = Date.now();
})
.catch((err) => {
job.status = "error";
job.error = err instanceof Error ? err.message : String(err);
job.finished_at = Date.now();
});
// Index under EVERY key (deduped) so any of them adopts this one writer. Uses the
// entry-scoped registerKey guard so a fresh registration can never overwrite a row
// still owned by a different, live writer (rule 3).
const entry = { job, settled, keys: [] };
for (const k of new Set(lookupKeys))
registerKey(entry, k);
return entry;
}
export function getDownloadJob(id) {
// The registry indexes each job under its request key and (when local) its
// destination key; the public id is one of those, so a direct get resolves it.
return jobs.get(id)?.job;
}
export function listDownloadJobs() {
// One Entry is indexed under multiple keys — dedup by identity so a job appears
// once regardless of how many keys point at it.
const seen = new Set();
const out = [];
for (const e of jobs.values()) {
if (seen.has(e))
continue;
seen.add(e);
out.push(e.job);
}
return out.sort((a, b) => b.started_at - a.started_at);
}
/** Test seam — the registry is process-global otherwise. */
export function resetDownloadJobs() {
jobs.clear();
}
//# sourceMappingURL=download-jobs.js.map