Python
history: keep the app's declared output nodes, in order
neuregex0bf67b7fbeb3
diff --git a/server/history.ts b/server/history.tsindex 70a40e7..21e33c8 100644--- a/server/history.ts+++ b/server/history.ts@@ -132,6 +132,16 @@ export async function waitPatiently(job: { wait(timeoutMs?: number): Promise<unk}}+/** The media outputs worth keeping: those of the app's declared output nodes, in the declared order (the first one+ * is the card's thumbnail), and only when none of them produced anything, every media output of the job. The+ * backend lists more than the results: core LoadVideo reports the clip it was given (mixed-media's first web run,+ * 23 oct: the card showed the input clip, the result was out-02). */+export function pickOutputs<T extends { type: string; nodeId: string }>(outputs: readonly T[], wanted: readonly string[]): T[] {+ const media = outputs.filter((o) => MEDIA.has(o.type));+ const declared = wanted.flatMap((node) => media.filter((o) => o.nodeId === node));+ return declared.length ? declared : media;+}+const secondsBetween = (a: string | Date | null | undefined, b: string | Date | null | undefined) =>a && b ? Math.max(0, Math.round((new Date(b).getTime() - new Date(a).getTime()) / 100) / 10) : null;@@ -141,10 +151,13 @@ export class History {readonly dir: string;private onContact: () => void;+ private outputNodes: (appId: string) => string[];- constructor(dir: string, onContact: () => void = () => {}) {+ /** outputNodes(app): the nodes app.json declares as outputs, in order; [] means "keep every media output". */+ constructor(dir: string, onContact: () => void = () => {}, outputNodes: (appId: string) => string[] = () => []) {this.dir = dir;this.onContact = onContact;+ this.outputNodes = outputNodes;mkdirSync(dir, { recursive: true });}@@ -265,8 +278,7 @@ export class History {if (entry.status === "succeeded") {const folder = join(this.dir, entry.id);let n = 0;- for (const out of job.outputs) {- if (!MEDIA.has(out.type)) continue;+ for (const out of pickOutputs(job.outputs, this.outputNodes(entry.app))) {try {const bytes = await out.toBytes();const ext = safeExt(out.name, out.contentType);diff --git a/server/server.ts b/server/server.tsindex f8e1972..09f5665 100644--- a/server/server.ts+++ b/server/server.ts@@ -44,7 +44,7 @@ const WARM_MS = 9 * 60 * 1000;let lastContact = 0;let waking: { since: number; error?: string } | null = null;const contact = () => { lastContact = Date.now(); };-const history = new History(join(root, "out", "history"), contact);+const history = new History(join(root, "out", "history"), contact, (id) => (apps.find((a) => a.id === id)?.outputs ?? []).map((o) => o.node));const flags = new Flags(join(root, "lab", "flags.json"), apps);const MIME: Record<string, string> = {diff --git a/test/history.test.ts b/test/history.test.tsindex 5f7bf80..87f1e53 100644--- a/test/history.test.ts+++ b/test/history.test.ts@@ -6,7 +6,7 @@ import { join } from "node:path";import type { ComfyEvent } from "@comfyorg/sdk";import { loadRegistry } from "../server/registry.ts";import { resolveValues } from "../server/runner.ts";-import { History, type JobLike } from "../server/history.ts";+import { History, pickOutputs, type JobLike } from "../server/history.ts";const { apps } = loadRegistry();const byId = (id: string) => apps.find((a) => a.id === id)!;@@ -77,6 +77,16 @@ test("following a job stores its media outputs and timings", async () => {} finally { done(); }});+test("the app's declared output nodes come first; the backend also lists what LoadVideo was given", () => {+ const given = { name: "clip.mp4", type: "video", nodeId: "5001" };+ const made = { name: "mixed-media_00001_.mp4", type: "video", nodeId: "4852" };+ const latent = { name: "x.latent", type: "latent", nodeId: "3" };+ assert.deepEqual(pickOutputs([given, made, latent], ["4852"]), [made]);+ assert.deepEqual(pickOutputs([given, made], ["ff_hdr_save", "4852"]), [made], "declared nodes that made nothing are skipped");+ assert.deepEqual(pickOutputs([given, made, latent], []), [given, made], "no declaration: every media output");+ assert.deepEqual(pickOutputs([given, made], ["999"]), [given, made], "a wrong declaration still shows the results");+});+test("a failed job keeps the node and the message", async () => {const { history, done } = tempHistory();try {