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.

27.5 KB
Raw Blame
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { runnerByHand, tick } from "./helpers/app-process";
import { apiAs, loadRoutes, type ComputerRoutes } from "./helpers/computer-tab";
import { FILE_TROUBLE_WORDS } from "@/lib/relay";
import { FAKE_PNG } from "./helpers/fake-local-comfy";
import { plainFailure, RunWatch, type RunLike } from "./helpers/run-watch";
import { FREE_RUNS_POLICY, startStandInBilling, type StandInBilling } from "./helpers/standin-billing";
import { tempDir } from "./helpers/temp-dir";

/**
 * Recovery checks on the worker's side: the worker program itself (bin/lib/worker/run.mjs) runs accounts' jobs on its own
 * stand-in ComfyUI (tests/helpers/fake-local-comfy.ts, a process the worker starts), against the studio's real routes
 * and a stand-in billing service. Each check asks the same three things as tests/recovery-server.test.ts: the output
 * arrives exactly once, every look shows only "In line", "Working" and the result, and nothing is charged twice.
 *
 * They use the worker program's recovery parts (results kept on its disk until the studio has them, its ComfyUI
 * restarted when it stops or hangs, retries with backoff, staying awake, model folders looked at again, and studio jobs
 * before local ComfyUI work). The first check below fails when one of those parts is missing (renamed or left out), so
 * the checks after it always run against all of them.
 *
 * Each job's graph is the stand-in's own (a sampler and the run's save node, with the run's file prefix), so the
 * worker's node and model checks pass without model files; everything else about the job is the studio's. An account's
 * job comes as a job ticket, so the worker's composer here gives the stand-in's graph for it.
 */

const ROOT = path.resolve(import.meta.dirname, "..");
/** The worker program's recovery parts these checks load. */
const PARTS = ["bin/lib/worker/hold.mjs", "bin/lib/worker/turns.mjs", "bin/lib/worker/local-work.mjs", "bin/lib/worker/retry.mjs", "bin/lib/worker/awake.mjs", "scripts/lib/worker-catalog.mjs"];

vi.mock("next/server", async (importOriginal) => ({ ...(await importOriginal<typeof import("next/server")>()), connection: async () => {} }));
vi.setConfig({ testTimeout: 180_000, hookTimeout: 180_000 });

const dir = tempDir("fuobe-recovery-worker-");
process.env.FUOBE_DATA_DIR = dir;
const DEAD = "http://127.0.0.1:7";
process.env.COMFYUI_URL = DEAD;
process.env.FUOBE_PUBLIC_SIGNUP = "1";
for (const k of ["FUOBE_PASSWORD", "FUOBE_HOSTED_FREE", "FUOBE_BILLING_URL", "FUOBE_BILLING_TOKEN", "FUOBE_MODERATION_URL", "FUOBE_POOL_MEDIA_INPUTS"]) delete process.env[k];
globalThis.__fuobeTestDiskFree = 1e13;

const FAKE = path.join(ROOT, "tests/helpers/fake-local-comfy.ts");
/** The address the worker program is told the studio is at: its calls there go to the studio's route handlers. */
const SITE = "http://127.0.0.1:9";
const HOST = "localhost:3000";
const SITE_HEADERS = { host: HOST, "sec-fetch-site": "same-origin", origin: `http://${HOST}` };
const MIN = 60_000;
const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
const realFetch = globalThis.fetch;
const sha = (s: string) => crypto.createHash("sha256").update(s).digest("hex");
const IMAGE = { feature: "image", modelId: "oss-z-image-turbo", prompt: "a lighthouse at dusk" };
const ALLOWED = new Set(["LoadImage", "KSampler", "SaveImage"]);

/** The worker program's modules, loaded once it is in this tree (any: they are plain JavaScript). */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Any = any;
const load = (rel: string): Promise<Any> => import(/* @vite-ignore */ pathToFileURL(path.join(ROOT, rel)).href);

