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(models): the counter asks again when ComfyUI arrives, instead of keeping the zero
purplee902e37ebd56
diff --git a/src/hooks/useModels.ts b/src/hooks/useModels.tsindex bfe19d95..9c048ed2 100644--- a/src/hooks/useModels.ts+++ b/src/hooks/useModels.ts@@ -1,6 +1,9 @@import { useCallback, useEffect, useMemo } from 'react'import { listModels, pullModel as pullModelApi, pullModelTauri, deleteModel as deleteModelApi } from '../api/ollama'-import { isTauri, isMacOS } from '../api/backend'+import { isTauri, isMacOS, backendCall } from '../api/backend'+import {+ inventoryOwesRetry, refetchWhenComfyReady, type ComfyReadyStatus,+} from '../lib/comfy-ready-retry'import {getInstalledImageModels as getComfyImageModels,getInstalledVideoModels as getComfyVideoModels,@@ -45,6 +48,50 @@ async function resumeBuiltinEngines(bundled: BundledModel[]) {await resumeEmbedServer(bundled)}+/** One arm at a time. fetchModels runs from several mounted components, and a+ * cold start would otherwise start a wait per caller. */+let comfyRetryRunning = false++/**+ * Meldung 2 of the R5 re-measure (2026-08-30): opening the Model Manager while+ * ComfyUI was still coming up left the counter on `Installed 0` for good,+ * beside cards that carried green Installed ticks. The first pass asked an+ * engine that could not answer, wrote the empty answer down as the count, and+ * nothing ever asked again. Only a manual Refresh repaired it.+ *+ * The counter stays on "counting" for as long as this runs, because that is+ * the truth: nothing has been counted. beginInventoryRefresh is what says so,+ * and it is held until the wait settles one way or the other.+ */+function armComfyInventoryRetry(refetch: () => Promise<void>): void {+ if (comfyRetryRunning) return+ if (isMacOS()) return+ comfyRetryRunning = true+ useModelStore.getState().beginInventoryRefresh()+ void refetchWhenComfyReady({+ status: async () => {+ try {+ return await backendCall<ComfyReadyStatus>('comfyui_status')+ } catch {+ return null+ }+ },+ refetch: async () => { await refetch() },+ })+ .then((outcome) => { log.info('[useModels] ComfyUI inventory second pass', { outcome }) })+ .catch((err) => { log.warn('[useModels] ComfyUI inventory second pass failed', { err }) })+ .finally(() => {+ comfyRetryRunning = false+ useModelStore.getState().endInventoryRefresh()+ })+}++/** Test seam. The arm is module state on purpose (one per app, not one per+ * mounted component), so a test needs a way back to a clean slate. */+export function __resetComfyInventoryRetryForTests(): void {+ comfyRetryRunning = false+}+// The bundled embeddings server serves RAG/memory for ANY local backend that// downloaded the embed GGUF in onboarding (LM Studio/openai-compat too), so// its resume must not depend on the chat engine being the managed builtin.@@ -191,10 +238,18 @@ export function useModels() {}let comfyModels: AIModel[] = []+ // Did the ComfyUI lanes produce an answer at all this pass. Not whether+ // the answer had anything in it: an engine that is up and holds no+ // models is a counted zero, an engine that could not be reached has+ // counted nothing. Drives the second pass at the bottom of this+ // function (Meldung 2, R5 re-measure 2026-08-30).+ // True on the Mac and in the web build, where there is nothing to ask.+ let comfyAnswered = true// Hard rule: Mac local media is MLX-only — ComfyUI never auto-starts// there (process.rs::auto_start_comfyui), so skip the probe outright// instead of a doomed connection check on every model-list refresh.const comfyOk = !isMacOS() && (await checkComfyConnection())+ if (!isMacOS() && !comfyOk) comfyAnswered = falseif (comfyOk) {// Settled, not all: a folder ComfyUI cannot read costs that one lane,// never the whole list. The old code lost both to a single throw.@@ -220,6 +275,11 @@ export function useModels() {}const imageModels = imageResult.status === 'fulfilled' ? imageResult.value : []const videoModels = videoResult.status === 'fulfilled' ? videoResult.value : []+ // Both lanes down is not an inventory, it is an engine that answered+ // the handshake and then nothing else.+ if (imageResult.status === 'rejected' && videoResult.status === 'rejected') {+ comfyAnswered = false+ }// No second partial filter here. The inventory readers above are the// one reader for this list and they already decided what is on the@@ -244,6 +304,8 @@ export function useModels() {]}setModels([...allModels, ...comfyModels])+ // eslint-disable-next-line @typescript-eslint/no-use-before-define+ if (inventoryOwesRetry(comfyAnswered)) armComfyInventoryRetry(fetchModels)} catch (err) {log.warn('[useModels] Model list refresh failed', { err })} finally {diff --git a/src/lib/__tests__/comfy-ready-retry.test.ts b/src/lib/__tests__/comfy-ready-retry.test.tsnew file mode 100644index 00000000..0cda2884--- /dev/null+++ b/src/lib/__tests__/comfy-ready-retry.test.ts@@ -0,0 +1,167 @@+/**+ * Meldung 2 of the R5 re-measure on the 2.6.7 Windows build+ * (2026-08-30, ergebnis-r5-nachmessung.md).+ *+ * Cold start, Model Manager opened while ComfyUI was still coming up:+ *+ * 1 ms | Chat:Chat+ * 233 ms | Chat··· Image··· Video··· || Installed···+ * 4469 ms | Chat:3 Image··· Video··· || Installed···+ * 11311 ms | Chat:3 Image Video || Installed0+ *+ * And there it stayed. Measured again minutes later, with port 8188 long+ * open, it still read `Installed 0` while the cards below it carried green+ * Installed ticks. Two independent reproductions. One click on Refresh+ * repaired it in two seconds.+ *+ * inventory-counter.ts already promised the fix: "a second pass is what brings+ * the ComfyUI lanes in: the engine is often not up yet on the first one".+ * There was no second pass. Nothing ever asked again.+ *+ * Run: npx vitest run src/lib/__tests__/comfy-ready-retry.test.ts+ */+import { describe, it, expect, vi } from 'vitest'+import { readFileSync } from 'node:fs'+import { fileURLToPath } from 'node:url'+import { dirname, resolve } from 'node:path'+import { inventoryOwesRetry, refetchWhenComfyReady } from '../comfy-ready-retry'++const here = dirname(fileURLToPath(import.meta.url))+const useModels = readFileSync(resolve(here, '../../hooks/useModels.ts'), 'utf8')++/** No real seconds are spent in here. */+const nowait = async () => {}++describe('a pass that could not ask the engine owes a second one', () => {+ it('THE FIX: an unreachable engine has counted nothing, so it owes a retry', () => {+ expect(inventoryOwesRetry(false)).toBe(true)+ })++ it('NEGATIVE CONTROL: an engine that answered and holds nothing owes nothing', () => {+ // A counted zero is the truth and must settle. Retrying it forever would+ // turn the loading mark into the new lie.+ expect(inventoryOwesRetry(true)).toBe(false)+ })+})++describe('the second pass runs the moment ComfyUI is there', () => {+ it('THE FIX: the exact cold-start frame, still starting and then up', async () => {+ const refetch = vi.fn(async () => {})+ // Three rounds of "the process is alive, the port is not open yet", which+ // is what the box showed for the first eleven seconds, then up.+ const answers = [+ { running: false, starting: true },+ { running: false, starting: true },+ { running: false, starting: true },+ { running: true, starting: false },+ ]+ const status = vi.fn(async () => answers.shift() ?? null)++ const outcome = await refetchWhenComfyReady({ status, refetch, wait: nowait })++ expect(outcome).toBe('refetched')+ expect(refetch).toHaveBeenCalledTimes(1)+ })++ it('THE FIX: an engine already up is asked again straight away', async () => {+ const refetch = vi.fn(async () => {})+ const outcome = await refetchWhenComfyReady({+ status: async () => ({ running: true }),+ refetch,+ wait: nowait,+ })+ expect(outcome).toBe('refetched')+ expect(refetch).toHaveBeenCalledTimes(1)+ })++ it('THE FIX: the refetch runs exactly once, never in a loop', async () => {+ const refetch = vi.fn(async () => {})+ await refetchWhenComfyReady({+ status: async () => ({ running: true }),+ refetch,+ wait: nowait,+ rounds: 50,+ })+ expect(refetch).toHaveBeenCalledTimes(1)+ })++ it('NEGATIVE CONTROL: nothing is starting, so the zero is left alone', async () => {+ const refetch = vi.fn(async () => {})+ const status = vi.fn(async () => ({ running: false, starting: false }))++ const outcome = await refetchWhenComfyReady({ status, refetch, wait: nowait })++ expect(outcome).toBe('not-starting')+ expect(refetch).not.toHaveBeenCalled()+ // One look, not sixty. ComfyUI is not installed or was never started, and+ // the Installed tab has its own honest "Start ComfyUI" state for that.+ expect(status).toHaveBeenCalledTimes(1)+ })++ it('NEGATIVE CONTROL: a backend that cannot answer ends the wait', async () => {+ const refetch = vi.fn(async () => {})+ const outcome = await refetchWhenComfyReady({+ status: async () => { throw new Error('Unknown backend command') },+ refetch,+ wait: nowait,+ })+ expect(outcome).toBe('not-starting')+ expect(refetch).not.toHaveBeenCalled()+ })++ it('NEGATIVE CONTROL: a boot that never finishes gives up on a budget', async () => {+ const refetch = vi.fn(async () => {})+ const status = vi.fn(async () => ({ running: false, starting: true }))++ const outcome = await refetchWhenComfyReady({+ status, refetch, wait: nowait, rounds: 5,+ })++ expect(outcome).toBe('timeout')+ expect(status).toHaveBeenCalledTimes(5)+ expect(refetch).not.toHaveBeenCalled()+ })+})++describe('the model list arms that second pass, and holds the counter while it runs', () => {+ it('THE FIX: an unreachable ComfyUI marks the pass as unanswered', () => {+ expect(useModels).toMatch(/if \(!isMacOS\(\) && !comfyOk\) comfyAnswered = false/)+ })++ it('THE FIX: both lanes rejected marks the pass as unanswered too', () => {+ expect(useModels).toMatch(/imageResult\.status === 'rejected' && videoResult\.status === 'rejected'/)+ expect(useModels).toMatch(/comfyAnswered = false/)+ })++ it('THE FIX: the arm hangs on comfyui_status, the readiness signal that exists', () => {+ expect(useModels).toMatch(/backendCall<ComfyReadyStatus>\('comfyui_status'\)/)+ })++ it('THE FIX: the counter says counting for as long as the second pass is owed', () => {+ // beginInventoryRefresh is what counterView reads as "not counted yet".+ // Held from arming until the wait settles, or the page states a zero it+ // has not counted for the whole of a ComfyUI boot.+ const arm = useModels.slice(+ useModels.indexOf('function armComfyInventoryRetry'),+ useModels.indexOf('export function __resetComfyInventoryRetryForTests'),+ )+ expect(arm).toMatch(/beginInventoryRefresh\(\)/)+ expect(arm).toMatch(/endInventoryRefresh\(\)/)+ })++ it('NEGATIVE CONTROL: one arm at a time, not one per mounted component', () => {+ const arm = useModels.slice(+ useModels.indexOf('function armComfyInventoryRetry'),+ useModels.indexOf('export function __resetComfyInventoryRetryForTests'),+ )+ expect(arm).toMatch(/if \(comfyRetryRunning\) return/)+ })++ it('NEGATIVE CONTROL: the Mac never arms it, there is no ComfyUI to wait for', () => {+ const arm = useModels.slice(+ useModels.indexOf('function armComfyInventoryRetry'),+ useModels.indexOf('export function __resetComfyInventoryRetryForTests'),+ )+ expect(arm).toMatch(/if \(isMacOS\(\)\) return/)+ })+})diff --git a/src/lib/comfy-ready-retry.ts b/src/lib/comfy-ready-retry.tsnew file mode 100644index 00000000..55491174--- /dev/null+++ b/src/lib/comfy-ready-retry.ts@@ -0,0 +1,92 @@+/**+ * The second pass the inventory always assumed it would get.+ *+ * Meldung 2 of the R5 re-measure on the 2.6.7 Windows build (2026-08-30). On a+ * cold start, with the Model Manager opened while ComfyUI was still coming up,+ * the counter ended on `Installed 0` and stayed there. Two independent+ * reproductions, and minutes later, with port 8188 long open, it still read 0+ * while the cards beside it carried green Installed ticks. Only a click on+ * Refresh repaired it.+ *+ * inventory-counter.ts already says "a second pass is what brings the ComfyUI+ * lanes in: the engine is often not up yet on the first one". There was no+ * second pass. The first pass asked an engine that could not answer, wrote its+ * empty answer down as the count, and the count settled.+ *+ * So the app asks again, once, when the engine is there. Not a standing poll:+ * this is armed only after a pass whose ComfyUI lanes could not answer, it+ * runs at most one at a time, it hangs on the readiness signal ComfyUI already+ * has in this app (`comfyui_status`, which reports `running` and `starting`+ * separately for exactly this reason), and it stops the moment the engine is+ * up, the engine is no longer starting, or the budget runs out. It is the same+ * shape and the same budget useCreate's startAndAwait uses to wait out a+ * ComfyUI boot, because it is the same wait.+ */++/** The fields of `comfyui_status` this wait cares about. */+export interface ComfyReadyStatus {+ /** The port answers. */+ running?: boolean+ /** The process is alive but has not bound the port yet. */+ starting?: boolean+}++/** How the wait ended. Every one of them is a settled answer for the counter. */+export type ReadyRetryOutcome =+ /** The engine came up and the refetch ran. */+ | 'refetched'+ /** The engine is not coming: nothing is starting, so the count of 0 is true. */+ | 'not-starting'+ /** The engine was still starting when the budget ran out. */+ | 'timeout'++/** Same 60 rounds of 2 seconds useCreate spends waiting out a ComfyUI boot. */+export const READY_ROUNDS = 60+export const READY_DELAY_MS = 2000++const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms))++/**+ * Does the pass that just ran owe a second one.+ *+ * `answered` is the honest question: did the ComfyUI lanes produce an answer,+ * not whether that answer had anything in it. An engine that is up and holds+ * no models is a counted zero and owes nothing. An engine that could not be+ * reached, or whose lanes all threw, has counted nothing at all.+ */+export function inventoryOwesRetry(answered: boolean): boolean {+ return !answered+}++/**+ * Ask again once the engine is there.+ *+ * `status` returning null (the command is unavailable, the web build, a+ * backend that does not know it) ends the wait rather than spinning on an+ * unanswerable question.+ */+export async function refetchWhenComfyReady(deps: {+ status: () => Promise<ComfyReadyStatus | null>+ refetch: () => Promise<void>+ wait?: (ms: number) => Promise<void>+ rounds?: number+ delayMs?: number+}): Promise<ReadyRetryOutcome> {+ const { status, refetch } = deps+ const wait = deps.wait ?? sleep+ const rounds = deps.rounds ?? READY_ROUNDS+ const delayMs = deps.delayMs ?? READY_DELAY_MS++ for (let i = 0; i < rounds; i++) {+ const s = await status().catch(() => null)+ if (s?.running) {+ await refetch()+ return 'refetched'+ }+ // Not up and not on its way. The zero the counter is holding is the truth,+ // and the Installed tab's own "Start ComfyUI" state says the rest.+ if (!s || s.starting !== true) return 'not-starting'+ await wait(delayMs)+ }+ return 'timeout'+}