Skip to content

armgit / fuobe

Public Imported from GitHub
0

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.

50.5 KB
Raw Blame
/**
 * What went wrong for an account's own GPU or its own key, in plain words, with the ways out: one sentence for the
 * person, a short details line for the curious, the buttons that help, and whether waiting can fix it. Pure, so the
 * server (Test, the status check, a run's hand-over, its polls and the copy of its result) and the browser (Settings →
 * Your GPU, the composer's check before Run, a failed run's tile) say the same thing (tests/diagnose.test.ts).
 *
 * Only an account's own GPU and keys are read this way. The owner's runs and runs on the studio's GPUs keep their words.
 */
import { OUT_OF_MEMORY } from "./comfy-reason";
import { knownProblemOf } from "./gpu/known-problems";
import { balanceMessage, CLOUD_KEY, keyRejectedMessage, type CloudProvider } from "./your-keys";

/** RunPod's list of pods, where a stopped pod is started again. */
export const RUNPOD_CONSOLE_URL = "https://console.runpod.io/pods";
/** ComfyUI's own guide to updating it (every install kind: portable, Desktop, git). */
export const COMFY_UPDATE_URL = "https://docs.comfy.org/installation/update_comfyui";

/** Where a provider account gets credit. */
export const BILLING_URL: Record<CloudProvider, string> = { fal: "https://fal.ai/dashboard/billing", replicate: "https://replicate.com/account/billing" };

export type TroubleCause =
  // Your GPU: the address
  | "address-fixed"
  | "address-format"
  | "address-credentials"
  | "address-local"
  | "address-studio"
  | "checks-too-fast"
  // Your GPU: reaching it
  | "gpu-stopped"
  | "gpu-starting"
  | "gpu-offline"
  | "gpu-not-found"
  | "gpu-certificate"
  | "gpu-password"
  | "gpu-password-wrong"
  | "gpu-sign-in"
  | "gpu-not-comfy"
  | "gpu-busy"
  // Your GPU: a run on it
  | "gpu-too-big"
  | "gpu-slow-upload"
  | "gpu-refused"
  | "gpu-error"
  | "gpu-out-of-memory"
  | "gpu-stopped-run"
  | "gpu-job-error"
  /** A model file a loader can't read (damaged, cut short) or doesn't have: words name the file. */
  | "gpu-model-file"
  /**
   * A known problem of the person's ComfyUI version on their system (src/lib/gpu/known-problems.ts: ComfyUI 0.43 on a Linux
   * Python without memfd_create): every run stops the same way until ComfyUI is updated, so never Retry.
   */
  | "gpu-comfy-known"
  | "gpu-restarted"
  | "gpu-moved"
  | "gpu-went-away"
  | "gpu-save-failed"
  | "gpu-isolated"
  | "gpu-too-long"
  // Your keys
  | "key-rejected"
  | "key-balance"
  | "key-permission"
  | "model-unavailable"
  | "bad-input"
  | "content-flag"
  | "provider-busy"
  | "provider-down"
  | "provider-unreachable"
  | "provider-failed"
  | "provider-stopped"
  | "no-result"
  | "result-expired"
  | "provider-too-long"
  | "unknown";

export type TroubleActionKind =
  | "retry"
  | "check-gpu"
  | "gpu-settings"
  | "key-settings"
  | "run-with-key"
  | "add-credit"
  | "pick-model"
  /** Open the page where the GPU is started (RunPod's pods). */
  | "start-gpu"
  /** Pair a ComfyUI with a code (/connect): no web address needed. */
  | "pair"
  /** Run on the ComfyUI on this computer instead. */
  | "use-computer"
  /**
   * Hosted runs: run this once on this install's GPUs ("Run on Our GPUs"), offered only where they take it. One-off:
   * the account's pick of where its runs go stays as it was.
   */
  | "run-on-site"
  /** Send a smaller copy of the file the GPU's address refused as too big (a run made again with it, on a click). */
  | "send-smaller"
  /** Run it again one size or length smaller (out of memory), on a click. */
  | "try-smaller"
  /** Open where a model file downloads from (an https link: the file, or a gated file's Hugging Face page). */
  | "get-file"
  /** Open how to update ComfyUI (an https link: ComfyUI's own guide). */
  | "update-comfy";

export interface TroubleAction {
  kind: TroubleActionKind;
  label: string;
  /** A link (start-gpu, add-credit, gpu-settings, key-settings, pair, get-file, update-comfy) or the provider (run-with-key). */
  target?: string;
  /** run-with-key: the model that runs this tool on that key (its cloud version), and its operation there. */
  modelId?: string;
  op?: string;
}

export interface Diagnosis {
  cause: TroubleCause;
  /** One plain sentence: what is wrong and what to do. Never a status code, an address or a program's own words. */
  words: string;
  /** For the curious: the status, the kind of address (never the address itself) and the reason, short. */
  details?: string;
  actions: TroubleAction[];
  /** Waiting can fix it: the run (or the check) tries again by itself. */
  retryable: boolean;
  /** The other side said when to try again. */
  retryAfterMs?: number;
}

/** What a failed run keeps of its diagnosis (the words are the run's error): generations.fix_json `trouble`. */
export interface RunTrouble {
  cause: TroubleCause;
  details?: string;
  actions: TroubleAction[];
}

export const troubleOf = (d: Diagnosis): RunTrouble => ({ cause: d.cause, ...(d.details && { details: d.details }), actions: d.actions });

// ---------------------------------------------------------------------------
// Where the GPU is

/** An account's GPU: a saved address, a ComfyUI paired with a code (Fuobe Connect), or the ComfyUI on this computer. */
export type GpuPlace = { kind: "link"; url: string; hasToken?: boolean } | { kind: "connect"; name?: string } | { kind: "computer" };

/** <pod id>-<port>.proxy.runpod.net: RunPod's web address of a pod's port. */
const RUNPOD_HOST = /^[a-z0-9]+-(\d{2,5})\.proxy\.runpod\.net$/i;
const TUNNEL_HOST = /(^|\.)(trycloudflare\.com|ngrok-free\.app|ngrok-free\.dev|ngrok\.app|ngrok\.dev|ngrok\.io|loca\.lt)$/i;

/** What kind of address a GPU has: a RunPod pod's port, a quick tunnel, or another web address. */
export function gpuHostOf(url: string): { kind: "runpod"; port: number } | { kind: "tunnel" } | { kind: "web" } {
  let host = "";
  try {
    host = new URL(url).hostname.toLowerCase();
  } catch {
    return { kind: "web" };
  }
  const pod = RUNPOD_HOST.exec(host);
  if (pod) return { kind: "runpod", port: Number(pod[1]) };
  return TUNNEL_HOST.test(host) ? { kind: "tunnel" } : { kind: "web" };
}

