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(webv2): pin the reference-extend continuity anchor last

Lincoln Steine36562f41334
diff --git a/invokeai/frontend/webv2/public/locales/en.json b/invokeai/frontend/webv2/public/locales/en.json
index 237b103703..6df1b51204 100644
--- a/invokeai/frontend/webv2/public/locales/en.json
+++ b/invokeai/frontend/webv2/public/locales/en.json
@@ -3110,6 +3110,7 @@
"initialFrames": "Start & End Images",
"initialVideo": "Initial Video",
"initialVideoBlocked": "Clear the first frame to extend a video instead.",
+ "initialVideoCapBlocked": "All 3 video reference slots are in use, so there is no room for the continuity reference this clip needs. Remove a video reference first.",
"lastFrame": "Last Frame",
"lastFrameExtendHelp": "Optional destination: the extension ends on this image.",
"lastFrameHelp": "Optional: the video ends on this image (interpolates from the first frame).",
diff --git a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts b/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
index 24cb085fff..be775970d8 100644
--- a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
@@ -6,6 +6,7 @@ import { MINIMAX_H3_NUM_FRAMES_CHOICES } from './dimensions';
import {
applyReferenceExtendSourceVideo,
applyReferenceExtendNumFrames,
+ pinReferenceExtendAnchor,
clearDeletedVideoMedia,
cloneVideoWidgetValues,
createVideoSourceClip,
@@ -336,6 +337,7 @@ 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.
const tooMany = [
...Array.from({ length: 4 }, (_, index) => ({
...VIDEO_REFERENCE,
@@ -347,13 +349,33 @@ 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.
+ const tooMany = [
+ IMAGE_REFERENCE,
+ ...Array.from({ length: 5 }, (_unused, index) => ({
+ ...VIDEO_REFERENCE,
+ clip: { ...VIDEO_REFERENCE.clip, video_name: `v${index}.mp4` },
+ })),
+ ];
+ 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',
+ ]);
+ });
+
it('isVideoSettings rejects references combined with frame media', () => {
expect(isVideoSettings(createSettings({ firstFrameImage: FIRST_FRAME, references: [IMAGE_REFERENCE] }))).toBe(
false
@@ -551,6 +573,35 @@ describe('reference-extend linkage', () => {
});
});
+ it('pins the continuity anchor last, whatever the add or drag order', () => {
+ // Request order is rotary order: the generated frames continue from the
+ // LAST reference block. An image added after the Initial Video would
+ // otherwise take a rotary slot between the initial video's tail and the
+ // first generated frame.
+ const linked = applyReferenceExtendSourceVideo([], source24, 3, FRAMES);
+ const anchor = linked[0]!;
+
+ expect(pinReferenceExtendAnchor([anchor, IMAGE_REFERENCE])).toEqual([IMAGE_REFERENCE, anchor]);
+ expect(pinReferenceExtendAnchor([IMAGE_REFERENCE, VIDEO_REFERENCE, anchor])).toEqual([
+ IMAGE_REFERENCE,
+ VIDEO_REFERENCE,
+ anchor,
+ ]);
+ // Identity-preserving when already last, and when there is no anchor.
+ const settled = [IMAGE_REFERENCE, anchor];
+
+ expect(pinReferenceExtendAnchor(settled)).toBe(settled);
+ const none = [IMAGE_REFERENCE, VIDEO_REFERENCE];
+
+ expect(pinReferenceExtendAnchor(none)).toBe(none);
+
+ // Adopting a recalled entry re-pins it rather than leaving it in place.
+ const recalled = { ...VIDEO_REFERENCE, clip: { ...VIDEO_REFERENCE.clip, video_name: 'long.mp4' } };
+ const adopted = applyReferenceExtendSourceVideo([recalled, IMAGE_REFERENCE], source24, 3, FRAMES);
+
+ expect(adopted[1]).toMatchObject({ fromSourceVideo: true, kind: 'video' });
+ });
+
it('applyReferenceExtendNumFrames leaves unlinked references alone', () => {
const unlinked = [VIDEO_REFERENCE, IMAGE_REFERENCE];
diff --git a/invokeai/frontend/webv2/src/features/video/core/settings.ts b/invokeai/frontend/webv2/src/features/video/core/settings.ts
index de4da8cca4..da3d3eb07f 100644
--- a/invokeai/frontend/webv2/src/features/video/core/settings.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.ts
@@ -101,28 +101,36 @@ export const isVideoReferenceItem = (value: unknown): value is VideoReferenceIte
return false;
};
-/** Drops invalid entries and enforces the per-kind caps, preserving order. */
+/**
+ * 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.
+ *
+ * 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.
+ */
const sanitizeVideoReferences = (value: unknown): VideoReferenceItem[] => {
if (!Array.isArray(value)) {
return [];
}
+ const valid = value.filter((entry) => isVideoReferenceItem(entry));
+ let videosToDrop = Math.max(0, valid.filter((entry) => entry.kind === 'video').length - VIDEO_REFERENCE_MAX_VIDEOS);
const result: VideoReferenceItem[] = [];
- let videos = 0;
let images = 0;
- for (const entry of value) {
- if (!isVideoReferenceItem(entry)) {
- continue;
- }
+ for (const entry of valid) {
if (entry.kind === 'image') {
if (images >= VIDEO_REFERENCE_MAX_IMAGES) {
continue;
}
images += 1;
- } else {
- if (videos >= VIDEO_REFERENCE_MAX_VIDEOS) {
- continue;
- }
- videos += 1;
+ } else if (videosToDrop > 0) {
+ videosToDrop -= 1;
+ continue;
}
result.push(entry);
}
@@ -484,6 +492,32 @@ export const applyReferenceExtendNumFrames = (
return changed ? next : references;
};
+/**
+ * Moves the linked tail reference to the end of the list.
+ *
+ * Request order is rotary order: `build_ref2va_packed_sequence` lays the
+ * reference blocks out in order, each advancing a shared clock, and the
+ * generated rows start at the position the LAST block left behind. The
+ * continuity anchor only anchors anything if it IS that block — an image
+ * dropped afterwards wedges itself (a whole rotary slot) between the initial
+ * video's tail and the first generated frame, and the model continues from the
+ * image instead.
+ *
+ * Add order and drag order must not decide that, so the anchor's position is
+ * derived like its trim: last, always. Identity-preserving when it is already
+ * last, or when there is no anchor.
+ */
+export const pinReferenceExtendAnchor = (references: VideoReferenceItem[]): VideoReferenceItem[] => {
+ const index = references.findIndex((entry) => entry.kind === 'video' && entry.fromSourceVideo === true);
+
+ if (index < 0 || index === references.length - 1) {
+ return references;
+ }
+ const pinned = references[index]!;
+
+ return [...references.slice(0, index), ...references.slice(index + 1), pinned];
+};
+
/**
* Keeps the reference list in step with the Initial Video on a reference-extend
* panel (pure; the setter and the model-selection transition both use it):
@@ -536,8 +570,10 @@ export const applyReferenceExtendSourceVideo = (
: references.findIndex((entry) => entry.kind === 'video' && entry.clip.video_name === sourceVideo.video_name);
if (linkedIndex >= 0) {
- return references.map((entry, index) =>
- index === linkedIndex && entry.kind === 'video' ? { ...linked, conditioning: entry.conditioning } : entry
+ return pinReferenceExtendAnchor(
+ references.map((entry, index) =>
+ index === linkedIndex && entry.kind === 'video' ? { ...linked, conditioning: entry.conditioning } : entry
+ )
);
}
diff --git a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
index 4aa586b2df..483196894e 100644
--- a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
@@ -11,6 +11,7 @@ import { getVideoDurationSeconds, invertVideoAspectRatioId } from '@features/vid
import {
applyReferenceExtendSourceVideo,
applyReferenceExtendNumFrames,
+ pinReferenceExtendAnchor,
normalizeVideoWidgetValues,
resolveVideoMode,
VIDEO_ASPECT_RATIO_IDS,
@@ -294,14 +295,21 @@ export const VideoWidgetView = () => {
},
[maxVideoReferences, patch, referenceExtend, t, values.numFrames, values.references]
);
+ // The single choke point for every list edit the reference field makes — add,
+ // remove, retrim, reorder — so pinning the continuity anchor here covers all
+ // of them. Request order is rotary order and the generation continues from
+ // the LAST reference, so the anchor's position is derived, not user-set.
const setReferences = useCallback(
- (references: VideoReferenceItem[]) =>
+ (references: VideoReferenceItem[]) => {
+ const next = referenceExtend ? pinReferenceExtendAnchor(references) : references;
+
patch({
- references,
- ...(references.length > 0
+ references: next,
+ ...(next.length > 0
? { firstFrameImage: null, lastFrameImage: null, ...(referenceExtend ? {} : { sourceVideo: null }) }
: {}),
- }),
+ });
+ },
[patch, referenceExtend]
);
const clearReferences = useCallback(() => patch({ references: [] }), [patch]);
@@ -372,6 +380,13 @@ export const VideoWidgetView = () => {
const supportsExtend = policy.modes.includes('extend');
const supportsReferences = policy.modes.includes('reference');
const supportsInitialVideo = supportsExtend || referenceExtend;
+ // Setting an Initial Video on a reference-extend panel has to place a linked
+ // tail reference, which needs a free video slot. With an anchor already
+ // present the drop only re-derives it in place, so it stays available.
+ const initialVideoCapBlocked =
+ referenceExtend &&
+ !values.references.some((entry) => entry.kind === 'video' && entry.fromSourceVideo === true) &&
+ values.references.filter((entry) => entry.kind === 'video').length >= maxVideoReferences;
const hasConditioningMedia = Boolean(values.firstFrameImage || values.lastFrameImage || values.sourceVideo);
const derivedSourceText = dimensions ? t(`widgets.video.dimensionSource.${dimensions.source}`) : undefined;
const derivedSizeText = dimensions
@@ -504,8 +519,14 @@ export const VideoWidgetView = () => {
</Text>
) : null}
<VideoSourceClipField
- disabled={Boolean(values.firstFrameImage)}
- disabledReason={values.firstFrameImage ? t('widgets.video.initialVideoBlocked') : undefined}
+ disabled={Boolean(values.firstFrameImage) || initialVideoCapBlocked}
+ disabledReason={
+ values.firstFrameImage
+ ? t('widgets.video.initialVideoBlocked')
+ : initialVideoCapBlocked
+ ? t('widgets.video.initialVideoCapBlocked')
+ : undefined
+ }
sourceVideo={values.sourceVideo}
onChange={setSourceVideo}
/>