Skip to content

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.

fix(create): a batch edit brings the chat models back once, after the last image

purple04c1b3dd5858
diff --git a/src/api/__tests__/create-render-handoff.test.ts b/src/api/__tests__/create-render-handoff.test.ts
index 7b4ba47c..6d44b6e3 100644
--- a/src/api/__tests__/create-render-handoff.test.ts
+++ b/src/api/__tests__/create-render-handoff.test.ts
@@ -57,6 +57,7 @@ vi.mock('../comfyui-ws', () => ({
import {
evictChatBackendsForRender,
restoreChatBackendsAfterRender,
+ holdRenderRestore,
__resetRenderJuggleForTests,
type RenderEviction,
} from '../vram-handoff'
@@ -218,6 +219,72 @@ describe('restoreChatBackendsAfterRender', () => {
})
})
+// Box run, 03.10.2026: a batch edit of three pictures loaded the image model
+// anew before each one. Every picture of a batch ended with its own restore,
+// and a picture that took longer than the grace window to start paid for it:
+// /free dropped the image model and the chat model was loaded just to be
+// evicted again. A batch holds the restore until its last picture is done.
+describe('holdRenderRestore: a run of several renders restores once, at its end', () => {
+ const haulOf = (ollamaModel: string): RenderEviction => ({ ollamaModel, lms: null, bundled: null })
+
+ it('no /free and no reload between the pictures, however long the next one takes to start', async () => {
+ vi.useFakeTimers()
+ mockBackends({ engineRunning: false })
+ const release = holdRenderRestore()
+ for (let i = 0; i < 3; i++) {
+ const haul = await evictChatBackendsForRender()
+ const restored = restoreChatBackendsAfterRender(i === 0 ? haulOf('qwen:14b') : haul)
+ // Far past the grace window before the next picture starts.
+ await vi.advanceTimersByTimeAsync(30_000)
+ await restored
+ }
+ expect(freeMemory).not.toHaveBeenCalled()
+ expect(loadModel).not.toHaveBeenCalled()
+
+ release()
+ await vi.advanceTimersByTimeAsync(5_000)
+ expect(freeMemory).toHaveBeenCalledTimes(1)
+ expect(loadModel).toHaveBeenCalledTimes(1)
+ expect(loadModel).toHaveBeenCalledWith('qwen:14b')
+ })
+
+ it('a hold released with nothing evicted touches nothing', async () => {
+ vi.useFakeTimers()
+ const release = holdRenderRestore()
+ release()
+ await vi.advanceTimersByTimeAsync(5_000)
+ expect(freeMemory).not.toHaveBeenCalled()
+ expect(backendCall).not.toHaveBeenCalled()
+ })
+
+ it('releasing twice restores once', async () => {
+ vi.useFakeTimers()
+ const release = holdRenderRestore()
+ const restored = restoreChatBackendsAfterRender(haulOf('qwen:14b'))
+ await vi.advanceTimersByTimeAsync(5_000)
+ await restored
+ release()
+ release()
+ await vi.advanceTimersByTimeAsync(5_000)
+ expect(loadModel).toHaveBeenCalledTimes(1)
+ })
+
+ it('a render that starts right after the release still inherits the haul', async () => {
+ vi.useFakeTimers()
+ mockBackends({ engineRunning: false })
+ localFetch.mockResolvedValue({ ok: true, json: async () => ({ models: [] }) })
+ const release = holdRenderRestore()
+ const restored = restoreChatBackendsAfterRender(haulOf('qwen:14b'))
+ await vi.advanceTimersByTimeAsync(5_000)
+ await restored
+ release()
+ const next = evictChatBackendsForRender()
+ await vi.advanceTimersByTimeAsync(5_000)
+ expect((await next).ollamaModel).toBe('qwen:14b')
+ expect(loadModel).not.toHaveBeenCalled()
+ })
+})
+
describe('wiring: useCreate uses the hand-off helpers', () => {
const read = (rel: string) => fs.readFileSync(path.resolve(__dirname, rel), 'utf8')
diff --git a/src/api/vram-handoff.ts b/src/api/vram-handoff.ts
index c4f148a3..9b3edc22 100644
--- a/src/api/vram-handoff.ts
+++ b/src/api/vram-handoff.ts
@@ -2206,6 +2206,7 @@ function evictionEmpty(e: RenderEviction): boolean {
let _renderJuggle: Promise<unknown> = Promise.resolve()
let _renderEpoch = 0
let _pendingRestore: RenderEviction | null = null
+let _restoreHolds = 0
export const RENDER_RESTORE_GRACE_MS = 2_000
/** Test-only: reset the render-juggle chain state between unit tests. */
@@ -2213,6 +2214,7 @@ export function __resetRenderJuggleForTests(): void {
_renderJuggle = Promise.resolve()
_renderEpoch = 0
_pendingRestore = null
+ _restoreHolds = 0
}
function mergeEvictions(base: RenderEviction | null, add: RenderEviction): RenderEviction {
@@ -2318,9 +2320,29 @@ export function restoreChatBackendsAfterRender(
return run.catch(() => {})
}
+/**
+ * A run of several renders in a row (batch edit) holds the restore: each
+ * render still parks its haul, but nothing is freed or reloaded until the
+ * returned release is called. Then the chat backends come back once, after
+ * the usual grace window. Without the hold a picture that took longer than
+ * that window to start paid `/free` plus a chat model reload, and ComfyUI
+ * read the image model back in before every picture.
+ */
+export function holdRenderRestore(): () => void {
+ _restoreHolds++
+ let released = false
+ return () => {
+ if (released) return
+ released = true
+ _restoreHolds--
+ void restoreChatBackendsAfterRender({ ...EMPTY_EVICTION })
+ }
+}
+
async function restoreBody(evicted: RenderEviction, graceMs: number, myEpoch: number): Promise<void> {
- if (evictionEmpty(evicted)) return
- _pendingRestore = mergeEvictions(_pendingRestore, evicted)
+ if (!evictionEmpty(evicted)) _pendingRestore = mergeEvictions(_pendingRestore, evicted)
+ if (!_pendingRestore) return
+ if (_restoreHolds > 0) return // the run restores once, at its end
if (_renderEpoch !== myEpoch) return // a newer render inherits the haul
if (graceMs > 0) await sleep(graceMs)
if (_renderEpoch !== myEpoch) return // a newer render inherits the haul
diff --git a/src/components/create/experimental/__tests__/batch-surface.test.tsx b/src/components/create/experimental/__tests__/batch-surface.test.tsx
index f3d194fc..f917b35a 100644
--- a/src/components/create/experimental/__tests__/batch-surface.test.tsx
+++ b/src/components/create/experimental/__tests__/batch-surface.test.tsx
@@ -33,6 +33,13 @@ vi.mock('../../../../api/backend', async (importOriginal) => ({
...(await importOriginal<typeof import('../../../../api/backend')>()),
openExternal: vi.fn(),
}))
+const juggle = vi.hoisted(() => ({ log: [] as string[] }))
+vi.mock('../../../../api/vram-handoff', () => ({
+ holdRenderRestore: () => {
+ juggle.log.push('hold')
+ return () => { juggle.log.push('release') }
+ },
+}))
vi.mock('../../../../hooks/useCloudCreate', () => ({
resolveCharacterModel: () => undefined,
takeCloudRunStop: () => null,
@@ -254,6 +261,29 @@ describe('a local run over the list', () => {
expect(s.error).toBeNull()
})
+ // Box run, 03.10.2026: the image model was loaded anew before each picture
+ // of a batch. The chat models come back once, after the last picture.
+ it('holds the restore of the chat models from before the first image until after the last', async () => {
+ start('local', 'edit')
+ await addBatchFiles([png('a.png'), png('b.png'), png('c.png')])
+ juggle.log.length = 0
+ const run = vi.fn(async () => {
+ juggle.log.push('run')
+ const s = useCreateStore.getState()
+ s.addToGallery(item(`h-${atob(s.source!.url.split(',')[1])}`))
+ })
+ await runBatchEdit(run)
+ expect(juggle.log).toEqual(['hold', 'run', 'run', 'run', 'release'])
+ })
+
+ it('a cloud batch renders nothing on this machine and holds nothing', async () => {
+ start('cloud', 'removebg')
+ await addBatchFiles([png('a.png'), png('b.png')])
+ juggle.log.length = 0
+ await runBatchEdit(vi.fn(async () => {}), { sleep: async () => {} })
+ expect(juggle.log).toEqual([])
+ })
+
it('a failing image does not stop the others and stays in the list with an image that would not load', async () => {
start('local', 'removebg')
await addBatchFiles([png('a.png'), png('b.png'), png('c.png'), png('broken.png')])
diff --git a/src/components/create/experimental/batchRun.ts b/src/components/create/experimental/batchRun.ts
index ac12106e..e7f89ad8 100644
--- a/src/components/create/experimental/batchRun.ts
+++ b/src/components/create/experimental/batchRun.ts
@@ -10,6 +10,7 @@ import { defaultCloudModel, editNeedsMask, modelForOp, useCloudCatalogStore } fr
import { isStudioModel } from '../../../lib/render/create-studio'
import { runImageCount } from '../../../lib/render/image-count'
import { dataUrlToBlob, takeCloudRunStop } from '../../../hooks/useCloudCreate'
+import { holdRenderRestore } from '../../../api/vram-handoff'
import {
MAX_BATCH_IMAGES, batchOffered, batchPickNote, batchSummary, cloudRunsPerMinute, createPacer,
pickBatchFiles, runBatch, type RunOutcome,
@@ -189,6 +190,9 @@ export async function runBatchEdit(
return { made: made.length, error: after.error, stop: stop?.reason, retryAfterMs: stop?.retryAfterMs }
}
+ // On this machine the chat models leave the card before the first image
+ // and come back after the last one, not around every image of the list.
+ const releaseRestore = backend === 'local' ? holdRenderRestore() : null
let report: Awaited<ReturnType<typeof runBatch>> | null = null
try {
report = await runBatch(entries, {
@@ -208,6 +212,7 @@ export async function runBatchEdit(
onProgress: (statuses, index) => useCreateStore.getState().setBatchRun({ index, statuses, names, thumbs }),
})
} finally {
+ releaseRestore?.()
const st = useCreateStore.getState()
st.setBatchRun(null)
// What has no result stays in the list, so Create runs exactly the rest.