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.

refactor(queue): inject model-load sink as a coordinator port

joshistoast0b66f9a987d4
diff --git a/invokeai/frontend/webv2/ARCHITECTURE.md b/invokeai/frontend/webv2/ARCHITECTURE.md
index 9808864808..3a3b22b3e4 100644
--- a/invokeai/frontend/webv2/ARCHITECTURE.md
+++ b/invokeai/frontend/webv2/ARCHITECTURE.md
@@ -68,7 +68,7 @@ The mixed-folder rules are longest-prefix rules in the manifest, so the Backend,
The target dependency graph intentionally records the current migration cycle: Workbench and the eight destination Features are one transitional strongly connected component. That is evidence for the cycle-breaking sequence, not permission for the destination architecture to retain the cycle. The migration must replace implementation imports with public module interfaces or composition-root adapters until the only direction is `app -> workbench/features -> platform`.
-Feature interfaces remain ordinary module interfaces unless two adapters add leverage. Queue's backend port has production transport/realtime and test adapters; Identity's token/session port has browser-session and test adapters; Models and Nodes use production event/transport and test adapters. Gallery, Generation, Workflow, and Upscale keep their compilation and UI seams internal until their feature tickets demonstrate another independent adapter. Workbench image actions likewise remain internal rather than becoming a shallow Image Feature.
+Feature interfaces remain ordinary module interfaces unless two adapters add leverage. Queue's backend port has production transport/realtime and test adapters; Identity's token/session port has browser-session and test adapters; Models and Nodes use production event/transport adapters; their sinks (model-load activity, node execution) are injected into Queue's runtime at the App composition root, with test doubles in Queue's runtime tests. Gallery, Generation, Workflow, and Upscale keep their compilation and UI seams internal until their feature tickets demonstrate another independent adapter. Workbench image actions likewise remain internal rather than becoming a shallow Image Feature.
| Feature | Responsibility and representative callers | Allowed dependencies and candidate public interface |
| ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
diff --git a/invokeai/frontend/webv2/src/app/QueueRuntimeAdapter.tsx b/invokeai/frontend/webv2/src/app/QueueRuntimeAdapter.tsx
index e934e2c2fa..81d2082315 100644
--- a/invokeai/frontend/webv2/src/app/QueueRuntimeAdapter.tsx
+++ b/invokeai/frontend/webv2/src/app/QueueRuntimeAdapter.tsx
@@ -1,10 +1,11 @@
+import { modelLoadActivitySink } from '@features/models';
import { nodeExecutionStore } from '@features/nodes';
import { createProductionQueueRuntime } from '@features/queue';
import { ensureInvocationTemplatesLoaded } from '@features/workflow/react';
import { useMountEffect } from '@platform/react/useMountEffect';
import { useWorkbenchCommands, useWorkbenchQueries, useWorkbenchSubscription } from '@workbench/WorkbenchContext';
-/** App-owned production composition of Queue, Workbench, Gallery, and Workflow adapters. */
+/** App-owned production composition of Queue, Workbench, Gallery, Workflow, Nodes, and Models adapters. */
export const QueueRuntimeAdapter = () => {
const { notifications, queue } = useWorkbenchCommands();
const queries = useWorkbenchQueries();
@@ -33,6 +34,7 @@ export const QueueRuntimeAdapter = () => {
},
subscribe,
},
+ modelLoads: modelLoadActivitySink,
nodeExecution: nodeExecutionStore,
});
diff --git a/invokeai/frontend/webv2/src/features/queue/publicApi.ts b/invokeai/frontend/webv2/src/features/queue/publicApi.ts
index 8264a747ac..220b775cbc 100644
--- a/invokeai/frontend/webv2/src/features/queue/publicApi.ts
+++ b/invokeai/frontend/webv2/src/features/queue/publicApi.ts
@@ -1,7 +1,7 @@
import type { QueueFeatureCommands, QueueQueryScope, QueueReadModel } from './core/types';
import type { QueueItemProgressPort, QueueRealtimeRuntime } from './data/realtimeRuntime';
import type { QueueHistoryPort, QueueResultDestinationPort, QueueRuntime } from './runtime';
-import type { QueueNodeExecutionPort } from './runtime/coordinator';
+import type { QueueModelLoadPort, QueueNodeExecutionPort } from './runtime/coordinator';
import { queueBackend } from './data/httpRealtimeQueueBackend';
import { queueReadModelOptions } from './data/queries';
@@ -44,11 +44,20 @@ export const createProductionQueueRuntime = ({
destinations,
ensureTemplatesLoaded,
history,
+ modelLoads,
nodeExecution,
}: {
destinations: QueueResultDestinationPort;
ensureTemplatesLoaded: () => void;
history: QueueHistoryPort;
+ modelLoads: QueueModelLoadPort;
nodeExecution: QueueNodeExecutionPort;
}): QueueRuntime =>
- createQueueRuntime({ backend: queueBackend, destinations, ensureTemplatesLoaded, history, nodeExecution });
+ createQueueRuntime({
+ backend: queueBackend,
+ destinations,
+ ensureTemplatesLoaded,
+ history,
+ modelLoads,
+ nodeExecution,
+ });
diff --git a/invokeai/frontend/webv2/src/features/queue/runtime.test.ts b/invokeai/frontend/webv2/src/features/queue/runtime.test.ts
index 292a902e52..67018ecfd4 100644
--- a/invokeai/frontend/webv2/src/features/queue/runtime.test.ts
+++ b/invokeai/frontend/webv2/src/features/queue/runtime.test.ts
@@ -98,6 +98,11 @@ describe('queue runtime', () => {
getSnapshot: () => ({ connectionStatus: 'connected', isHydrated: true, projects: [project] }),
subscribe: vi.fn(() => vi.fn()),
},
+ modelLoads: {
+ completed: vi.fn(),
+ reset: vi.fn(),
+ started: vi.fn(),
+ },
nodeExecution: {
clearAll: vi.fn(),
completed: vi.fn(),
diff --git a/invokeai/frontend/webv2/src/features/queue/runtime.ts b/invokeai/frontend/webv2/src/features/queue/runtime.ts
index 19a25d0257..9fbff1a953 100644
--- a/invokeai/frontend/webv2/src/features/queue/runtime.ts
+++ b/invokeai/frontend/webv2/src/features/queue/runtime.ts
@@ -13,6 +13,7 @@ import {
createQueueCoordinator,
QueueItemCancelledError,
type QueueCoordinator,
+ type QueueModelLoadPort,
type QueueNodeExecutionPort,
type ReconcileInput,
} from '@features/queue/runtime/coordinator';
@@ -130,12 +131,14 @@ export const createQueueRuntime = ({
destinations,
ensureTemplatesLoaded,
history,
+ modelLoads,
nodeExecution,
}: {
backend: QueueBackendPort;
destinations: QueueResultDestinationPort;
ensureTemplatesLoaded: () => void;
history: QueueHistoryPort;
+ modelLoads: QueueModelLoadPort;
nodeExecution: QueueNodeExecutionPort;
}): QueueRuntime => {
const commands = history.commands;
@@ -280,7 +283,7 @@ export const createQueueRuntime = ({
},
onGalleryRefresh: commands.refreshBackendData,
},
- { backend, nodeExecution }
+ { backend, modelLoads, nodeExecution }
);
const submitQueueItem = (project: QueueHistoryProject, queueItem: QueueItem): void => {
diff --git a/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.test.ts b/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.test.ts
index b5f5d05edb..3fdd55cc3f 100644
--- a/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.test.ts
+++ b/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.test.ts
@@ -21,6 +21,7 @@ import {
type QueueCoordinator,
type QueueCoordinatorBackendPort,
type QueueCoordinatorCallbacks,
+ type QueueModelLoadPort,
type QueueNodeExecutionPort,
} from './coordinator';
@@ -115,6 +116,7 @@ interface Harness {
callbacks: { [Key in keyof QueueCoordinatorCallbacks]: ReturnType<typeof vi.fn> };
coordinator: QueueCoordinator;
hub: ReturnType<typeof createSocketHub>;
+ modelLoads: { [Key in keyof QueueModelLoadPort]: ReturnType<typeof vi.fn> };
nodeExecution: { [Key in keyof QueueNodeExecutionPort]: ReturnType<typeof vi.fn> };
progressImage: { clear: ReturnType<typeof vi.fn>; set: ReturnType<typeof vi.fn> };
progressEntries: Map<string, QueueItemProgress>;
@@ -146,6 +148,11 @@ const createHarness = (options: { galleryRefreshCoalesceMs?: number } = {}): Har
const callbacks = {
onGalleryRefresh: vi.fn(),
};
+ const modelLoads = {
+ completed: vi.fn(),
+ reset: vi.fn(),
+ started: vi.fn(),
+ };
const nodeExecution = {
clearAll: vi.fn(),
completed: vi.fn(),
@@ -167,12 +174,13 @@ const createHarness = (options: { galleryRefreshCoalesceMs?: number } = {}): Har
onConnectionChange: hub.onConnectionChange,
},
galleryRefreshCoalesceMs: options.galleryRefreshCoalesceMs ?? 1,
+ modelLoads,
nodeExecution,
progress,
progressImage,
});
- return { api, callbacks, coordinator, hub, nodeExecution, progressImage, progressEntries, socket };
+ return { api, callbacks, coordinator, hub, modelLoads, nodeExecution, progressImage, progressEntries, socket };
};
describe('queueCoordinator', () => {
@@ -473,6 +481,25 @@ describe('queueCoordinator', () => {
);
});
+ it('routes model load socket events to the model-load port', () => {
+ harness.coordinator.connect();
+
+ harness.socket.fire('model_load_started', { config: { name: 'model-a' } });
+ harness.socket.fire('model_load_complete', { config: { name: 'model-a' } });
+
+ expect(harness.modelLoads.started).toHaveBeenCalledWith({ config: { name: 'model-a' } });
+ expect(harness.modelLoads.completed).toHaveBeenCalledWith({ config: { name: 'model-a' } });
+ });
+
+ it('resets model-load activity when the connection status changes', () => {
+ harness.coordinator.connect();
+ harness.modelLoads.reset.mockClear();
+
+ harness.hub.disconnect();
+
+ expect(harness.modelLoads.reset).toHaveBeenCalled();
+ });
+
it('ignores untracked queue events before mutating local execution state', () => {
vi.useFakeTimers();
harness = createHarness({ galleryRefreshCoalesceMs: 400 });
diff --git a/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.ts b/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.ts
index 14556e4c71..2b7706cd3c 100644
--- a/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.ts
+++ b/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.ts
@@ -10,7 +10,6 @@ import type {
} from '@features/queue/core/types';
import type { BackendConnectionStatus } from '@platform/transport/types';
-import { modelLoadActivitySink } from '@features/models';
import {
isTerminalBackendStatus,
parseQueueItemOrigin,
@@ -33,6 +32,17 @@ const GALLERY_REFRESH_COALESCE_MS = 400;
const SAFETY_SWEEP_INTERVAL_MS = 30_000;
const TERMINAL_EVENT_BUFFER_LIMIT = 256;
+/**
+ * Queue's view of model-load activity derived from socket events. The
+ * production adapter is Models' modelLoadActivitySink, injected by the App
+ * composition root (see app/QueueRuntimeAdapter); tests inject a double.
+ */
+export interface QueueModelLoadPort {
+ completed(payload: unknown): void;
+ reset(): void;
+ started(payload: unknown): void;
+}
+
export interface QueueNodeExecutionPort {
clearAll(): void;
completed(event: InvocationCompleteEvent): void;
@@ -166,6 +176,7 @@ export const createQueueCoordinator = (
/** One adapter owns Queue HTTP commands and realtime events. */
backend: QueueCoordinatorBackendPort;
galleryRefreshCoalesceMs?: number;
+ modelLoads: QueueModelLoadPort;
nodeExecution: QueueNodeExecutionPort;
progress?: QueueItemProgressSink;
progressImage?: ProgressImageSink;
@@ -175,6 +186,7 @@ export const createQueueCoordinator = (
): QueueCoordinator => {
const backend = options.backend;
const progress = options.progress ?? queueItemProgressStore;
+ const modelLoads = options.modelLoads;
const nodeExecution = options.nodeExecution;
const progressImage = options.progressImage ?? progressImageStore;
const revealHold = options.revealHold ?? revealHoldStore;
@@ -436,7 +448,7 @@ export const createQueueCoordinator = (
const handleConnectionChange = (status: BackendConnectionStatus): void => {
progress.clearAll?.();
nodeExecution.clearAll();
- modelLoadActivitySink.reset();
+ modelLoads.reset();
if (status === 'connected') {
scheduleGalleryRefresh();
@@ -477,10 +489,10 @@ export const createQueueCoordinator = (
nodeExecution.failed(event);
}),
backend.on('model_load_started', (payload: never) => {
- modelLoadActivitySink.started(payload);
+ modelLoads.started(payload);
}),
backend.on('model_load_complete', (payload: never) => {
- modelLoadActivitySink.completed(payload);
+ modelLoads.completed(payload);
})
);