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): scope the reference ref to its project, derive the anchor flag on load

Lincoln Stein9fcd0233181e
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 a0fe1b558a..351780f91e 100644
--- a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
@@ -573,6 +573,50 @@ describe('reference-extend linkage', () => {
expect(applyReferenceExtendSourceVideo(unlinked, null, 3, FRAMES)).toBe(unlinked);
});
+ it('normalization re-establishes the linkage recall drops, and the invariants reach it', () => {
+ // `fromSourceVideo` never reaches metadata, so a recalled reference-extend
+ // panel arrives with its anchor UNFLAGGED beside the source video. Every
+ // invariant keys on the flag, so before this: not pinned (the model
+ // continued from whatever followed it), and not re-budgeted (a Frames
+ // change left the window overrunning -- 2s cut off the seam at 345 -> 90).
+ const recalled = { ...VIDEO_REFERENCE, clip: { ...VIDEO_REFERENCE.clip, video_name: 'long.mp4' } };
+ const normalized = normalizeVideoSettings(
+ createSettings({ references: [recalled, IMAGE_REFERENCE], sourceVideo: source24 })
+ );
+
+ // Flagged by clip identity -- the same rule the setter adopts by -- and pinned.
+ expect(normalized?.references).toHaveLength(2);
+ expect(normalized?.references[0]).toBe(IMAGE_REFERENCE);
+ expect(normalized?.references[1]).toMatchObject({
+ clip: { video_name: 'long.mp4' },
+ fromSourceVideo: true,
+ });
+
+ // The frame-count re-budget now reaches the recalled window.
+ const rebudgeted = applyReferenceExtendNumFrames(normalized!.references, 90);
+
+ expect(rebudgeted[1]).toMatchObject({ clip: { endFrame: 47, startFrame: 9 } });
+
+ // A flagged entry stays authoritative: an unflagged same-name entry beside
+ // it is NOT a second anchor.
+ const flaggedElsewhere = normalizeVideoSettings(
+ createSettings({
+ references: [recalled, { ...VIDEO_REFERENCE, fromSourceVideo: true }],
+ sourceVideo: source24,
+ })
+ );
+
+ expect(
+ flaggedElsewhere?.references.filter((entry) => entry.kind === 'video' && entry.fromSourceVideo === true)
+ ).toHaveLength(1);
+ expect(flaggedElsewhere?.references[1]).toMatchObject({ clip: { video_name: 'ref.mp4' }, fromSourceVideo: true });
+
+ // No source video: nothing to link, nothing flagged.
+ const unlinked = normalizeVideoSettings(createSettings({ references: [recalled] }));
+
+ expect(unlinked?.references[0]).not.toHaveProperty('fromSourceVideo');
+ });
+
it('adopts an unflagged reference for the same clip instead of duplicating it (recall shape)', () => {
const recalled = { ...VIDEO_REFERENCE, clip: { ...VIDEO_REFERENCE.clip, video_name: 'long.mp4' } };
const result = applyReferenceExtendSourceVideo([IMAGE_REFERENCE, recalled], source24, 3, FRAMES);
diff --git a/invokeai/frontend/webv2/src/features/video/core/settings.ts b/invokeai/frontend/webv2/src/features/video/core/settings.ts
index e2dbab72b8..5742977d4f 100644
--- a/invokeai/frontend/webv2/src/features/video/core/settings.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.ts
@@ -114,17 +114,41 @@ export const isVideoReferenceItem = (value: unknown): value is VideoReferenceIte
* 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[] => {
+const sanitizeVideoReferences = (value: unknown, sourceVideoName?: string): VideoReferenceItem[] => {
if (!Array.isArray(value)) {
return [];
}
+ let valid = value.filter((entry) => isVideoReferenceItem(entry));
+
+ // Re-establish the linkage recall drops. `fromSourceVideo` is panel state
+ // and never reaches metadata, so a recalled panel arrives with its anchor
+ // UNFLAGGED beside the source video -- and every invariant keyed on the flag
+ // (the pin, the frame-count re-budget, the seam-anchored start index)
+ // silently lapses: a Frames change left the recalled window unbudgeted and
+ // the backend cut 2s off the seam. The flag is derivable, not just
+ // storable: the video reference naming the Initial Video's clip IS the
+ // anchor, the same identity `applyReferenceExtendSourceVideo` adopts by.
+ // An already-flagged entry stays authoritative.
+ if (
+ sourceVideoName !== undefined &&
+ !valid.some((entry) => entry.kind === 'video' && entry.fromSourceVideo === true)
+ ) {
+ const adopt = valid.findIndex((entry) => entry.kind === 'video' && entry.clip.video_name === sourceVideoName);
+
+ if (adopt >= 0) {
+ valid = valid.map((entry, index) =>
+ index === adopt && entry.kind === 'video' ? { ...entry, fromSourceVideo: true } : entry
+ );
+ }
+ }
+
// Pin BEFORE the cap trim. The front-drop below assumes the anchor is last,
// but a project saved by the build that PREPENDED it loads with the anchor
// first -- so without this the overflow rule deletes the one entry it exists
// to protect. Pinning here also heals those panels on load: normalization
// runs on every read, whereas `setReferences` only fires once the user
// touches the reference list.
- const valid = pinReferenceExtendAnchor(value.filter((entry) => isVideoReferenceItem(entry)));
+ valid = pinReferenceExtendAnchor(valid);
let videosToDrop = Math.max(0, valid.filter((entry) => entry.kind === 'video').length - VIDEO_REFERENCE_MAX_VIDEOS);
const result: VideoReferenceItem[] = [];
let images = 0;
@@ -221,7 +245,10 @@ export const normalizeVideoSettings = (values: unknown): VideoSettings | null =>
// record somehow holds both, the references win deterministically. A source
// video COEXISTS with references (Ref2VA reference-extend) — validation
// rejects the pair on models that cannot consume it.
- const references = sanitizeVideoReferences(values.references);
+ const references = sanitizeVideoReferences(
+ values.references,
+ isVideoSourceClip(values.sourceVideo) ? values.sourceVideo.video_name : undefined
+ );
const hasReferences = references.length > 0;
const firstFrameImage = !hasReferences && isImageWithDims(values.firstFrameImage) ? values.firstFrameImage : null;
// A first frame and a source video are mutually exclusive; if a stale
diff --git a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
index e9342de410..ea6ce8828b 100644
--- a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
@@ -161,11 +161,17 @@ export const VideoWidgetView = () => {
// 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);
+ // Keyed by PROJECT, not just latest: the widget is reconciled, never
+ // remounted, across a project switch (the <Activity> key is the panel
+ // instance), while `patch` stays bound to the project its render belonged
+ // to. A bare latest-list ref therefore tracks whichever project is ACTIVE —
+ // and a gallery resolve landing after a switch would write the new project's
+ // reference list into the old project, wholesale.
+ const referencesRef = useRef({ projectId, references: values.references });
useEffect(() => {
- referencesRef.current = values.references;
- }, [values.references]);
+ referencesRef.current = { projectId, references: values.references };
+ }, [projectId, values.references]);
// Chakra's `Field.Root` hands its single `ids.control` to EVERY control
// inside it, and this Field holds three. Without an id of its own the
@@ -286,15 +292,17 @@ export const VideoWidgetView = () => {
const setNumFrames = useCallback(
(numFrames: number) =>
patch(
- referenceExtend
- ? { numFrames, references: applyReferenceExtendNumFrames(referencesRef.current, numFrames) }
+ referenceExtend && referencesRef.current.projectId === projectId
+ ? { numFrames, references: applyReferenceExtendNumFrames(referencesRef.current.references, numFrames) }
: { numFrames }
),
// Reads the list through the ref so the Frames control keeps a stable
// prop identity: depending on `values.references` re-created this on every
// panel patch, re-rendering the slider against the file's stable-identity
- // contract. Safe because the re-derive is idempotent in `numFrames`.
- [patch, referenceExtend]
+ // contract. Safe because the re-derive is idempotent in `numFrames`. The
+ // project guard is unreachable for this synchronous caller; it keeps the
+ // ref's contract uniform.
+ [patch, projectId, referenceExtend]
);
const setSourceVideo = useCallback(
(sourceVideo: VideoSourceClip | null) => {
@@ -329,7 +337,14 @@ export const VideoWidgetView = () => {
// the LAST reference, so the anchor's position is derived, not user-set.
const setReferences = useCallback(
(update: (current: VideoReferenceItem[]) => VideoReferenceItem[]) => {
- const updated = update(referencesRef.current);
+ // A pending edit whose project has moved on is DROPPED: this callback's
+ // `patch` still targets the project it rendered for, but that project's
+ // current list is gone from the ref. Losing one add because the user
+ // switched projects mid-resolve beats overwriting a list they can see.
+ if (referencesRef.current.projectId !== projectId) {
+ return;
+ }
+ const updated = update(referencesRef.current.references);
const next = referenceExtend ? pinReferenceExtendAnchor(updated) : updated;
patch({
@@ -339,7 +354,7 @@ export const VideoWidgetView = () => {
: {}),
});
},
- [patch, referenceExtend]
+ [patch, projectId, referenceExtend]
);
const clearReferences = useCallback(() => patch({ references: [] }), [patch]);
const setLoras = useCallback(