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(sync): take a record's baseline from the document this realm holds

Lincoln Stein533d398e70f7
diff --git a/invokeai/frontend/webv2/src/workbench/persistence.test.ts b/invokeai/frontend/webv2/src/workbench/persistence.test.ts
index e9fc4822a2..c9e2bf3e9d 100644
--- a/invokeai/frontend/webv2/src/workbench/persistence.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/persistence.test.ts
@@ -1,5 +1,6 @@
import type { WorkbenchState } from '@workbench/projectContracts';
+import { registerImageCluster } from '@features/gallery/contracts';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
@@ -164,6 +165,46 @@ describe('workbench persistence migration', () => {
expect(persistedValues?.galleryPage).toBe(0);
});
+ it('drops a session-scoped search on save even while this session can still resolve it', async () => {
+ // In the owning session the registry entry is live, so a test of whether
+ // the reference resolves would strip nothing — and the realm reading this
+ // back cannot resolve it. The save path has to ask what the value IS.
+ const clusterId = registerImageCluster(['a.png', 'b.png'], 'beaches');
+ 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: { clusterId, kind: 'cluster', label: 'beaches' },
+ },
+ },
+ },
+ },
+ },
+ ],
+ });
+ 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/projects/syncedPersistence.test.ts b/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.test.ts
index 151cdc5e85..d3ca9145e8 100644
--- a/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.test.ts
@@ -801,6 +801,70 @@ describe('authoritative project boards', () => {
expect(galleryBoardIds(hydrated!).projectBoardId).toBe(`board-for-${draft.id}`);
});
+ it('does not push a project it only opened, even when hydrating it changed the document', async () => {
+ // Another tab's search ranks a dropped file — a reference only that
+ // session can resolve — and its footer sits on a rank page. Hydrating
+ // that document here drops both. If the baseline were the wire bytes,
+ // the next autosave would read the drop as a local edit and push it,
+ // bumping the revision under the tab that still holds the live search
+ // and forking its next save as "changed elsewhere".
+ const draft = { ...createDraftProject([]), name: 'Ranked elsewhere' };
+ const document = persistence.serializeProjectDocument(draft);
+ const widgetInstances = document.widgetInstances as Record<string, { state: { values: Record<string, unknown> } }>;
+
+ widgetInstances.gallery!.state.values = {
+ ...widgetInstances.gallery!.state.values,
+ galleryPage: 3,
+ paginationMode: 'paginated',
+ semanticImageQuery: { fileId: 'external-1-other-realm', kind: 'file', label: 'dropped.png' },
+ };
+ api.__seed(document);
+ seedSessionBlob({ openProjectIds: [draft.id] });
+
+ const loaded = await service.loadWorkbench();
+ const opened = loaded!.state.projects.find((project) => project.id === draft.id)!;
+ const openedValues = opened.widgetInstances.gallery!.state.values;
+
+ expect(openedValues.semanticImageQuery).toBeNull();
+ expect(openedValues.galleryPage).toBe(0);
+
+ await service.saveWorkbench(stateWithProjects([opened]));
+
+ expect(api.updateProject).not.toHaveBeenCalled();
+ expect(api.__records.get(draft.id)?.revision).toBe(1);
+ });
+
+ it('retries rather than forks when the revision moved but the document did not', async () => {
+ // A rename elsewhere bumps the revision and leaves the data alone. The
+ // server's copy still carries the other session's search; compared as
+ // this realm holds it, it is the baseline this edit started from.
+ const draft = { ...createDraftProject([]), name: 'Ranked elsewhere' };
+ const document = persistence.serializeProjectDocument(draft);
+ const widgetInstances = document.widgetInstances as Record<string, { state: { values: Record<string, unknown> } }>;
+
+ widgetInstances.gallery!.state.values = {
+ ...widgetInstances.gallery!.state.values,
+ galleryPage: 3,
+ paginationMode: 'paginated',
+ semanticImageQuery: { fileId: 'external-1-other-realm', kind: 'file', label: 'dropped.png' },
+ };
+ api.__seed(document);
+ seedSessionBlob({ openProjectIds: [draft.id] });
+
+ const loaded = await service.loadWorkbench();
+ const opened = loaded!.state.projects.find((project) => project.id === draft.id)!;
+ const record = api.__records.get(draft.id)!;
+
+ api.__records.set(draft.id, { ...record, name: 'Renamed elsewhere', revision: record.revision + 1 });
+
+ const result = await service.saveWorkbench(stateWithProjects([{ ...opened, name: 'Edited here' }]));
+
+ expect(result.conflicts).toHaveLength(0);
+ expect(api.createProject).not.toHaveBeenCalled();
+ expect(api.__records.get(draft.id)?.name).toBe('Edited here');
+ expect(api.__records.get(draft.id)?.revision).toBe(3);
+ });
+
it('forks rather than resurrects a project deleted on another device', async () => {
const project = seedServerProject('Deleted elsewhere');
const opened = await service.hydrateProjectFromServer(project.id);
diff --git a/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.ts b/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.ts
index 5f0a2b0f7b..55b6ff0eac 100644
--- a/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.ts
+++ b/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.ts
@@ -194,6 +194,32 @@ const getSerializedProjectDocument = (
* `projectBoardId` is a stale-able cache, so overwriting it here means every path that reads from
* the server agrees on one answer. The saved destination is left alone — it is a deliberate choice.
*/
+/**
+ * The baseline a server record establishes for the push comparison.
+ *
+ * It is the serialization of what this realm holds after hydrating the
+ * record, not the raw wire bytes. Hydration can legitimately change a
+ * document — a search only the writing session could resolve is dropped,
+ * along with the pages set against it — and a baseline taken from the bytes
+ * would read that as a local edit: the next autosave would push the hydrated
+ * copy unprompted, bump the revision under the tab that still holds the live
+ * search, and fork that tab's next save as "changed elsewhere" with nobody
+ * having edited anything. Taking the baseline after hydration means a project
+ * that has only been opened is never pushed, and a real edit here conflicts
+ * with a real edit there exactly as before.
+ */
+const adoptRecordBaseline = (
+ syncState: SyncedPersistenceState,
+ record: ProjectRecordDTO
+): { project: Project | null; pushedDoc: string } => {
+ const project = deserializeProjectRecord(record);
+
+ return {
+ project,
+ pushedDoc: project === null ? JSON.stringify(record.data) : getSerializedProjectDocument(syncState, project).json,
+ };
+};
+
const deserializeProjectRecord = (record: ProjectRecordDTO): Project | null => {
const project = deserializeProjectDocument(
applyAuthoritativeProjectBoard(record.data, record.board_id, { selectBoard: false })
@@ -291,7 +317,7 @@ const pushNewProject = async (syncState: SyncedPersistenceState, project: Projec
assertOwner(syncState);
syncState.syncEntries.set(project.id, {
- pushedDoc: JSON.stringify(existing.data),
+ pushedDoc: adoptRecordBaseline(syncState, existing).pushedDoc,
revision: existing.revision,
});
syncState.pendingBoardAssignments.push({ boardId: existing.board_id, projectId: project.id });
@@ -362,7 +388,11 @@ const recoverConflictingProject = async (
const server = await apiGetProject(project.id, syncState.owner.signal);
assertOwner(syncState);
- const serverDocJson = JSON.stringify(server.data);
+ // Compared as this realm would hold it, the same way the baseline was
+ // taken: the wire bytes of an unchanged document differ from the baseline
+ // whenever hydration changes it, and that would read a revision that
+ // merely drifted as a divergence.
+ const { project: serverProject, pushedDoc: serverDocJson } = adoptRecordBaseline(syncState, server);
syncState.syncEntries.set(project.id, { pushedDoc: serverDocJson, revision: server.revision });
@@ -374,8 +404,6 @@ const recoverConflictingProject = async (
return { kind: 'retry' };
}
- const serverProject = deserializeProjectRecord(server);
-
if (!serverProject) {
return { kind: 'failed' };
}
@@ -711,14 +739,11 @@ const loadFromBackend = async (
continue;
}
- const project = deserializeProjectRecord(record);
+ const { project, pushedDoc } = adoptRecordBaseline(syncState, record);
if (project) {
serverProjects.push(project);
- syncState.syncEntries.set(record.project_id, {
- pushedDoc: JSON.stringify(record.data),
- revision: record.revision,
- });
+ syncState.syncEntries.set(record.project_id, { pushedDoc, revision: record.revision });
}
}
@@ -914,16 +939,13 @@ export const createSyncedWorkbenchPersistence = (
const adoptProjectRecord = (record: ProjectRecordDTO): Project | null => {
assertOwner(syncState);
- const project = deserializeProjectRecord(record);
+ const { project, pushedDoc } = adoptRecordBaseline(syncState, record);
if (!project) {
return null;
}
- syncState.syncEntries.set(record.project_id, {
- pushedDoc: JSON.stringify(record.data),
- revision: record.revision,
- });
+ syncState.syncEntries.set(record.project_id, { pushedDoc, revision: record.revision });
persistSyncMap(syncState);
return project;