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(ui): single-path relationship pruning on model deletes

joshistoast9cf21af567cf
diff --git a/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.test.ts b/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.test.ts
index 2046682049..9937fb844a 100644
--- a/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.test.ts
+++ b/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.test.ts
@@ -71,6 +71,46 @@ describe('relationships store', () => {
expect(store.getRelationshipsSnapshot().relatedKeysByModelKey).toEqual({ a: ['c'], c: ['a'] });
});
+ it('prunes a library delete exactly once via the subscription', async () => {
+ const libraryModel = (key: string): ModelConfig => ({ base: 'sdxl', key, name: key, type: 'main' }) as ModelConfig;
+ const models = await import('./modelsStore');
+
+ // Loaded before this module evaluates: the seeded baseline must cover it.
+ models.setModelsSnapshotForTests({ models: [libraryModel('a'), libraryModel('b')], status: 'loaded' });
+
+ const store = await import('./relationshipsStore');
+
+ store.setRelationshipsSnapshotForTests({ relatedKeysByModelKey: { a: ['b'], b: ['a'] } });
+
+ let notifications = 0;
+ const unsubscribe = store.subscribeToRelationships(() => {
+ notifications += 1;
+ });
+
+ models.removeModelsFromStore(['b']);
+
+ expect(store.getRelationshipsSnapshot().relatedKeysByModelKey).toEqual({ a: [] });
+ expect(notifications).toBe(1);
+ unsubscribe();
+ });
+
+ it('records tombstones without notifying when nothing referenced the removed keys', async () => {
+ const store = await import('./relationshipsStore');
+
+ store.setRelationshipsSnapshotForTests({ relatedKeysByModelKey: { a: ['b'] } });
+
+ let notifications = 0;
+ const unsubscribe = store.subscribeToRelationships(() => {
+ notifications += 1;
+ });
+
+ store.removeModelsFromRelationships(['unrelated']);
+
+ expect(notifications).toBe(0);
+ expect(store.getRelationshipsSnapshot().relatedKeysByModelKey).toEqual({ a: ['b'] });
+ unsubscribe();
+ });
+
it('discards a stale refresh that resolves after a link and keeps the patched entry', async () => {
let resolveStale: ((keys: string[]) => void) | undefined;
api.getRelatedModelKeys
diff --git a/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.ts b/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.ts
index d732e957a7..ab02ecc529 100644
--- a/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.ts
+++ b/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.ts
@@ -82,7 +82,7 @@ registerAccountOwnedResource({
*/
let lastLoadedModels: readonly ModelConfig[] | null = null;
-subscribeModels(() => {
+const pruneAgainstLibrary = (): void => {
const { models, status } = getModelsSnapshot();
if (status !== 'loaded' || models === lastLoadedModels) {
@@ -99,7 +99,12 @@ subscribeModels(() => {
}
lastLoadedModels = models;
-});
+};
+
+subscribeModels(pruneAgainstLibrary);
+// The library may already be loaded when this lazy module first evaluates;
+// seed the baseline now so the very next delete still prunes.
+pruneAgainstLibrary();
const setEntry = (modelKey: string, keys: readonly string[]): void => {
store.patchSnapshot({
@@ -255,16 +260,26 @@ export const removeModelsFromRelationships = (keys: readonly string[]): void =>
const removed = new Set(keys);
const next: Record<string, readonly string[]> = {};
+ let changed = false;
for (const [key, entry] of Object.entries(store.getSnapshot().relatedKeysByModelKey)) {
- if (!removed.has(key)) {
- next[key] = entry.some((related) => removed.has(related))
- ? entry.filter((related) => !removed.has(related))
- : entry;
+ if (removed.has(key)) {
+ changed = true;
+ continue;
+ }
+
+ if (entry.some((related) => removed.has(related))) {
+ changed = true;
+ next[key] = entry.filter((related) => !removed.has(related));
+ } else {
+ next[key] = entry;
}
}
- store.patchSnapshot({ relatedKeysByModelKey: next });
+ // Tombstones above always record; only broadcast when an entry moved.
+ if (changed) {
+ store.patchSnapshot({ relatedKeysByModelKey: next });
+ }
};
export const getRelationshipsSnapshot = (): ModelRelationshipsSnapshot => store.getSnapshot();
diff --git a/invokeai/frontend/webv2/src/features/models/ui/detail/useModelActions.ts b/invokeai/frontend/webv2/src/features/models/ui/detail/useModelActions.ts
index efd8ca62f8..45448245dd 100644
--- a/invokeai/frontend/webv2/src/features/models/ui/detail/useModelActions.ts
+++ b/invokeai/frontend/webv2/src/features/models/ui/detail/useModelActions.ts
@@ -2,7 +2,6 @@ import type { ModelConfig } from '@features/models/core/types';
import { convertModelToDiffusers, deleteModel, reidentifyModel } from '@features/models/data/api';
import { removeModelsFromStore, replaceModelInStore } from '@features/models/data/modelsStore';
-import { removeModelsFromRelationships } from '@features/models/data/relationshipsStore';
import { refreshStartersIfLoaded } from '@features/models/data/startersStore';
import { pruneModelsUiKeys } from '@features/models/ui/uiStore';
import { useNotify } from '@features/models/ui/useModelsNotify';
@@ -35,8 +34,8 @@ export const useModelActions = () => {
await deleteModel(model.key, owner.signal);
assertAccountScopeCurrent(owner);
+ // The relationships store prunes itself off this library change.
removeModelsFromStore([model.key]);
- removeModelsFromRelationships([model.key]);
pruneModelsUiKeys([model.key]);
// A deleted starter must lose its "Installed" badge in Add Models.
refreshStartersIfLoaded();
diff --git a/invokeai/frontend/webv2/src/features/models/ui/manager/LibraryColumn.tsx b/invokeai/frontend/webv2/src/features/models/ui/manager/LibraryColumn.tsx
index 271b9303de..0abcd77eba 100644
--- a/invokeai/frontend/webv2/src/features/models/ui/manager/LibraryColumn.tsx
+++ b/invokeai/frontend/webv2/src/features/models/ui/manager/LibraryColumn.tsx
@@ -3,7 +3,6 @@ import { Box, Flex, HStack, Icon, Separator, Text } from '@chakra-ui/react';
import { collectBases, collectTypes, filterModels } from '@features/models/core/library';
import { bulkDeleteModels, bulkReidentifyModels } from '@features/models/data/api';
import { refreshModels, removeModelsFromStore, useModelsSelector } from '@features/models/data/modelsStore';
-import { removeModelsFromRelationships } from '@features/models/data/relationshipsStore';
import { refreshStartersIfLoaded } from '@features/models/data/startersStore';
import { MaintenanceMenu } from '@features/models/ui/library/MaintenanceMenu';
import { ModelFilterBar } from '@features/models/ui/library/ModelFilterBar';
@@ -78,8 +77,8 @@ export const LibraryColumn = () => {
const result = await bulkDeleteModels(keys, owner.signal);
assertAccountScopeCurrent(owner);
+ // The relationships store prunes itself off this library change.
removeModelsFromStore(result.deleted);
- removeModelsFromRelationships(result.deleted);
pruneModelsUiKeys(result.deleted);
updateModelsUi({ selectedKeys: new Set(result.failed.map((failure) => failure.key)) });
if (result.deleted.length > 0) {