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
Evaluation harness: the cloud spend cap counts every cost
Claude8f20ea8b36f0
diff --git a/scripts/eval/README.md b/scripts/eval/README.mdindex accca683..3bc6562d 100644--- a/scripts/eval/README.md+++ b/scripts/eval/README.md@@ -80,8 +80,14 @@ Useful `run` options:jobs.- `--variants recipe,prompt-only` or `all`: the recipe variants to run. Each suite has a default.- `--model <id>`: one model id for every job, instead of the suite's targets.-- `--max-usd 2`: the cloud spend cap. The run is priced first. It refuses to start over the cap, and- it stops before a job would go over.+- `--max-usd 2`: the cloud spend cap. The run is priced first, training included. It refuses to start+ over the cap, and it starts no job that would go over it: each job reserves its price before it+ starts, and its real cost replaces the reservation when it ends. A resumed run counts what its earlier+ attempts spent (every attempt that reached the server, and any training).+- A cloud job or training the server can't price is skipped, because the cap can't cover it.+ `--allow-unpriced` runs it anyway: it holds whatever is left of the cap while it runs, so nothing+ starts beside it, and its cost counts once the server reports it. Such a job can take the run over+ the cap by its own cost. A training target on Higgsfield's API never runs.- `--concurrency`: open 1, others 4.- `--resume <runId>`: runs only the failed and missing jobs again. Ctrl+C stops new jobs fromstarting; the jobs already running finish on the server.diff --git a/scripts/eval/eval.mjs b/scripts/eval/eval.mjsindex 0e4936b3..5ce2ecab 100644--- a/scripts/eval/eval.mjs+++ b/scripts/eval/eval.mjs@@ -42,7 +42,9 @@ const HELP = {--model <id> one model id for every job instead of the suite's targets--limit <n> at most n cases · --prompts <n> at most n prompts per case · --seeds 1,2--dry-run list the jobs and the estimated spend; uploads and starts nothing- --max-usd <n> the cloud spend cap (default $${DEFAULT_CLOUD_CAP}); the run stops before going over+ --max-usd <n> the cloud spend cap (default $${DEFAULT_CLOUD_CAP}): jobs, training and an earlier attempt's spend count;+ the run refuses to start over it and starts no job that would go over it+ --allow-unpriced also run cloud jobs (and training) the server can't price; they count once their cost is known--concurrency <n> jobs at a time (open 1, others 4)--resume <runId> run the failed and missing jobs of an earlier run again--allow-fixtures use the synthetic fixtures where your own inputs are missing@@ -159,6 +161,7 @@ async function run(args, ctx) {"dry-run": { type: "boolean" },list: { type: "boolean" },"max-usd": { type: "string" },+ "allow-unpriced": { type: "boolean" },concurrency: { type: "string" },resume: { type: "string" },"allow-fixtures": { type: "boolean" },@@ -202,6 +205,7 @@ async function run(args, ctx) {model: v.model,dryRun: v["dry-run"],maxUsd: num(v["max-usd"], "max-usd"),+ allowUnpriced: v["allow-unpriced"],concurrency: num(v.concurrency, "concurrency"),root: rootDir,resume: resumeRun?.runId,diff --git a/scripts/eval/lib/report.mjs b/scripts/eval/lib/report.mjsindex 4a4f8035..75f12b6e 100644--- a/scripts/eval/lib/report.mjs+++ b/scripts/eval/lib/report.mjs@@ -153,7 +153,8 @@ export function summarize(loaded) {failed: rows.filter((r) => ["failed", "timeout", "canceled", "interrupted"].includes(r.status)).length,skipped: rows.filter((r) => r.status === "skipped").length,},- costUsd: { estimate: run.estimateUsd ?? null, spent: ok.reduce((a, r) => a + (r.costUsd?.actual ?? r.costUsd?.estimate ?? 0), 0) },+ // Spent: every attempt that reached the server and any training, as the runner counted it against the cap.+ costUsd: { estimate: run.estimateUsd ?? null, spent: run.spentUsd ?? ok.reduce((a, r) => a + (r.costUsd?.actual ?? r.costUsd?.estimate ?? 0), 0), ...(run.trainingUsd && { training: run.trainingUsd }) },metrics: { keys: metricKeys, models: metrics?.models ?? {}, skipped: metrics?.skipped ?? (metrics ? {} : { all: "not scored yet: pnpm eval:report runs the metrics" }), device: metrics?.device ?? null },variants: perVariant,pairs,diff --git a/scripts/eval/lib/run.mjs b/scripts/eval/lib/run.mjsindex 8ac042ea..fbb6c90a 100644--- a/scripts/eval/lib/run.mjs+++ b/scripts/eval/lib/run.mjs@@ -13,7 +13,7 @@ import { expandJobs, planSummary } from "./plan.mjs";import { fillTemplate, probeClip, requestIdFor, uploader } from "./media.mjs";import { RECIPES_FILE, LOOKS_FILE } from "./recipes.mjs";import { engineProblem, serverFacts } from "./server.mjs";-import { appendJsonl, latestJobs, newRunId, readJson, runDir, writeJson } from "./store.mjs";+import { appendJsonl, latestJobs, newRunId, readJson, readJsonl, runDir, writeJson } from "./store.mjs";const TERMINAL = new Set(["succeeded", "failed", "canceled"]);/** The cloud's spend cap when none is given (US$). The open engine and demo runs are free. */@@ -37,6 +37,27 @@ const gitCommit = () => {}};+/**+ * What a job's row cost: the server's actual figure, else the estimate once anything was submitted+ * (a job that never reached the server cost nothing). Every attempt's row counts: a retry pays again.+ */+export const rowSpend = (r) => r.costUsd?.actual ?? (r.generations?.length || r.status === "succeeded" ? (r.costUsd?.estimate ?? 0) : 0);++/** What earlier attempts of a run already spent: every row in jobs.jsonl, and the training runs in elements.json. */+export function spentSoFar(dir) {+ const rows = readJsonl(path.join(dir, "jobs.jsonl"));+ const trainings = Object.values(readJson(path.join(dir, "elements.json"), {}));+ return sum(rows.map(rowSpend)) + sum(trainings.map((t) => (t.training ? (t.costUsd ?? t.estimateUsd ?? 0) : 0) + (t.earlierUsd ?? 0)));+}++/** A training target's price as the server words it ("≈ $0.85 (1,000 steps)", "Free"); null when it names no amount. */+export function trainingPrice(target) {+ if (!target) return null;+ if (/^\s*free\b/i.test(target.price ?? "")) return 0;+ const m = /\$\s*([\d,]+(?:\.\d+)?)/.exec(target.price ?? "");+ return m ? Number(m[1].replaceAll(",", "")) : null;+}+/** The LoRA files the jobs load that aren't in ComfyUI's list (null list: ComfyUI didn't answer, so nothing to say). */export function lorasMissing(jobs, installed) {if (!installed) return null;@@ -82,6 +103,7 @@ const routeResult = (answer, how) => {* @property {number} [limit] @property {number} [prompts] @property {number[]} [seeds] @property {string} [model]* @property {boolean} [dryRun] price and list the jobs; nothing is uploaded or started* @property {number} [maxUsd] stop before the run's cost goes over this (cloud default DEFAULT_CLOUD_CAP)+ * @property {boolean} [allowUnpriced] run cloud jobs the server can't price (they count once their cost is known)* @property {number} [concurrency] jobs at a time (open 1, cloud 4, demo 4)* @property {string} root the results folder* @property {string} [resume] a run id to continue (failed and missing jobs run again)@@ -140,6 +162,24 @@ export async function runEval(opts, io) {const why = trainingProblem(j, engine);if (why) for (const x of jobs.filter((y) => y.caseId === j.caseId && !y.skip)) x.skip = why;}+ // What each training would cost (cloud): the server's price for its target. A Higgsfield target never runs+ // (Fuobe measures its own Originals), and one without a price can't sit under the spend cap.+ const trainingUsd = new Map();+ const savedElements = dir ? readJson(path.join(dir, "elements.json"), {}) : {};+ // A training an earlier attempt started (and paid for) isn't priced again: the resume waits for it.+ const willTrain = trainCases.filter((j) => !j.skip && engine === "cloud" && !j.train.element && !["succeeded", "training"].includes(savedElements[j.caseId]?.status));+ if (willTrain.length) {+ const targets = (await client.request("GET", "/api/elements/eval-price/trainings").catch(() => null))?.targets ?? [];+ for (const j of willTrain) {+ const t = targets.find((x) => x.id === j.train.target);+ const usd = trainingPrice(t);+ let why = null;+ if (t?.provider === "higgsfield") why = `training target ${j.train.target} runs on Higgsfield's API: Fuobe's evaluation never uses it (train on fal or your GPU)`;+ else if (usd == null && !opts.allowUnpriced) why = `training on ${j.train.target} has no price${t ? ` (the server says “${t.price}”)` : ""}, so the spend cap can't cover it: --allow-unpriced runs it anyway`;+ if (why) for (const x of jobs.filter((y) => y.caseId === j.caseId && !y.skip)) x.skip = why;+ else trainingUsd.set(j.caseId, usd ?? 0);+ }+ }// Price every runnable job with what the server says the route runs.const priced = [];@@ -158,12 +198,14 @@ export async function runEval(opts, io) {: `would run as a demo: ${p.modelName ?? job.model.id} has no ${engine === "open" ? "ComfyUI" : "cloud provider"} connected on that Fuobe`;}else if (engine !== "demo" && p.engine && p.engine !== engine) job.skip = `${p.modelName ?? job.model.id} is ${p.engine === "open" ? "an open-source" : "a cloud"} model, not ${engine}`;+ else if (engine === "cloud" && p.costUsd == null && !opts.allowUnpriced) job.skip = "the server can't price it, so the spend cap can't cover it: --allow-unpriced runs it anyway (it counts once its cost is known)";else priced.push(job);} catch (e) {job.skip = `the server refused it: ${e.message}`;}}- const estimate = sum(priced.map((j) => j.preview.costUsd));+ const training = sum([...trainingUsd.entries()].filter(([caseId]) => jobs.some((x) => x.caseId === caseId && !x.skip)).map(([, usd]) => usd));+ const estimate = sum(priced.map((j) => j.preview.costUsd)) + training;const unknownCost = priced.filter((j) => j.preview.costUsd == null).length;// On the open engine: which of the LoRAs the jobs load ComfyUI doesn't have yet (they are never committed;@@ -175,10 +217,15 @@ export async function runEval(opts, io) {if (opts.dryRun) {const final = planSummary(jobs);const { jobs: total, ...counts } = final;- return { dryRun: true, suite: suite.suite, engine, server: io.target?.url ?? client.url, total, ...counts, estimateUsd: estimate, unknownCost, maxUsd: Number.isFinite(maxUsd) ? maxUsd : null, ...(missingLoras && { missingLoras }), jobs: jobs.map(publicJob) };+ return { dryRun: true, suite: suite.suite, engine, server: io.target?.url ?? client.url, total, ...counts, estimateUsd: estimate, ...(training && { trainingUsd: training }), unknownCost, maxUsd: Number.isFinite(maxUsd) ? maxUsd : null, ...(missingLoras && { missingLoras }), jobs: jobs.map(publicJob) };}- if (engine === "cloud" && estimate > maxUsd) {- throw new Error(`This run would cost about ${money(estimate)}, over the spend cap of ${money(maxUsd)}. Raise it with --max-usd ${Math.ceil(estimate)} or run fewer jobs (--looks, --cases, --limit, --prompts).`);+ // On a resume, what the earlier attempts spent counts against the same cap; jobs already done don't run again.+ const spentBefore = previous ? spentSoFar(dir) : 0;+ const doneBefore = new Set(previous ? latestJobs(dir).filter((r) => r.status === "succeeded").map((r) => r.id) : []);+ const toSpend = sum(priced.filter((j) => !doneBefore.has(j.id)).map((j) => j.preview.costUsd)) + training;+ if (engine === "cloud" && spentBefore + toSpend > maxUsd + 1e-9) {+ const already = spentBefore ? ` (${money(spentBefore)} already spent in this run, ${money(toSpend)} to go)` : "";+ throw new Error(`This run would cost about ${money(spentBefore + toSpend)}${already}, over the spend cap of ${money(maxUsd)}. Raise it with --max-usd ${Math.ceil(spentBefore + toSpend)} or run fewer jobs (--looks, --cases, --limit, --prompts).`);}// The run's record: what ran, where, with which research files, before anything starts.@@ -198,6 +245,7 @@ export async function runEval(opts, io) {variantInfo: Object.fromEntries(summary.variants.map((v) => [v, suite.variants?.[v]?.description ?? ""])),maxUsd: Number.isFinite(maxUsd) ? maxUsd : null,estimateUsd: estimate,+ ...(training && { trainingUsd: training }),harness: { commit: gitCommit(), recipes: { file: RECIPES_FILE, sha: fileSha(path.join(REPO_DIR, RECIPES_FILE)) }, looks: { file: LOOKS_FILE, sha: fileSha(path.join(REPO_DIR, LOOKS_FILE)) }, suiteFile: path.relative(REPO_DIR, suite.file ?? "") },startedAt: previous?.startedAt ?? now(),resumedAt: previous ? now() : undefined,@@ -214,35 +262,74 @@ export async function runEval(opts, io) {appendJsonl(path.join(dir, "jobs.jsonl"), { ...recordBase(job), status: "skipped", skip: job.skip, at: now() });}+ let spent = spentBefore;for (const j of trainCases) {if (jobs.some((x) => x.caseId === j.caseId && !x.skip)) {try {- elements.set(j.caseId, await prepareElement(client, j, { engine, runId, dir, up, log, sleep, now, signal: io.signal }));+ const el = await prepareElement(client, j, { engine, runId, dir, up, log, sleep, now, signal: io.signal, estimateUsd: trainingUsd.get(j.caseId) });+ elements.set(j.caseId, el);+ // A training that ran now is paid for (its actual cost when the server knows it).+ if (!el.reused && el.training) spent += el.costUsd ?? el.estimateUsd ?? 0;} catch (e) {for (const x of priced.filter((y) => y.caseId === j.caseId)) x.skip = `training didn't finish: ${e.message}`;+ // A training that failed after starting may still have been billed: count its estimate.+ if (readJson(path.join(dir, "elements.json"), {})[j.caseId]?.training) spent += trainingUsd.get(j.caseId) ?? 0;}}}- let spent = 0;const queue = priced.filter((j) => !j.skip && done.get(j.id)?.status !== "succeeded");for (const j of priced.filter((x) => x.skip)) appendJsonl(path.join(dir, "jobs.jsonl"), { ...recordBase(j), status: "skipped", skip: j.skip, at: now() });const kept = priced.length - queue.length - priced.filter((x) => x.skip).length;if (kept) log(`${kept} job${kept === 1 ? "" : "s"} already done in this run; running the other ${queue.length}`);+ // The cap holds while jobs run side by side: each job reserves its price before it starts, and one the server+ // couldn't price (--allow-unpriced) reserves whatever is left of the cap, so nothing else starts beside it.+ // A job that doesn't fit waits for running ones to settle, and is skipped only when nothing running could free+ // money. What a job really cost replaces its reservation as it ends.+ let inFlight = 0;+ let waiting = [];+ const settle = () => {+ const w = waiting;+ waiting = [];+ for (const wake of w) wake();+ };+ const reserve = async (job) => {+ if (engine !== "cloud") return 0;+ for (;;) {+ const price = job.preview.costUsd;+ const need = price ?? Math.max(0, maxUsd - spent);+ if (price != null ? spent + need <= maxUsd + 1e-9 : spent < maxUsd - 1e-9) {+ spent += need;+ return need;+ }+ if (!inFlight) return null;+ await new Promise((wake) => waiting.push(wake));+ }+ };let finished = 0;await pool(queue, concurrency, async (job) => {if (io.signal?.aborted) return;- const est = job.preview.costUsd ?? 0;- if (engine === "cloud" && spent + est > maxUsd + 1e-9) {+ const est = await reserve(job);+ if (est === null) {appendJsonl(path.join(dir, "jobs.jsonl"), { ...recordBase(job), status: "skipped", skip: `the spend cap (${money(maxUsd)}) was reached`, at: now() });return;}- spent += est;+ if (io.signal?.aborted) {+ spent -= est;+ return settle();+ }const attempt = (done.get(job.id)?.attempt ?? 0) + 1;- const row = await runJob(client, job, { engine, runId, dir, up, comfy, element: elements.get(job.caseId), attempt, log, sleep, now, signal: io.signal, pollMs: io.pollMs ?? (engine === "demo" ? 500 : 2000) });- // The actual cost replaces the estimate once the server knows it.- if (row.costUsd?.actual != null) spent += row.costUsd.actual - est;+ inFlight++;+ let row;+ try {+ row = await runJob(client, job, { engine, runId, dir, up, comfy, element: elements.get(job.caseId), attempt, log, sleep, now, signal: io.signal, pollMs: io.pollMs ?? (engine === "demo" ? 500 : 2000) });+ } finally {+ inFlight--;+ }+ // What it really cost replaces the reservation: the server's figure, the estimate when it has none, nothing when it never started.+ spent += rowSpend(row) - est;+ settle();appendJsonl(path.join(dir, "jobs.jsonl"), row);finished++;const took = row.timing?.wallMs != null ? ` in ${(row.timing.wallMs / 1000).toFixed(1)} s` : "";@@ -253,7 +340,7 @@ export async function runEval(opts, io) {const count = (s) => rows.filter((r) => r.status === s).length;run.finishedAt = now();run.interrupted = !!io.signal?.aborted || undefined;- run.spentUsd = sum(rows.map((r) => r.costUsd?.actual ?? (r.status === "succeeded" ? r.costUsd?.estimate : 0)));+ run.spentUsd = Math.round(spentSoFar(dir) * 1e6) / 1e6;run.results = { succeeded: count("succeeded"), failed: count("failed") + count("timeout") + count("canceled"), skipped: count("skipped"), interrupted: count("interrupted") };writeJson(path.join(dir, "run.json"), run);return { runId, dir, run, rows };@@ -345,10 +432,17 @@ async function prepareElement(client, job, ctx) {const t = job.train;const file = path.join(ctx.dir, "elements.json");const saved = readJson(file, {});- const reuse = t.element ?? saved[job.caseId]?.id;- if (reuse && saved[job.caseId]?.status !== "failed") {+ const prev = saved[job.caseId];+ // An element the case names, one this run already trained, or (demo) already made: use it as it is.+ const reuse = t.element ?? (prev?.id && (prev.status === "succeeded" || prev.status === "demo") ? prev.id : null);+ if (reuse) {const el = await client.request("GET", `/api/elements/${encodeURIComponent(reuse)}`);- if (ctx.engine === "demo" || t.element || el.status !== "training") return { id: el.id, handle: el.handle, reused: true };+ return { id: el.id, handle: el.handle, reused: true };+ }+ // A training this run started and didn't see finish (a resume): wait for it instead of paying for another.+ if (prev?.id && prev.training && prev.status === "training") {+ ctx.log(`Waiting for the training this run started (${prev.training})`);+ return waitForTraining(client, prev, { ...ctx, file, caseId: job.caseId, resumed: true });}// Demo runs don't train: the fixtures' drawn faces become the element's reference images.const photos = ctx.engine === "demo" ? Object.values(job.inputs).flat() : listMedia(t.photos).filter((f) => /\.(png|jpe?g|webp)$/i.test(f));@@ -362,24 +456,33 @@ async function prepareElement(client, job, ctx) {return record;}ctx.log(`Training ${name} on ${t.target} with ${refs.length} photos`);- const started = ctx.now();const tr = await client.request("POST", `/api/elements/${encodeURIComponent(el.id)}/trainings`, { json: { target: t.target } });- writeJson(file, { ...saved, [job.caseId]: { ...record, training: tr.id, target: t.target, status: "training", startedAt: started } });+ // An earlier training of this case that failed was paid for too: its cost stays in the run's spend.+ const earlierUsd = prev?.training ? (prev.costUsd ?? prev.estimateUsd ?? 0) + (prev.earlierUsd ?? 0) : prev?.earlierUsd;+ const started = { ...record, training: tr.id, target: t.target, status: "training", startedAt: ctx.now(), estimateUsd: ctx.estimateUsd ?? null, ...(earlierUsd && { earlierUsd }) };+ writeJson(file, { ...saved, [job.caseId]: started });+ return waitForTraining(client, started, { ...ctx, file, caseId: job.caseId });+}++/** Poll an element's training until it ends; the record in elements.json follows it. */+async function waitForTraining(client, record, ctx) {+ const save = (patch) => writeJson(ctx.file, { ...readJson(ctx.file, {}), [ctx.caseId]: { ...record, ...patch } });for (;;) {if (ctx.signal?.aborted) throw new Error("stopped");await ctx.sleep(15_000);- const list = await client.request("GET", `/api/elements/${encodeURIComponent(el.id)}/trainings`);- const now = (list.items ?? []).find((x) => x.id === tr.id);+ const list = await client.request("GET", `/api/elements/${encodeURIComponent(record.id)}/trainings`);+ const now = (list.items ?? []).find((x) => x.id === record.training);if (now?.status === "succeeded") {- const done = { ...record, training: tr.id, target: t.target, status: "succeeded", costUsd: now.costUsd ?? null, startedAt: started, finishedAt: ctx.now() };- writeJson(file, { ...readJson(file, {}), [job.caseId]: done });- return done;+ const done = { ...record, status: "succeeded", costUsd: now.costUsd ?? null, finishedAt: ctx.now() };+ save(done);+ // A training seen through from an earlier attempt was counted in that attempt's spend already.+ return ctx.resumed ? { ...done, reused: true } : done;}if (now?.status === "failed" || now?.status === "canceled") {- writeJson(file, { ...readJson(file, {}), [job.caseId]: { ...record, training: tr.id, status: "failed", error: now.error } });+ save({ status: "failed", error: now.error, costUsd: now.costUsd ?? null });throw new Error(now.error ?? "the training failed");}- if (ctx.now() - started > 4 * 3600_000) throw new Error("still training after 4 hours");+ if (ctx.now() - record.startedAt > 4 * 3600_000) throw new Error("still training after 4 hours");}}@@ -392,6 +495,8 @@ async function runJob(client, job, ctx) {const t0 = ctx.now();const row = { ...base, attempt: ctx.attempt, at: t0, costUsd: { estimate: job.preview?.costUsd ?? null } };let watched = [];+ /** The generations as last seen: a download that breaks after they finished still keeps their cost and timing. */+ let final = [];ctx.comfy?.start();try {const inputs = job.train ? {} : await ctx.up.inputs(job.inputs);@@ -422,7 +527,7 @@ async function runJob(client, job, ctx) {// Wait for every generation (a studio route's final step waits for the steps before it).const deadline = t0 + (job.timeoutSeconds ?? 1800) * 1000;- let final = watched;+ final = watched;for (;;) {final = await Promise.all(watched.map((g) => (TERMINAL.has(g.status) ? g : getGeneration(client, g.id))));watched = final;@@ -446,9 +551,9 @@ async function runJob(client, job, ctx) {if (row.status === "succeeded") row.outputs = await download(client, final, path.join(ctx.dir, "outputs", safeName(job.id)), ctx.dir);return row;} catch (e) {+ row.error = row.status === "succeeded" ? `the results didn't download: ${e.message}` : e.message;row.status = "failed";- row.error = e.message;- return finish(row, [], t0, ctx);+ return finish(row, final, t0, ctx);} finally {ctx.comfy?.stop();}diff --git a/tests/eval-harness.test.ts b/tests/eval-harness.test.tsindex c6e38071..835c4f47 100644--- a/tests/eval-harness.test.ts+++ b/tests/eval-harness.test.ts@@ -40,6 +40,7 @@ const cases = (await import("../scripts/eval/lib/cases.mjs")) as { loadSuite: (nconst runner = (await import("../scripts/eval/lib/run.mjs")) as {runEval: (opts: object, io: object) => Promise<Record<string, unknown>>;lorasMissing: (jobs: object[], installed: string[] | null) => { file: string; url?: string; jobs: number }[] | null;+ trainingPrice: (target: { price?: string } | null) => number | null;};const report = (await import("../scripts/eval/lib/report.mjs")) as { writeReport: (dir: string, root: string) => Record<string, unknown> };const review = (await import("../scripts/eval/lib/review.mjs")) as {@@ -516,3 +517,130 @@ describe("runEval as a library", () => {expect(promptOnly.loras?.applied).toEqual([]);});});++/**+ * A stand-in Fuobe for the runner's cloud logic: previews at a fixed price, generations that finish on the+ * second look and report their own cost, and (optionally) downloads that break. Records what was started and+ * how many ran at once.+ */+function cloudStub(o: { previewUsd: number | null; actualUsd: number | null; failDownload?: boolean; models?: string[]; trainingTargets?: object[]; route?: { provider: string; endpoint: string } }) {+ let n = 0;+ const started: string[] = [];+ const looked = new Set<string>();+ let running = 0;+ let most = 0;+ const models = o.models ?? ["trim-video", "join-clips", "extract-frame"];+ const client = {+ url: "http://stub.local",+ async request(method: string, p: string, opts: { json?: Record<string, unknown>; query?: Record<string, unknown> } = {}) {+ if (p === "/api/status") return { engineMode: "both", demoMode: "auto", cloudProvider: "auto", providers: { fal: { configured: true } } };+ if (p === "/api/models") return { items: models.map((id) => ({ id })) };+ if (p === "/api/uploads") return { url: `/api/files/uploads/${++n}.mp4` };+ if (p.endsWith("/trainings") && method === "GET") return { targets: o.trainingTargets ?? [], items: [] };+ if (p === "/api/generations" && opts.query?.preview) return { demo: false, providerId: o.route?.provider ?? "fal", costUsd: o.previewUsd, model: { id: opts.json?.modelId }, route: o.route ?? { provider: "fal", endpoint: "fal-ai/ffmpeg-api" }, prompt: opts.json?.prompt ?? "", loras: { applied: [], dropped: [] } };+ if (p === "/api/generations" && method === "POST") {+ const id = `g${++n}`;+ started.push(id);+ running++;+ most = Math.max(most, running);+ return { items: [{ id, status: "queued" }] };+ }+ const m = /^\/api\/generations\/(.+)$/.exec(p);+ if (m) {+ if (!looked.has(m[1])) {+ looked.add(m[1]);+ return { id: m[1], status: "running" };+ }+ running--;+ return { id: m[1], status: "succeeded", costUsd: o.actualUsd, modelId: "trim-video", provider: "fal", createdAt: 1, completedAt: 2, outputs: [{ url: `/api/files/out/${m[1]}.mp4`, kind: "video" }] };+ }+ throw new Error(`stub: no ${method} ${p}`);+ },+ async download(_url: string, dest: string) {+ if (o.failDownload) throw new Error("connection reset");+ fs.mkdirSync(path.dirname(dest), { recursive: true });+ fs.writeFileSync(dest, "x");+ return 1;+ },+ };+ return { client, started, most: () => most };+}++describe("the cloud spend cap", () => {+ const root = path.join(dir, "cap-results");+ const run = (o: Record<string, unknown>, stub: ReturnType<typeof cloudStub>) =>+ runner.runEval({ suite: cases.loadSuite("selftest"), engine: "cloud", seeds: [1, 2, 3], root, ...o }, { client: stub.client, sleep: async () => {}, pollMs: 0 }) as Promise<{ runId: string; dir: string; run: { spentUsd: number; results: Record<string, number> }; rows: Row[] }>;++ it("doesn't start a job the server can't price, unless asked, and then runs it alone and stops at the cap", async () => {+ const none = cloudStub({ previewUsd: null, actualUsd: 2 });+ const r = await run({ maxUsd: 1 }, none);+ expect(none.started).toEqual([]);+ const cloudRows = r.rows.filter((x) => !/^runs on/.test(String(x.skip)));+ expect(cloudRows).toHaveLength(9);+ expect(cloudRows.every((x) => x.status === "skipped" && /can't price it/.test(String(x.skip)))).toBe(true);+ expect(r.run.spentUsd).toBe(0);+ // --allow-unpriced: the first one holds what's left of the cap, so nothing starts beside it; once it has cost $2 nothing else starts.+ const some = cloudStub({ previewUsd: null, actualUsd: 2 });+ const r2 = await run({ maxUsd: 1, allowUnpriced: true }, some);+ expect(some.started).toHaveLength(1);+ expect(some.most()).toBe(1);+ expect(r2.run.spentUsd).toBe(2);+ expect(r2.rows.filter((x) => x.status === "skipped" && !/^runs on/.test(String(x.skip))).map((x) => x.skip)).toEqual(Array(8).fill("the spend cap ($1.00) was reached"));+ });++ it("keeps the cap with jobs running side by side, counts an earlier attempt's spend on --resume, and keeps a paid job's cost when its download breaks", async () => {+ const stub = cloudStub({ previewUsd: 0.4, actualUsd: 0.4 });+ // 9 jobs at $0.40 are $3.60: the run refuses to start over a $1 cap.+ await expect(run({ maxUsd: 1 }, stub)).rejects.toThrow(/about \$3\.60, over the spend cap of \$1\.00/);+ expect(stub.started).toEqual([]);+ const first = await run({ maxUsd: 5, limit: 1 }, stub);+ expect(first.run.spentUsd).toBeCloseTo(1.2, 6);+ // Resuming the same run under a $1.50 cap: $1.20 is already spent, and the jobs left can't fit.+ const statusOf = (rows: Row[]) => rows.map((x) => x.status).join(",");+ fs.appendFileSync(path.join(first.dir, "jobs.jsonl"), `${JSON.stringify({ ...first.rows[0], status: "failed", error: "pretend", attempt: 1, costUsd: { estimate: 0.4, actual: 0.4 }, generations: ["gx"] })}\n`);+ await expect(run({ maxUsd: 1.5, resume: first.runId }, stub)).rejects.toThrow(/\$1\.60 already spent in this run/);+ const again = await run({ maxUsd: 5, resume: first.runId }, stub);+ expect(statusOf(again.rows)).toBe("succeeded,succeeded,succeeded");+ // Three paid attempts, one pretend failure and one retry: $2.00.+ expect(again.run.spentUsd).toBeCloseTo(2, 6);+ // Jobs that cost more than their price: once the real spend reaches the cap, nothing else starts.+ const dear = cloudStub({ previewUsd: 0.25, actualUsd: 1 });+ const capped = await run({ maxUsd: 1, limit: 1, seeds: [1, 2, 3, 4], concurrency: 1 }, dear);+ expect(dear.started).toHaveLength(1);+ expect(capped.run.spentUsd).toBe(1);+ const broken = cloudStub({ previewUsd: 0.4, actualUsd: 0.5, failDownload: true });+ const r = await run({ maxUsd: 5, limit: 1 }, broken);+ expect(r.rows.every((x) => x.status === "failed" && /didn't download: connection reset/.test(String(x.error)))).toBe(true);+ expect(r.rows.map((x) => (x.costUsd as { actual: number }).actual)).toEqual([0.5, 0.5, 0.5]);+ expect(r.run.spentUsd).toBeCloseTo(1.5, 6);+ });++ it("prices cloud training before anything starts, and never trains on Higgsfield", async () => {+ const suiteFile = path.join(dir, "train-suite.json");+ const train = (target: string) => ({ photos: path.join(process.cwd(), "scripts/eval/fixtures/character"), targets: { cloud: target }, minPhotos: 3, consent: true });+ fs.writeFileSync(+ suiteFile,+ JSON.stringify({+ suite: "train-cap",+ title: "Training under the cap",+ description: "x",+ original: "fuobe-soul",+ kind: "image",+ targets: { cloud: { models: ["z-image-turbo-cloud"] } },+ cases: [+ { id: "t/fal", title: "fal", train: train("fal-z-image"), prompts: [{ id: "p", text: "{character} in a cafe", seed: 1 }] },+ { id: "t/hf", title: "hf", train: train("soul-id-v2"), prompts: [{ id: "p", text: "{character} in a cafe", seed: 1 }] },+ ],+ }),+ );+ const stub = cloudStub({ previewUsd: 0.01, actualUsd: 0.01, models: ["z-image-turbo-cloud"], trainingTargets: [{ id: "fal-z-image", provider: "fal", price: "≈ $0.85 (1,000 steps)" }, { id: "soul-id-v2", provider: "higgsfield", price: "Higgsfield balance" }] });+ const suite = cases.loadSuite(suiteFile);+ const dry = (await runner.runEval({ suite, engine: "cloud", dryRun: true, root }, { client: stub.client })) as { estimateUsd: number; trainingUsd: number; jobs: { case: string; skip?: string }[] };+ expect(dry.trainingUsd).toBe(0.85);+ expect(dry.estimateUsd).toBeCloseTo(0.86, 6);+ expect(dry.jobs.find((j) => j.case === "t/hf")?.skip).toMatch(/Higgsfield's API/);+ await expect(runner.runEval({ suite, engine: "cloud", maxUsd: 0.5, root }, { client: stub.client })).rejects.toThrow(/about \$0\.86, over the spend cap/);+ expect(runner.trainingPrice({ price: "Free" })).toBe(0);+ expect(runner.trainingPrice({ price: "Higgsfield balance" })).toBeNull();+ });+});