let studio: {
  db: typeof import("@/server/db").db;
  getGeneration: (id: string) => RunLike | null;
  pool: typeof import("@/server/computer/pool");
  outbox: typeof import("@/server/billing-outbox");
  generations: typeof import("@/app/api/generations/route");
  one: typeof import("@/app/api/generations/[id]/route");
  proxy: typeof import("@/proxy").proxy;
  NextRequest: typeof import("next/server").NextRequest;
  lanes: () => string[];
  workflows: () => string[];
};
let program: { runWorker: Any; rules: Any; parseCounters: Any; ComputerRunner: unknown; catalogVersion: string };
/** The stand-in graph of each run's job, by the run's id: what the worker's composer gives for its ticket. */
const standInGraphs = new Map<string, Record<string, unknown>>();
const composer = {
  workerFactsFrom: () => ({}),
  composeTicket: async (job: { generationId: string }) => {
    const graph = standInGraphs.get(job.generationId);
    return graph ? { ok: true, graph, workflowId: "stand-in", size: { megapixels: 0, seconds: 0, outputs: 1 }, made: [] } : { ok: false, reason: "bad-ticket", detail: "no stand-in graph" };
  },
};
let routes: ComputerRoutes;
let svc: StandInBilling;
let a1 = "";
let b1 = "";

describe("the worker program's recovery parts", () => {
  it("are all in this tree, so the checks below run against all of them", () => {
    expect(PARTS.filter((f) => !fs.existsSync(path.join(ROOT, f)))).toEqual([]);
  });
});

