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): make reference-list writes functional updates

Lincoln Steinf9749d338525
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 5201bc3e69..cab2901dff 100644
--- a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
-import type { VideoSettings } from './types';
+import type { VideoReferenceItem, VideoSettings } from './types';
import { MINIMAX_H3_NUM_FRAMES_CHOICES } from './dimensions';
import {
@@ -604,6 +604,31 @@ describe('reference-extend linkage', () => {
});
});
+ it('an updater applied after a concurrent write keeps both changes', () => {
+ // The reference field hands `setReferences` an UPDATER because its add
+ // handlers await a gallery resolve before writing. Modelled here: the
+ // handler captures the list, the Initial Video field places the anchor
+ // during the await, then the resolve lands. A captured-array write would
+ // drop the anchor entirely; an updater over live state keeps both.
+ const captured: VideoReferenceItem[] = [];
+ const add = (current: VideoReferenceItem[]): VideoReferenceItem[] => [...current, IMAGE_REFERENCE];
+
+ // ...the Initial Video lands mid-flight.
+ const live = applyReferenceExtendSourceVideo(captured, source24, 3, FRAMES);
+
+ expect(live.some((entry) => entry.kind === 'video' && entry.fromSourceVideo === true)).toBe(true);
+
+ // ...then the resolve writes, against LIVE state rather than `captured`.
+ const merged = pinReferenceExtendAnchor(add(live));
+
+ expect(merged).toHaveLength(2);
+ expect(merged[0]).toBe(IMAGE_REFERENCE);
+ expect(merged[1]).toMatchObject({ fromSourceVideo: true, kind: 'video' });
+
+ // The snapshot write this replaced would have produced just the image.
+ expect(add(captured)).toEqual([IMAGE_REFERENCE]);
+ });
+
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
diff --git a/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx b/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx
index 5af4792db6..cf5d7815d7 100644
--- a/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx
+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx
@@ -230,7 +230,13 @@ export const VideoReferenceListField = memo(function VideoReferenceListField({
disabled?: boolean;
maxImages: number;
maxVideos: number;
- onChange: (references: VideoReferenceItem[]) => void;
+ /**
+ * Accepts an UPDATER, not a snapshot. The add handlers `await` a gallery
+ * resolve before writing, and the Initial Video field and Frames slider both
+ * write references too -- a captured array would clobber whichever of those
+ * landed during the await.
+ */
+ onChange: (update: (current: VideoReferenceItem[]) => VideoReferenceItem[]) => void;
references: VideoReferenceItem[];
}) {
const { t } = useTranslation();
@@ -289,8 +295,8 @@ export const VideoReferenceListField = memo(function VideoReferenceListField({
const [resolved] = await galleryImages.resolveMany([imageName]);
if (resolved) {
- onChange([
- ...references,
+ onChange((current) => [
+ ...current,
{
detail: 'max',
image: { height: resolved.height, image_name: resolved.imageName, width: resolved.width },
@@ -306,7 +312,7 @@ export const VideoReferenceListField = memo(function VideoReferenceListField({
setIsLoading(false);
}
},
- [onChange, references, reportError]
+ [onChange, reportError]
);
const addVideoReference = useCallback(
@@ -326,8 +332,8 @@ export const VideoReferenceListField = memo(function VideoReferenceListField({
width: item.width,
});
- onChange([
- ...references,
+ 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.
@@ -345,7 +351,7 @@ export const VideoReferenceListField = memo(function VideoReferenceListField({
setIsLoading(false);
}
},
- [onChange, references, reportError]
+ [onChange, reportError]
);
const handleDragEnd = useCallback(
@@ -425,32 +431,36 @@ export const VideoReferenceListField = memo(function VideoReferenceListField({
const updateReference = useCallback(
(index: number, reference: VideoReferenceItem) => {
- onChange(references.map((entry, entryIndex) => (entryIndex === index ? reference : entry)));
+ onChange((current) => current.map((entry, entryIndex) => (entryIndex === index ? reference : entry)));
},
- [onChange, references]
+ [onChange]
);
const removeReference = useCallback(
(index: number) => {
- onChange(references.filter((_, entryIndex) => entryIndex !== index));
+ onChange((current) => current.filter((_, entryIndex) => entryIndex !== index));
},
- [onChange, references]
+ [onChange]
);
const moveReference = useCallback(
(index: number, direction: -1 | 1) => {
- const target = index + direction;
+ onChange((current) => {
+ const target = index + direction;
- if (target < 0 || target >= references.length) {
- return;
- }
- const next = [...references];
- const [entry] = next.splice(index, 1);
+ if (target < 0 || target >= current.length) {
+ return current;
+ }
+ const next = [...current];
+ const [entry] = next.splice(index, 1);
- if (entry) {
+ if (!entry) {
+ return current;
+ }
next.splice(target, 0, entry);
- onChange(next);
- }
+
+ return next;
+ });
},
- [onChange, references]
+ [onChange]
);
return (
diff --git a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
index ba6d790bbe..4dec56af68 100644
--- a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
@@ -31,7 +31,7 @@ import { Button } from '@platform/ui/Button';
import { SliderNumberField } from '@platform/ui/SliderNumberField';
import { toaster } from '@platform/ui/toaster';
import { ArrowLeftRightIcon, DicesIcon } from 'lucide-react';
-import { useCallback, useId, useMemo } from 'react';
+import { useCallback, useEffect, useId, useMemo, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { areVideoValuesEqual } from './videoComparators';
@@ -302,13 +302,29 @@ export const VideoWidgetView = () => {
},
[maxVideoReferences, patch, referenceExtend, t, values.numFrames, values.references]
);
+ // Always the newest normalized list. The reference field's add handlers
+ // `await` a gallery resolve before writing, and the Initial Video field and
+ // the Frames slider write references too — so an updater that resolved
+ // against a captured array would clobber whichever of those landed during
+ // the await, silently deleting the anchor or restoring a window the frame
+ // count had already re-derived.
+ // Synced in an effect rather than during render: this file's react-compiler
+ // rule forbids touching a ref while rendering, and a gallery resolve lands
+ // whole frames later, long after the commit.
+ const referencesRef = useRef(values.references);
+
+ useEffect(() => {
+ referencesRef.current = values.references;
+ }, [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[]) => {
- const next = referenceExtend ? pinReferenceExtendAnchor(references) : references;
+ (update: (current: VideoReferenceItem[]) => VideoReferenceItem[]) => {
+ const updated = update(referencesRef.current);
+ const next = referenceExtend ? pinReferenceExtendAnchor(updated) : updated;
patch({
references: next,