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): harden reference-extend after adversarial review
Lincoln Stein7fe9df04e533
diff --git a/invokeai/frontend/webv2/public/locales/en.json b/invokeai/frontend/webv2/public/locales/en.jsonindex 09e35472ee..c2d988db5d 100644--- a/invokeai/frontend/webv2/public/locales/en.json+++ b/invokeai/frontend/webv2/public/locales/en.json@@ -3163,6 +3163,8 @@"referenceConditioningVideoAudio": "Video + audio","referenceDetailMatch": "Match generation size","referenceDetailMax": "Max detail (2048px, slower)",+ "referenceExtendCapFull": "No continuity reference added",+ "referenceExtendCapFullDescription": "All 3 video reference slots are in use, so the extension has no reference sampling the initial video. Remove a video reference to add one.","referenceExtendHelp": "The generated video is appended to this clip at its End Frame. A linked video reference samples the last ~5 seconds (141 frames) before that cutpoint for continuity — its trim re-derives whenever the cutpoints change.","referenceFromInitialVideo": "Initial video","references": "References",diff --git a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts b/invokeai/frontend/webv2/src/features/video/core/settings.test.tsindex 3a73a63fa2..2b37ce0776 100644--- a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts+++ b/invokeai/frontend/webv2/src/features/video/core/settings.test.ts@@ -439,6 +439,26 @@ describe('reference-extend linkage', () => {});});+ it('prefers the flagged entry over an earlier same-name reference on a source swap', () => {+ // User: linked ref for clip A, plus their OWN hand-trimmed reference for+ // clip B sitting above it. Swapping the Initial Video to clip B must+ // update the FLAGGED entry — not rewrite the user's B reference.+ const linkedA = applyReferenceExtendSourceVideo([], longSource, 3)[0];+ const handTrimmedB = {+ ...VIDEO_REFERENCE,+ clip: { ...VIDEO_REFERENCE.clip, endFrame: 200, numFrames: 300, startFrame: 100, video_name: 'b.mp4' },+ };+ const sourceB = { ...longSource, endFrame: 290, numFrames: 300, video_name: 'b.mp4' };+ const result = applyReferenceExtendSourceVideo([handTrimmedB, linkedA!], sourceB, 3);++ expect(result[0]).toBe(handTrimmedB);+ expect(result[1]).toMatchObject({+ clip: { endFrame: 290, startFrame: 150, video_name: 'b.mp4' },+ fromSourceVideo: true,+ });+ expect(result.filter((entry) => entry.kind === 'video' && entry.fromSourceVideo === true)).toHaveLength(1);+ });+it('leaves a full video-reference list unchanged instead of overflowing the cap', () => {const full = [VIDEO_REFERENCE,diff --git a/invokeai/frontend/webv2/src/features/video/core/settings.ts b/invokeai/frontend/webv2/src/features/video/core/settings.tsindex 983769957b..38b3eebdf8 100644--- a/invokeai/frontend/webv2/src/features/video/core/settings.ts+++ b/invokeai/frontend/webv2/src/features/video/core/settings.ts@@ -435,10 +435,15 @@ export const applyReferenceExtendSourceVideo = (fromSourceVideo: true,kind: 'video',};- const linkedIndex = references.findIndex(- (entry) =>- entry.kind === 'video' && (entry.fromSourceVideo === true || entry.clip.video_name === sourceVideo.video_name)- );+ // The flagged entry is THE linked reference; adopt-by-name only when none+ // exists (a recall-restored pair carries no flag). A flag-or-name findIndex+ // would rewrite a user's own same-clip reference — hand trim and all —+ // whenever it sat above the flagged one.+ const flaggedIndex = references.findIndex((entry) => entry.kind === 'video' && entry.fromSourceVideo === true);+ const linkedIndex =+ flaggedIndex >= 0+ ? flaggedIndex+ : references.findIndex((entry) => entry.kind === 'video' && entry.clip.video_name === sourceVideo.video_name);if (linkedIndex >= 0) {return references.map((entry, index) =>diff --git a/invokeai/frontend/webv2/src/features/video/core/videoPolicies.test.ts b/invokeai/frontend/webv2/src/features/video/core/videoPolicies.test.tsindex 5e552280a1..505c57bba1 100644--- a/invokeai/frontend/webv2/src/features/video/core/videoPolicies.test.ts+++ b/invokeai/frontend/webv2/src/features/video/core/videoPolicies.test.ts@@ -1296,6 +1296,22 @@ describe('reference-extend policy', () => {kind: 'video',});+ // A task-neutral re-selection of the same model must NOT re-derive a+ // hand-tuned linked trim (only cutpoint changes do, via the setter).+ const tuned = {+ ...toRef.settings,+ references: toRef.settings.references.map((entry, index) =>+ index === 0 && entry.kind === 'video' ? { ...entry, clip: { ...entry.clip, startFrame: 300 } } : entry+ ),+ };+ const reselected = getVideoModelSelectionResult({+ currentSettings: tuned,+ model: ref2vaTransformer(),+ models: [fl2va],+ });++ expect(reselected.settings.references[0]).toMatchObject({ clip: { startFrame: 300 } });+// And back: the references (linked one included) clear; the clip stays// for FL2VA's own extend mode.const backToFl = getVideoModelSelectionResult({diff --git a/invokeai/frontend/webv2/src/features/video/core/videoPolicies.ts b/invokeai/frontend/webv2/src/features/video/core/videoPolicies.tsindex 56d32118e7..3e04ecd3ca 100644--- a/invokeai/frontend/webv2/src/features/video/core/videoPolicies.ts+++ b/invokeai/frontend/webv2/src/features/video/core/videoPolicies.ts@@ -1414,9 +1414,16 @@ export const getVideoModelSelectionResult = ({}// Reference-extend: a surviving Initial Video (e.g. carried over from an- // FL2VA extend setup) gets its linked tail reference derived/re-derived, so- // the switch lands on a generatable panel.- if (config.references?.extend && next.sourceVideo) {+ // FL2VA extend setup) gets its linked tail reference derived, so the switch+ // lands on a generatable panel. Only when none exists yet: the transition+ // also runs on task-neutral edits (a same-model re-selection, a component+ // change), and re-deriving there would reset a hand-tuned trim the help+ // text only promises to reset on cutpoint changes.+ if (+ config.references?.extend &&+ next.sourceVideo &&+ !next.references.some((entry) => entry.kind === 'video' && entry.fromSourceVideo === true)+ ) {next.references = applyReferenceExtendSourceVideo(next.references, next.sourceVideo, config.references.maxVideos);}diff --git a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsxindex c5c3d7f41f..c80189f38b 100644--- a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx@@ -254,16 +254,24 @@ export const VideoWidgetView = () => {const setSourceVideo = useCallback((sourceVideo: VideoSourceClip | null) => {if (referenceExtend) {- patch({- references: applyReferenceExtendSourceVideo(values.references, sourceVideo, maxVideoReferences),- sourceVideo,- ...(sourceVideo ? { firstFrameImage: null } : {}),- });+ const references = applyReferenceExtendSourceVideo(values.references, sourceVideo, maxVideoReferences);++ // Unchanged identity with a clip set means the video cap is full and+ // no same-clip entry could be adopted: the extension would run with+ // no continuity anchor at all, which deserves more than silence.+ if (sourceVideo && references === values.references) {+ toaster.create({+ description: t('widgets.video.referenceExtendCapFullDescription'),+ title: t('widgets.video.referenceExtendCapFull'),+ type: 'warning',+ });+ }+ patch({ references, sourceVideo, ...(sourceVideo ? { firstFrameImage: null } : {}) });return;}patch({ sourceVideo, ...(sourceVideo ? { firstFrameImage: null } : {}) });},- [maxVideoReferences, patch, referenceExtend, values.references]+ [maxVideoReferences, patch, referenceExtend, t, values.references]);const setReferences = useCallback((references: VideoReferenceItem[]) =>diff --git a/invokeai/frontend/webv2/src/workbench/image-actions/galleryOptimisticRollback.test.ts b/invokeai/frontend/webv2/src/workbench/image-actions/galleryOptimisticRollback.test.tsindex a9142bc459..71b746de6c 100644--- a/invokeai/frontend/webv2/src/workbench/image-actions/galleryOptimisticRollback.test.ts+++ b/invokeai/frontend/webv2/src/workbench/image-actions/galleryOptimisticRollback.test.ts@@ -217,6 +217,43 @@ describe('gallery optimistic rollback against the real reducer', () => {expect(getProjectWidgetValues(getProject(restored, projectId), 'gallery').selectedImage).toEqual(galleryItem);});+ it('restores swept references even when an initial video occupies the panel (reference-extend pair)', () => {+ // sourceVideo <-> references is a LEGAL pair on a Ref2VA reference-extend+ // panel; treating them as rivals made a failed deletion permanently drop+ // the reference the sweep removed.+ const seeded = seedProjectWithItem();+ const projectId = seeded.projectId;+ const sourceClip = {+ endFrame: 79,+ fps: 16,+ height: 480,+ numFrames: 81,+ startFrame: 0,+ video_name: 'clip.mp4',+ width: 832,+ };+ const imageReference = { detail: 'max', image: { height: 480, image_name: 'a.png', width: 832 }, kind: 'image' };+ const state = workbenchReducer(seeded.state, {+ projectId,+ type: 'patchWidgetValues',+ values: { references: [imageReference], sourceVideo: sourceClip },+ widgetId: 'video',+ });++ const before = captureGalleryWidgetKeyValues(state.projects);+ const afterRemoval = workbenchReducer(state, { itemKeys: ['image:a.png'], type: 'removeGalleryItems' });++ expect(getProjectWidgetValues(getProject(afterRemoval, projectId), 'video').references).toEqual([]);++ const entries = diffGalleryWidgetKeyValues(before, afterRemoval.projects);+ const patches = selectRestorableGalleryWidgetPatches(entries, afterRemoval.projects);+ const restored = applyRestorePatches(afterRemoval, patches);+ const videoValues = getProjectWidgetValues(getProject(restored, projectId), 'video');++ expect(videoValues.references).toEqual([imageReference]);+ expect(videoValues.sourceVideo).toEqual(sourceClip);+ });+it('still restores a cleared video slot when its rival stayed empty', () => {const seeded = seedProjectWithItem();const projectId = seeded.projectId;diff --git a/invokeai/frontend/webv2/src/workbench/image-actions/galleryOptimisticRollback.ts b/invokeai/frontend/webv2/src/workbench/image-actions/galleryOptimisticRollback.tsindex e3790d8309..c80abc882c 100644--- a/invokeai/frontend/webv2/src/workbench/image-actions/galleryOptimisticRollback.ts+++ b/invokeai/frontend/webv2/src/workbench/image-actions/galleryOptimisticRollback.ts@@ -51,10 +51,12 @@ const EXCLUSIVE_RIVAL_FIELDS: ReadonlyArray<{ key: string; rivalKey: string; wid{ key: 'firstFrameImage', rivalKey: 'references', widgetId: 'video' },{ key: 'lastFrameImage', rivalKey: 'references', widgetId: 'video' },{ key: 'sourceVideo', rivalKey: 'firstFrameImage', widgetId: 'video' },- { key: 'sourceVideo', rivalKey: 'references', widgetId: 'video' },{ key: 'references', rivalKey: 'firstFrameImage', widgetId: 'video' },{ key: 'references', rivalKey: 'lastFrameImage', widgetId: 'video' },- { key: 'references', rivalKey: 'sourceVideo', widgetId: 'video' },+ // sourceVideo <-> references is deliberately NOT a rival pair: the two+ // coexist on a Ref2VA reference-extend panel, and normalization keeps both+ // (validation owns rejecting the pair per model). Treating them as rivals+ // made a FAILED gallery deletion permanently drop the other slot's value.];const trackedFieldKey = (projectId: string, widgetId: WidgetTypeId, key: string): string =>