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): drop a session-scoped search on every route into a realm

Lincoln Steinffcc96f30654
diff --git a/invokeai/frontend/webv2/src/features/gallery/contracts.ts b/invokeai/frontend/webv2/src/features/gallery/contracts.ts
index ed8f2949c2..52c5805e65 100644
--- a/invokeai/frontend/webv2/src/features/gallery/contracts.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/contracts.ts
@@ -37,8 +37,8 @@ export { GALLERY_RECENT_IMAGE_LIMIT, getBoundedRecentImages } from './core/recen
export {
gallerySemanticReferenceKey,
getImageCluster,
- isSessionScopedGallerySemanticReference,
parseGallerySemanticReference,
+ stripSessionScopedGallerySearch,
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 40bda76b63..e41370d991 100644
--- a/invokeai/frontend/webv2/src/features/gallery/core/semanticImageQuery.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/core/semanticImageQuery.ts
@@ -184,11 +184,50 @@ export const pruneImageClusterMembers = (imageNames: readonly string[]): (() =>
* it named — the page the footer was on, above all — describes a list that
* will not exist there either.
*/
-export const isSessionScopedGallerySemanticReference = (value: unknown): boolean =>
+const isSessionScopedGallerySemanticReference = (value: unknown): boolean =>
!!value &&
typeof value === 'object' &&
((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.
+ *
+ * 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
+ * position are rewritten: normalizing must not mint state that was never
+ * stored.
+ */
+export const stripSessionScopedGallerySearch = (values: Record<string, unknown>): Record<string, unknown> | null => {
+ if (!isSessionScopedGallerySemanticReference(values.semanticImageQuery)) {
+ return null;
+ }
+
+ const nextValues: Record<string, unknown> = { ...values, semanticImageQuery: null };
+
+ if (typeof values.galleryPage === 'number' && values.galleryPage !== 0) {
+ nextValues.galleryPage = 0;
+ }
+
+ if (typeof values.selectedImagePage === 'number' && values.selectedImagePage !== 0) {
+ nextValues.selectedImagePage = 0;
+ }
+
+ const selectedImageQuery =
+ values.selectedImageQuery && typeof values.selectedImageQuery === 'object'
+ ? (values.selectedImageQuery as Record<string, unknown>)
+ : null;
+
+ if (selectedImageQuery && typeof selectedImageQuery.page === 'number' && selectedImageQuery.page !== 0) {
+ nextValues.selectedImageQuery = { ...selectedImageQuery, page: 0 };
+ }
+
+ return nextValues;
+};
+
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 264b5ac6e0..bffe58fdb5 100644
--- a/invokeai/frontend/webv2/src/workbench/persistence.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/persistence.test.ts
@@ -91,6 +91,15 @@ describe('workbench persistence migration', () => {
...galleryInstance.state.values,
galleryPage: 3,
paginationMode: 'paginated',
+ selectedImagePage: 3,
+ selectedImageQuery: {
+ boardId: 'none',
+ galleryView: 'images',
+ imageOrderDir: 'DESC',
+ page: 3,
+ paginationMode: 'paginated',
+ searchTerm: '',
+ },
semanticImageQuery: { clusterId: 'cluster-1', kind: 'cluster', label: 'beaches' },
};
const snapshot = await localStorageWorkbenchPersistence.saveWorkbench({
@@ -109,6 +118,11 @@ describe('workbench persistence migration', () => {
expect(persistedValues?.semanticImageQuery).toBeNull();
expect(persistedValues?.galleryPage).toBe(0);
+ // The selection's page is stamped from the gallery's, so under a ranking
+ // it is a rank page too — dropping only the grid's would leave Preview
+ // anchored 180 rows from what the grid shows.
+ expect(persistedValues?.selectedImagePage).toBe(0);
+ expect((persistedValues?.selectedImageQuery as { page: number } | undefined)?.page).toBe(0);
});
it('keeps a paginated page whose search survives the reload', async () => {
diff --git a/invokeai/frontend/webv2/src/workbench/persistence.ts b/invokeai/frontend/webv2/src/workbench/persistence.ts
index 631404c982..f6a8476622 100644
--- a/invokeai/frontend/webv2/src/workbench/persistence.ts
+++ b/invokeai/frontend/webv2/src/workbench/persistence.ts
@@ -1,11 +1,7 @@
import type { HydratedWorkbenchSnapshot, PersistedWorkbenchSnapshotV1 } from '@workbench/persistenceContracts';
import type { Project, WorkbenchState } from '@workbench/projectContracts';
-import {
- getGalleryPage,
- getGallerySettings,
- isSessionScopedGallerySemanticReference,
-} from '@features/gallery/contracts';
+import { getGalleryPage, getGallerySettings, stripSessionScopedGallerySearch } from '@features/gallery/contracts';
import { timeWorkbenchPerf } from './performanceMarks';
@@ -33,13 +29,14 @@ const isBrowser = (): boolean => typeof window !== 'undefined' && typeof window.
* user was reading.
*
* A dropped file or an image-map cluster is likewise session-scoped: it names
- * an entry in an in-memory registry that the next session cannot resolve, so
- * the ranking evaporates on the first parse over there. Nothing observes that
- * moment, so anything set against the ranking outlives it — and in paginated
- * mode the footer paginates the RANKING, which makes `galleryPage` a rank
- * page that the board listing would then read as its own and answer with an
- * unrelated slice. The reference cannot survive the reload, so neither may
- * the page it named.
+ * an entry in an in-memory registry that no other realm can resolve, so the
+ * ranking evaporates on the first parse over there. Nothing observes that
+ * moment, so anything set against the ranking outlives it — and the footer
+ * 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.
*/
const stripSessionScopedGalleryState = (project: Project): Project => {
let didChange = false;
@@ -51,10 +48,10 @@ const stripSessionScopedGalleryState = (project: Project): Project => {
return [instanceId, instance];
}
- const hasSessionScopedSearch = isSessionScopedGallerySemanticReference(values.semanticImageQuery);
+ const strippedValues = stripSessionScopedGallerySearch(values);
const hasWindowAnchor = getGallerySettings(values).paginationMode === 'infinite' && getGalleryPage(values) > 0;
- if (!hasSessionScopedSearch && !hasWindowAnchor) {
+ if (strippedValues === null && !hasWindowAnchor) {
return [instanceId, instance];
}
@@ -66,9 +63,8 @@ const stripSessionScopedGalleryState = (project: Project): Project => {
state: {
...instance.state,
values: {
- ...values,
- galleryPage: 0,
- ...(hasSessionScopedSearch ? { semanticImageQuery: null } : {}),
+ ...(strippedValues ?? values),
+ ...(hasWindowAnchor ? { galleryPage: 0 } : {}),
},
},
},
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
index b1b856b317..5c9ca879d8 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
@@ -31,6 +31,7 @@ import {
normalizeGraphHistory,
shouldSnapPanelShut,
normalizeWorkbenchAccount,
+ normalizeWorkbenchProject,
} from './workbenchState';
import {
createInitialWorkbenchState,
@@ -794,6 +795,66 @@ describe('workbench widget region opening', () => {
});
});
+describe('adopting a project from another realm', () => {
+ const galleryProject = (values: Record<string, unknown>) => {
+ const project = createInitialWorkbenchState().projects[0]!;
+ const galleryInstance = Object.values(project.widgetInstances).find((instance) => instance.typeId === 'gallery')!;
+ const galleryInstanceId = Object.keys(project.widgetInstances).find(
+ (instanceId) => project.widgetInstances[instanceId]?.typeId === 'gallery'
+ )!;
+
+ return normalizeWorkbenchProject({
+ ...project,
+ widgetInstances: {
+ ...project.widgetInstances,
+ [galleryInstanceId]: {
+ ...galleryInstance,
+ state: { ...galleryInstance.state, values: { ...galleryInstance.state.values, ...values } },
+ },
+ },
+ });
+ };
+ const galleryValuesOf = (project: Project) =>
+ Object.values(project.widgetInstances).find((instance) => instance.typeId === 'gallery')!.state.values;
+
+ it('drops a session-scoped search and the rank pages set against it', () => {
+ // A project opened from the server — the Open dialog, a deep link, or a
+ // conflict fork — arrives in a realm that never ran the session its values
+ // describe, and never passes the save path where this rule also runs. The
+ // ranking cannot be rebuilt here, so the pages indexing it would be read
+ // as board positions.
+ const values = galleryValuesOf(
+ galleryProject({
+ galleryPage: 3,
+ selectedImagePage: 3,
+ selectedImageQuery: {
+ boardId: 'none',
+ galleryView: 'images',
+ imageOrderDir: 'DESC',
+ page: 3,
+ paginationMode: 'paginated',
+ searchTerm: '',
+ },
+ semanticImageQuery: { fileId: 'file-1', kind: 'file', label: 'dropped.png' },
+ })
+ );
+
+ expect(values.semanticImageQuery).toBeNull();
+ expect(values.galleryPage).toBe(0);
+ expect(values.selectedImagePage).toBe(0);
+ expect((values.selectedImageQuery as { page: number }).page).toBe(0);
+ });
+
+ 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' } })
+ );
+
+ expect(values.semanticImageQuery).toEqual({ kind: 'text', query: 'sunset' });
+ expect(values.galleryPage).toBe(3);
+ });
+});
+
describe('workbench widget state updates', () => {
it('patches a missing undefined widget value key as a real state change', () => {
const initial = createInitialWorkbenchState();
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.ts
index c71841d158..7588959759 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchState.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.ts
@@ -54,6 +54,7 @@ import {
getBoundedRecentImages,
getGalleryPage,
getPersistedSelectedGalleryItemKeys,
+ stripSessionScopedGallerySearch,
getGallerySettings,
getSelectedGalleryItemFromValues,
legacyGeneratedImageToGalleryItem,
@@ -1734,18 +1735,29 @@ export const normalizeWorkbenchProject = (project: Project): Project => {
}
for (const [instanceId, instance] of Object.entries(widgetInstances)) {
- if (instance.typeId !== 'gallery' || !('recentImages' in instance.state.values)) {
+ if (instance.typeId !== 'gallery') {
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);
+ const hasRecentImages = 'recentImages' in instance.state.values;
+
+ if (strippedValues === null && !hasRecentImages) {
+ continue;
+ }
+
+ const values = strippedValues ?? instance.state.values;
+
widgetInstances[instanceId] = {
...instance,
state: {
...instance.state,
- values: {
- ...instance.state.values,
- recentImages: getBoundedRecentImages(instance.state.values.recentImages),
- },
+ values: hasRecentImages ? { ...values, recentImages: getBoundedRecentImages(values.recentImages) } : values,
},
};
}