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): drop relationship entries for models gone after a refresh

joshistoastaef872ecda84
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 cfab449e8e..2046682049 100644
--- a/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.test.ts
+++ b/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.test.ts
@@ -1,3 +1,5 @@
+import type { ModelConfig } from '@features/models/core/types';
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
const api = vi.hoisted(() => ({
@@ -228,3 +230,56 @@ describe('relationships store', () => {
expect(store.getRelationshipsSnapshot().relatedKeysByModelKey).toEqual({});
});
});
+
+describe('relationships pruning from library snapshots', () => {
+ beforeEach(() => {
+ vi.resetModules();
+ api.getRelatedModelKeys.mockReset();
+ });
+
+ const model = (key: string) => ({ key, name: key }) as ModelConfig;
+
+ it('drops entries for models missing from the next loaded snapshot and scrubs siblings', async () => {
+ const store = await import('./relationshipsStore');
+ const models = await import('./modelsStore');
+
+ models.setModelsSnapshotForTests({ models: [model('a'), model('b'), model('c')], status: 'loaded' });
+ store.setRelationshipsSnapshotForTests({ relatedKeysByModelKey: { a: ['b'], b: ['a', 'c'], c: ['b'] } });
+
+ models.setModelsSnapshotForTests({ models: [model('b'), model('c')], status: 'loaded' });
+
+ expect(store.getRelationshipsSnapshot().relatedKeysByModelKey).toEqual({ b: ['c'], c: ['b'] });
+ });
+
+ it('does not prune from non-loaded snapshots or before a first loaded baseline', async () => {
+ const store = await import('./relationshipsStore');
+ const models = await import('./modelsStore');
+
+ store.setRelationshipsSnapshotForTests({ relatedKeysByModelKey: { a: ['b'], b: ['a'] } });
+
+ // First loaded snapshot only establishes the baseline.
+ models.setModelsSnapshotForTests({ models: [model('b')], status: 'loaded' });
+ expect(store.getRelationshipsSnapshot().relatedKeysByModelKey).toEqual({ a: ['b'], b: ['a'] });
+
+ // A loading patch must not prune either.
+ models.setModelsSnapshotForTests({ models: [], status: 'loading' });
+ expect(store.getRelationshipsSnapshot().relatedKeysByModelKey).toEqual({ a: ['b'], b: ['a'] });
+ });
+
+ it('lets a pruned model recover once the server answers for its key again', async () => {
+ const store = await import('./relationshipsStore');
+ const models = await import('./modelsStore');
+
+ models.setModelsSnapshotForTests({ models: [model('a'), model('b')], status: 'loaded' });
+ store.setRelationshipsSnapshotForTests({ relatedKeysByModelKey: { a: ['b'], b: ['a'] } });
+ models.setModelsSnapshotForTests({ models: [model('b')], status: 'loaded' });
+ expect(store.getRelationshipsSnapshot().relatedKeysByModelKey).toEqual({ b: [] });
+
+ // Reinstalled: the library lists it again and a fetch succeeds.
+ models.setModelsSnapshotForTests({ models: [model('a'), model('b')], status: 'loaded' });
+ api.getRelatedModelKeys.mockResolvedValueOnce(['b']);
+ await store.ensureRelatedModelKeysLoaded('a');
+
+ expect(store.getRelationshipsSnapshot().relatedKeysByModelKey).toEqual({ a: ['b'], b: [] });
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.ts b/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.ts
index e2f0490266..d732e957a7 100644
--- a/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.ts
+++ b/invokeai/frontend/webv2/src/features/models/data/relationshipsStore.ts
@@ -1,3 +1,5 @@
+import type { ModelConfig } from '@features/models/core/types';
+
import {
captureAccountScope,
isAccountScopeCurrent,
@@ -6,6 +8,7 @@ import {
import { createExternalStore } from '@platform/state/externalStore';
import { areArraysEqual } from '@platform/state/selectors';
+import { getModelsSnapshot, subscribeModels } from './modelsStore';
import { addModelRelationship, getRelatedModelKeys, removeModelRelationship } from './relationshipsApi';
/**
@@ -62,11 +65,42 @@ registerAccountOwnedResource({
failedFetchKeys.clear();
latestPairMutation.clear();
removedKeys.clear();
+ lastLoadedModels = null;
store.setSnapshot(EMPTY_RELATIONSHIPS_SNAPSHOT);
},
name: 'model-relationships',
});
+/**
+ * Library-driven pruning: when a loaded library snapshot no longer contains a
+ * model we hold an entry for (deleted by another surface, or gone after a
+ * refresh), drop the entry and scrub the key from siblings. Entries for
+ * still-present models stay cache-first; freshness of links edited elsewhere
+ * is out of scope by design. This store subscribes to the library — not the
+ * other way around — so the eagerly-loaded modelsStore never pulls this lazy
+ * chunk into the entry graph.
+ */
+let lastLoadedModels: readonly ModelConfig[] | null = null;
+
+subscribeModels(() => {
+ const { models, status } = getModelsSnapshot();
+
+ if (status !== 'loaded' || models === lastLoadedModels) {
+ return;
+ }
+
+ if (lastLoadedModels !== null) {
+ const presentKeys = new Set(models.map((model) => model.key));
+ const removed = lastLoadedModels.filter((model) => !presentKeys.has(model.key)).map((model) => model.key);
+
+ if (removed.length > 0) {
+ removeModelsFromRelationships(removed);
+ }
+ }
+
+ lastLoadedModels = models;
+});
+
const setEntry = (modelKey: string, keys: readonly string[]): void => {
store.patchSnapshot({
relatedKeysByModelKey: { ...store.getSnapshot().relatedKeysByModelKey, [modelKey]: keys },