/**
 * A GPU's address said by its kind, for the details line and logs: "RunPod pod, port 8188", "your tunnel", "your web
 * address". Never the address itself: a pod's or a tunnel's address is enough to reach a ComfyUI without a password.
 */
export function gpuPlaceWords(url: string): string {
  const k = gpuHostOf(url);
  return k.kind === "runpod" ? `RunPod pod, port ${k.port}` : k.kind === "tunnel" ? "your tunnel" : "your web address";
}

/** The host names (and host:port) of an address, to take out of a text that names them. */
function hostsOf(url: string): string[] {
  try {
    const u = new URL(url);
    return [...new Set([u.host, u.hostname].filter((h) => h.length > 2))].sort((a, b) => b.length - a.length);
  } catch {
    return [];
  }
}

/** An IP address with its port: v4, v6 in brackets, or the v6 a socket error names after its code ("ECONNREFUSED ::1:443"). */
const IP_PORT = /\[[0-9a-f:]{2,}\](?::\d{1,5})?|\b(?:\d{1,3}\.){3}\d{1,3}(?::\d{1,5})?\b/gi;
const SOCKET_V6 = /(\bE[A-Z]{3,}\s+)[0-9a-f]*:[0-9a-f:.]+/g;

/**
 * A text with a GPU's address taken out: its host (and any IP address and port, a socket error's "1.2.3.4:443") become
 * "your GPU". For the details line, logs and anything kept with a run.
 */
export function withoutAddress(text: string, url?: string): string {
  let t = text;
  for (const h of url ? hostsOf(url) : []) t = t.split(h).join("your GPU");
  return t.replace(SOCKET_V6, "$1your GPU").replace(IP_PORT, "your GPU");
}

/** Credentials a text may echo back (a page that repeats the request's headers): taken out. */
export function withoutSecrets(text: string): string {
  return text
    .replace(/\b(Bearer|Basic|Key|Token)\s+[A-Za-z0-9._~+/=:-]{6,}/gi, "$1 ••••")
    .replace(/("?(?:authorization|x-api-key|api[_-]?key|access[_-]?token|token|password|secret)"?\s*[:=]\s*"?)[^"\s,;}&]{4,}/gi, "$1••••");
}

// ---------------------------------------------------------------------------
// The details line

/** A short details line from an answer's text: an HTML page's title (or nothing), other text on one line, cut short. */
export function cleanDetails(text: string | undefined, max = 160): string {
  const t = withoutSecrets(String(text ?? ""));
  if (!t.trim()) return "";
  if (/^\s*</.test(t) || /<html|<!doctype/i.test(t)) {
    const title = /<title[^>]*>([^<]{1,200})<\/title>/i.exec(t)?.[1];
    return title ? title.replace(/\s+/g, " ").trim().slice(0, max) : "";
  }
  const one = t.replace(/\s+/g, " ").trim();
  return one.length > max ? `${one.slice(0, max - 1)}…` : one;
}

const joinDetails = (...parts: (string | undefined | false)[]) => parts.filter(Boolean).join(" · ") || undefined;

// ---------------------------------------------------------------------------
// Buttons

const A = {
  retry: (): TroubleAction => ({ kind: "retry", label: "Retry" }),
  check: (): TroubleAction => ({ kind: "check-gpu", label: "Check my GPU" }),
  gpuSettings: (label = "Open Settings → Your GPU"): TroubleAction => ({ kind: "gpu-settings", label, target: "/settings#your-gpu" }),
  keySettings: (label = "Open Settings → Your keys"): TroubleAction => ({ kind: "key-settings", label, target: "/settings#your-keys" }),
  startPod: (): TroubleAction => ({ kind: "start-gpu", label: "Start your pod", target: RUNPOD_CONSOLE_URL }),
  pair: (): TroubleAction => ({ kind: "pair", label: "Pair with a code", target: "/connect" }),
  pick: (): TroubleAction => ({ kind: "pick-model", label: "Pick another model" }),
  credit: (p: CloudProvider): TroubleAction => ({ kind: "add-credit", label: `Add credit on ${CLOUD_KEY[p].label}`, target: BILLING_URL[p] }),
  withKey: (p: CloudProvider, price?: string | null): TroubleAction => ({ kind: "run-with-key", label: `Run with your ${CLOUD_KEY[p].label} key${price ? ` · ${price}` : ""}`, target: p }),
  smaller: (): TroubleAction => ({ kind: "send-smaller", label: "Send a smaller copy" }),
  trySmaller: (): TroubleAction => ({ kind: "try-smaller", label: "Try one size smaller" }),
  connectGpu: (): TroubleAction => ({ kind: "gpu-settings", label: "Connect your GPU", target: "/settings#your-gpu" }),
  getFile: (url: string): TroubleAction => ({ kind: "get-file", label: "Get the file", target: url }),
  updateComfy: (): TroubleAction => ({ kind: "update-comfy", label: "How to update ComfyUI", target: COMFY_UPDATE_URL }),
};

/**
 * "Run with your fal.ai key": offered on a GPU trouble when the account has a key that runs this tool's cloud version
 * (`modelId`). An explicit click: it spends the person's own credit, so with its price when known ("Run with your
 * fal.ai key · ≈ $0.04").
 */
export function withKeyAction(p: CloudProvider, modelId?: string, op?: string, price?: string | null): TroubleAction {
  return { ...A.withKey(p, price), ...(modelId && { modelId }), ...(modelId && op && { op }) };
}

/** "Run on Our GPUs": this run, once, on this install's GPUs (hosted runs), by the name visitors read for them. */
export const runOnSiteAction = (placeLabel: string): TroubleAction => ({ kind: "run-on-site", label: `Run on ${placeLabel}` });

/**
 * A run's buttons with "Run with your … key" right after the first (the one that fixes the GPU): a tile that shows only a
 * few keeps it. Once only.
 */
export function withOfferAfterFirst(actions: TroubleAction[], offer: TroubleAction | null | undefined): TroubleAction[] {
  if (!offer || actions.some((a) => a.kind === "run-with-key")) return actions;
  return [...actions.slice(0, 1), offer, ...actions.slice(1)];
}

// ---------------------------------------------------------------------------
// Your GPU

/** Where the GPU was asked: a check (Settings' Test or Connect, the status check) or a step of a run. */
export type GpuStage = "test" | "status" | "upload" | "prompt" | "poll" | "result";

/** What came back from the GPU (or didn't): an answer's status and body, or a network error's message. */
export interface GpuFailure {
  stage: GpuStage;
  status?: number;
  /** The answer's text (its first part is enough). */
  body?: string;
  contentType?: string;
  /** A redirect's Location. */
  location?: string;
  /** A network error's message (no answer at all). */
  error?: string;
  /** The size of the file sent (an upload answered 413). */
  bytes?: number;
  retryAfterS?: number;
}

