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(webv2): re-check reference caps at apply time, overflow drops the newest
Lincoln Stein7deea6afcb95
diff --git a/invokeai/frontend/webv2/public/locales/en.json b/invokeai/frontend/webv2/public/locales/en.jsonindex 640aa2bc8f..35fa8bebfa 100644--- a/invokeai/frontend/webv2/public/locales/en.json+++ b/invokeai/frontend/webv2/public/locales/en.json@@ -3167,6 +3167,8 @@"referenceExtendCapFull": "Initial video not set","referenceExtendCapFullDescription": "All 3 video reference slots are in use, so there is no room for the continuity reference this clip needs. Remove a video reference, then drop the clip again.","referenceExtendHelp": "The generated video is appended to this clip at its End Frame. A linked video reference samples up to the last ~5 seconds before that cutpoint for continuity — never more than the generated clip's own length, since the model discards the overrun at the seam. Its trim re-derives whenever the cutpoints or the frame count change.",+ "referenceImageCapRace": "All {{max}} image reference slots filled while this one was loading, so it was not added.",+ "referenceVideoCapRace": "All {{max}} video reference slots filled while this one was loading, so it was not added.","referenceFromInitialVideo": "Initial video","references": "References","referencesHelp": "Up to 3 videos and 9 images condition the generation, in order — reordering references changes the result. A video reference can contribute its image track, its soundtrack, or both.",diff --git a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts b/invokeai/frontend/webv2/src/features/video/core/settings.test.tsindex 351780f91e..4606edc190 100644--- a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts+++ b/invokeai/frontend/webv2/src/features/video/core/settings.test.ts@@ -338,7 +338,8 @@ describe('references', () => {});it('normalization drops malformed entries and enforces the caps, preserving order', () => {- // Videos over the cap overflow from the FRONT — see the over-cap test below.+ // Videos over the cap drop the NEWEST non-anchor entries — the surplus is+ // whatever arrived last. See the over-cap tests below.const tooMany = [...Array.from({ length: 4 }, (_, index) => ({...VIDEO_REFERENCE,@@ -350,16 +351,16 @@ describe('references', () => {const normalized = normalizeVideoSettings(createSettings({ references: tooMany as never }));expect(normalized?.references.map((entry) => (entry.kind === 'video' ? entry.clip.video_name : 'img'))).toEqual([+ 'v0.mp4','v1.mp4','v2.mp4',- 'v3.mp4','img',]);});- it('an over-cap video list overflows from the front, keeping the continuity anchor', () => {- // The anchor is last, so dropping from the tail would discard exactly the- // entry the extension depends on. Reachable only from a stale record.+ it('over-cap overflow drops the newest videos but never the anchor', () => {+ const named = (entry: { kind: string; clip?: { video_name: string } }) =>+ entry.kind === 'video' ? entry.clip!.video_name : 'img';const tooMany = [IMAGE_REFERENCE,...Array.from({ length: 5 }, (_unused, index) => ({@@ -369,12 +370,32 @@ describe('references', () => {];const normalized = normalizeVideoSettings(createSettings({ references: tooMany as never }));- expect(normalized?.references.map((entry) => (entry.kind === 'video' ? entry.clip.video_name : 'img'))).toEqual([- 'img',- 'v2.mp4',- 'v3.mp4',- 'v4.mp4',- ]);+ expect(normalized?.references.map(named as never)).toEqual(['img', 'v0.mp4', 'v1.mp4', 'v2.mp4']);++ // The race shape: an add slipped past the render-time cap gate while the+ // Initial Video was placing its anchor. The surplus is the racing add (D),+ // not the user's oldest reference (B) — a plain front-drop deleted B.+ const anchor = {+ ...VIDEO_REFERENCE,+ clip: { ...VIDEO_REFERENCE.clip, video_name: 'anchor.mp4' },+ fromSourceVideo: true,+ };+ const raced = ['b.mp4', 'c.mp4'].map((video_name) => ({+ ...VIDEO_REFERENCE,+ clip: { ...VIDEO_REFERENCE.clip, video_name },+ }));+ const healed = normalizeVideoSettings(+ createSettings({+ references: [+ ...raced,+ anchor,+ { ...VIDEO_REFERENCE, clip: { ...VIDEO_REFERENCE.clip, video_name: 'd.mp4' } },+ ] as never,+ })+ );++ expect(healed?.references.map(named as never)).toEqual(['b.mp4', 'c.mp4', 'anchor.mp4']);+ expect(healed?.references.at(-1)).toMatchObject({ fromSourceVideo: true });});it('normalization heals a panel saved with the anchor prepended', () => {@@ -404,7 +425,7 @@ describe('references', () => {];const trimmed = normalizeVideoSettings(createSettings({ references: stale as never }));- expect(trimmed?.references.map(named as never)).toEqual(['v1.mp4', 'v2.mp4', 'anchor.mp4']);+ expect(trimmed?.references.map(named as never)).toEqual(['v0.mp4', 'v1.mp4', 'anchor.mp4']);expect(trimmed?.references.at(-1)).toMatchObject({ fromSourceVideo: true });});diff --git a/invokeai/frontend/webv2/src/features/video/core/settings.ts b/invokeai/frontend/webv2/src/features/video/core/settings.tsindex 5742977d4f..acec796f57 100644--- a/invokeai/frontend/webv2/src/features/video/core/settings.ts+++ b/invokeai/frontend/webv2/src/features/video/core/settings.ts@@ -104,15 +104,17 @@ export const isVideoReferenceItem = (value: unknown): value is VideoReferenceIte/*** Drops invalid entries and enforces the per-kind caps, preserving order.*- * An over-cap list should never reach here — both add paths are gated on the- * cap and the Initial Video field disables itself when the video slots are- * full — so this is the guard for a stale or hand-edited project record.+ * An over-cap list should never reach here — the add paths re-check the cap+ * against the live list at apply time and the Initial Video field disables+ * itself when the video slots are full — so this is the guard for a stale or+ * hand-edited project record.*- * Videos overflow from the FRONT. Request order is rotary order and the- * generated frames continue from the LAST reference block, so on a- * reference-extend panel the final video is the continuity anchor: dropping- * from the tail would discard exactly the entry the extension depends on.- * Images carry no ordering role and keep the front.+ * Video overflow drops the NEWEST non-anchor entries: the surplus is whatever+ * arrived last, and a plain front-drop deleted the user's OLDEST reference+ * whenever a racing add slipped past the cap. The anchor is exempt whatever+ * its position — request order is rotary order and the generated frames+ * continue from the LAST block, so it is the one entry the extension depends+ * on. Images carry no ordering role and keep the front.*/const sanitizeVideoReferences = (value: unknown, sourceVideoName?: string): VideoReferenceItem[] => {if (!Array.isArray(value)) {@@ -149,7 +151,18 @@ const sanitizeVideoReferences = (value: unknown, sourceVideoName?: string): Vide// runs on every read, whereas `setReferences` only fires once the user// touches the reference list.valid = pinReferenceExtendAnchor(valid);+let videosToDrop = Math.max(0, valid.filter((entry) => entry.kind === 'video').length - VIDEO_REFERENCE_MAX_VIDEOS);+ const dropped = new Set<VideoReferenceItem>();+ for (let index = valid.length - 1; index >= 0 && videosToDrop > 0; index -= 1) {+ const entry = valid[index]!;++ if (entry.kind === 'video' && entry.fromSourceVideo !== true) {+ dropped.add(entry);+ videosToDrop -= 1;+ }+ }+const result: VideoReferenceItem[] = [];let images = 0;for (const entry of valid) {@@ -158,8 +171,7 @@ const sanitizeVideoReferences = (value: unknown, sourceVideoName?: string): Videcontinue;}images += 1;- } else if (videosToDrop > 0) {- videosToDrop -= 1;+ } else if (dropped.has(entry)) {continue;}result.push(entry);diff --git a/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx b/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsxindex cbf6de925e..e9df66c6f4 100644--- a/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx@@ -301,14 +301,32 @@ export const VideoReferenceListField = memo(function VideoReferenceListField({const [resolved] = await galleryImages.resolveMany([imageName]);if (resolved) {- onChange((current) => [- ...current,- {- detail: 'max',- image: { height: resolved.height, image_name: resolved.imageName, width: resolved.width },- kind: 'image',- },- ]);+ // Re-check the cap against the LIVE list: the render-time gate was+ // evaluated before the await, and another writer (a second drop, or+ // the Initial Video placing its anchor) can fill the slots meanwhile.+ // An over-cap write would survive to normalization, whose overflow+ // rule then has to delete SOMETHING the user placed.+ let declined = false;++ onChange((current) => {+ if (current.filter((entry) => entry.kind === 'image').length >= maxImages) {+ declined = true;++ return current;+ }++ return [+ ...current,+ {+ detail: 'max',+ image: { height: resolved.height, image_name: resolved.imageName, width: resolved.width },+ kind: 'image',+ },+ ];+ });+ if (declined) {+ setErrorMessage(t('widgets.video.referenceImageCapRace', { max: maxImages }));+ }}} catch (error) {const message = error instanceof Error ? error.message : String(error);@@ -318,7 +336,7 @@ export const VideoReferenceListField = memo(function VideoReferenceListField({setIsLoading(false);}},- [onChange, reportError]+ [maxImages, onChange, reportError, t]);const addVideoReference = useCallback(@@ -338,16 +356,31 @@ export const VideoReferenceListField = memo(function VideoReferenceListField({width: item.width,});- onChange((current) => [- ...current,- {- // References are truncated to the generated duration, not joined: default to- // the whole clip rather than the extend-mode 2-frame-tail trim.- clip: { ...clip, endFrame: Math.max(0, clip.numFrames - 1), startFrame: 0 },- conditioning: 'video_audio',- kind: 'video',- },- ]);+ // Same live cap re-check as the image path -- the Initial Video's+ // anchor is the writer that most easily fills the slots mid-await.+ let declined = false;++ onChange((current) => {+ if (current.filter((entry) => entry.kind === 'video').length >= maxVideos) {+ declined = true;++ return current;+ }++ return [+ ...current,+ {+ // References are truncated to the generated duration, not joined: default to+ // the whole clip rather than the extend-mode 2-frame-tail trim.+ clip: { ...clip, endFrame: Math.max(0, clip.numFrames - 1), startFrame: 0 },+ conditioning: 'video_audio',+ kind: 'video',+ },+ ];+ });+ if (declined) {+ setErrorMessage(t('widgets.video.referenceVideoCapRace', { max: maxVideos }));+ }}} catch (error) {const message = error instanceof Error ? error.message : String(error);@@ -357,7 +390,7 @@ export const VideoReferenceListField = memo(function VideoReferenceListField({setIsLoading(false);}},- [onChange, reportError]+ [maxVideos, onChange, reportError, t]);const handleDragEnd = useCallback(