Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(image-map): do not drop projection-ready events during the first load
Lincoln Stein6c861a462757
diff --git a/invokeai/frontend/webv2/src/workbench/image-map/imageMap.test.ts b/invokeai/frontend/webv2/src/workbench/image-map/imageMap.test.tsindex e8a10f228f..f804773fe6 100644--- a/invokeai/frontend/webv2/src/workbench/image-map/imageMap.test.ts+++ b/invokeai/frontend/webv2/src/workbench/image-map/imageMap.test.ts@@ -283,6 +283,78 @@ describe('image map store', () => {await refresh;}});++ it('collapses mid-flight refresh requests into one rerun', async () => {+ // The socket runtime can call refresh while the first load is still in+ // flight (projection-ready is admitted during 'loading'), and several+ // events may land inside one fetch's window. Every caller must join the+ // in-flight request, and exactly one rerun may follow — not one per+ // caller, and never a parallel fetch.+ const pointsResolvers: Array<(value: typeof BACKEND_RESPONSE) => void> = [];+ mocks.apiFetchJson.mockImplementation((url: string) => {+ if (url.startsWith('/api/v1/image_map/points')) {+ return new Promise((resolve) => {+ pointsResolvers.push(resolve);+ });+ }++ return Promise.resolve({+ labels: { '0': { label: 'cats' } },+ updated_at: BACKEND_RESPONSE.updated_at,+ visible_hash: BACKEND_RESPONSE.visible_hash,+ });+ });++ const calls = [refreshImageMapPoints(), refreshImageMapPoints(), refreshImageMapPoints()];+ // All three joined the one in-flight fetch; no parallel point set.+ expect(pointsResolvers).toHaveLength(1);++ pointsResolvers[0]?.(BACKEND_RESPONSE);+ await Promise.all(calls);++ // Exactly one rerun follows the settle.+ await vi.waitFor(() => expect(pointsResolvers).toHaveLength(2));+ pointsResolvers[1]?.(BACKEND_RESPONSE);+ await new Promise((resolve) => {+ setTimeout(resolve, 0);+ });++ // No runaway: the rerun found nothing to queue behind it.+ expect(pointsResolvers).toHaveLength(2);+ expect(imageMapStore.getSnapshot().loadState).toBe('loaded');+ });++ it('still runs the queued rerun when the in-flight fetch fails', async () => {+ // A refresh admitted during 'loading' must not be lost to the first fetch+ // failing: the rerun runs anyway and can still recover the map.+ let pointsCall = 0;+ mocks.apiFetchJson.mockImplementation((url: string) => {+ if (!url.startsWith('/api/v1/image_map/points')) {+ return Promise.resolve({+ labels: { '0': { label: 'cats' } },+ updated_at: BACKEND_RESPONSE.updated_at,+ visible_hash: BACKEND_RESPONSE.visible_hash,+ });+ }++ pointsCall += 1;+ return pointsCall === 1 ? Promise.reject(new Error('backend down')) : Promise.resolve(BACKEND_RESPONSE);+ });++ const first = refreshImageMapPoints();+ refreshImageMapPoints();+ await first;++ // The rerun ran (a second points fetch) despite the first one failing,+ // and it recovered the map. Without the queued rerun, pointsCall would+ // sit at 1 and loadState at 'error'.+ await vi.waitFor(() => expect(pointsCall).toBe(2));+ await new Promise((resolve) => {+ setTimeout(resolve, 0);+ });++ expect(imageMapStore.getSnapshot().loadState).toBe('loaded');+ });});describe('cluster palette', () => {diff --git a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapDataRuntime.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapDataRuntime.test.tsindex f69ea9abb9..2cd3231c1f 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapDataRuntime.test.ts+++ b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapDataRuntime.test.ts@@ -107,7 +107,12 @@ describe('attachImageMapDataRuntime', () => {const detach = attachImageMapDataRuntime();// `loading` used to pass the guard, so the event set `rerunRequested` and- // forced a second full point set the moment the first settled.+ // forced a second full point set the moment the first settled. The poke+ // still must not pass: it fires at indexer quiescence, so a backfill+ // running while the map first loads would double every fetch. The+ // accepted trade: embedding enqueues no recompute, so a poke landing in+ // the first-load window is the only announcement of that image — it stays+ // off the map until the next event or a manual refresh.imageMapStore.patchSnapshot({ loadState: 'loading' });getHandler('image_index_updated')({ user_id: 'u1' });@@ -115,6 +120,64 @@ describe('attachImageMapDataRuntime', () => {detach();});+ it('refetches for a projection ready during the first load', () => {+ const detach = attachImageMapDataRuntime();+ const onReady = getHandler('image_map_projection_ready');++ // A recompute can finish while the fetch it was enqueued by is still in+ // flight: that fetch was served the projection the recompute replaced, and+ // dropping the announcement strands the stale points (with labels the+ // mismatch check keeps discarding) until some later event fires. The+ // store's dedup turns this call into a single queued rerun, not a parallel+ // fetch.+ imageMapStore.patchSnapshot({ loadState: 'loading' });+ onReady({ user_id: 'u1' });++ expect(mocks.refreshImageMapPoints).toHaveBeenCalledTimes(1);+ detach();+ });++ it('refetches for a projection ready after a failed load', () => {+ const detach = attachImageMapDataRuntime();+ const onReady = getHandler('image_map_projection_ready');++ // loadState 'error' with no fetch in flight (the first load failed): the+ // event starts a fresh fetch rather than being dropped — the failing+ // first load may have missed the projection it announced.+ imageMapStore.patchSnapshot({ loadState: 'error' });+ onReady({ user_id: 'u1' });++ expect(mocks.refreshImageMapPoints).toHaveBeenCalledTimes(1);+ detach();+ });++ it('still ignores a foreign projection ready during the first load', () => {+ const detach = attachImageMapDataRuntime();+ const onReady = getHandler('image_map_projection_ready');++ mocks.getAuthSession.mockReturnValue({ user: { user_id: 'me' } });+ imageMapStore.patchSnapshot({ loadState: 'loading' });++ onReady({ user_id: 'someone-else' });+ expect(mocks.refreshImageMapPoints).not.toHaveBeenCalled();++ detach();+ });++ it('leaves a failed canvas alone for a projection ready during the first load', () => {+ const detach = attachImageMapDataRuntime();+ const onReady = getHandler('image_map_projection_ready');++ // A retry in flight after a WebGL failure: the canvas stays failed until+ // that retry's own success clears the error, and a mid-flight event must+ // not start another refresh that would remount into the same failure.+ imageMapStore.patchSnapshot({ loadState: 'loading', renderError: 'The map failed to render.' });+ onReady({ user_id: 'u1' });++ expect(mocks.refreshImageMapPoints).not.toHaveBeenCalled();+ detach();+ });+it('ignores a projection recomputed for a different user', () => {const detach = attachImageMapDataRuntime();const onReady = getHandler('image_map_projection_ready');diff --git a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapDataRuntime.tsx b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapDataRuntime.tsxindex c8e1d842c2..107b1778b0 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapDataRuntime.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapDataRuntime.tsx@@ -46,7 +46,10 @@ export const attachImageMapDataRuntime = (): (() => void) => {// Only once the widget has actually loaded the map. `loading` used to pass// this guard, so an event landing during the first fetch queued a rerun- // and forced a second full point set the moment it settled.+ // and forced a second full point set the moment it settled. It still must+ // not pass here — but `image_map_projection_ready` below is the exception:+ // that one event can announce a projection newer than the in-flight+ // response, where dropping it strands a stale map.if (loadState !== 'loaded' && loadState !== 'error') {return;}@@ -62,12 +65,30 @@ export const attachImageMapDataRuntime = (): (() => void) => {void refreshImageMapPoints();};+ // A finished projection recompute can overtake the fetch that triggered it:+ // the request was served the projection the recompute then replaced, so its+ // points are stale and the labels request that follows it answers over the+ // new record — discarded on the hash mismatch, with no labels to show for+ // it. Dropping the event strands that map until some later one happens to+ // fire; admitting `loading` costs one extra point set at most, because the+ // store's dedup queues a single rerun when a refresh lands mid-flight (the+ // `rerunRequested` comment describes exactly this arrival).+ const refreshProjectionReady = () => {+ const { loadState, renderError } = imageMapStore.getSnapshot();++ if (renderError || (loadState !== 'loaded' && loadState !== 'error' && loadState !== 'loading')) {+ return;+ }++ void refreshImageMapPoints();+ };+const detachers = [// The backend routes this to the requesting user's room plus admins, so// receipt alone does not mean "my map changed".socketHub.on('image_map_projection_ready', (payload: never) => {if (!isForAnotherUser((payload as unknown as ImageMapProjectionReadyEvent | undefined)?.user_id)) {- refreshIfLoaded();+ refreshProjectionReady();}}),// Counts-free per-user poke: the owner's images just reached the index.