const NOT_FOUND = /ENOTFOUND|EAI_AGAIN|EAI_NONAME|getaddrinfo|Couldn't resolve|couldn't find/i;
const CERTIFICATE = /certificate|CERT_|self[- ]signed|SSL|TLS|unable to verify|ERR_TLS|wrong version number/i;
const PRIVATE = /won't fetch .*(this machine|private network)/i;
const TOO_LONG = /took over \d+ s|Timed out|ETIMEDOUT|timeout/i;
const NGROK_OFFLINE = /ERR_NGROK_3200|ERR_NGROK_8012|endpoint .{0,40}is offline|tunnel .{0,40}not found/i;
const CF_TUNNEL_DOWN = /Error 1033|Argo Tunnel error|Cloudflare Tunnel error/i;
/** cloudflared (or another tunnel) is up, but nothing listens behind it: ComfyUI is closed. */
const ORIGIN_DOWN = /unable to reach the origin|origin service .{0,40}(down|not responding)|connection refused.{0,40}(127\.0\.0\.1|localhost)|dial tcp .{0,60}connect: connection refused/i;

const isHtml = (f: GpuFailure) => /text\/html/i.test(f.contentType ?? "") || /^\s*</.test(f.body ?? "");

const mb = (n: number) => Math.max(1, Math.ceil(n / 2 ** 20));

/** A check (Test, Connect, the status strip) or a run: the words end in "check again" or in "press Retry". */
const isCheck = (s: GpuStage) => s === "test" || s === "status";

/**
 * A GPU that didn't answer as ComfyUI, read from what came back: the cause, plain words, a details line and the buttons
 * that help. `place` names what the GPU is (a RunPod pod's port gets its own words and "Start your pod").
 */
export function diagnoseGpu(f: GpuFailure, place: GpuPlace): Diagnosis {
  const d = readGpuFailure(f, place);
  // At ComfyUI's /prompt, a failure that may have reached it (a reset, a timeout, a proxy's 5xx) is never sent again: the
  // job may be on the GPU already. Only what proves nothing arrived (nothing listens, no such name, a stopped pod, a
  // tunnel that is down) is waited out.
  return f.stage === "prompt" && d.retryable && !nothingArrived(f, place) ? { ...d, retryable: false } : d;
}

/** A failure that proves the request never reached ComfyUI. */
function nothingArrived(f: GpuFailure, place: GpuPlace): boolean {
  if (f.error && !f.status) return /ECONNREFUSED|ENOTFOUND|EAI_AGAIN|EAI_NONAME|getaddrinfo|Couldn't resolve/i.test(f.error);
  const pod = place.kind === "link" && gpuHostOf(place.url).kind === "runpod";
  return (pod && f.status === 404) || f.status === 530 || NGROK_OFFLINE.test(f.body ?? "") || CF_TUNNEL_DOWN.test(f.body ?? "");
}