describe("the worker program's recovery (accounts' runs, end to end)", () => {
  beforeAll(async () => {
    const auth = await import("@/server/auth");
    const { db } = await import("@/server/db");
    const { setSetting } = await import("@/server/settings");
    const { getGeneration, ensureRunner } = await import("@/server/generations");
    const { loadManifest } = await import("@/server/providers/comfyui");
    const { loadIndex } = await import("@/server/gpu/index-file");
    const { allLanes } = await import("@/lib/lanes");
    studio = {
      db,
      getGeneration: (id) => getGeneration(id) as RunLike | null,
      pool: await import("@/server/computer/pool"),
      outbox: await import("@/server/billing-outbox"),
      generations: await import("@/app/api/generations/route"),
      one: await import("@/app/api/generations/[id]/route"),
      proxy: (await import("@/proxy")).proxy,
      NextRequest: (await import("next/server")).NextRequest,
      lanes: () => allLanes(loadIndex(), sha).map((l) => l.lane),
      workflows: () => loadManifest().workflows.map((x) => x.id),
    };
    const { runWorker } = await load("bin/lib/worker/run.mjs");
    const { inputRules, readCatalog } = await load("scripts/lib/worker-catalog.mjs");
    const { parseCounters } = await load("bin/lib/worker/local-work.mjs");
    program = { runWorker, rules: inputRules(readCatalog(ROOT)), parseCounters, ComputerRunner: (await import("@/lib/computer-runner")).ComputerRunner, catalogVersion: (await import("@/server/computer/catalog-version")).catalogVersion()! };
    routes = await loadRoutes();
    svc = await startStandInBilling();
    auth.setPassword("install password");
    const session = (id: string) => `${auth.SESSION_COOKIE}=${auth.createSession(id).value}`;
    a1 = session(auth.createAccount("[email protected]", "long enough").id);
    b1 = session(auth.createAccount("[email protected]", "long enough").id);
    setSetting("comfyWhere", "workers");
    setSetting("comfyUrl", "");
    setSetting("engineMode", "open");
    ensureRunner();
    runnerByHand();
  });
  afterAll(async () => {
    globalThis.__fuobeTestDiskFree = undefined;
    await svc?.close();
  });
  beforeEach(async () => {
    const { resetRates } = await import("@/server/rate");
    const billing = await import("@/server/billing");
    resetRates();
    billing.resetBillingCache();
    process.env.FUOBE_HOSTED_RUNS = "1";
    process.env.FUOBE_BILLING_URL = svc.address;
    process.env.FUOBE_BILLING_TOKEN = svc.token;
    Object.assign(svc, { priced: false, slowMs: 0, gather: null, policy: JSON.parse(JSON.stringify(FREE_RUNS_POLICY)) });
    await svc.start();
    const d = studio.db();
    d.prepare("DELETE FROM relay_workers").run();
    d.prepare("UPDATE relay_tasks SET state = 'canceled' WHERE state IN ('waiting','sending','queued','running','collecting')").run();
    d.prepare("UPDATE generations SET status = 'canceled' WHERE status IN ('queued', 'running')").run();
    d.prepare("DELETE FROM pool_admissions").run();
  });
  afterEach(async () => {
    vi.useRealTimers();
    for (const r of running.splice(0)) {
      r.ctrl.abort();
      await r.done.catch(() => {});
    }
  });

  // -------------------------------------------------------------------------
  // The studio

  type W = { id: string; token: string };
  function addWorker(): W {
    const w = studio.pool.addWorker(`pc-${Math.random().toString(36).slice(2, 8)}`);
    const lanes = studio.lanes();
    studio.pool.patchWorker(w.id, { paused: false, hours: { always: true }, allow: Object.fromEntries(lanes.map((l) => [l, { delayMs: 0 }])), claimAfterMs: 0 });
    // Connected once: it said its build (one that takes this studio's job tickets).
    studio.db().prepare("UPDATE relay_workers SET lanes_json = ?, facts_json = ?, catalog_version = ?, protocol = 3 WHERE id = ?").run(JSON.stringify({ lanes, workflows: studio.workflows() }), JSON.stringify({ vramBytes: 32 * 2 ** 30 }), program.catalogVersion, w.id);
    // Connected, as a worker that beat a moment ago (a run is refused while every worker that could run it is away).
    studio.db().prepare("UPDATE relay_workers SET seen_at = ?, paused_local = 0 WHERE id = ?").run(Date.now(), w.id);
    return w;
  }

  const request = (url: string, cookie: string, body?: unknown) => {
    const text = body === undefined ? undefined : JSON.stringify(body);
    return new Request(`http://${HOST}${url}`, { method: text ? "POST" : "GET", headers: { ...SITE_HEADERS, cookie, ...(text && { "content-type": "application/json", "content-length": String(Buffer.byteLength(text)) }) }, body: text });
  };
  const taskOf = (id: string) => studio.db().prepare("SELECT * FROM relay_tasks WHERE generation_id = ? ORDER BY created_at DESC").get(id) as import("@/server/computer/tasks").TaskRow | undefined;
  const done = (g: RunLike | null) => !!g && g.status !== "queued" && g.status !== "running";

  /** Press Run as an account; its job's graph becomes the stand-in's (the run's save node and file prefix kept). */
  async function pressRun(cookie = a1, o: { input?: boolean } = {}) {
    const r = await studio.generations.POST(request("/api/generations", cookie, IMAGE));
    const body = (await r.json()) as { items?: { id: string }[] };
    expect(r.status, JSON.stringify(body)).toBe(201);
    const id = body.items![0].id;
    let t = taskOf(id);
    for (let i = 0; i < 200 && !t; i++) {
      await sleep(20);
      t = taskOf(id);
    }
    // The graph the studio built for the run's ticket: its save node and file prefix stay.
    const { graphOfTask } = await import("./helpers/ticket-worker");
    const graph = await graphOfTask(t!);
    const save = Object.entries(graph).find(([, n]) => n.class_type === "SaveImage")!;
    let input: string | null = null;
    if (o.input) {
      const tasks = await import("@/server/computer/tasks");
      input = tasks.stageInput(t!.principal, FAKE_PNG, "image/png", "in.png");
      studio.db().prepare("UPDATE relay_inputs SET task_id = ?, n = 0 WHERE name = ?").run(t!.id, input);
    }
    const own = {
      ...(input && { "1": { class_type: "LoadImage", inputs: { image: input } } }),
      "3": { class_type: "KSampler", inputs: { seed: 1, steps: 4 } },
      [save[0]]: { class_type: "SaveImage", inputs: { images: ["3", 0], filename_prefix: save[1].inputs.filename_prefix } },
    };
    const needs = { ...(JSON.parse(t!.needs_json ?? "{}") as object), files: [], packs: [], minVramBytes: 0 };
    studio.db().prepare("UPDATE relay_tasks SET needs_json = ? WHERE id = ?").run(JSON.stringify(needs), t!.id);
    standInGraphs.set(id, own);
    return { id, task: t! };
  }

  /** GET /api/generations/<id> as a session's browser asks it: the sign-in proxy first, then the route. */
  const sessionGet = async (id: string, cookie: string) => {
    const url = `/api/generations/${id}`;
    const passed = await studio.proxy(new studio.NextRequest(`http://${HOST}${url}`, { headers: { ...SITE_HEADERS, cookie } }));
    return passed.headers.get("x-middleware-next") === "1" ? studio.one.GET(request(url, cookie), { params: Promise.resolve({ id }) } as never) : passed;
  };

  /** Every place the account looks from (an answer that isn't OK is a look that can't find the run); the other account is refused. */
  const watchOf = (id: string, cookie = a1) =>
    new RunWatch(
      {
        refresh: () => studio.getGeneration(id),
        session: async () => {
          const r = await sessionGet(id, cookie);
          if (!r.ok) throw new Error(`GET /api/generations/${id} answered ${r.status}`);
          return (await r.json()) as RunLike;
        },
      },
      { "another account": async () => (await sessionGet(id, cookie === a1 ? b1 : a1)).status },
    );

  /** Look at the run until it ends (the runner turning by hand), then the three checks. */
  async function expectOnce(id: string, watch: RunWatch, o: { prompts?: () => number; promptsWanted?: number; place?: number } = {}) {
    let g = studio.getGeneration(id);
    for (let i = 0; i < 3000 && !done(g); i++) {
      await tick();
      await watch.look();
      await sleep(50);
      g = studio.getGeneration(id);
    }
    await watch.look();
    expect(watch.problems({ place: o.place ?? 1 }), JSON.stringify(watch.path())).toEqual([]);
    expect(g?.status, g?.error ?? "").toBe("succeeded");
    expect(g!.outputs).toHaveLength(1);
    if (o.prompts && o.promptsWanted !== undefined) expect(o.prompts()).toBe(o.promptsWanted);
    for (let i = 0; i < 100 && svc.reportsFor(id).length < 1; i++) {
      await studio.outbox.sweepBilling();
      await sleep(10);
    }
    expect(svc.reportsFor(id).map((r) => r.kind)).toEqual(["settle"]);
  }

  // -------------------------------------------------------------------------
  // The worker program

  interface Pc {
    home: string;
    workDir: string;
    comfyDir: string;
    turns: string;
    lock: string;
    countFile: string;
    logs: string[];
  }
  function pc(): Pc {
    const base = tempDir("fuobe-recovery-pc-");
    const p = { home: path.join(base, "home"), workDir: path.join(base, "work"), comfyDir: path.join(base, "comfy"), turns: path.join(base, "turns"), lock: path.join(base, "gpu.lock"), countFile: path.join(base, "prompts.txt"), logs: [] };
    for (const d of [p.home, p.workDir, p.comfyDir, p.turns]) fs.mkdirSync(d, { recursive: true });
    return p;
  }
  const comfyOf = (p: Pc) => {
    try {
      return JSON.parse(fs.readFileSync(path.join(p.workDir, "comfyui.pid"), "utf8")) as { pid: number; port: number };
    } catch {
      return null;
    }
  };
  const stats = async (p: Pc) => {
    const c = comfyOf(p);
    return c ? ((await realFetch(`http://127.0.0.1:${c.port}/__fake/stats`).then((r) => r.json()).catch(() => null)) as { posts: number; running: string | null; step: number | null } | null) : null;
  };
  const prompts = (p: Pc) => (fs.existsSync(p.countFile) ? fs.readFileSync(p.countFile, "utf8").trim().split("\n").filter(Boolean).length : 0);
  async function until<T>(fn: () => T | Promise<T>, ms = 60_000): Promise<NonNullable<T>> {
    const end = Date.now() + ms;
    for (;;) {
      const v = await fn();
      if (v) return v as NonNullable<T>;
      if (Date.now() > end) throw new Error(`Timed out after ${ms} ms`);
      await tick();
      await sleep(50);
    }
  }

  const running: { ctrl: AbortController; done: Promise<unknown> }[] = [];
  /** A test steps between the worker and the studio, or its ComfyUI (return a Response to answer in the studio's place). */
  type Intercept = (url: string, init: RequestInit) => Response | undefined | Promise<Response | undefined>;

  /** Start `fuobe worker run` for this worker, its ComfyUI a stand-in process of its own. */
  function start(w: W, p: Pc, o: { steps?: number; stepMs?: number; failNext?: string; stallMin?: number; modelDirs?: string[]; pins?: Record<string, string>; rescanMs?: number; intercept?: Intercept; gpu?: Record<string, unknown>; awake?: boolean[] } = {}) {
    const ctrl = new AbortController();
    const api = apiAs(routes, null);
    const fetchFn = async (url: string, init: RequestInit = {}) => {
      const mine = await o.intercept?.(String(url), init);
      if (mine) return mine;
      if (!String(url).startsWith(SITE)) return realFetch(url, init);
      const u = new URL(url);
      // Its inventory is noted, not saved: the worker keeps the lanes the test gave it (no model files here).
      if (u.pathname === "/api/gpu/computer/inventory") return Response.json({ ok: true, lanes: studio.lanes(), workflows: studio.workflows(), missing: [] });
      return api(u.pathname + u.search, init);
    };
    const cfg = {
      site: SITE,
      token: w.token,
      comfyDir: p.comfyDir,
      workDir: p.workDir,
      modelDirs: o.modelDirs ?? [],
      comfyCommand: [
        process.execPath, "--disable-warning=MODULE_TYPELESS_PACKAGE_JSON", FAKE, "--port", "{port}", "--cors", "off",
        "--steps", String(o.steps ?? 4), "--step-ms", String(o.stepMs ?? 40), "--worker-guard", "1", "--count-file", p.countFile,
        ...(o.failNext ? ["--fail-next", o.failNext] : []),
      ],
      comfyGuard: true,
      stallMin: o.stallMin ?? 10,
      hungMin: 3,
      ffmpeg: null,
      yieldFolder: p.turns,
      desktopBaselineMb: 600,
      gpuLockFile: p.lock,
      gpuLockComfyOwners: [],
    };
    const r = {
      ctrl,
      done: program.runWorker(cfg, {
        fetch: fetchFn,
        home: p.home,
        signal: ctrl.signal,
        requirements: { folders: [], files: {}, packs: {}, workflows: {} },
        ComputerRunner: program.ComputerRunner,
        rules: program.rules,
        pins: o.pins ?? {},
        allowed: ALLOWED,
        ffmpegVersion: async () => null,
        awake: { set: (on: boolean) => void o.awake?.push(on) },
        readGpu: async () => null,
        sleep: (ms: number) => sleep(Math.min(ms, 50)),
        log: (line: string) => void p.logs.push(line),
        composer,
        catalogVersion: program.catalogVersion,
        // Local ComfyUI work is never a real port here (it answers nothing, so it is never taken for idle).
        localFetch: async () => new Response("not here", { status: 404 }),
        // The folders are looked at again on both clocks: a full rescan, and the check for model folders that were missing.
        ...(o.rescanMs && { rescanMs: o.rescanMs, modelsCheckMs: o.rescanMs }),
        ...o.gpu,
      }) as Promise<unknown>,
    };
    running.push(r);
    return r;
  }

  // -------------------------------------------------------------------------

  it("its ComfyUI killed at 50%: the output arrives", async () => {
    const w = addWorker();
    const p = pc();
    const { id } = await pressRun();
    const watch = watchOf(id);
    start(w, p, { steps: 10, stepMs: 150 });
    await until(async () => ((await stats(p))?.step ?? 0) >= 5);
    process.kill(comfyOf(p)!.pid, "SIGKILL");
    await expectOnce(id, watch);
  });

  it("its ComfyUI hangs: the job goes back in line within the stall limit, keeps its place, and the output arrives", async () => {
    const w = addWorker();
    const p = pc();
    const { id } = await pressRun();
    const watch = watchOf(id);
    start(w, p, { failNext: "never", stallMin: 0.05 });
    await expectOnce(id, watch);
    expect(prompts(p)).toBeGreaterThanOrEqual(2);
  });

  it("its ComfyUI runs out of memory: it frees memory, tries once more, and the output arrives", async () => {
    const w = addWorker();
    const p = pc();
    const { id } = await pressRun();
    const watch = watchOf(id);
    start(w, p, { failNext: "oom" });
    await expectOnce(id, watch, { prompts: () => prompts(p), promptsWanted: 2 });
  });

  it("the worker killed mid-render and started again: the output arrives once", async () => {
    const w = addWorker();
    const p = pc();
    const { id } = await pressRun();
    const watch = watchOf(id);
    const first = start(w, p, { steps: 10, stepMs: 150 });
    await until(async () => ((await stats(p))?.step ?? 0) >= 3);
    first.ctrl.abort();
    await first.done.catch(() => {});
    start(w, p);
    await expectOnce(id, watch);
  });

  it("the worker stopped after its render and before the upload: the output arrives, from exactly one prompt", async () => {
    const w = addWorker();
    const p = pc();
    const { id } = await pressRun();
    const watch = watchOf(id);
    const first = start(w, p, {
      intercept: (url, init) => {
        if (!url.includes("/outputs/") || init.method !== "PUT") return undefined;
        first.ctrl.abort();
        return new Response("stopping", { status: 503 });
      },
    });
    await first.done.catch(() => {});
    expect(taskOf(id)!.state).toBe("collecting");
    start(w, p);
    await expectOnce(id, watch, { prompts: () => prompts(p), promptsWanted: 1 });
  });

  it("the job's input doesn't reach its ComfyUI 7 times (the connection drops): the studio sends the input once, and the output arrives", async () => {
    const w = addWorker();
    const p = pc();
    const { id } = await pressRun(a1, { input: true });
    const watch = watchOf(id);
    let dropped = 0;
    let fetched = 0;
    start(w, p, {
      intercept: (url) => {
        if (url.includes("/inputs/")) fetched++;
        if (url.includes("/upload/image") && dropped < 7) {
          dropped++;
          throw new TypeError("fetch failed");
        }
        return undefined;
      },
    });
    await expectOnce(id, watch);
    expect(dropped).toBe(7);
    expect(fetched).toBe(1);
  });

  it("a job whose input crashes its ComfyUI fails in plain words, refunded, and the next account's run finishes", async () => {
    const w = addWorker();
    const p = pc();
    const bad = await pressRun(a1);
    const next = await pressRun(b1);
    const watch = watchOf(next.id, b1);
    start(w, p, { steps: 10, stepMs: 100 });
    // Every time its ComfyUI runs the bad job, it dies.
    const badPrompt = bad.task.prompt_id;
    const killer = setInterval(async () => {
      const s = await stats(p);
      if (s?.running === badPrompt) process.kill(comfyOf(p)!.pid, "SIGKILL");
    }, 50);
    try {
      await until(() => done(studio.getGeneration(bad.id)), 120_000);
      // Its words say it is these files or settings (the person can change them), never the machines.
      expect(plainFailure(studio.getGeneration(bad.id)!, { charged: false, words: FILE_TROUBLE_WORDS.crashed })).toEqual([]);
      await expectOnce(next.id, watch, { place: 2 });
      expect(svc.reportsFor(bad.id).map((r) => r.kind)).toEqual(["refund"]);
    } finally {
      clearInterval(killer);
    }
  });

  it("the PC sleeps for 5 minutes mid-run, then wakes: the output arrives once, and the job lost no attempt", async () => {
    vi.useFakeTimers({ toFake: ["Date"], shouldAdvanceTime: true });
    const w = addWorker();
    const p = pc();
    const { id } = await pressRun();
    const watch = watchOf(id);
    const awake: boolean[] = [];
    let asleep = false;
    let wake: () => void = () => {};
    const woken = new Promise<void>((r) => (wake = r));
    start(w, p, { steps: 10, stepMs: 150, awake, intercept: async () => (asleep ? (await woken, undefined) : undefined) });
    await until(async () => ((await stats(p))?.step ?? 0) >= 3);
    expect(awake).toContain(true);
    // Asleep: nothing of the worker answers for 5 minutes on the studio's clock.
    asleep = true;
    for (let m = 0; m < 5; m++) {
      vi.setSystemTime(Date.now() + MIN);
      await tick();
      await watch.look();
    }
    asleep = false;
    wake();
    await expectOnce(id, watch);
    expect(taskOf(id)!.requeues).toBe(0);
  });

  it("a model folder missing at start and added a minute later: the waiting job is taken within one look at the folders", async () => {
    const w = addWorker();
    const p = pc();
    const models = path.join(p.home, "models");
    const bytes = "stand-in model!!";
    const file = { folder: "checkpoints", name: "stand-in.safetensors", size: bytes.length, sha256: sha(bytes) };
    const { id, task } = await pressRun();
    studio.db().prepare("UPDATE relay_tasks SET needs_json = ? WHERE id = ?").run(JSON.stringify({ ...(JSON.parse(task.needs_json ?? "{}") as object), files: [file] }), task.id);
    const watch = watchOf(id);
    start(w, p, { modelDirs: [models], pins: { [file.name]: file.sha256 }, rescanMs: 500 });
    await sleep(1500);
    expect(taskOf(id)!.state).toBe("waiting");
    fs.mkdirSync(path.join(models, file.folder), { recursive: true });
    fs.writeFileSync(path.join(models, file.folder, file.name), bytes);
    await until(() => taskOf(id)!.state !== "waiting", 5000);
    await expectOnce(id, watch);
  });

  describe("users first: accounts' jobs before local ComfyUI work", () => {
    const MB = 2 ** 20;
    const LUID = "luid_0x00000000_0x0000D1B5_phys_0";
    /** The PC's GPU as fakes show it: local ComfyUI work on 8188, maybe a game, the desktop, and the worker's own ComfyUI. */
    const fakeGpu = (p: Pc, o: { workMb: number; gameMb?: number }) => {
      const procs = () => [{ pid: 4242, mb: o.workMb }, { pid: 5555, mb: o.gameMb ?? 0 }, { pid: 77, mb: 500 }, ...(comfyOf(p) ? [{ pid: comfyOf(p)!.pid, mb: 1000 }] : [])];
      return {
        gpuEveryMs: 200,
        turnGraceMs: 1500,
        readGpu: async () => ({ usedMb: procs().reduce((n, c) => n + c.mb, 0), tempC: 50, apps: [] }),
        readCounters: async () =>
          program.parseCounters(
            [`"(PDH-CSV 4.0)",${procs().map((r) => `"\\\\PC\\GPU Process Memory(pid_${r.pid}_${LUID})\\Dedicated Usage"`).join(",")}`, `"11/04/2026 09:00:00.000",${procs().map((r) => `"${r.mb * MB}"`).join(",")}`].join("\r\n"),
          ),
        readListeners: async () => ({
          listeners: [{ port: 8188, pid: 4242 }, ...(comfyOf(p) ? [{ port: comfyOf(p)!.port, pid: comfyOf(p)!.pid }] : [])],
          names: { 4242: "python", 5555: "game", 77: "dwm", ...(comfyOf(p) ? { [comfyOf(p)!.pid]: "node" } : {}) },
        }),
      };
    };
    const site = (p: Pc) => {
      try {
        return JSON.parse(fs.readFileSync(path.join(p.turns, "site.json"), "utf8")) as { siteWaiting: number };
      } catch {
        return null;
      }
    };

    it("only local ComfyUI work holds the GPU: the worker asks for a turn, the job stays In line in its place, and once the turn is given it is taken within 5 s and its output arrives", async () => {
      const w = addWorker();
      const p = pc();
      const { id } = await pressRun();
      const watch = watchOf(id);
      start(w, p, { gpu: fakeGpu(p, { workMb: 9000 }) });
      await until(() => (site(p)?.siteWaiting ?? 0) >= 1);
      await tick();
      await watch.look();
      expect(taskOf(id)!.state).toBe("waiting");
      // The PC's own side gives the turn.
      const at = Date.now();
      fs.writeFileSync(path.join(p.turns, "yielded.json"), JSON.stringify({ state: "yielded", at }));
      await until(() => taskOf(id)!.state !== "waiting", 5000);
      expect(Date.now() - at).toBeLessThan(5000);
      await expectOnce(id, watch);
    });

    it("a game holds the GPU: the worker takes the busy pause, asks for no turn, and the job stays In line in its place", async () => {
      const w = addWorker();
      const p = pc();
      const { id } = await pressRun();
      const watch = watchOf(id);
      start(w, p, { gpu: fakeGpu(p, { workMb: 9000, gameMb: 6000 }) });
      await until(() => (studio.db().prepare("SELECT local_why FROM relay_workers WHERE id = ?").get(w.id) as { local_why: string | null }).local_why?.startsWith("gpu-busy"));
      expect(site(p)?.siteWaiting ?? 0).toBe(0);
      await tick();
      await watch.look();
      expect(taskOf(id)!.state).toBe("waiting");
      expect(watch.problems({ place: 1 })).toEqual([]);
    });
  });
});