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(webv2): harden workbench autosave scheduling

joshistoast69e26fd2f689
diff --git a/invokeai/frontend/webv2/src/workbench/WorkbenchContext.tsx b/invokeai/frontend/webv2/src/workbench/WorkbenchContext.tsx
index 9da92cb7cf..8cb7351f75 100644
--- a/invokeai/frontend/webv2/src/workbench/WorkbenchContext.tsx
+++ b/invokeai/frontend/webv2/src/workbench/WorkbenchContext.tsx
@@ -110,7 +110,10 @@ export const WorkbenchProvider = ({
}
if (snapshot) {
- lastSavedStateKeyRef.current = getPersistedStateKey(snapshot.state);
+ if (!syncedWorkbenchPersistence.hasPendingChanges()) {
+ lastSavedStateKeyRef.current = getPersistedStateKey(snapshot.state);
+ }
+
dispatch({ state: snapshot.state, type: 'hydrateWorkbench' });
}
@@ -148,8 +151,9 @@ export const WorkbenchProvider = ({
// Revision conflicts surfaced by a save are applied to state here: the
// server version adopts the project id and the local edits continue in a
- // recovered fork. The follow-up autosave is a no-op for both (the sync
- // layer already acknowledged them).
+ // recovered fork. The follow-up autosave still persists the reconciled
+ // session/cache, while project document pushes are no-ops because the sync
+ // layer already acknowledged them.
const applySaveResult = (result: WorkbenchSaveResult): void => {
for (const conflict of result.conflicts) {
dispatch({
diff --git a/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.test.ts b/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.test.ts
index b40110a8a1..46642eeba8 100644
--- a/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.test.ts
@@ -192,6 +192,7 @@ describe('loadWorkbench session hydration', () => {
expect(snapshot?.state.projects.map((project) => project.id)).toEqual([second.id]);
expect(snapshot?.state.activeProjectId).toBe(second.id);
expect(api.getProject).toHaveBeenCalledTimes(1);
+ expect(persistence.syncedWorkbenchPersistence.hasPendingChanges()).toBe(false);
const libraryIds = library.getProjectLibrary().summaries.map((summary) => summary.id);
@@ -232,6 +233,7 @@ describe('loadWorkbench session hydration', () => {
expect(snapshot?.state.projects).toHaveLength(1);
expect(snapshot?.state.projects[0].id).not.toBe(existing.id);
expect(snapshot?.state.activeProjectId).toBe(snapshot?.state.projects[0].id);
+ expect(persistence.syncedWorkbenchPersistence.hasPendingChanges()).toBe(true);
});
it('joins a deep-linked project into the open set and focuses it', async () => {
@@ -245,6 +247,7 @@ describe('loadWorkbench session hydration', () => {
expect(snapshot?.state.projects.map((project) => project.id)).toEqual([first.id, second.id]);
expect(snapshot?.state.activeProjectId).toBe(second.id);
+ expect(persistence.syncedWorkbenchPersistence.hasPendingChanges()).toBe(true);
});
it('appends and activates a draft when a new project is requested', async () => {
@@ -257,6 +260,7 @@ describe('loadWorkbench session hydration', () => {
expect(snapshot?.state.projects).toHaveLength(2);
expect(snapshot?.state.activeProjectId).not.toBe(first.id);
+ expect(persistence.syncedWorkbenchPersistence.hasPendingChanges()).toBe(true);
});
});
diff --git a/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.ts b/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.ts
index 61367fa46b..0c13f3448b 100644
--- a/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.ts
+++ b/invokeai/frontend/webv2/src/workbench/projects/syncedPersistence.ts
@@ -439,6 +439,20 @@ const loadFromBackend = async (
activeProjectId = draft.id;
}
+ const state: WorkbenchState = {
+ ...base,
+ account: sessionBlob?.account ?? base.account,
+ activeProjectId,
+ autosave: { status: 'idle' },
+ backendConnection: { status: 'connecting' },
+ notifications: [],
+ projects,
+ };
+
+ if (serializeSessionBlob(state) !== lastPushedAccount) {
+ hasPending = true;
+ }
+
reportProjectSync({
hasPendingChanges: hasPending,
projects: Object.fromEntries(
@@ -450,16 +464,6 @@ const loadFromBackend = async (
),
});
- const state: WorkbenchState = {
- ...base,
- account: sessionBlob?.account ?? base.account,
- activeProjectId,
- autosave: { status: 'idle' },
- backendConnection: { status: 'connecting' },
- notifications: [],
- projects,
- };
-
persistSyncMap();
const snapshot = createSnapshot(state);
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchAutosave.test.ts b/invokeai/frontend/webv2/src/workbench/workbenchAutosave.test.ts
new file mode 100644
index 0000000000..9f891ab535
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/workbenchAutosave.test.ts
@@ -0,0 +1,65 @@
+import { describe, expect, it } from 'vitest';
+
+import { getAutosaveScheduleDecision } from './workbenchAutosave';
+
+describe('getAutosaveScheduleDecision', () => {
+ it('does not schedule already-saved or already-scheduled state', () => {
+ expect(
+ getAutosaveScheduleDecision({
+ failedStateKey: null,
+ lastSavedStateKey: 'project-a',
+ persistedStateKey: 'project-a',
+ scheduledStateKey: null,
+ })
+ ).toEqual({ failedStateKey: null, shouldSchedule: false });
+
+ expect(
+ getAutosaveScheduleDecision({
+ failedStateKey: null,
+ lastSavedStateKey: 'project-a',
+ persistedStateKey: 'project-b',
+ scheduledStateKey: 'project-b',
+ })
+ ).toEqual({ failedStateKey: null, shouldSchedule: false });
+ });
+
+ it('holds a failed state key until a new durable edit happens', () => {
+ expect(
+ getAutosaveScheduleDecision({
+ failedStateKey: 'project-b',
+ lastSavedStateKey: 'project-a',
+ persistedStateKey: 'project-b',
+ scheduledStateKey: null,
+ })
+ ).toEqual({ failedStateKey: 'project-b', shouldSchedule: false });
+
+ expect(
+ getAutosaveScheduleDecision({
+ failedStateKey: 'project-b',
+ lastSavedStateKey: 'project-a',
+ persistedStateKey: 'project-c',
+ scheduledStateKey: null,
+ })
+ ).toEqual({ failedStateKey: null, shouldSchedule: true });
+ });
+
+ it('clears a failed state key after returning to the last saved state', () => {
+ expect(
+ getAutosaveScheduleDecision({
+ failedStateKey: 'project-b',
+ lastSavedStateKey: 'project-a',
+ persistedStateKey: 'project-a',
+ scheduledStateKey: null,
+ })
+ ).toEqual({ failedStateKey: null, shouldSchedule: false });
+
+ expect(
+ getAutosaveScheduleDecision({
+ failedStateKey: null,
+ lastSavedStateKey: 'project-a',
+ persistedStateKey: 'project-b',
+ scheduledStateKey: null,
+ })
+ ).toEqual({ failedStateKey: null, shouldSchedule: true });
+ });
+});