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(queue): track progress through preparation and completion

Josh Corbettfa81536d40dc
diff --git a/invokeai/frontend/webv2/src/features/queue/contracts.ts b/invokeai/frontend/webv2/src/features/queue/contracts.ts
index a6e3de5196..3fe3c39c06 100644
--- a/invokeai/frontend/webv2/src/features/queue/contracts.ts
+++ b/invokeai/frontend/webv2/src/features/queue/contracts.ts
@@ -75,3 +75,12 @@ export {
type InvocationStartedEvent,
type QueueItemStatusChangedEvent,
} from './data/events';
+
+export {
+ getQueueActiveSessions,
+ getQueueProgressSessions,
+ isGalleryProgressItem,
+ type QueueActiveSession,
+ type QueueProgressSession,
+} from './core/activeSessions';
+export type { QueueItemProgressTarget } from './core/types';
diff --git a/invokeai/frontend/webv2/src/features/queue/core/activeSessions.test.ts b/invokeai/frontend/webv2/src/features/queue/core/activeSessions.test.ts
new file mode 100644
index 0000000000..f6e685c67f
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/queue/core/activeSessions.test.ts
@@ -0,0 +1,88 @@
+import { describe, expect, it } from 'vitest';
+
+import type { QueueItem } from './historyTypes';
+
+import { getQueueActiveSessions, getQueueProgressSessions, isGalleryProgressItem } from './activeSessions';
+
+const run = (id: string, backendItemIds: number[], destination: 'gallery' | 'canvas' = 'gallery'): QueueItem => ({
+ id,
+ backendItemIds,
+ status: 'running',
+ cancellable: true,
+ snapshot: {
+ backendSubmission: { kind: 'invalid', error: 'not submitted by this test' },
+ destination,
+ filterIntermediateResults: true,
+ galleryBoardId: 'destination-board',
+ graph: { id: 'workflow', label: 'Multi-output workflow' },
+ presentation: { batchCount: 3, width: 512, height: 768 },
+ sourceId: 'workflow',
+ submittedAt: '2026-09-15T00:00:00Z',
+ },
+});
+const target = (queueItemId: string, itemIndex: number) => ({ queueItemId, itemIndex });
+
+describe('active queue sessions', () => {
+ it('shows executing sessions in backend order, without reserving pending slots or workflow outputs', () => {
+ const items = [run('newer', [20]), run('batch', [10, 11, 12])];
+ const targets = [target('newer', 1), target('batch', 2), target('batch', 1)];
+ expect(
+ getQueueActiveSessions(items, targets, targets).map(({ id, backendItemId, state }) => ({
+ id,
+ backendItemId,
+ state,
+ }))
+ ).toEqual([
+ { id: 'batch:1', backendItemId: 10, state: 'running' },
+ { id: 'batch:2', backendItemId: 11, state: 'running' },
+ { id: 'newer:1', backendItemId: 20, state: 'running' },
+ ]);
+ });
+ it('keeps a settling session until routing releases it, even after its result enters project state', () => {
+ const item = { ...run('batch', [10, 11]), completedBackendItemIds: [10] };
+ expect(
+ getQueueActiveSessions([item], [target('batch', 2)], [target('batch', 1), target('batch', 2)])
+ ).toMatchObject([
+ { id: 'batch:1', state: 'settling' },
+ { id: 'batch:2', state: 'running' },
+ ]);
+ expect(getQueueActiveSessions([item], [], [])).toEqual([]);
+ });
+ it('filters destinations through presentation policy and rejects targets outside the project or batch', () => {
+ const items = [run('gallery', [10]), run('canvas', [11], 'canvas')];
+ const targets = [target('gallery', 1), target('canvas', 1), target('other-project', 1), target('gallery', 2)];
+ expect(
+ getQueueActiveSessions(items.filter(isGalleryProgressItem), targets, targets).map((session) => session.id)
+ ).toEqual(['gallery:1']);
+ expect(getQueueActiveSessions(items, targets, targets).map((session) => session.id)).toEqual([
+ 'gallery:1',
+ 'canvas:1',
+ ]);
+ });
+});
+
+describe('gallery batch progress slots', () => {
+ it('shows all three slots before submission and keeps their identities as execution starts', () => {
+ const pending = { ...run('batch', []), backendItemIds: undefined, status: 'pending' as const };
+ const queued = getQueueProgressSessions([pending], []);
+ expect(queued.map(({ id, state }) => ({ id, state }))).toEqual([
+ { id: 'batch:1', state: 'queued' },
+ { id: 'batch:2', state: 'queued' },
+ { id: 'batch:3', state: 'queued' },
+ ]);
+ const item = run('batch', [10, 11, 12]);
+ const targets = [target('batch', 1)];
+ const sessions = getQueueProgressSessions([item], getQueueActiveSessions([item], targets, targets));
+ expect(sessions.map(({ id }) => id)).toEqual(queued.map(({ id }) => id));
+ expect(sessions.map(({ state }) => state)).toEqual(['running', 'queued', 'queued']);
+ });
+ it('removes completed and cancelled slots, retaining only a settling frame until release', () => {
+ const item = { ...run('batch', [10, 11, 12]), completedBackendItemIds: [10], cancelledBackendItemIds: [12] };
+ expect(getQueueProgressSessions([item], []).map(({ id }) => id)).toEqual(['batch:2']);
+ const active = getQueueActiveSessions([item], [], [target('batch', 1)]);
+ expect(getQueueProgressSessions([item], active).map(({ state }) => state)).toEqual(['settling', 'queued']);
+ for (const status of ['completed', 'cancelled', 'failed'] as const) {
+ expect(getQueueProgressSessions([{ ...item, status }], [])).toEqual([]);
+ }
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/queue/core/activeSessions.ts b/invokeai/frontend/webv2/src/features/queue/core/activeSessions.ts
new file mode 100644
index 0000000000..793be2012b
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/queue/core/activeSessions.ts
@@ -0,0 +1,102 @@
+import type { QueueItem } from './historyTypes';
+import type { QueueItemProgressTarget, QueueSourceId } from './types';
+
+import { getQueueItemSnapshotBatchCount, getQueueItemSnapshotDimensions } from './historySnapshot';
+
+/** A running backend session, not a prediction of its output images or boards. */
+export interface QueueActiveSession extends QueueItemProgressTarget {
+ id: string;
+ backendItemId: number;
+ label: string;
+ sourceId: QueueSourceId;
+ width: number;
+ height: number;
+ itemCount: number;
+ state: 'running' | 'settling';
+}
+
+/** Presentation policy shared by Gallery and Preview; discovery remains destination-neutral. */
+export const isGalleryProgressItem = (item: QueueItem): boolean => item.snapshot.destination === 'gallery';
+
+export const getQueueActiveSessions = (
+ items: readonly QueueItem[],
+ running: readonly QueueItemProgressTarget[],
+ followed: readonly QueueItemProgressTarget[]
+): QueueActiveSession[] => {
+ const itemsById = new Map(items.map((item) => [item.id, item]));
+ const runningKeys = new Set(running.map((target) => `${target.queueItemId}:${target.itemIndex}`));
+ const sessions: QueueActiveSession[] = [];
+
+ for (const target of followed) {
+ const item = itemsById.get(target.queueItemId);
+ const backendItemId = item?.backendItemIds?.[target.itemIndex - 1];
+ if (!item || backendItemId === undefined) {
+ continue;
+ }
+ const id = `${target.queueItemId}:${target.itemIndex}`;
+ sessions.push({
+ ...target,
+ ...getQueueItemSnapshotDimensions(item, { width: 1024, height: 1024 }),
+ id,
+ backendItemId,
+ label: item.snapshot.graph.label,
+ sourceId: item.snapshot.sourceId,
+ itemCount: item.backendItemIds!.length,
+ state: runningKeys.has(id) ? 'running' : 'settling',
+ });
+ }
+
+ return sessions.sort((left, right) => left.backendItemId - right.backendItemId);
+};
+
+/** A tile per unfinished batch slot, including work waiting to start. */
+export type QueueProgressSession = Omit<QueueActiveSession, 'backendItemId' | 'state'> & {
+ backendItemId: number | null;
+ state: 'queued' | 'running' | 'settling';
+};
+
+export const getQueueProgressSessions = (
+ items: readonly QueueItem[],
+ active: readonly QueueActiveSession[]
+): QueueProgressSession[] => {
+ const sessions: QueueProgressSession[] = [...active];
+ const activeIds = new Set(active.map((session) => session.id));
+ for (const item of items) {
+ if (item.status !== 'pending' && item.status !== 'running') {
+ continue;
+ }
+ const finished = new Set([...(item.completedBackendItemIds ?? []), ...(item.cancelledBackendItemIds ?? [])]);
+ const itemCount = item.backendItemIds?.length ?? getQueueItemSnapshotBatchCount(item);
+ for (let index = 0; index < itemCount; index += 1) {
+ const id = `${item.id}:${index + 1}`;
+ const backendItemId = item.backendItemIds?.[index] ?? null;
+ if (activeIds.has(id) || (backendItemId !== null && finished.has(backendItemId))) {
+ continue;
+ }
+ sessions.push({
+ ...getQueueItemSnapshotDimensions(item, { width: 1024, height: 1024 }),
+ id,
+ queueItemId: item.id,
+ itemIndex: index + 1,
+ backendItemId,
+ label: item.snapshot.graph.label,
+ sourceId: item.snapshot.sourceId,
+ itemCount,
+ state: 'queued',
+ });
+ }
+ }
+ const itemOrder = new Map(items.map((item, index) => [item.id, index]));
+ return sessions.sort((left, right) => {
+ if (left.backendItemId !== null && right.backendItemId !== null) {
+ return left.backendItemId - right.backendItemId;
+ }
+ if (left.backendItemId !== null) {
+ return -1;
+ }
+ if (right.backendItemId !== null) {
+ return 1;
+ }
+ return itemOrder.get(left.queueItemId)! - itemOrder.get(right.queueItemId)! || left.itemIndex - right.itemIndex;
+ });
+};
diff --git a/invokeai/frontend/webv2/src/features/queue/data/progressImageStore.test.ts b/invokeai/frontend/webv2/src/features/queue/data/progressImageStore.test.ts
index 724101b83a..4f60c83945 100644
--- a/invokeai/frontend/webv2/src/features/queue/data/progressImageStore.test.ts
+++ b/invokeai/frontend/webv2/src/features/queue/data/progressImageStore.test.ts
@@ -44,7 +44,7 @@ describe('progressImageStore held frames', () => {
expect(getQueueItemSwapProgressImage('queue-1', 'result.png')).toBeNull();
});
- it('paints the swap frame only over the images its backend item delivered', () => {
+ it('bridges only to the last selected output of a multi-output workflow', () => {
// Item 3 of a batch finishing must not put its denoise frame over item 1's
// image when the user clicks that one, nor over anything before routing
// has said which images the frame belongs to.
@@ -55,7 +55,7 @@ describe('progressImageStore held frames', () => {
progressImageStore.bindSwapImages('queue-1', ['image-3.png', 'image-3-control.png']);
- expect(getQueueItemSwapProgressImage('queue-1', 'image-3.png')).toEqual(frame('third'));
+ expect(getQueueItemSwapProgressImage('queue-1', 'image-3.png')).toBeNull();
expect(getQueueItemSwapProgressImage('queue-1', 'image-3-control.png')).toEqual(frame('third'));
expect(getQueueItemSwapProgressImage('queue-1', 'image-1.png')).toBeNull();
diff --git a/invokeai/frontend/webv2/src/features/queue/data/progressImageStore.ts b/invokeai/frontend/webv2/src/features/queue/data/progressImageStore.ts
index dd1d68915d..11b4acea9f 100644
--- a/invokeai/frontend/webv2/src/features/queue/data/progressImageStore.ts
+++ b/invokeai/frontend/webv2/src/features/queue/data/progressImageStore.ts
@@ -150,7 +150,9 @@ export const progressImageStore = {
const entry = swapFrames.get(queueItemId);
if (entry) {
- swapFrames.set(queueItemId, { image: entry.image, imageNames });
+ // A workflow may save unrelated images in several boards. One preview
+ // bridges only to the latest output selected by result routing while it decodes.
+ swapFrames.set(queueItemId, { image: entry.image, imageNames: imageNames.slice(-1) });
}
},
/** Forget a queue item's held frames: its run is gone (detached or canceled). */
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 e14911e021..a8bba83ee6 100644
--- a/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.test.ts
+++ b/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.test.ts
@@ -777,6 +777,39 @@ describe('queueCoordinator', () => {
);
});
+ it('publishes a running session before its first preview and on reconnect', async () => {
+ harness.coordinator.connect();
+ await harness.coordinator.submitGenerate('local-1', generateRequest);
+ expect(harness.activeProgressTarget.set).not.toHaveBeenCalled();
+ harness.socket.fire('queue_item_status_changed', createStatusEvent({ item_id: 1, status: 'in_progress' }));
+ expect(harness.activeProgressTarget.set).toHaveBeenCalledWith({ itemIndex: 1, queueItemId: 'local-1' });
+ expect(harness.progressImage.set).not.toHaveBeenCalled();
+ harness.activeProgressTarget.set.mockClear();
+ harness.api.getItem.mockResolvedValue(
+ createQueueBackendItem({ id: 1, origin: buildQueueItemOrigin('local-1', 'project-1'), status: 'in_progress' })
+ );
+ await harness.coordinator.reconcile([
+ { backendItemIds: [1], id: 'local-1', projectId: 'project-1', status: 'running' },
+ ]);
+ expect(harness.activeProgressTarget.set).toHaveBeenCalledWith({ itemIndex: 1, queueItemId: 'local-1' });
+ });
+
+ it.each(['pending', 'waiting'] as const)(
+ 'clears a running session when reconciliation discovers a missed %s transition',
+ async (status) => {
+ harness.coordinator.connect();
+ await harness.coordinator.submitGenerate('local-1', generateRequest);
+ harness.socket.fire('queue_item_status_changed', createStatusEvent({ item_id: 1, status: 'in_progress' }));
+ harness.api.getItem.mockResolvedValue(
+ createQueueBackendItem({ id: 1, origin: buildQueueItemOrigin('local-1', 'project-1'), status })
+ );
+ await harness.coordinator.reconcile([
+ { backendItemIds: [1], id: 'local-1', projectId: 'project-1', status: 'running' },
+ ]);
+ expect(harness.activeProgressTarget.clear).toHaveBeenCalledWith({ itemIndex: 1, queueItemId: 'local-1' });
+ }
+ );
+
it('reopens the revision gate when an item goes back to waiting', async () => {
harness.coordinator.connect();
await harness.coordinator.submitGenerate('local-1', generateRequest);
diff --git a/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.ts b/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.ts
index e3ca731b20..f0f6cf7c59 100644
--- a/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.ts
+++ b/invokeai/frontend/webv2/src/features/queue/runtime/coordinator.ts
@@ -381,9 +381,14 @@ export const createQueueCoordinator = (
trackNodeForItem(nodeEvent.event.item_id, nodeEvent.event.invocation_source_id, nodeEvent.sequence);
switch (nodeEvent.kind) {
- case 'started':
+ case 'started': {
+ const wait = waits.get(nodeEvent.event.item_id);
+ if (wait) {
+ activeProgressTarget.set(getProgressImageTarget(wait.localQueueItemId, nodeEvent.event.item_id));
+ }
nodeExecution.started(nodeEvent.event);
return;
+ }
case 'completed':
nodeExecution.completed(nodeEvent.event);
return;
@@ -518,6 +523,15 @@ export const createQueueCoordinator = (
};
const settleFromQueueItem = (queueItem: QueueBackendItem): void => {
+ const wait = waits.get(queueItem.id);
+ if (wait) {
+ const target = getProgressImageTarget(wait.localQueueItemId, queueItem.id);
+ if (queueItem.status === 'in_progress') {
+ activeProgressTarget.set(target);
+ } else if (queueItem.status === 'pending' || queueItem.status === 'waiting') {
+ activeProgressTarget.clear(target);
+ }
+ }
if (isTerminalBackendStatus(queueItem.status)) {
settleWait(queueItem.id, toTerminalOutcome(queueItem.status, queueItem.errorMessage, queueItem.errorType));
}
@@ -526,10 +540,10 @@ export const createQueueCoordinator = (
const isTrackedEvent = (event: { item_id: number }): boolean => waits.has(event.item_id);
const trackBackendItem = (localQueueItemId: string, backendItemId: number): Promise<TerminalOutcome> => {
- replayNodeEvents(backendItemId);
const bufferedOutcome = recentTerminalOutcomes.get(backendItemId);
if (bufferedOutcome) {
+ replayNodeEvents(backendItemId);
recentTerminalOutcomes.delete(backendItemId);
settleNodes(backendItemId, bufferedOutcome.status);
@@ -538,6 +552,7 @@ export const createQueueCoordinator = (
return new Promise<TerminalOutcome>((settle) => {
waits.set(backendItemId, { localQueueItemId, settle });
+ replayNodeEvents(backendItemId);
});
};
@@ -546,12 +561,6 @@ export const createQueueCoordinator = (
throw new QueueItemCancelledError(localQueueItemId);
}
- runs.set(localQueueItemId, {
- backendBatchId,
- backendItemIds,
- outcomePromises: backendItemIds.map((backendItemId) => trackBackendItem(localQueueItemId, backendItemId)),
- });
-
runProgress.set(localQueueItemId, {
backendItemIds,
cancelledBackendItemIds: new Set(),
@@ -559,6 +568,12 @@ export const createQueueCoordinator = (
message: '',
percentage: null,
});
+ runs.set(localQueueItemId, {
+ backendBatchId,
+ backendItemIds,
+ outcomePromises: backendItemIds.map((backendItemId) => trackBackendItem(localQueueItemId, backendItemId)),
+ });
+
publishRunProgress(localQueueItemId);
};
@@ -656,10 +671,17 @@ export const createQueueCoordinator = (
}
if (!isTerminalBackendStatus(event.status)) {
+ const wait = waits.get(event.item_id);
+ if (wait && event.status === 'in_progress') {
+ activeProgressTarget.set(getProgressImageTarget(wait.localQueueItemId, event.item_id));
+ }
// Back to the queue (a workflow-call parent waiting on its child, a retry):
// whatever frames follow belong to a new leg, and after a backend restart
// their revisions start over.
if (event.status === 'pending' || event.status === 'waiting') {
+ if (wait) {
+ activeProgressTarget.clear(getProgressImageTarget(wait.localQueueItemId, event.item_id));
+ }
latestFrameGates.delete(event.item_id);
}