function readGpuFailure(f: GpuFailure, place: GpuPlace): Diagnosis {
  const link = place.kind === "link" ? place : null;
  const kind = link ? gpuHostOf(link.url) : { kind: "web" as const };
  const pod = kind.kind === "runpod" ? kind : null;
  const where = link ? gpuPlaceWords(link.url) : "";
  const check = isCheck(f.stage);
  const status = f.status ?? 0;
  // What came back, without the GPU's address (its host, an IP and port) or a credential it echoed.
  const clean = (text: string | undefined, max?: number) => cleanDetails(withoutAddress(String(text ?? ""), link?.url), max);
  const said = clean(f.body);
  const retryAfterMs = f.retryAfterS && f.retryAfterS > 0 ? f.retryAfterS * 1000 : undefined;
  const detailsFor = (what?: string) => joinDetails(where, status ? `HTTP ${status}` : undefined, what);
  // How a waiting problem goes on: a check connects once it answers; a run goes on by itself, then "press Retry".
  const thenCheck = check ? "and we connect as soon as it answers" : "then press Retry";
  const gpuButtons = (): TroubleAction[] => (check ? [A.check()] : [A.retry(), A.check()]);

  // The pod's port answers nothing (404: the pod is stopped, or nothing is on that port).
  const podDown = (): Diagnosis => ({
    cause: "gpu-stopped",
    words: `Your RunPod pod isn't running, or ComfyUI isn't on port ${pod!.port}. Start your pod, ${thenCheck}.`,
    details: detailsFor(said),
    actions: [A.startPod(), ...gpuButtons(), A.gpuSettings()],
    retryable: true,
  });
  const offline = (what?: string): Diagnosis =>
    place.kind === "connect"
      ? {
          cause: "gpu-offline",
          words: `${place.name ? `${place.name} isn't` : "Your paired ComfyUI isn't"} online. Start ComfyUI there, and your runs go on by themselves.`,
          details: what,
          actions: [A.pair(), A.gpuSettings()],
          retryable: true,
        }
      : {
          cause: "gpu-offline",
          words: check
            ? "Your GPU didn't answer. Start ComfyUI and its tunnel, and we connect as soon as it answers."
            : "We can't reach your GPU. Start ComfyUI and its tunnel, then press Retry.",
          details: detailsFor(what),
          actions: [...(pod ? [A.startPod()] : []), ...gpuButtons(), A.gpuSettings()],
          retryable: true,
        };
  const starting = (): Diagnosis => ({
    cause: "gpu-starting",
    words: pod
      ? check
        ? `Your pod is on, but ComfyUI on port ${pod.port} isn't ready yet. We connect as soon as it is.`
        : `Your pod is on, but ComfyUI on port ${pod.port} wasn't ready for this run. Press Retry in a minute.`
      : check
        ? "Your GPU is starting up. This can take a few minutes, and we connect as soon as ComfyUI is ready."
        : "Your GPU is starting up, so ComfyUI wasn't ready for this run. Press Retry in a minute.",
    details: detailsFor(said),
    actions: [...(pod ? [A.startPod()] : []), ...gpuButtons(), A.gpuSettings()],
    retryable: true,
    ...(retryAfterMs && { retryAfterMs }),
  });

  // No answer at all.
  if (f.error && !status) {
    const e = f.error;
    const reason = clean(e);
    if (PRIVATE.test(e)) return diagnoseAddress("local");
    if (CERTIFICATE.test(e)) {
      return {
        cause: "gpu-certificate",
        words: "Your GPU's address has a certificate problem. Use the https address your tunnel or pod gives you.",
        details: detailsFor(reason),
        actions: [A.gpuSettings(), A.pair()],
        retryable: false,
      };
    }
    if (NOT_FOUND.test(e)) {
      return {
        cause: "gpu-not-found",
        words: "We can't find your GPU's address. If your tunnel restarted, it has a new address: paste it in Settings → Your GPU.",
        details: detailsFor(reason),
        actions: [A.gpuSettings("Paste the new address"), A.pair(), ...(check ? [] : [A.retry()])],
        retryable: true,
      };
    }
    // A big file over a slow address: sending it took longer than the upload may take (once it was going: a connection
    // that never opened is a GPU that doesn't answer).
    if (f.stage === "upload" && TOO_LONG.test(e) && !/Timed out reaching|ETIMEDOUT|ECONNREFUSED|EHOSTUNREACH/i.test(e) && (f.bytes ?? 0) > 20 * 2 ** 20) {
      return {
        cause: "gpu-slow-upload",
        words: "Sending your file to your GPU took too long. Try a shorter clip, or a smaller file.",
        details: detailsFor(reason),
        actions: [A.smaller(), A.retry()],
        retryable: false,
      };
    }
    return offline(reason);
  }

  // An answer, but not ComfyUI's.
  if (status === 401 || status === 403) {
    const wrong = !!link?.hasToken;
    return {
      cause: wrong ? "gpu-password-wrong" : "gpu-password",
      words: wrong
        ? check
          ? "That password didn't work. Check it, and paste it in Access token again."
          : "Your GPU didn't take its password. Update it in Settings → Your GPU, then press Retry."
        : check
          ? "Your GPU asks for a password. Paste it in Access token."
          : "Your GPU asks for a password. Add it in Settings → Your GPU, then press Retry.",
      details: detailsFor(),
      actions: [A.gpuSettings(wrong ? "Update the password" : "Add the password"), ...(check ? [] : [A.retry()])],
      retryable: false,
    };
  }
  if (status >= 300 && status < 400) {
    let to: URL | null = null;
    try {
      to = new URL(f.location ?? "", link?.url ?? "https://x.invalid");
    } catch {}
    const toHost = to?.hostname.toLowerCase() ?? "";
    if (toHost.endsWith(".cloudflareaccess.com")) {
      return {
        cause: "gpu-sign-in",
        words: "Your GPU's address asks for a sign-in first. Turn the sign-in off for this address, or pair your ComfyUI with a code.",
        details: detailsFor("Cloudflare Access"),
        actions: [A.pair(), A.gpuSettings()],
        retryable: false,
      };
    }
    // Where it sends: another site by its name only (never a path, a query or a password in it); the GPU's own address
    // never by name.
    const same = !!to && !!link && hostsOf(link.url).includes(to.host);
    const target = !to || !f.location ? undefined : same ? "to another page of your GPU's address" : `to ${withoutAddress(to.host, link?.url)}`;
    return {
      cause: "gpu-not-comfy",
      words: "Your GPU's address sends us to another page. Paste the final https address in Settings → Your GPU.",
      details: detailsFor(target),
      actions: [A.gpuSettings("Fix the address"), ...(check ? [] : [A.retry()])],
      retryable: false,
    };
  }
  if (pod && status === 404) return podDown();
  // A tunnel whose program isn't running, or whose ComfyUI behind it doesn't listen: ngrok's offline pages (3200: no
  // agent; 8012: the agent can't reach ComfyUI), Cloudflare's 530 or 1033, cloudflared's "unable to reach the origin".
  if (NGROK_OFFLINE.test(f.body ?? "") || ORIGIN_DOWN.test(f.body ?? "")) return offline(said || `HTTP ${status}`);
  if (status === 530 || CF_TUNNEL_DOWN.test(f.body ?? "")) return offline(said || `HTTP ${status}`);
  if (status === 502 || status === 503 || status === 504 || (status >= 520 && status <= 529)) return starting();
  if (status === 429) {
    return {
      cause: "gpu-busy",
      words: check ? "Your GPU's address is busy. We check again in a moment." : "Your GPU's address was busy. Press Retry in a minute.",
      details: detailsFor(said),
      actions: gpuButtons(),
      retryable: true,
      ...(retryAfterMs && { retryAfterMs }),
    };
  }
  if (status === 413) {
    const size = f.bytes ? ` (${mb(f.bytes)} MB)` : "";
    // A web page: the tunnel's or the pod's own size limit. Plain words: ComfyUI's own (--max-upload-size).
    return isHtml(f)
      ? {
          cause: "gpu-too-big",
          words: `This file is too big to send to your GPU's address${size}. Send a smaller copy, or pair your ComfyUI with a code.`,
          details: detailsFor(),
          actions: [A.smaller(), A.pair(), A.retry()],
          retryable: false,
        }
      : {
          cause: "gpu-too-big",
          words: `This file is too big for your ComfyUI${size}. Send a smaller copy, or start ComfyUI with a higher --max-upload-size.`,
          details: detailsFor(said),
          actions: [A.smaller(), A.retry()],
          retryable: false,
        };
  }
  if (status >= 500) {
    return {
      cause: "gpu-error",
      words: "Your ComfyUI couldn't take this run. Restart ComfyUI, then press Retry.",
      details: detailsFor(joinDetails(f.stage === "upload" ? "upload" : f.stage === "prompt" ? "start" : undefined, said)),
      actions: [A.retry(), A.check()],
      retryable: false,
    };
  }
  // Another app, a web page, a 404 off a pod, or JSON without ComfyUI's facts.
  return {
    cause: "gpu-not-comfy",
    words: pod
      ? `That address opens a different app. Use the address of ComfyUI's port (on RunPod, the one ending in -8188).`
      : "That address opens a different app. Use the address of ComfyUI's port.",
    details: detailsFor(said || (isHtml(f) ? "a web page" : undefined)),
    actions: [A.gpuSettings("Fix the address"), ...(check ? [] : [A.retry()])],
    retryable: false,
  };
}

/** An address the check refused before calling it (checkGpuAddress), in the words Settings → Your GPU shows. */
export function diagnoseAddress(problem: "format" | "scheme" | "credentials" | "local" | "studio" | "not-found", url?: string): Diagnosis {
  switch (problem) {
    case "format":
    case "scheme":
      return {
        cause: "address-format",
        words: "Paste the whole web address of your ComfyUI, starting with https://.",
        actions: [],
        retryable: false,
      };
    case "credentials":
      return { cause: "address-credentials", words: "A password goes in the Access token box, not in the address.", actions: [], retryable: false };
    case "local":
      return {
        cause: "address-local",
        words: "That address only works on your own network. Use the ComfyUI on this computer, or pair your ComfyUI with a code.",
        actions: [{ kind: "use-computer", label: "Use this computer" }, A.pair()],
        retryable: false,
      };
    case "studio":
      return { cause: "address-studio", words: "Fuobe can't use this address. Connect your own ComfyUI.", actions: [A.pair()], retryable: false };
    case "not-found":
      return {
        cause: "gpu-not-found",
        words: "We can't find this address. If your tunnel restarted, it has a new address: paste the new one.",
        ...(url && { details: joinDetails(gpuPlaceWords(url), "name not found") }),
        actions: [A.pair()],
        retryable: true,
      };
  }
}

