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.

Your computer's ComfyUI: only a computer whose ComfyUI answers runs the jobs

Adarsh1d7331a39b1c
diff --git a/src/app/api/gpu/computer/beat/route.ts b/src/app/api/gpu/computer/beat/route.ts
index 2c491972..4398d7ed 100644
--- a/src/app/api/gpu/computer/beat/route.ts
+++ b/src/app/api/gpu/computer/beat/route.ts
@@ -24,7 +24,7 @@ export async function POST(request: Request) {
return Response.json({ error: "Send JSON" }, { status: 400 });
}
try {
- return Response.json(beat(principal, { device: String(body?.device ?? ""), port: body?.port, facts: body?.facts, running: Array.isArray(body?.running) ? body.running : [], takeover: body?.takeover === true, stopped: Array.isArray(body?.stopped) ? body.stopped : [] }), {
+ return Response.json(beat(principal, { device: String(body?.device ?? ""), port: body?.port, facts: body?.facts, running: Array.isArray(body?.running) ? body.running : [], takeover: body?.takeover === true, stopped: Array.isArray(body?.stopped) ? body.stopped : [], ready: body?.ready !== false }), {
headers: { "Cache-Control": "no-store" },
});
} catch (e) {
diff --git a/src/lib/computer-runner.ts b/src/lib/computer-runner.ts
index 03677232..1776ec87 100644
--- a/src/lib/computer-runner.ts
+++ b/src/lib/computer-runner.ts
@@ -245,13 +245,16 @@ export class ComputerRunner {
async beat(takeover = false): Promise<BeatAnswer | null> {
if (!this.alive) return null;
const t = this.now();
- // Facts every minute; the LoRA names every 5 minutes.
- const loras = t - this.factsWithLoras > 5 * 60_000;
- const facts = t - this.factsSentAt > 60_000 ? await this.facts(loras) : null;
+ // A look at ComfyUI here every beat (only a computer whose ComfyUI answers takes the jobs); its facts go to the
+ // server every minute, the LoRA names every 5 minutes.
+ const due = t - this.factsSentAt > 60_000;
+ const loras = due && t - this.factsWithLoras > 5 * 60_000;
+ const read = await this.facts(loras);
+ const facts = due ? read : null;
if (facts) {
this.factsSentAt = t;
if (loras) this.factsWithLoras = t;
- } else if (this.view.comfy !== "ok") await this.facts(false);
+ }
const running = this.view.jobs
.filter((j) => j.phase !== "done" && j.phase !== "failed")
.map((j) => {
@@ -262,7 +265,7 @@ export class ComputerRunner {
const res = await this.deps.api("/api/gpu/computer/beat", {
method: "POST",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ device: this.o.device, port: this.o.port, ...(facts && { facts }), running, ...(stopped.length && { stopped }), ...(takeover && { takeover: true }) }),
+ body: JSON.stringify({ device: this.o.device, port: this.o.port, ready: !!read, ...(facts && { facts }), running, ...(stopped.length && { stopped }), ...(takeover && { takeover: true }) }),
});
if (res.ok) for (const s of stopped) this.stoppedDone.delete(s);
if (res.status === 401) {
diff --git a/src/server/computer/tasks.ts b/src/server/computer/tasks.ts
index 74f48a9b..bdd31bed 100644
--- a/src/server/computer/tasks.ts
+++ b/src/server/computer/tasks.ts
@@ -279,18 +279,22 @@ export interface BeatInput {
takeover?: boolean;
/** Tasks whose prompts this computer took off its ComfyUI, as the server asked. */
stopped?: string[];
+ /** False: this computer's ComfyUI doesn't answer (a phone, a laptop without one, or ComfyUI restarting). */
+ ready?: boolean;
}
/**
* A computer's beat: it takes or renews the runner's place when that is free, lapsed, its own, or taken over; it keeps
- * the tasks it works on alive; and it hears which of its prompts to stop.
+ * the tasks it works on alive; and it hears which of its prompts to stop. A computer whose ComfyUI doesn't answer only
+ * keeps a place it has (its ComfyUI restarting), so a phone or a laptop without ComfyUI never takes the jobs.
*/
export function beat(principal: Principal, b: BeatInput, now = Date.now()): BeatAnswer {
if (!DEVICE_ID.test(b.device)) throw new RelayError(400, "Bad device");
const d = rdb();
sweepPrincipal(principal);
const cur = runnerOf(principal);
- const free = !cur || cur.device === b.device || cur.lease_until < now || b.takeover === true;
+ const elsewhere = !!cur && cur.device !== b.device && cur.lease_until >= now;
+ const free = b.ready === false ? cur?.device === b.device : !elsewhere || b.takeover === true;
const port = Number.isInteger(b.port) && b.port! > 0 && b.port! < 65536 ? b.port! : null;
if (free) {
const facts = b.facts !== undefined ? cleanFacts(b.facts, json<ComputerFacts>(cur?.facts_json ?? null, {})) : json<ComputerFacts>(cur?.device === b.device ? cur.facts_json : null, {});
@@ -314,7 +318,7 @@ export function beat(principal: Principal, b: BeatInput, now = Date.now()): Beat
d.prepare("SELECT id, prompt_id, prefix FROM relay_tasks WHERE principal = ? AND device = ? AND stop = 1").all(principal, b.device) as { id: string; prompt_id: string; prefix: string | null }[]
).map((s) => ({ task: s.id, promptId: s.prompt_id, ...(s.prefix && { prefix: s.prefix }) }));
const waiting = (d.prepare("SELECT COUNT(*) AS n FROM relay_tasks WHERE principal = ? AND state = 'waiting'").get(principal) as { n: number }).n;
- return { runner: free, heldElsewhere: !free, waiting, stops };
+ return { runner: free, heldElsewhere: !free && elsewhere, waiting, stops };
}
function noteProgress(id: string, r: Record<string, unknown>, now: number) {
diff --git a/tests/computer-relay.test.ts b/tests/computer-relay.test.ts
index 102f63b8..4e3555d4 100644
--- a/tests/computer-relay.test.ts
+++ b/tests/computer-relay.test.ts
@@ -237,6 +237,28 @@ describe("a run on the ComfyUI on the person's own computer", () => {
expect(fake.posts() - posts).toBe(1);
});
+ it("a tab with no ComfyUI to reach (a phone, a laptop without one) never takes the jobs from the computer that has one", async () => {
+ const phone = tab(routes, cookie, fake.port, { device: "device-phone-1", local: async () => Promise.reject(new TypeError("Failed to fetch")) });
+ const pic = await uploadPng();
+ const [id] = await run({ feature: "app", modelId: "oss-qwen-edit", presetId: "app-headshot-generator", prompt: "", inputs: { images: [pic.url] } });
+ await handedOver(id);
+ expect(await phone.runner.beat()).toMatchObject({ runner: false, heldElsewhere: false, waiting: 1 });
+ expect(phone.runner.view.comfy).toBe("down");
+ expect(await phone.runner.claimAndRun()).toBe(false);
+ // The computer with ComfyUI takes them, and the phone hears that another computer runs them.
+ const desk = tab(routes, cookie, fake.port, { device: "device-desk-1" });
+ expect(await desk.runner.beat()).toMatchObject({ runner: true });
+ expect(await phone.runner.beat()).toMatchObject({ runner: false, heldElsewhere: true });
+ expect(await phone.runner.beat(true)).toMatchObject({ runner: false });
+ await work(desk);
+ const g = await settle(id);
+ expect(g?.status, g?.error ?? "").toBe("succeeded");
+ // A runner whose ComfyUI restarts keeps its place meanwhile.
+ fake.opts.cors = "off";
+ expect(await desk.runner.beat()).toMatchObject({ runner: true });
+ fake.opts.cors = "http://localhost:3000";
+ });
+
it("sends a job again once when ComfyUI restarts and loses it, and fails it kindly the second time", async () => {
const t = tab(routes, cookie, fake.port);
await t.runner.beat();
diff --git a/tests/helpers/computer-tab.ts b/tests/helpers/computer-tab.ts
index c1b4ac32..e1894997 100644
--- a/tests/helpers/computer-tab.ts
+++ b/tests/helpers/computer-tab.ts
@@ -84,10 +84,10 @@ export function browserLocal(site = SITE): RunnerDeps["local"] & { urls: string[
export const sha256 = async (data: ArrayBuffer) => Buffer.from(await crypto.subtle.digest("SHA-256", data)).toString("hex");
-/** A tab on this computer for `cookie`, its ComfyUI on `port`. */
-export function tab(routes: ComputerRoutes, cookie: string, port: number, o: { device?: string; clientId?: string } = {}) {
+/** A tab on this computer for `cookie`, its ComfyUI on `port` (`local`: how it reaches it, when not as a browser does). */
+export function tab(routes: ComputerRoutes, cookie: string, port: number, o: { device?: string; clientId?: string; local?: RunnerDeps["local"] } = {}) {
const api = apiAs(routes, cookie);
- const local = browserLocal();
+ const local = o.local ?? browserLocal();
const runner = new ComputerRunner(
{ api, local, socket: (url) => new WebSocket(url) as never, sha256, origin: SITE, sleep: (ms) => new Promise((r) => setTimeout(r, Math.min(ms, 50))) },
{ device: o.device ?? "device-test-1", port, clientId: o.clientId ?? "client-test-1" },