Skip to content

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.

fix(gallery): ask what a realm can resolve, not what the value is

Lincoln Steinfd6d9e62cd09
diff --git a/invokeai/frontend/webv2/src/features/gallery/contracts.ts b/invokeai/frontend/webv2/src/features/gallery/contracts.ts
index 52c5805e65..22a497ba6e 100644
--- a/invokeai/frontend/webv2/src/features/gallery/contracts.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/contracts.ts
@@ -39,6 +39,7 @@ export {
getImageCluster,
parseGallerySemanticReference,
stripSessionScopedGallerySearch,
+ stripUnresolvableGallerySearch,
registerImageCluster,
type GallerySemanticReference,
} from './core/semanticImageQuery';
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/semanticImageQuery.ts b/invokeai/frontend/webv2/src/features/gallery/core/semanticImageQuery.ts
index e41370d991..5a85576854 100644
--- a/invokeai/frontend/webv2/src/features/gallery/core/semanticImageQuery.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/core/semanticImageQuery.ts
@@ -190,19 +190,24 @@ const isSessionScopedGallerySemanticReference = (value: unknown): boolean =>
((value as Record<string, unknown>).kind === 'file' || (value as Record<string, unknown>).kind === 'cluster');
/**
- * The same values with every position that was set against a session-scoped
- * ranking dropped, or null when there is nothing to drop.
+ * The same values with every position that was set against a ranking the
+ * caller's test rejects, or null when there is nothing to drop.
*
- * While such a search is on screen the footer paginates the RANKING, so the
- * gallery's page and the page stamped on the selection are both indexes into
- * a list no other realm can rebuild. Left behind, they are read as board pages
- * and answer with an unrelated slice, so the reference and the positions set
- * against it are dropped together. Only keys that are actually carrying a
+ * While a similarity search is on screen the footer paginates the RANKING, so
+ * the gallery's page and the page stamped on the selection are both indexes
+ * into it. Left behind by a search that is gone, they are read as board pages
+ * and answer with an unrelated slice. Only keys that are actually carrying a
* position are rewritten: normalizing must not mint state that was never
* stored.
+ *
+ * The test differs by caller, and the difference matters. See
+ * `stripSessionScopedGallerySearch` and `stripUnresolvableGallerySearch`.
*/
-export const stripSessionScopedGallerySearch = (values: Record<string, unknown>): Record<string, unknown> | null => {
- if (!isSessionScopedGallerySemanticReference(values.semanticImageQuery)) {
+const stripGallerySearchPositions = (
+ values: Record<string, unknown>,
+ shouldStrip: (value: unknown) => boolean
+): Record<string, unknown> | null => {
+ if (!shouldStrip(values.semanticImageQuery)) {
return null;
}
@@ -228,6 +233,38 @@ export const stripSessionScopedGallerySearch = (values: Record<string, unknown>)
return nextValues;
};
+/**
+ * For the SAVE path: drop what the realm reading this back will not have.
+ *
+ * A dropped file or an image-map cluster names an entry in an in-memory
+ * registry, and its id carries a per-realm token, so it resolves here and
+ * nowhere else — the test has to be what the value IS, not whether it works
+ * right now, because right now it does.
+ */
+export const stripSessionScopedGallerySearch = (values: Record<string, unknown>): Record<string, unknown> | null =>
+ stripGallerySearchPositions(
+ values,
+ (value) =>
+ isSessionScopedGallerySemanticReference(value) ||
+ (value !== null && value !== undefined && parseGallerySemanticReference(value) === null)
+ );
+
+/**
+ * For the ADOPTION path: drop only what this realm cannot resolve.
+ *
+ * `normalizeWorkbenchProject` runs on projects that never left — closing and
+ * reopening one, a conflict fork rescuing the live copy, an export re-imported
+ * into the same session. There the registry entry is still live and the
+ * ranking is still on screen, so asking what the value IS would delete the
+ * search the user is looking at. Asking whether it resolves HERE answers both
+ * cases with one test.
+ */
+export const stripUnresolvableGallerySearch = (values: Record<string, unknown>): Record<string, unknown> | null =>
+ stripGallerySearchPositions(
+ values,
+ (value) => value !== null && value !== undefined && parseGallerySemanticReference(value) === null
+ );
+
export const parseGallerySemanticReference = (value: unknown): GallerySemanticReference | null => {
// Legacy shape: a bare image name.
if (typeof value === 'string' && value) {
diff --git a/invokeai/frontend/webv2/src/workbench/persistence.test.ts b/invokeai/frontend/webv2/src/workbench/persistence.test.ts
index bffe58fdb5..e9fc4822a2 100644
--- a/invokeai/frontend/webv2/src/workbench/persistence.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/persistence.test.ts
@@ -125,6 +125,45 @@ describe('workbench persistence migration', () => {
expect((persistedValues?.selectedImageQuery as { page: number } | undefined)?.page).toBe(0);
});
+ it('drops a stored search that no longer parses, and the page it left', async () => {
+ // A value that cannot be read back is a search that is gone, whatever
+ // kind it claims: the page indexing its results is as stranded as one
+ // left by a reference this realm cannot resolve.
+ const state = createInitialWorkbenchState();
+ const project = state.projects[0];
+ const galleryEntry = Object.entries(project?.widgetInstances ?? {}).find(
+ ([, instance]) => instance.typeId === 'gallery'
+ );
+ const [galleryInstanceId, galleryInstance] = galleryEntry!;
+ const snapshot = await localStorageWorkbenchPersistence.saveWorkbench({
+ ...state,
+ projects: [
+ {
+ ...project!,
+ widgetInstances: {
+ ...project!.widgetInstances,
+ [galleryInstanceId]: {
+ ...galleryInstance,
+ state: {
+ ...galleryInstance.state,
+ values: {
+ ...galleryInstance.state.values,
+ galleryPage: 3,
+ paginationMode: 'paginated',
+ semanticImageQuery: { kind: 'text', query: '' },
+ },
+ },
+ },
+ },
+ },
+ ],
+ });
+ const persistedValues = snapshot.state.projects[0]?.widgetInstances[galleryInstanceId]?.state.values;
+
+ expect(persistedValues?.semanticImageQuery).toBeNull();
+ expect(persistedValues?.galleryPage).toBe(0);
+ });
+
it('keeps a paginated page whose search survives the reload', async () => {
// A text search is rebuilt from the persisted value, so the page the user
// was reading is still a page of the same list.
diff --git a/invokeai/frontend/webv2/src/workbench/persistence.ts b/invokeai/frontend/webv2/src/workbench/persistence.ts
index f6a8476622..7454b73a6f 100644
--- a/invokeai/frontend/webv2/src/workbench/persistence.ts
+++ b/invokeai/frontend/webv2/src/workbench/persistence.ts
@@ -35,8 +35,12 @@ const isBrowser = (): boolean => typeof window !== 'undefined' && typeof window.
* paginates the RANKING, which makes both the gallery's page and the page
* stamped on the selection rank pages that the board listing would then read
* as its own. `stripSessionScopedGallerySearch` drops the reference and those
- * positions together; the same rule runs again when a project is adopted from
- * the server, which is a route into a fresh realm that never passes here.
+ * positions together.
+ *
+ * Adoption applies a narrower version of this (see `normalizeWorkbenchProject`):
+ * it can only drop a reference that fails to resolve, because it also runs on
+ * projects that never left this realm. The window anchor below is dropped here
+ * only — telling a foreign document from a live one is what that would need.
*/
const stripSessionScopedGalleryState = (project: Project): Project => {
let didChange = false;
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
index 5c9ca879d8..eb6b1c86e5 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
@@ -11,7 +11,11 @@ import type {
import type { GraphContract } from '@workbench/graphContracts';
import type { Project, WorkbenchState } from '@workbench/projectContracts';
-import { GALLERY_RECENT_IMAGE_LIMIT, legacyGeneratedImageToGalleryItem } from '@features/gallery/contracts';
+import {
+ GALLERY_RECENT_IMAGE_LIMIT,
+ legacyGeneratedImageToGalleryItem,
+ registerImageCluster,
+} from '@features/gallery/contracts';
import { MAX_PROMPT_HISTORY } from '@features/generation/settings';
import { createDefaultUpscaleWidgetValues } from '@features/upscale';
import { afterEach, describe, expect, it, vi } from 'vitest';
@@ -845,6 +849,23 @@ describe('adopting a project from another realm', () => {
expect((values.selectedImageQuery as { page: number }).page).toBe(0);
});
+ it('keeps a session-scoped search this realm can still resolve', () => {
+ // Adoption is not only a foreign document arriving: closing and reopening
+ // a project runs it, and so does the conflict fork that rescues the LIVE
+ // copy. The registry entry is still here, the ranking is still on screen,
+ // and deleting it would take the user's search with it.
+ const clusterId = registerImageCluster(['a.png', 'b.png'], 'beaches');
+ const values = galleryValuesOf(
+ galleryProject({
+ galleryPage: 3,
+ semanticImageQuery: { clusterId, kind: 'cluster', label: 'beaches' },
+ })
+ );
+
+ expect(values.semanticImageQuery).toEqual({ clusterId, kind: 'cluster', label: 'beaches' });
+ expect(values.galleryPage).toBe(3);
+ });
+
it('keeps a search the new realm can rebuild, and the page it was read on', () => {
const values = galleryValuesOf(
galleryProject({ galleryPage: 3, semanticImageQuery: { kind: 'text', query: 'sunset' } })
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.ts
index 7588959759..335c092ecb 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchState.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.ts
@@ -54,7 +54,7 @@ import {
getBoundedRecentImages,
getGalleryPage,
getPersistedSelectedGalleryItemKeys,
- stripSessionScopedGallerySearch,
+ stripUnresolvableGallerySearch,
getGallerySettings,
getSelectedGalleryItemFromValues,
legacyGeneratedImageToGalleryItem,
@@ -1739,12 +1739,15 @@ export const normalizeWorkbenchProject = (project: Project): Project => {
continue;
}
- // A project adopted from the server arrives in a realm that never ran the
- // session its values describe: opening one from the dialog, from a deep
- // link, or from a conflict fork all land here without passing the save
- // path. A search only that session could resolve, and the rank pages set
- // against it, are meaningless here and would be read as board positions.
- const strippedValues = stripSessionScopedGallerySearch(instance.state.values);
+ // A project can arrive here from a realm that never ran the session its
+ // values describe — the Open dialog, a deep link — where a search only
+ // that session could resolve, and the rank pages set against it, would be
+ // read as board positions. But this also runs on projects that never
+ // left: closing and reopening one, and the conflict fork that deliberately
+ // rescues the LIVE copy. So the test is whether the reference resolves
+ // here, not what kind it is; the latter would delete the ranking the user
+ // is looking at.
+ const strippedValues = stripUnresolvableGallerySearch(instance.state.values);
const hasRecentImages = 'recentImages' in instance.state.values;
if (strippedValues === null && !hasRecentImages) {