/** What the check changed in an address as pasted (account-gpu.ts cleanGpuAddress), said under the field. */
export function addressFixNote(fixed: readonly ("scheme" | "query")[]): string | null {
  const said = [fixed.includes("scheme") && "We added https:// to the start of your address.", fixed.includes("query") && "We kept only the address part."].filter(Boolean);
  return said.length ? said.join(" ") : null;
}

/**
 * An address whose GPU is off right now (a stopped pod, one starting up or not answering) can be saved anyway: its runs
 * wait for it. One that can't work as it is (not found, a password, another app) can't.
 */
export const canSaveAnyway = (cause: TroubleCause): boolean => cause === "gpu-stopped" || cause === "gpu-starting" || cause === "gpu-offline" || cause === "gpu-busy";

/** Too many checks in a minute: the next one goes by itself once the wait is over. */
export function checksTooFast(retryAfterS: number): Diagnosis {
  return { cause: "checks-too-fast", words: "Checking again in a few seconds.", actions: [], retryable: true, retryAfterMs: Math.max(1, retryAfterS) * 1000 };
}

/** The out-of-memory words of a run on the person's own GPU (the computer path says the same). */
export const OUT_OF_MEMORY_WORDS = "Your GPU ran out of memory. Try a smaller size or a shorter clip.";

export const isOutOfMemoryText = (s: string) => OUT_OF_MEMORY.test(s);

const minutes = (n: number) => `${n} minute${n === 1 ? "" : "s"}`;

// ---------------------------------------------------------------------------
// A model file a loader can't read

