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(gallery): sort recent generations by backend creation time

Josh Corbett61579779c3f4
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/items.test.ts b/invokeai/frontend/webv2/src/features/gallery/core/items.test.ts
index 08b8d88d45..b2f82ad943 100644
--- a/invokeai/frontend/webv2/src/features/gallery/core/items.test.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/core/items.test.ts
@@ -128,6 +128,16 @@ describe('gallery item compatibility', () => {
});
});
+ it('prefers the backend creation timestamp over the submission instant when both are present', () => {
+ const item = legacyGeneratedImageToGalleryItem({
+ ...generatedImage,
+ createdAt: '2026-07-30T12:45:00.000Z',
+ queuedAt: '2026-07-30T12:00:00.000Z',
+ });
+
+ expect(item.createdAt).toBe('2026-07-30T12:45:00.000Z');
+ });
+
it('round-trips an image item into the legacy gallery contract with a backend source fallback', () => {
const item: GalleryImageItem = {
boardId: 'board-1',
@@ -145,6 +155,7 @@ describe('gallery item compatibility', () => {
const expected: GalleryImage = {
boardId: 'board-1',
+ createdAt: '2026-07-30T12:00:00.000Z',
height: 512,
imageCategory: 'user',
imageName: 'upload.png',
@@ -157,6 +168,10 @@ describe('gallery item compatibility', () => {
};
expect(galleryImageItemToGalleryImage(item)).toEqual(expected);
+ expect(legacyGeneratedImageToGalleryItem(galleryImageItemToGalleryImage(item))).toEqual({
+ ...item,
+ sourceQueueItemId: 'backend-gallery',
+ });
});
});
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/items.ts b/invokeai/frontend/webv2/src/features/gallery/core/items.ts
index c63a37a981..d02a867a34 100644
--- a/invokeai/frontend/webv2/src/features/gallery/core/items.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/core/items.ts
@@ -126,7 +126,7 @@ type LegacyGalleryImage = GeneratedImageContract & Partial<Pick<GalleryImage, 'b
export const legacyGeneratedImageToGalleryItem = (image: LegacyGalleryImage): GalleryImageItem => ({
boardId: image.boardId ?? 'none',
category: image.imageCategory ?? 'general',
- createdAt: image.queuedAt,
+ createdAt: image.createdAt ?? image.queuedAt,
fullUrl: image.imageUrl,
height: image.height,
isIntermediate: false,
@@ -140,6 +140,7 @@ export const legacyGeneratedImageToGalleryItem = (image: LegacyGalleryImage): Ga
export const galleryImageItemToGalleryImage = (item: GalleryImageItem): GalleryImage => ({
boardId: item.boardId,
+ createdAt: item.createdAt,
height: item.height,
imageCategory: item.category,
imageName: item.name,
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/recentImages.ts b/invokeai/frontend/webv2/src/features/gallery/core/recentImages.ts
index 638b3fbaca..697d9d8627 100644
--- a/invokeai/frontend/webv2/src/features/gallery/core/recentImages.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/core/recentImages.ts
@@ -10,6 +10,7 @@ const isGeneratedImageContract = (value: unknown): value is GeneratedImageContra
const image = value as Partial<GeneratedImageContract>;
return (
+ (image.createdAt === undefined || typeof image.createdAt === 'string') &&
typeof image.height === 'number' &&
Number.isFinite(image.height) &&
typeof image.imageName === 'string' &&
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/selection.test.ts b/invokeai/frontend/webv2/src/features/gallery/core/selection.test.ts
index 9a34f5248e..ca9ef3b0fd 100644
--- a/invokeai/frontend/webv2/src/features/gallery/core/selection.test.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/core/selection.test.ts
@@ -113,6 +113,7 @@ describe('persisted Gallery selection readers', () => {
it('converts a canonical image item for legacy image-only consumers', () => {
expect(selection.getSelectedGalleryImageFromValues({ selectedImage: imageItem })).toEqual({
boardId: imageItem.boardId,
+ createdAt: imageItem.createdAt,
height: imageItem.height,
imageCategory: imageItem.category,
imageName: imageItem.name,
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/types.ts b/invokeai/frontend/webv2/src/features/gallery/core/types.ts
index c0856c6b70..3a916aa4fb 100644
--- a/invokeai/frontend/webv2/src/features/gallery/core/types.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/core/types.ts
@@ -1,4 +1,10 @@
export interface GeneratedImageContract {
+ /**
+ * Backend creation timestamp, when known. `queuedAt` is the submission
+ * instant, which for a queued batch predates creation by the whole wait —
+ * ordering against backend rows must prefer this field.
+ */
+ createdAt?: string;
height: number;
imageName: string;
imageUrl: string;
diff --git a/invokeai/frontend/webv2/src/features/gallery/data/backend.test.ts b/invokeai/frontend/webv2/src/features/gallery/data/backend.test.ts
index 9a166654b4..c14c839752 100644
--- a/invokeai/frontend/webv2/src/features/gallery/data/backend.test.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/data/backend.test.ts
@@ -181,6 +181,7 @@ describe('getGalleryImageByName', () => {
await expect(getGalleryImageByName('folder/workflow result.png', controller.signal)).resolves.toEqual({
boardId: 'board-1',
+ createdAt: '2026-07-09T12:00:00.000Z',
height: 360,
imageCategory: 'general',
imageName: 'folder/workflow result.png',
diff --git a/invokeai/frontend/webv2/src/features/gallery/data/backend.ts b/invokeai/frontend/webv2/src/features/gallery/data/backend.ts
index a60ef02c90..9c470b8953 100644
--- a/invokeai/frontend/webv2/src/features/gallery/data/backend.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/data/backend.ts
@@ -262,6 +262,7 @@ const getGalleryVideoTotal = async ({
const mapImage = (image: BackendImageDTO): GalleryImage => ({
boardId: image.board_id ?? 'none',
+ createdAt: image.created_at,
height: image.height,
imageCategory: image.image_category,
imageName: image.image_name,
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/useGalleryData.test.ts b/invokeai/frontend/webv2/src/features/gallery/ui/useGalleryData.test.ts
index 362cde54a0..6e8983d308 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/useGalleryData.test.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/useGalleryData.test.ts
@@ -30,6 +30,20 @@ const filter = {
starredFirst: false,
};
+const createBackendItem = (name: string, createdAt: string): GalleryItem => ({
+ boardId: 'none',
+ category: 'general',
+ createdAt,
+ fullUrl: `/images/${name}`,
+ height: 512,
+ isIntermediate: false,
+ kind: 'image',
+ name,
+ starred: false,
+ thumbnailUrl: `/thumbnails/${name}`,
+ width: 512,
+});
+
describe('mergeGalleryItemWindow', () => {
it('deduplicates by qualified key and mirrors server starred/time/kind/name ordering', () => {
const image = {
@@ -79,20 +93,9 @@ describe('mergeGalleryItemWindow', () => {
});
it('bounds the optimistic image overlay and the mixed rendered window', () => {
- const createItem = (index: number): GalleryItem => ({
- boardId: 'none',
- category: 'general',
- createdAt: new Date(Date.UTC(2026, 0, 1, 0, 0, index)).toISOString(),
- fullUrl: `/images/${index}`,
- height: 512,
- isIntermediate: false,
- kind: 'image',
- name: `image-${index}.png`,
- starred: false,
- thumbnailUrl: `/thumbnails/${index}`,
- width: 512,
- });
- const backendItems = Array.from({ length: 600 }, (_, index) => createItem(index));
+ const backendItems = Array.from({ length: 600 }, (_, index) =>
+ createBackendItem(`image-${index}.png`, new Date(Date.UTC(2026, 0, 1, 0, 0, index)).toISOString())
+ );
const recentImages = getBoundedRecentImages(
Array.from({ length: 1_000 }, (_, index) => asGenerated(createImage(1_000 + index)))
);
@@ -104,24 +107,11 @@ describe('mergeGalleryItemWindow', () => {
});
it('preserves backend relevance order and skips the recent overlay while a semantic query is active', () => {
- const createRankedItem = (name: string, createdAt: string): GalleryItem => ({
- boardId: 'none',
- category: 'general',
- createdAt,
- fullUrl: `/images/${name}`,
- height: 64,
- isIntermediate: false,
- kind: 'image',
- name,
- starred: false,
- thumbnailUrl: `/thumbnails/${name}`,
- width: 64,
- });
// Deliberately out of chronological order: relevance is the order.
const rankedItems = [
- createRankedItem('oldest.png', '2026-01-01T00:00:00.000Z'),
- createRankedItem('newest.png', '2026-07-01T00:00:00.000Z'),
- createRankedItem('middle.png', '2026-03-01T00:00:00.000Z'),
+ createBackendItem('oldest.png', '2026-01-01T00:00:00.000Z'),
+ createBackendItem('newest.png', '2026-07-01T00:00:00.000Z'),
+ createBackendItem('middle.png', '2026-03-01T00:00:00.000Z'),
];
expect(
@@ -139,19 +129,6 @@ describe('mergeGalleryItemWindow', () => {
// overlay carries the queue's ISO `submittedAt`. The overlaid recent is
// older than every loaded backend row and absent from the window: with
// Newest first it must sort below them, not above them.
- const createBackendItem = (name: string, createdAt: string): GalleryItem => ({
- boardId: 'none',
- category: 'general',
- createdAt,
- fullUrl: `/images/${name}`,
- height: 512,
- isIntermediate: false,
- kind: 'image',
- name,
- starred: false,
- thumbnailUrl: `/thumbnails/${name}`,
- width: 512,
- });
const backendItems = [
createBackendItem('newer.png', '2026-08-29 13:01:20.649'),
createBackendItem('middle.png', '2026-08-29 12:00:00.000'),
@@ -179,21 +156,34 @@ describe('mergeGalleryItemWindow', () => {
).toEqual(['older.png', 'middle.png', 'newer.png']);
});
+ it('places a completed batch image by its creation time, not the batch submission time', () => {
+ // A batch is submitted at T0 and its images complete one by one much
+ // later. Each overlaid recent must sort by when its image was actually
+ // created, or every fresh completion lands *below* its already-listed
+ // siblings until the backend refetch catches up, reshuffling the grid.
+ const backendItems = [
+ createBackendItem('batch-2.png', '2026-08-29 13:05:00.000'),
+ createBackendItem('batch-1.png', '2026-08-29 13:04:00.000'),
+ ];
+ const recentImages = [
+ asGenerated(
+ createImage(3, {
+ createdAt: '2026-08-29T13:06:00.000Z',
+ imageName: 'batch-3.png',
+ queuedAt: '2026-08-29T13:00:00.000Z',
+ })
+ ),
+ ];
+
+ expect(
+ mergeGalleryItemWindow({ backendItems, filter, maxRows: 60, recentImages }).map((item) => item.name)
+ ).toEqual(['batch-3.png', 'batch-2.png', 'batch-1.png']);
+ });
+
it('uses SQLite binary ordering for mixed-case and punctuation name ties in both directions', () => {
- const createTiedItem = (name: string): GalleryItem => ({
- boardId: 'none',
- category: 'general',
- createdAt: '2026-07-30T12:00:00.000Z',
- fullUrl: `/images/${name}`,
- height: 64,
- isIntermediate: false,
- kind: 'image',
- name,
- starred: false,
- thumbnailUrl: `/thumbnails/${name}`,
- width: 64,
- });
- const items = ['a.png', 'Z.png', '_draft.png', 'A.png', '!bang.png'].map(createTiedItem);
+ const items = ['a.png', 'Z.png', '_draft.png', 'A.png', '!bang.png'].map((name) =>
+ createBackendItem(name, '2026-07-30T12:00:00.000Z')
+ );
expect(
mergeGalleryItemWindow({
diff --git a/invokeai/frontend/webv2/src/features/queue/core/types.ts b/invokeai/frontend/webv2/src/features/queue/core/types.ts
index 63481e1655..628198de50 100644
--- a/invokeai/frontend/webv2/src/features/queue/core/types.ts
+++ b/invokeai/frontend/webv2/src/features/queue/core/types.ts
@@ -109,6 +109,8 @@ export interface QueueBackendItem {
}
export interface QueueResultImage {
+ /** Backend creation timestamp; `queuedAt` is the submission instant. */
+ createdAt?: string;
height: number;
imageName: string;
imageUrl: string;
diff --git a/invokeai/frontend/webv2/src/features/queue/data/serverTypes.ts b/invokeai/frontend/webv2/src/features/queue/data/serverTypes.ts
index 0bec27b6ea..eda6b43f45 100644
--- a/invokeai/frontend/webv2/src/features/queue/data/serverTypes.ts
+++ b/invokeai/frontend/webv2/src/features/queue/data/serverTypes.ts
@@ -35,6 +35,7 @@ export interface QueueServerItemDTO {
}
export interface QueueImageDTO {
+ created_at: string;
height: number;
image_name: string;
image_url: string;
diff --git a/invokeai/frontend/webv2/src/features/queue/data/submissionApi.test.ts b/invokeai/frontend/webv2/src/features/queue/data/submissionApi.test.ts
index 7349524797..3fe0ec864d 100644
--- a/invokeai/frontend/webv2/src/features/queue/data/submissionApi.test.ts
+++ b/invokeai/frontend/webv2/src/features/queue/data/submissionApi.test.ts
@@ -284,6 +284,40 @@ describe('getResultImages', () => {
expect(images.map((image) => image.imageName)).toEqual(['external-a.png', 'external-b.png']);
});
+ it('carries the backend creation timestamp, normalized to ISO, alongside the submission instant', async () => {
+ mocks.apiFetchJson.mockImplementation((url: string) => {
+ if (url === '/api/v1/queue/default/i/1') {
+ return Promise.resolve({
+ item_id: 1,
+ session: {
+ results: {
+ output: { image: { image_name: 'result.png' }, type: 'image_output' },
+ },
+ },
+ status: 'completed',
+ });
+ }
+
+ const imageName = decodeURIComponent(url.replace('/api/v1/images/i/', ''));
+ return Promise.resolve({
+ created_at: '2026-06-15 00:12:34.000',
+ height: 768,
+ image_name: imageName,
+ image_url: `/images/${imageName}`,
+ is_intermediate: false,
+ thumbnail_url: `/thumbs/${imageName}`,
+ width: 1024,
+ });
+ });
+
+ const { getResultImages } = await import('./submissionApi');
+
+ const [image] = await getResultImages(1, 'source-1', '2026-06-15T00:00:00.000Z');
+
+ expect(image?.createdAt).toBe('2026-06-15T00:12:34.000Z');
+ expect(image?.queuedAt).toBe('2026-06-15T00:00:00.000Z');
+ });
+
it('does not issue image requests from queue data resolved after an account switch', async () => {
accountLifecycle.activate('user-a');
let resolveItem: ((item: unknown) => void) | undefined;
diff --git a/invokeai/frontend/webv2/src/features/queue/data/submissionApi.ts b/invokeai/frontend/webv2/src/features/queue/data/submissionApi.ts
index f985b34e71..847fc96e98 100644
--- a/invokeai/frontend/webv2/src/features/queue/data/submissionApi.ts
+++ b/invokeai/frontend/webv2/src/features/queue/data/submissionApi.ts
@@ -9,6 +9,7 @@ import type {
import { buildGeneratePromptBatchPlan, sanitizeBatchCount } from '@features/queue/core/promptBatch';
import { assertAccountScopeCurrent, captureAccountScope } from '@platform/state/accountLifecycle';
+import { normalizeServerTimestamp } from '@platform/time/serverTimestamp';
import { absolutizeApiUrl, ApiError, apiFetchJson } from '@platform/transport/http';
import type { QueueImageDTO, QueueServerItemDTO } from './serverTypes';
@@ -141,6 +142,7 @@ const getResultImage = async (
const image = await apiFetchJson<QueueImageDTO>(`/api/v1/images/i/${encodeURIComponent(imageName)}`, { signal });
return {
+ createdAt: normalizeServerTimestamp(image.created_at),
height: image.height,
imageName: image.image_name,
imageUrl: absolutizeApiUrl(image.image_url),
diff --git a/invokeai/frontend/webv2/src/workbench/palette/paletteProviders.ts b/invokeai/frontend/webv2/src/workbench/palette/paletteProviders.ts
index 177e192c4f..ed0300e84a 100644
--- a/invokeai/frontend/webv2/src/workbench/palette/paletteProviders.ts
+++ b/invokeai/frontend/webv2/src/workbench/palette/paletteProviders.ts
@@ -325,7 +325,7 @@ export const createImagesProvider = ({
// Images carry no prompt, so the creation date+time is the title; the time
// keeps same-day results (the common case for 20 recents) distinguishable.
return page.images.map<PaletteEntry>((image) => {
- const createdAt = new Date(normalizeServerTimestamp(image.queuedAt));
+ const createdAt = new Date(normalizeServerTimestamp(image.createdAt ?? image.queuedAt));
return {
group: 'Images',