The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(create): a missed completion event cannot hold the next batch (W32)
purple54ce972dbc2a
diff --git a/src/api/comfyui.ts b/src/api/comfyui.tsindex 3bc645b8..9edc68e0 100644--- a/src/api/comfyui.ts+++ b/src/api/comfyui.ts@@ -825,9 +825,14 @@ export { COMPONENT_REGISTRY } from './component-registry'// ─── Connection & Info ───-export async function checkComfyConnection(): Promise<boolean> {+/** W32 follow-up: the limit of the check before a run and of the render+ * heartbeat's check (the proxy's own default is five minutes). */+export const COMFY_PROBE_LIMIT_MS = 15_000++/** Does ComfyUI answer. `limitMs` bounds the wait (none: the proxy's default). */+export async function checkComfyConnection(limitMs?: number): Promise<boolean> {try {- const res = await localFetch(comfyuiUrl('/system_stats'))+ const res = await localFetch(comfyuiUrl('/system_stats'), limitMs ? { timeoutMs: limitMs } : undefined)return res.ok} catch {return falsediff --git a/src/hooks/__tests__/useCreate-a-missed-completion-does-not-hold-the-batches.test.ts b/src/hooks/__tests__/useCreate-a-missed-completion-does-not-hold-the-batches.test.tsnew file mode 100644index 00000000..a3087d27--- /dev/null+++ b/src/hooks/__tests__/useCreate-a-missed-completion-does-not-hold-the-batches.test.ts@@ -0,0 +1,179 @@+// @vitest-environment jsdom+/**+ * W32 follow-up (Zentrale, 15.11.2026): in the first real run (test build "LU+ * w32", 11:23Z) the app stood about 10 minutes after batch 2 of 3 without+ * sending batch 3, while ComfyUI had finished both and its queue was empty.+ *+ * How a batch is known to be done: the WebSocket event execution_complete for+ * its prompt_id, and a heartbeat every 10 s that reads /history/<id>. A missed+ * event alone is caught by the heartbeat. But the heartbeat asked ComfyUI's+ * /system_stats FIRST, and that check had no limit of its own (the proxy's+ * default is five minutes; a failed proxy call then tries a direct fetch), so+ * one check that did not come back held the history read behind it: the batch+ * was done in ComfyUI and nobody looked. The check before each batch had the+ * same unbounded wait.+ *+ * Now the history is read first, on its own limit; the checks have a limit+ * of 15 s; ticks do not stack; a ComfyUI that does not answer within the+ * limit is said on the line, one that refuses ends the run with a message.+ *+ * Reproduced here with the completion event missed and the unbounded check+ * never answering. Before the fix: batch 1 never ends (red). After: green.+ *+ * Run: npx vitest run src/hooks/__tests__/useCreate-a-missed-completion-does-not-hold-the-batches.test.ts+ */+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'+import { renderHook } from '@testing-library/react'++const seen = vi.hoisted(() => ({+ builds: [] as Record<string, unknown>[],+ lanes: [] as Record<string, unknown>[],+ submits: 0,+ freed: 0,+ onBuild: null as null | (() => void),+ historyGone: false,+ mode: 'hangs' as 'hangs' | 'healthy' | 'gone',+ calls: 0,+}))+/** The connection check. 'hangs': the first check answers, every later one+ * without a limit of its own never comes back (the proxy's five minutes);+ * one asked with a limit answers within it. 'gone': refused at once after+ * the first. */+function probe(limitMs?: number): Promise<boolean> {+ seen.calls+++ if (seen.calls === 1 || seen.mode === 'healthy') return Promise.resolve(true)+ if (seen.mode === 'gone') return Promise.resolve(false)+ return limitMs ? Promise.resolve(true) : new Promise<boolean>(() => {})+}++vi.mock('../../api/mlx-image', () => ({+ isMlxImageHost: () => false,+ isMlxImageModel: () => false,+ mlxStatus: vi.fn(async () => ({ installed: false })),+ listMlxImageModels: vi.fn(async () => []),+ buildMlxImageModels: vi.fn(() => []),+ mergeImageModels: vi.fn((a: unknown[]) => a),+ mlxModelIdFor: vi.fn(() => ''),+ generateMlxImageDataUrl: vi.fn(),+}))+vi.mock('../../api/comfyui', async (orig) => ({+ ...((await orig()) as Record<string, unknown>),+ checkComfyConnection: vi.fn((limitMs?: number) => probe(limitMs)),+ listedLoras: vi.fn(async () => ['char_mira_zimage.safetensors', 'film_grain.safetensors']),+ uploadMediaFile: vi.fn(async (_b: Blob, name: string) => `up_${name}`),+ freeMemory: vi.fn(async () => { seen.freed++ }),+ submitWorkflow: vi.fn(async () => `p${++seen.submits}`),+ getHistory: vi.fn(async (id: string) => seen.historyGone ? null : ({+ status: { status_str: 'success', completed: true },+ outputs: { '9': { images: [{ filename: isVideoRun(id) ? `clip_${id}.mp4` : `img_${id}.png`, subfolder: '', type: 'output' }] } },+ })),+}))+// A run's file is a clip when its build was a video one.+const videoRuns = vi.hoisted(() => new Set<string>())+function isVideoRun(id: string) { return videoRuns.has(id) }+vi.mock('../../api/comfyui-ws', () => ({+ CLIENT_ID: 'lu-test',+ comfyWS: {+ connect: vi.fn(async () => {}),+ mark: () => 0,+ onPreview: () => () => {},+ // W32 follow-up: the completion event never arrives (missed).+ on: () => () => {},+ },+}))+vi.mock('../../api/vram-handoff', async (orig) => ({+ ...((await orig()) as Record<string, unknown>),+ evictChatBackendsForRender: vi.fn(async () => null),+ restoreChatBackendsAfterRender: vi.fn(),+}))+vi.mock('../../api/dynamic-workflow', async (orig) => ({+ ...((await orig()) as Record<string, unknown>),+ checkVideoOutputCapability: vi.fn(async () => ({ webpOnly: false, mp4Capable: true })),+ buildDynamicWorkflow: vi.fn(async (params: Record<string, unknown>) => {+ seen.builds.push(params)+ seen.onBuild?.()+ if (typeof params.frames === 'number') videoRuns.add(`p${seen.submits + 1}`)+ return { '1': { class_type: 'SaveImage', inputs: {} } }+ }),+ buildLocalOpWorkflow: vi.fn(async (params: Record<string, unknown>) => {+ seen.lanes.push(params)+ videoRuns.add(`p${seen.submits + 1}`)+ return { '1': { class_type: 'SaveImage', inputs: {} } }+ }),+}))+vi.mock('../../components/create/experimental/galleryUrl', async (orig) => ({+ ...((await orig()) as Record<string, unknown>),+ fetchGalleryItemBlob: vi.fn(async () => new Blob(['png'], { type: 'image/png' })),+}))+vi.mock('../../components/create/experimental/loadImage', () => ({+ loadImageRef: vi.fn(async () => ({ filename: 'char-start.png', url: 'blob:start', width: 1024, height: 576 })),+}))++import { useCreate } from '../useCreate'+import { useCreateStore } from '../../stores/createStore'+import { classifyModel } from '../../api/comfyui'++const QWEN = 'qwen_image_2512_fp8_e4m3fn.safetensors'+const listed = (...names: string[]) => names.map((name) => ({ name, type: classifyModel(name) }))++function arrange() {+ useCreateStore.setState({+ backend: 'local', isGenerating: false, error: null, gallery: [], promptHistory: [], fixupPrompt: null,+ improvePrompt: false, cloudOp: null, utilityOp: null, removebg: false, source: null, mask: null, references: [],+ hiresFixEnabled: false, transparentBackground: false, seed: 7, batchSources: [], videoShots: [],+ imageModel: QWEN, imageModelType: classifyModel(QWEN), imageModelList: listed(QWEN),+ videoModelList: [], lipsyncModelList: [], motionModelList: [], localOpModel: '',+ selectedLoras: [], loraStrengths: {}, selectedCharacter: null,+ } as never)+ useCreateStore.getState().setIntent('image')+ useCreateStore.getState().setBatchSize(3)+ useCreateStore.getState().setBatchPacket(2)+ useCreateStore.getState().setPrompt('a lighthouse at dusk')+}++/** Runs Create on fake time, at most `ms`; true when it ended. */+async function createFor(ms: number): Promise<boolean> {+ const { result } = renderHook(() => useCreate())+ let ended = false+ void result.current.generate().then(() => { ended = true })+ for (let t = 0; t < ms && !ended; t += 1000) await vi.advanceTimersByTimeAsync(1000)+ return ended+}++beforeEach(() => {+ vi.useFakeTimers()+ seen.builds.length = 0+ seen.submits = 0+ seen.calls = 0+ seen.historyGone = false+ seen.onBuild = null+ videoRuns.clear()+})+afterEach(() => { vi.useRealTimers() })++describe('a missed completion event does not hold the batches', () => {+ it('the event is missed and the check never answers: the heartbeat still finds both batches done (red before the fix)', async () => {+ seen.mode = 'hangs'+ arrange()+ expect(await createFor(120_000)).toBe(true)+ expect(seen.builds.map((b) => b.batchSize)).toEqual([2, 1])+ expect(useCreateStore.getState().gallery).toHaveLength(2)+ expect(useCreateStore.getState().error).toBeNull()+ })++ it('the event is missed on a healthy ComfyUI: done from /history within a heartbeat or two', async () => {+ seen.mode = 'healthy'+ arrange()+ expect(await createFor(60_000)).toBe(true)+ expect(seen.builds.map((b) => b.batchSize)).toEqual([2, 1])+ })++ it('NEGATIVE CONTROL: a ComfyUI that is gone ends the run with a message, the rest is not started', async () => {+ seen.mode = 'gone'+ seen.historyGone = true+ arrange()+ expect(await createFor(120_000)).toBe(true)+ expect(seen.builds.map((b) => b.batchSize)).toEqual([2])+ expect(useCreateStore.getState().error).toMatch(/ComfyUI stopped responding/)+ })+})diff --git a/src/hooks/useCreate.ts b/src/hooks/useCreate.tsindex 6c70f3b0..48f4d3e7 100644--- a/src/hooks/useCreate.ts+++ b/src/hooks/useCreate.ts@@ -7,6 +7,7 @@ import { macHiresPass } from '../lib/mac-hires'import { v4 as uuid } from 'uuid'import {checkComfyConnection,+ COMFY_PROBE_LIMIT_MS,refreshComfyModels,getImageModels,getVideoModels,@@ -1758,7 +1759,9 @@ export function useCreate() {setIsGenerating(true)setProgress(0, 'Checking ComfyUI...')const guard = await ensureComfyForRender({- probe: () => checkComfyConnection(),+ // W32 follow-up: asked with a limit of its own, not the proxy's five+ // minutes in front of every batch.+ probe: () => checkComfyConnection(COMFY_PROBE_LIMIT_MS),status: () => backendCall<ComfyGuardStatus>('comfyui_status').catch(() => null),start: async () => { await backendCall('start_comfyui') },onProgress: (line) => setProgress(0, line),@@ -2329,72 +2332,92 @@ export function useCreate() {}}, 15000)- // Heartbeat: check ComfyUI every 10s + poll for completion (catches missed WS events)+ // Heartbeat: every 10s, poll for completion (catches missed WS events) and check ComfyUI.+ // W32 follow-up: the history is asked FIRST and on its own limit+ // (getHistory). It used to wait behind a /system_stats probe without+ // a limit of its own (the proxy's five minutes), so a missed+ // completion event could sit behind a probe that did not come back,+ // and the next batch never started. Ticks do not stack.let completionHandled = falselet queueCheckCounter = 0+ let beating = falseconst heartbeat = setInterval(async () => {- if (completionHandled) return- const alive = await checkComfyConnection()- if (!alive) { cleanup(); reject(new Error('ComfyUI stopped responding during generation')); return }- // A prompt still in ComfyUI's queue is alive even when the socket- // goes quiet (long model loads emit no events) — refresh the- // watchdog once a minute from the queue itself.- queueCheckCounter += 1- if (queueCheckCounter >= 6) {- queueCheckCounter = 0- if (await isPromptQueued(promptId).catch(() => false)) lastActivity = Date.now()- }- // Poll history to catch completion if WebSocket event was missed+ if (completionHandled || beating) return+ beating = truetry {- const history = await getHistory(promptId)- if (!history) return- const statusStr = history.status?.status_str- if (statusStr === 'success') {- completionHandled = true- console.log('[useCreate] Completion detected via polling (WS event missed)')- cleanup()- useCreateStore.getState().setProgressPhase('complete')- setProgress(95, 'Fetching results...')- const messages = historyMessages(history)- const startMsg = messages.find(([t]) => t === 'execution_start')- const endMsg = messages.find(([t]) => t === 'execution_success')- const comfyTime = startMsg?.[1]?.timestamp && endMsg?.[1]?.timestamp- ? ((endMsg[1].timestamp - startMsg[1].timestamp) / 1000).toFixed(1) : null- setProgress(100, 'Complete!')- useCreateStore.getState().setLastGenTime(comfyTime ? `${comfyTime}s` : null)- const outputs = history.outputs ?? {}- let found = false- for (const nodeId of Object.keys(outputs)) {- // Extract files from ANY keyed array, not just images/gifs/- // videos — custom save nodes post under other keys (audio,- // result, files, …); previously those outputs were dropped- // even though they existed on disk.- const files: ComfyUIOutput[] = extractComfyOutputFiles(outputs[nodeId])- for (const file of files) {- found = true- addToGallery({- id: uuid(), type: galleryTypeForFile(file.filename, mode),- filename: file.filename, subfolder: file.subfolder ?? '', comfyType: file.type ?? 'output',- prompt, ...improveFields, ...transparentFields(), negativePrompt, model: activeModel,- modelType: mode === 'image' ? imageModelType : (videoModelsList.find(m => m.name === activeModel)?.type ?? 'wan'),- seed: runSeed,- steps, cfgScale, sampler, scheduler, width: outputWidth, height: outputHeight, batchSize, ...ranLoras(runLoras),- createdAt: Date.now(), builderUsed, intent, ...toolFields, ...(skippedNote ? { runNote: skippedNote } : {}),- })+ // Poll history to catch completion if WebSocket event was missed+ try {+ const history = await getHistory(promptId)+ // Still running (no entry yet): on to the reachability check.+ if (!history) throw new Error('no history entry yet')+ const statusStr = history.status?.status_str+ if (statusStr === 'success') {+ completionHandled = true+ console.log('[useCreate] Completion detected via polling (WS event missed)')+ cleanup()+ useCreateStore.getState().setProgressPhase('complete')+ setProgress(95, 'Fetching results...')+ const messages = historyMessages(history)+ const startMsg = messages.find(([t]) => t === 'execution_start')+ const endMsg = messages.find(([t]) => t === 'execution_success')+ const comfyTime = startMsg?.[1]?.timestamp && endMsg?.[1]?.timestamp+ ? ((endMsg[1].timestamp - startMsg[1].timestamp) / 1000).toFixed(1) : null+ setProgress(100, 'Complete!')+ useCreateStore.getState().setLastGenTime(comfyTime ? `${comfyTime}s` : null)+ const outputs = history.outputs ?? {}+ let found = false+ for (const nodeId of Object.keys(outputs)) {+ // Extract files from ANY keyed array, not just images/gifs/+ // videos — custom save nodes post under other keys (audio,+ // result, files, …); previously those outputs were dropped+ // even though they existed on disk.+ const files: ComfyUIOutput[] = extractComfyOutputFiles(outputs[nodeId])+ for (const file of files) {+ found = true+ addToGallery({+ id: uuid(), type: galleryTypeForFile(file.filename, mode),+ filename: file.filename, subfolder: file.subfolder ?? '', comfyType: file.type ?? 'output',+ prompt, ...improveFields, ...transparentFields(), negativePrompt, model: activeModel,+ modelType: mode === 'image' ? imageModelType : (videoModelsList.find(m => m.name === activeModel)?.type ?? 'wan'),+ seed: runSeed,+ steps, cfgScale, sampler, scheduler, width: outputWidth, height: outputHeight, batchSize, ...ranLoras(runLoras),+ createdAt: Date.now(), builderUsed, intent, ...toolFields, ...(skippedNote ? { runNote: skippedNote } : {}),+ })+ }}+ if (!found) setError('Generation completed but no output was produced.')+ resolve()+ } else if (statusStr === 'error') {+ completionHandled = true+ cleanup()+ const msgs = historyMessages(history)+ const errEntry = msgs.find(([t]) => t === 'execution_error')?.[1]+ const raw = errEntry?.exception_message || 'ComfyUI execution error'+ const hint = comfyErrorHint(errEntry?.node_type, errEntry?.exception_type, String(raw))+ reject(new Error(hint ? `${raw}\n\n${hint}` : raw))}- if (!found) setError('Generation completed but no output was produced.')- resolve()- } else if (statusStr === 'error') {- completionHandled = true- cleanup()- const msgs = historyMessages(history)- const errEntry = msgs.find(([t]) => t === 'execution_error')?.[1]- const raw = errEntry?.exception_message || 'ComfyUI execution error'- const hint = comfyErrorHint(errEntry?.node_type, errEntry?.exception_type, String(raw))- reject(new Error(hint ? `${raw}\n\n${hint}` : raw))+ } catch { /* polling failure is non-fatal */ }+ if (completionHandled) return+ // Asked with a limit of its own. A request that fails before the+ // limit is a ComfyUI that is gone; no answer within it is one too+ // busy to serve HTTP (a long VAE decode, I1 30.10.2026), which is+ // said on the line instead of waited out in silence.+ const asked = Date.now()+ const alive = await checkComfyConnection(COMFY_PROBE_LIMIT_MS)+ if (completionHandled) return+ if (!alive && Date.now() - asked < COMFY_PROBE_LIMIT_MS * 0.8) { cleanup(); reject(new Error('ComfyUI stopped responding during generation')); return }+ if (!alive) line.setLabel('ComfyUI is busy and does not answer yet...')+ // A prompt still in ComfyUI's queue is alive even when the socket+ // goes quiet (long model loads emit no events) — refresh the+ // watchdog once a minute from the queue itself.+ queueCheckCounter += 1+ if (queueCheckCounter >= 6) {+ queueCheckCounter = 0+ if (await isPromptQueued(promptId).catch(() => false)) lastActivity = Date.now()}- } catch { /* polling failure is non-fatal */ }+ } finally {+ beating = false+ }}, 10000)let abortCheck: ReturnType<typeof setInterval> | null = null