/** The files a loader reads its model from. */
export const MODEL_FILE = /\.(safetensors|ckpt|pt|pth|bin|gguf|sft)$/i;
const MODEL_FILE_IN_TEXT = /[^\s'"`,:;()[\]{}<>|\\/]{1,200}\.(?:safetensors|ckpt|pt|pth|bin|gguf|sft)\b/gi;
/**
 * A node that loads a model file: VAELoader, UNETLoader, CheckpointLoaderSimple, CLIPLoader, DualCLIPLoader, LoraLoader
 * and LoraLoaderModelOnly, ControlNetLoader, UpscaleModelLoader, a GGUF loader.
 */
export const isLoaderNode = (nodeType: string | undefined): boolean => !!nodeType && /Loader/.test(nodeType);
/** A file that is there but can't be read: damaged, cut short, a web page saved under its name, or another format. */
const UNREADABLE_FILE =
  /['"]?utf-?8['"]? codec can'?t decode|invalid load key|Error while deserializing header|HeaderTooLarge|SafetensorError|incomplete metadata|MetadataIncompleteBuffer|unexpected EOF|PytorchStreamReader failed|not a zip file|failed finding central directory/i;
/**
 * A file that isn't there: the system's words, ComfyUI's own ("Model in folder 'vae' with filename '…' not found"), or
 * what an older ComfyUI says when a loader opens a name its folder doesn't have (no path to open).
 */
const MISSING_FILE = /No such file or directory|FileNotFoundError|\[Errno 2\]|Model in folder .{0,80} with filename .{0,400} not found|expected str, bytes or os\.PathLike object, not NoneType/i;

/**
 * What a node's error says about its model file: it can't be read ("damaged"), it isn't there ("missing"), or nothing
 * (another problem, or a node that loads no model file).
 */
export function modelFileProblem(nodeType: string | undefined, message: string | undefined): "damaged" | "missing" | null {
  if (!isLoaderNode(nodeType)) return null;
  const m = String(message ?? "").slice(0, 4096);
  if (MISSING_FILE.test(m) || (/not in list/i.test(m) && new RegExp(MODEL_FILE_IN_TEXT.source, "i").test(m))) return "missing";
  return UNREADABLE_FILE.test(m) ? "damaged" : null;
}

/** A file name as the person's ComfyUI lists it, safe to show: one line, short. */
const shownFile = (name: string) =>
  [...name]
    .filter((c) => c.charCodeAt(0) >= 32 && c.charCodeAt(0) !== 127)
    .join("")
    .trim()
    .slice(0, 160);

/** The model files among a node's inputs (a value that names a file; links to other nodes are left out). */
function fileValues(inputs: unknown): string[] {
  if (!inputs || typeof inputs !== "object") return [];
  const out: string[] = [];
  for (const v of Object.values(inputs as Record<string, unknown>)) {
    // ComfyUI's current_inputs keeps each value in a list.
    for (const x of Array.isArray(v) ? v : [v]) if (typeof x === "string" && MODEL_FILE.test(x.trim())) out.push(x.trim());
  }
  return out;
}

/** The last part of a file's name ("vae/x.safetensors" and "vae\\x.safetensors" are "x.safetensors"). */
const baseOf = (name: string) => name.split(/[\\/]/).pop() ?? name;

/**
 * The model file a loader that failed was reading: its inputs in the graph that was sent (by the error's node id), else
 * the inputs ComfyUI reported with the error, else every node of that class in the graph, else a file the error names.
 * One the error names wins; when it can't be told which, all of them (at most three).
 */
export function loaderFiles(err: { node_id?: unknown; node_type?: unknown; exception_message?: unknown; current_inputs?: unknown }, graph?: unknown): string[] {
  const g = graph && typeof graph === "object" ? (graph as Record<string, { class_type?: unknown; inputs?: unknown } | undefined>) : undefined;
  const id = typeof err.node_id === "string" || typeof err.node_id === "number" ? String(err.node_id) : undefined;
  const said = String(err.exception_message ?? "").slice(0, 4096);
  const node = id ? g?.[id] : undefined;
  let found = node ? fileValues(node.inputs) : [];
  if (!found.length) found = fileValues(err.current_inputs);
  if (!found.length && g && typeof err.node_type === "string") found = Object.values(g).flatMap((n) => (n && n.class_type === err.node_type ? fileValues(n.inputs) : []));
  found = [...new Set(found.map(shownFile).filter(Boolean))];
  const named = found.filter((f) => said.includes(baseOf(f)));
  if (named.length) return named.slice(0, 3);
  if (found.length) return found.slice(0, 3);
  const inText = [...new Set((said.match(MODEL_FILE_IN_TEXT) ?? []).map(shownFile).filter(Boolean))];
  return inText.slice(0, 1);
}

/** A model file a loader can't read, or doesn't have: the file named, where to get it again, never Retry (it can't help). */
function modelFileTrouble(problem: "damaged" | "missing", e: { nodeType?: string; message?: string; files?: string[]; fileUrl?: string }, place: GpuPlace): Diagnosis {
  const files = (e.files ?? []).map(shownFile).filter(Boolean).slice(0, 3);
  const named = files.join(" or ");
  const words =
    problem === "missing"
      ? named
        ? `Your GPU doesn't have the model file ${named}. Add it, then run.`
        : "Your GPU doesn't have a model file this needs. Add it, then run."
      : named
        ? `A model file on your GPU can't be read: ${named}. It's damaged or didn't finish downloading. Download it again, then run.`
        : "A model file on your GPU can't be read. It's damaged or didn't finish downloading. Download it again, then run.";
  const url = e.fileUrl && /^https:\/\/[^\s"'<>]+$/i.test(e.fileUrl) ? e.fileUrl : undefined;
  const said = cleanDetails(withoutAddress(String(e.message ?? ""), place.kind === "link" ? place.url : undefined), 160);
  return {
    cause: "gpu-model-file",
    words,
    details: joinDetails(e.nodeType, said),
    // Check my GPU asks a GPU at a web address (this computer and a paired ComfyUI have their own notes).
    actions: [...(url ? [A.getFile(url)] : []), ...(place.kind === "link" ? [A.check()] : [])],
    retryable: false,
  };
}

/** A job that failed on the person's own GPU while it ran: its own words for the details line, plain words above. */
export type GpuJobTrouble =
  | {
      kind: "error";
      nodeType?: string;
      message?: string;
      /** A loader's model file (loaderFiles), and where it downloads from (https), for a file it can't read or doesn't have. */
      files?: string[];
      fileUrl?: string;
    }
  | { kind: "interrupted" }
  | { kind: "lost"; resent?: boolean }
  | { kind: "moved" }
  | { kind: "disconnected" }
  | { kind: "gone"; minutes: number }
  | { kind: "busy"; minutes: number }
  | { kind: "too-long"; minutes: number }
  | { kind: "save"; over?: string }
  | { kind: "isolated" };

export function diagnoseGpuJob(e: GpuJobTrouble, place: GpuPlace): Diagnosis {
  const pod = place.kind === "link" && gpuHostOf(place.url).kind === "runpod";
  switch (e.kind) {
    case "error": {
      const msg = (e.message ?? "").trim();
      if (isOutOfMemoryText(msg)) return { cause: "gpu-out-of-memory", words: OUT_OF_MEMORY_WORDS, details: joinDetails(e.nodeType, cleanDetails(msg, 120)), actions: [A.trySmaller(), A.retry(), A.pick()], retryable: false };
      // A known problem of this ComfyUI on this system: named, with its fix; Retry would stop the same way.
      const known = knownProblemOf(msg);
      if (known) return { cause: "gpu-comfy-known", words: known.words, details: joinDetails(e.nodeType, cleanDetails(msg, 160)), actions: [A.updateComfy(), ...(place.kind === "link" ? [A.check()] : [])], retryable: false };
      const file = modelFileProblem(e.nodeType, msg);
      if (file) return modelFileTrouble(file, e, place);
      return {
        cause: "gpu-job-error",
        words: "Your GPU hit a problem while making this. Press Retry, or update your add-ons.",
        details: joinDetails(e.nodeType, cleanDetails(msg, 160)),
        actions: [A.retry(), A.pick()],
        retryable: false,
      };
    }
    case "interrupted":
      return { cause: "gpu-stopped-run", words: "This run was stopped on your GPU. Press Retry to run it again.", actions: [A.retry()], retryable: false };
    case "lost":
      return {
        cause: "gpu-restarted",
        words: e.resent ? "Your GPU restarted, so we sent this run again." : "Your GPU restarted and lost this run. Press Retry.",
        actions: e.resent ? [] : [A.retry()],
        retryable: !e.resent,
      };
    case "moved":
      return { cause: "gpu-moved", words: "This run stopped because your GPU moved to a new address. Press Retry.", actions: [A.retry()], retryable: false };
    case "disconnected":
      return { cause: "gpu-moved", words: "You disconnected your GPU, so this run stopped. Connect it again, then press Retry.", actions: [A.connectGpu(), A.retry()], retryable: false };
    case "gone":
      return {
        cause: "gpu-went-away",
        words: `Your GPU stopped answering for ${minutes(e.minutes)}, so this run stopped. Start it again, then press Retry.`,
        actions: [...(pod ? [A.startPod()] : []), A.retry(), A.check()],
        retryable: false,
      };
    case "busy":
      return { cause: "gpu-busy", words: `Your GPU was busy for ${minutes(e.minutes)}, so this run stopped. Press Retry when it's free.`, actions: [A.retry(), A.check()], retryable: false };
    case "too-long":
      return { cause: "gpu-too-long", words: `This took longer than ${minutes(e.minutes)}, so we stopped it. Press Retry, or try a shorter clip.`, actions: [A.retry(), A.trySmaller()], retryable: false };
    case "save":
      return e.over
        ? { cause: "gpu-save-failed", words: `Your ComfyUI made a file over ${e.over}. Try a shorter clip or a lower size.`, actions: [A.retry()], retryable: false }
        : {
            cause: "gpu-save-failed",
            words: "Your result was made, but we couldn't copy it from your GPU. Start it again if it stopped, then press Retry.",
            actions: [...(pod ? [A.startPod()] : []), A.retry(), A.check()],
            retryable: false,
          };
    case "isolated":
      return { cause: "gpu-isolated", words: ISOLATED_ON_GPU, actions: [A.pick()], retryable: false };
  }
}

/** A workflow that needs a second ComfyUI of its own (LatentSync 1.6, ThinkSound): an account's single GPU can't run it. */
export const ISOLATED_ON_GPU_WHY = "This one needs its own ComfyUI, so it can't run on your GPU next to your other add-ons.";
export const ISOLATED_ON_GPU = `${ISOLATED_ON_GPU_WHY} Pick another model.`;

/** ComfyUI refused the job (an add-on older than the tool expects, an input value it rejects): its words go in the details. */
export function diagnoseRefusal(comfyWords: string | undefined): Diagnosis {
  return {
    cause: "gpu-refused",
    words: "Your ComfyUI couldn't start this run. Update your add-ons, then press Retry.",
    details: cleanDetails(comfyWords, 200) || undefined,
    actions: [A.retry()],
    retryable: false,
  };
}

// ---------------------------------------------------------------------------
// Your keys

export type KeyStage = "submit" | "upload" | "poll" | "result";

/** What came back from fal.ai or Replicate (or didn't). */
export interface KeyFailure {
  stage: KeyStage;
  status?: number;
  /** The provider's words (already without the key). */
  text?: string;
  /** A network error's message (no answer at all). */
  error?: string;
  /** The job ran and failed there (fal's COMPLETED with an error, Replicate's "failed"). */
  failed?: boolean;
  /** Replicate's "canceled" or "aborted" from outside. */
  canceled?: boolean;
  retryAfterS?: number;
}

export interface KeyContext {
  provider: CloudProvider;
  /** Another key of the account that runs this tool too: offered as "Run with your … key". */
  other?: CloudProvider;
}

const BALANCE = /exhausted balance|insufficient (credit|balance|funds)|out of credit|top up your balance|payment required|balance has run out/i;
const CONTENT = /content[ _](checker|policy|moderation)|flagged|nsfw|safety (checker|filter|system)|E005|sensitive/i;
// fal.ai's 403 also says why in its `type` (providers/fal.ts keeps it): permission_denied, resource_restricted (a model
// restriction on the key, a spending limit). Only rejected_key is a key to replace.
const PERMISSION = /(do not|don't) have (permission|access)|not authori[sz]ed to (access|use|run)|no access to (this )?(model|app|endpoint)|not allowed to (use|run)|\b(permission_denied|resource_restricted)\b/i;
/** The provider's words without its "fal 422: " or "replicate 404: " start. */
const providerText = (t: string | undefined) => (t ?? "").replace(/^(fal|replicate)\s+(\d{3}|upload):\s*/i, "");
/** A request field fal or Replicate named in a refusal ("body.image_url", "input.image"), as the person knows it. */
function fieldWords(text: string): string {
  const t = text.toLowerCase();
  if (/image|photo|picture|start_frame|end_frame|reference/.test(t)) return "this picture";
  if (/video|clip/.test(t)) return "this clip";
  if (/audio|voice|sound|song/.test(t)) return "this sound";
  if (/prompt|text/.test(t)) return "these words";
  if (/duration|num_frames|length|seconds/.test(t)) return "this length";
  return "these settings";
}

/**
 * A refusal or failure of an account's run on its own fal.ai or Replicate key, read from the provider's status and words:
 * the key (Settings → Your keys), the credit (the provider's billing page), the model, the input, or a passing problem
 * that waiting fixes.
 */
export function diagnoseKey(f: KeyFailure, ctx: KeyContext): Diagnosis {
  const name = CLOUD_KEY[ctx.provider].label;
  const status = f.status ?? 0;
  const said = cleanDetails(providerText(f.text) || f.error, 200);
  const details = joinDetails(status ? `${name} answered ${status}` : undefined, said);
  const other = ctx.other ? [A.withKey(ctx.other)] : [];
  const retryAfterMs = f.retryAfterS && f.retryAfterS > 0 ? f.retryAfterS * 1000 : undefined;
  const text = `${f.text ?? ""} ${f.error ?? ""}`;
  if (f.canceled) return { cause: "provider-stopped", words: `This run was stopped on ${name}. Press Retry to run it again.`, details, actions: [A.retry()], retryable: false };
  if (status === 402 || ((status === 401 || status === 403) && BALANCE.test(text)) || BALANCE.test(text)) {
    return {
      cause: "key-balance",
      words: balanceMessage(ctx.provider),
      details,
      actions: [A.credit(ctx.provider), A.retry(), ...other],
      retryable: false,
    };
  }
  if (status === 401 || (status === 403 && !PERMISSION.test(providerText(f.text)))) {
    return {
      cause: "key-rejected",
      words: keyRejectedMessage(ctx.provider),
      details,
      actions: [A.keySettings("Replace key"), ...other, A.retry()],
      retryable: false,
    };
  }
  if (status === 403) {
    return {
      cause: "key-permission",
      words: `Your ${name} account can't run this model yet. Pick another model, or turn it on in your ${name} account.`,
      details,
      actions: [A.pick(), ...other],
      retryable: false,
    };
  }
  if (status === 404 && (f.stage === "poll" || f.stage === "result")) {
    // A 404 on a run's status or result is about the run, not the model: the provider has no such job for this key (it
    // belongs to another account, as when the key was replaced by another account's) or keeps it no longer (fal.ai docs:
    // Queue API, status and result; Replicate: Get a prediction). Asking again can't bring it back.
    return { cause: "provider-stopped", words: `${name} no longer has this run for your key. Press Retry to run it again.`, details, actions: [A.retry(), ...other], retryable: false };
  }
  if (status === 404) {
    return { cause: "model-unavailable", words: `This model isn't available on ${name} right now. Pick another model.`, details, actions: [A.pick(), ...other], retryable: false };
  }
  if (CONTENT.test(text)) {
    return { cause: "content-flag", words: "The safety check stopped this one. Try different words or another picture.", details, actions: [A.retry(), A.pick()], retryable: false };
  }
  if (status === 400 || status === 422) {
    return {
      cause: "bad-input",
      words: `${name} couldn't use ${fieldWords(providerText(f.text))}. Change it, then run again.`,
      details,
      actions: [A.retry(), A.pick()],
      retryable: false,
    };
  }
  if (status === 429) {
    return { cause: "provider-busy", words: `${name} is busy right now. Press Retry in a minute.`, details, actions: [A.retry(), ...other], retryable: true, ...(retryAfterMs && { retryAfterMs }) };
  }
  if (status >= 500) {
    return {
      cause: "provider-down",
      words: `${name} is having trouble right now. Press Retry in a few minutes.`,
      details,
      actions: [A.retry(), ...other],
      retryable: f.stage !== "submit" || status === 503,
      ...(retryAfterMs && { retryAfterMs }),
    };
  }
  if (f.error && !status) {
    return {
      cause: "provider-unreachable",
      words: `We couldn't reach ${name}. Press Retry in a minute.`,
      details: joinDetails(cleanDetails(f.error, 160)),
      actions: [A.retry(), ...other],
      // At submit, only an error raised before anything was sent (no job made) is safe to send again.
      retryable: f.stage !== "submit" || /ECONNREFUSED|ENOTFOUND|EAI_AGAIN/i.test(f.error),
    };
  }
  if (/no media|nothing back|replied:/i.test(text)) {
    return { cause: "no-result", words: `${name} finished but sent nothing back. Press Retry, or try other words.`, details, actions: [A.retry()], retryable: false };
  }
  if (f.failed) {
    return { cause: "provider-failed", words: `${name} couldn't finish this one. Press Retry, or try different words or another picture.`, details, actions: [A.retry(), A.pick()], retryable: false };
  }
  return { cause: "unknown", words: `${name} couldn't run this one. Press Retry.`, details, actions: [A.retry(), ...other], retryable: false };
}

/** A run on the person's own key that waited, or ran, past its time at the provider. */
export function diagnoseKeyJob(e: { kind: "busy" | "too-long"; minutes: number }, ctx: KeyContext): Diagnosis {
  const name = CLOUD_KEY[ctx.provider].label;
  const other = ctx.other ? [A.withKey(ctx.other)] : [];
  return e.kind === "busy"
    ? { cause: "provider-busy", words: `${name} was busy for ${minutes(e.minutes)}, so this run stopped. Press Retry in a few minutes.`, actions: [A.retry(), ...other], retryable: false }
    : { cause: "provider-too-long", words: `This took longer than ${minutes(e.minutes)} on ${name}, so we stopped it. Press Retry.`, actions: [A.retry(), ...other], retryable: false };
}

/** A finished result whose link at the provider expired before it was saved. */
export function resultExpired(): Diagnosis {
  return { cause: "result-expired", words: "The finished file expired before we could save it. Press Retry to make it again.", actions: [A.retry()], retryable: false };
}

/** A GPU's state in two or three words, for the status strip ("Your GPU · pod stopped"). */
export function shortState(cause: TroubleCause | undefined): string {
  switch (cause) {
    case "gpu-stopped":
      return "pod stopped";
    case "gpu-starting":
      return "starting up";
    case "gpu-password":
    case "gpu-password-wrong":
    case "gpu-sign-in":
      return "needs a password";
    case "gpu-not-found":
      return "address not found";
    case "gpu-not-comfy":
    case "address-local":
      return "check the address";
    default:
      return "offline";
  }
}

// ---------------------------------------------------------------------------
// Waiting

/** While a run waits for its GPU (or its provider) to come back: the tile's words. */
export function waitingWords(d: Pick<Diagnosis, "cause">, provider?: CloudProvider, pod?: boolean): string {
  switch (d.cause) {
    case "gpu-stopped":
      return "Your RunPod pod is off. Start it, and this run goes on by itself";
    case "gpu-starting":
      return pod ? "Waiting for ComfyUI to start on your pod" : "Waiting for your GPU to start up";
    case "gpu-not-found":
      return "We can't find your GPU's address. Paste the new one, and this run goes there";
    case "gpu-busy":
      return "Waiting for your GPU";
    case "provider-busy":
      return `${provider ? CLOUD_KEY[provider].label : "The provider"} is busy, trying again in a moment`;
    case "provider-down":
    case "provider-unreachable":
      return `Waiting for ${provider ? CLOUD_KEY[provider].label : "the provider"} to answer`;
    default:
      return "Waiting for your GPU";
  }
}

/**
 * The status check's words (Settings' words: "we connect as soon as it answers") as the composer says them before Run,
 * where nothing connects: runs start once the GPU answers, and what is in Settings is said to be there.
 */
export function beforeRunWords(words: string): string {
  return words
    .replace(/,? and we connect as soon as it answers\./, ", and your runs start as soon as it answers.")
    .replace(/,? and we connect as soon as ComfyUI is ready\./, ", and your runs start as soon as ComfyUI is ready.")
    .replace(/ We connect as soon as it is\./, " Your runs start as soon as it is.")
    .replace(/ We check again in a moment\./, " Your runs start as soon as it's free.")
    .replace(/Paste it in Access token\./, "Add it in Settings → Your GPU.")
    .replace(/Check it, and paste it in Access token again\./, "Update it in Settings → Your GPU.");
}

/**
 * A check's words after Test, which saves nothing (so nothing connects by itself): what to press once the GPU answers.
 * `budgetSpent`: the check stopped trying by itself, so it says so and names Save anyway too.
 */
export function testWords(words: string, budgetSpent = false): string {
  const t = words
    .replace(/,? and we connect as soon as it answers\./, ", then press Connect.")
    .replace(/,? and we connect as soon as ComfyUI is ready\./, ": press Connect once ComfyUI is ready.")
    .replace(/ We connect as soon as it is\./, " Press Connect once it is.")
    .replace(/ We check again in a moment\./, " Try again in a moment.");
  return budgetSpent ? `Still no answer. ${t.replace(/press Connect\.$/, "press Connect again, or Save anyway.").replace(/press Connect once (ComfyUI is ready|it is)\.$/, "press Connect again once $1, or Save anyway.")}` : t;
}

/** How long a run (or a check) keeps trying again by itself, from the first try to giving up, and its waits. */
export interface RetryPlan {
  budgetMs: number;
  firstMs: number;
  maxMs: number;
}

const MIN = 60_000;

/**
 * Self-healing: how long a passing problem is waited out, by cause. A run on a stopped pod or a GPU that doesn't answer
 * waits as long as a run may wait in its own GPU's queue (30 minutes); a GPU that is starting up, a few minutes; a
 * provider that is busy or down, a minute or three. Null: waiting can't fix it (a key, credit, a missing add-on, the
 * input), so it is never tried again by itself. A check (Settings' Test) waits less: 3 to 5 minutes.
 */
export function retryPlan(cause: TroubleCause, use: "run" | "check" = "run"): RetryPlan | null {
  const check = use === "check";
  switch (cause) {
    case "gpu-stopped":
      return { budgetMs: check ? 5 * MIN : 30 * MIN, firstMs: 15_000, maxMs: 60_000 };
    case "gpu-offline":
      return { budgetMs: check ? 3 * MIN : 30 * MIN, firstMs: 5_000, maxMs: check ? 30_000 : 60_000 };
    case "gpu-starting":
      return { budgetMs: check ? 5 * MIN : 10 * MIN, firstMs: 5_000, maxMs: check ? 30_000 : 60_000 };
    case "gpu-not-found":
      return check ? null : { budgetMs: 2 * MIN, firstMs: 10_000, maxMs: 30_000 };
    case "gpu-busy":
      return { budgetMs: 2 * MIN, firstMs: 5_000, maxMs: 30_000 };
    case "checks-too-fast":
      return { budgetMs: 2 * MIN, firstMs: 5_000, maxMs: 60_000 };
    case "provider-busy":
      return { budgetMs: 1 * MIN, firstMs: 3_000, maxMs: 20_000 };
    case "provider-down":
    case "provider-unreachable":
      return { budgetMs: 3 * MIN, firstMs: 5_000, maxMs: 60_000 };
    default:
      return null;
  }
}

/**
 * The wait before try number `attempt` (1 = the first try again): doubling from firstMs up to maxMs, or what the other
 * side said (never sooner: a wait longer than the time left ends the wait, in plain words).
 */
export function backoffMs(attempt: number, plan: RetryPlan, retryAfterMs?: number): number {
  const doubled = Math.min(plan.maxMs, plan.firstMs * 2 ** Math.max(0, attempt - 1));
  return retryAfterMs && retryAfterMs > 0 ? Math.max(retryAfterMs, plan.firstMs) : doubled;
}

/** The longest a run waits in all, whatever its problems were on the way (a GPU off, then starting up). */
export const HEAL_CAP_MS = 40 * MIN;