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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(gallery): sort overlaid recents against backend items by instant
Lincoln Stein476331d6e8ff
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/items.test.ts b/invokeai/frontend/webv2/src/features/gallery/core/items.test.tsindex ba9d1e865a..08b8d88d45 100644--- a/invokeai/frontend/webv2/src/features/gallery/core/items.test.ts+++ b/invokeai/frontend/webv2/src/features/gallery/core/items.test.ts@@ -4,6 +4,7 @@ import type { GalleryImage, GeneratedImageContract } from './types';import {assertNeverGalleryItem,+ compareGalleryItems,formatGalleryVideoDuration,galleryImageItemToGalleryImage,isGalleryImageItem,@@ -186,3 +187,54 @@ describe('gallery video duration formatting', () => {expect(formatGalleryVideoDuration(duration)).toBe(expected);});});++describe('gallery item ordering', () => {+ // The two timestamp shapes the grid mixes: backend rows carry SQLite's+ // `created_at`; overlaid recents carry the queue's ISO `submittedAt`.+ const sqliteItem = galleryItem('backend.png', false);+ const overlayItem = legacyGeneratedImageToGalleryItem(generatedImage);++ it('sorts a newer backend item before an older overlaid recent in DESC order', () => {+ const olderOverlay = { ...overlayItem, createdAt: '2026-08-29T02:28:40.566Z' };+ const newerBackend = { ...sqliteItem, createdAt: '2026-08-29 13:01:20.649' };++ const sorted = [olderOverlay, newerBackend].sort((a, b) => compareGalleryItems(a, b, { orderDir: 'DESC' }));++ expect(sorted.map((item) => item.name)).toEqual(['backend.png', 'legacy.png']);+ });++ it('sorts an older overlaid recent before a newer backend item in ASC order', () => {+ const olderOverlay = { ...overlayItem, createdAt: '2026-08-29T02:28:40.566Z' };+ const newerBackend = { ...sqliteItem, createdAt: '2026-08-29 13:01:20.649' };++ const sorted = [newerBackend, olderOverlay].sort((a, b) => compareGalleryItems(a, b, { orderDir: 'ASC' }));++ expect(sorted.map((item) => item.name)).toEqual(['legacy.png', 'backend.png']);+ });++ it('keeps starred items first regardless of timestamp shape', () => {+ const starredOlderBackend = { ...sqliteItem, createdAt: '2026-08-29 02:28:40.566', starred: true };+ const unstarredNewerOverlay = { ...overlayItem, createdAt: '2026-08-29T13:01:20.649Z' };++ const sorted = [unstarredNewerOverlay, starredOlderBackend].sort((a, b) =>+ compareGalleryItems(a, b, { orderDir: 'DESC', starredFirst: true })+ );++ expect(sorted.map((item) => item.name)).toEqual(['backend.png', 'legacy.png']);+ });++ it('falls through to the kind tie-breaker when the two shapes name the same instant', () => {+ const image = { ...sqliteItem, createdAt: '2026-08-29 13:01:20.649' };+ const video = { ...image, durationSeconds: 2, kind: 'video' as const, name: 'backend.png' };++ // The same instant written in both shapes is a chronological tie. (DESC+ // ties come back as -0 — direction * 0 — so compare with `===`.)+ expect(+ compareGalleryItems({ ...image, createdAt: '2026-08-29T13:01:20.649Z' }, image, { orderDir: 'DESC' }) === 0+ ).toBe(true);++ const sorted = [image, video].sort((a, b) => compareGalleryItems(a, b, { orderDir: 'DESC' }));++ expect(sorted.map((item) => item.kind)).toEqual(['video', 'image']);+ });+});diff --git a/invokeai/frontend/webv2/src/features/gallery/core/items.ts b/invokeai/frontend/webv2/src/features/gallery/core/items.tsindex 8cc0430747..c63a37a981 100644--- a/invokeai/frontend/webv2/src/features/gallery/core/items.ts+++ b/invokeai/frontend/webv2/src/features/gallery/core/items.ts@@ -1,3 +1,5 @@+import { normalizeServerTimestamp } from '@platform/time/serverTimestamp';+import type { GalleryImage, GalleryOrderDir, GeneratedImageContract } from './types';export type GalleryItemKind = 'image' | 'video';@@ -82,6 +84,17 @@ export const assertNeverGalleryItem = (item: never): never => {const compareSqliteBinaryText = (a: string, b: string): number => (a === b ? 0 : a < b ? -1 : 1);+/**+ * Chronological comparison across the two timestamp shapes the gallery mixes:+ * backend rows carry SQLite's `created_at` ("2026-08-29 13:01:20.649") while+ * overlaid recents carry the queue's `submittedAt` (ISO, "2026-08-29T02:28:40.566Z").+ * Comparing the raw strings reads the 'T' separator as later than every+ * space-separated time on the same day, so an older overlaid recent would sort+ * above every newer backend image (and below them, with ascending order).+ */+const compareCreatedAt = (a: string, b: string): number =>+ compareSqliteBinaryText(normalizeServerTimestamp(a), normalizeServerTimestamp(b));+/** Mirrors the backend's starred/time/kind/name order for mixed gallery items. */export const compareGalleryItems = (a: GalleryItem,@@ -93,7 +106,7 @@ export const compareGalleryItems = (}const direction = orderDir === 'ASC' ? 1 : -1;- const chronologicalOrder = compareSqliteBinaryText(a.createdAt, b.createdAt);+ const chronologicalOrder = compareCreatedAt(a.createdAt, b.createdAt);if (chronologicalOrder !== 0) {return direction * chronologicalOrder;diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/useGalleryData.test.ts b/invokeai/frontend/webv2/src/features/gallery/ui/useGalleryData.test.tsindex fb0c1d6dd0..362cde54a0 100644--- a/invokeai/frontend/webv2/src/features/gallery/ui/useGalleryData.test.ts+++ b/invokeai/frontend/webv2/src/features/gallery/ui/useGalleryData.test.ts@@ -134,6 +134,51 @@ describe('mergeGalleryItemWindow', () => {).toEqual(['oldest.png', 'newest.png', 'middle.png']);});+ it('places an overlaid recent by its instant, not by timestamp shape, against backend items', () => {+ // Backend rows carry SQLite `created_at` ("2026-08-29 13:01:20.649"); the+ // 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'),+ ];+ const recentImages = [+ asGenerated(+ createImage(1, {+ imageName: 'older.png',+ queuedAt: '2026-08-29T02:28:40.566Z',+ })+ ),+ ];++ expect(+ mergeGalleryItemWindow({ backendItems, filter, maxRows: 60, recentImages }).map((item) => item.name)+ ).toEqual(['newer.png', 'middle.png', 'older.png']);++ expect(+ mergeGalleryItemWindow({+ backendItems,+ filter: { ...filter, orderDir: 'ASC' },+ maxRows: 60,+ recentImages,+ }).map((item) => item.name)+ ).toEqual(['older.png', 'middle.png', 'newer.png']);+ });+it('uses SQLite binary ordering for mixed-case and punctuation name ties in both directions', () => {const createTiedItem = (name: string): GalleryItem => ({boardId: 'none',