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): do not persist a search only this session can resolve

Lincoln Stein14b28245b23b
diff --git a/invokeai/frontend/webv2/src/features/gallery/contracts.ts b/invokeai/frontend/webv2/src/features/gallery/contracts.ts
index e9ae782497..a5a39f8032 100644
--- a/invokeai/frontend/webv2/src/features/gallery/contracts.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/contracts.ts
@@ -36,6 +36,7 @@ export {
export { GALLERY_RECENT_IMAGE_LIMIT, getBoundedRecentImages } from './core/recentImages';
export {
getImageCluster,
+ isSessionScopedGallerySemanticReference,
parseGallerySemanticReference,
registerImageCluster,
type GallerySemanticReference,
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/semanticImageQuery.ts b/invokeai/frontend/webv2/src/features/gallery/core/semanticImageQuery.ts
index e2bbde928f..40bda76b63 100644
--- a/invokeai/frontend/webv2/src/features/gallery/core/semanticImageQuery.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/core/semanticImageQuery.ts
@@ -174,6 +174,21 @@ export const pruneImageClusterMembers = (imageNames: readonly string[]): (() =>
* legacy bare-image-name shape, and reads a file key that no longer resolves
* in the registry as no search at all.
*/
+/**
+ * A reference that only this session can resolve — a dropped file or an
+ * image-map cluster.
+ *
+ * Both kinds name an entry in an in-memory registry, and their ids carry a
+ * per-realm token, so a persisted one can never resolve again: a reload, or a
+ * second tab, reads it as no search at all. Anything set against the ranking
+ * 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 =>
+ !!value &&
+ typeof value === 'object' &&
+ ((value as Record<string, unknown>).kind === 'file' || (value as Record<string, unknown>).kind === 'cluster');
+
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 48b01ef929..264b5ac6e0 100644
--- a/invokeai/frontend/webv2/src/workbench/persistence.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/persistence.test.ts
@@ -72,6 +72,78 @@ describe('workbench persistence migration', () => {
expect(hydratePersistedWorkbenchSnapshot(persisted)?.state).toEqual(state);
});
+ it('does not persist a search only this session can resolve, nor the page it left behind', async () => {
+ // The footer paginates the RANKING while a similarity search is on
+ // screen, so this page is a rank page. Cluster members live in an
+ // in-memory registry the next session cannot read, so the ranking is gone
+ // over there — and a rank page read as a board page lands the grid on an
+ // unrelated slice.
+ const state = createInitialWorkbenchState();
+ const project = state.projects[0];
+ const galleryEntry = Object.entries(project?.widgetInstances ?? {}).find(
+ ([, instance]) => instance.typeId === 'gallery'
+ );
+
+ expect(galleryEntry).toBeDefined();
+
+ const [galleryInstanceId, galleryInstance] = galleryEntry!;
+ const values = {
+ ...galleryInstance.state.values,
+ galleryPage: 3,
+ paginationMode: 'paginated',
+ semanticImageQuery: { clusterId: 'cluster-1', kind: 'cluster', label: 'beaches' },
+ };
+ const snapshot = await localStorageWorkbenchPersistence.saveWorkbench({
+ ...state,
+ projects: [
+ {
+ ...project!,
+ widgetInstances: {
+ ...project!.widgetInstances,
+ [galleryInstanceId]: { ...galleryInstance, state: { ...galleryInstance.state, values } },
+ },
+ },
+ ],
+ });
+ 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.
+ const state = createInitialWorkbenchState();
+ const project = state.projects[0];
+ const galleryEntry = Object.entries(project?.widgetInstances ?? {}).find(
+ ([, instance]) => instance.typeId === 'gallery'
+ );
+ const [galleryInstanceId, galleryInstance] = galleryEntry!;
+ const values = {
+ ...galleryInstance.state.values,
+ galleryPage: 3,
+ paginationMode: 'paginated',
+ semanticImageQuery: { kind: 'text', query: 'sunset' },
+ };
+ const snapshot = await localStorageWorkbenchPersistence.saveWorkbench({
+ ...state,
+ projects: [
+ {
+ ...project!,
+ widgetInstances: {
+ ...project!.widgetInstances,
+ [galleryInstanceId]: { ...galleryInstance, state: { ...galleryInstance.state, values } },
+ },
+ },
+ ],
+ });
+ const persistedValues = snapshot.state.projects[0]?.widgetInstances[galleryInstanceId]?.state.values;
+
+ expect(persistedValues?.semanticImageQuery).toEqual({ kind: 'text', query: 'sunset' });
+ expect(persistedValues?.galleryPage).toBe(3);
+ });
+
it('drops corrupt localStorage snapshots instead of throwing', async () => {
storage.set('invokeai:v7:webv2:workbench', '{not json');
diff --git a/invokeai/frontend/webv2/src/workbench/persistence.ts b/invokeai/frontend/webv2/src/workbench/persistence.ts
index 8cfec4c489..631404c982 100644
--- a/invokeai/frontend/webv2/src/workbench/persistence.ts
+++ b/invokeai/frontend/webv2/src/workbench/persistence.ts
@@ -1,7 +1,11 @@
import type { HydratedWorkbenchSnapshot, PersistedWorkbenchSnapshotV1 } from '@workbench/persistenceContracts';
import type { Project, WorkbenchState } from '@workbench/projectContracts';
-import { getGalleryPage, getGallerySettings } from '@features/gallery/contracts';
+import {
+ getGalleryPage,
+ getGallerySettings,
+ isSessionScopedGallerySemanticReference,
+} from '@features/gallery/contracts';
import { timeWorkbenchPerf } from './performanceMarks';
@@ -17,30 +21,58 @@ export interface WorkbenchPersistenceService {
const isBrowser = (): boolean => typeof window !== 'undefined' && typeof window.localStorage !== 'undefined';
/**
- * In infinite mode the gallery's `galleryPage` is not a page number but the
- * anchor of a mid-board window, set by a reveal from the image map. That is a
- * "you are here" position for the current session: restored a day later it
- * would open the gallery stranded in the middle of a board, with no page
- * control (the footer's is paginated-only) and no way back to the top short of
- * switching boards. Paginated pages stay persisted — there the value really is
- * the page the user was reading.
+ * Two gallery positions describe the current session only, and both are read
+ * on reload as if they described the board.
+ *
+ * In infinite mode `galleryPage` is not a page number but the anchor of a
+ * mid-board window, set by a reveal from the image map. That is a "you are
+ * here" for this session: restored a day later it would open the gallery
+ * stranded in the middle of a board, with no page control (the footer's is
+ * paginated-only) and no way back to the top short of switching boards.
+ * Paginated pages stay persisted — there the value really is the page the
+ * 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.
*/
-const stripInfiniteWindowAnchor = (project: Project): Project => {
+const stripSessionScopedGalleryState = (project: Project): Project => {
let didChange = false;
const widgetInstances = Object.fromEntries(
Object.entries(project.widgetInstances).map(([instanceId, instance]) => {
const values = instance.state.values;
- if (
- instance.typeId !== 'gallery' ||
- getGallerySettings(values).paginationMode !== 'infinite' ||
- getGalleryPage(values) === 0
- ) {
+ if (instance.typeId !== 'gallery') {
+ return [instanceId, instance];
+ }
+
+ const hasSessionScopedSearch = isSessionScopedGallerySemanticReference(values.semanticImageQuery);
+ const hasWindowAnchor = getGallerySettings(values).paginationMode === 'infinite' && getGalleryPage(values) > 0;
+
+ if (!hasSessionScopedSearch && !hasWindowAnchor) {
return [instanceId, instance];
}
didChange = true;
- return [instanceId, { ...instance, state: { ...instance.state, values: { ...values, galleryPage: 0 } } }];
+ return [
+ instanceId,
+ {
+ ...instance,
+ state: {
+ ...instance.state,
+ values: {
+ ...values,
+ galleryPage: 0,
+ ...(hasSessionScopedSearch ? { semanticImageQuery: null } : {}),
+ },
+ },
+ },
+ ];
})
);
@@ -57,7 +89,7 @@ export const stripTransientWorkbenchState = (state: WorkbenchState): WorkbenchSt
// snapshots immediately and never let full-project undo entries consume
// localStorage quota or grow across browser sessions.
projects: nextState.projects.map((project) => ({
- ...stripInfiniteWindowAnchor(project),
+ ...stripSessionScopedGalleryState(project),
undoRedo: { future: [], past: [] },
})),
};