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): live-read setSourceVideo, close out the review's small findings

Lincoln Steinc42be9352e5d
diff --git a/invokeai/frontend/webv2/src/features/video/core/graph.ts b/invokeai/frontend/webv2/src/features/video/core/graph.ts
index dcddba2e12..6334b7ecd2 100644
--- a/invokeai/frontend/webv2/src/features/video/core/graph.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/graph.ts
@@ -137,6 +137,13 @@ const addReferenceNode = (
* clip, where its length cannot be honoured anyway; below the slop the absolute
* form is always in range, and the drift it costs is the estimate error itself,
* a frame or two.
+ *
+ * DECIDED: the slop is not widened beyond 3. An estimate error past the slop
+ * can still fail the relative form, but only on a clip barely longer than the
+ * window (it needs `error > startFrame`), and `TAIL_INDEX_SLOP` is this file's
+ * declared bound on how wrong the estimate gets. A wider guard would not
+ * remove the cliff — it would move it, and pay for the move with silent length
+ * drift on every clip inside the wider margin.
*/
const toReferenceStartIndex = (reference: Extract<VideoReferenceItem, { kind: 'video' }>, endIndex: number): number => {
const { clip } = reference;
diff --git a/invokeai/frontend/webv2/src/features/video/core/settings.ts b/invokeai/frontend/webv2/src/features/video/core/settings.ts
index acec796f57..7f0ce0eedd 100644
--- a/invokeai/frontend/webv2/src/features/video/core/settings.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.ts
@@ -519,17 +519,25 @@ const referenceExtendStartFrame = (clip: VideoSourceClip, numFrames: number): nu
/**
* The fewest source frames that still resample to at least `budget` frames.
*
- * `ceil(budget * fps / 24)` is the closed form and it is not exact: the
- * resample is a step function, so the rounded-up count can overshoot the
- * budget by a frame or two, and every overshot frame is discarded from the END
- * — the seam. Walking back down to the smallest count that still reaches the
- * budget lands on it exactly wherever the source's frame boundaries allow, and
- * within a frame where they do not (12 fps cannot hit an odd 141 at all). The
- * loop runs `24 / fps` times at most.
+ * `floor(t * 24/fps + 0.5) >= budget` is, for an integer budget, exactly
+ * `t >= (budget - 0.5) * fps / 24` — so the smallest such `t` has a closed
+ * form. The two bounded loops absorb the last-ulp disagreements between that
+ * expression and `resampledFrameCount`'s association of the same arithmetic;
+ * each runs at most a step or two. (An earlier version walked down from
+ * `ceil(budget * fps / 24)` instead, which is `~fps / 24` iterations — 124 ms
+ * per Frames keystroke at an absurd-but-probeable rate.)
+ *
+ * Landing short is what matters: the resample is a step function, and every
+ * frame past the budget is discarded from the END — the seam. The minimum
+ * lands on the budget exactly wherever the source's frame boundaries allow,
+ * and within a frame where they cannot (12 fps cannot hit an odd 141 at all).
*/
const tailSourceFrames = (budget: number, fps: number): number => {
- let tail = Math.max(MIN_VIDEO_TRIM_FRAMES, Math.ceil((budget * fps) / MINIMAX_H3_FPS));
+ let tail = Math.max(MIN_VIDEO_TRIM_FRAMES, Math.ceil(((budget - 0.5) * fps) / MINIMAX_H3_FPS));
+ while (resampledFrameCount(tail, fps) < budget) {
+ tail += 1;
+ }
while (tail > MIN_VIDEO_TRIM_FRAMES && resampledFrameCount(tail - 1, fps) >= budget) {
tail -= 1;
}
diff --git a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
index 3685fc75a5..70a98da720 100644
--- a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
@@ -307,12 +307,15 @@ export const VideoWidgetView = () => {
const setSourceVideo = useCallback(
(sourceVideo: VideoSourceClip | null) => {
if (referenceExtend) {
- const references = applyReferenceExtendSourceVideo(
- values.references,
- sourceVideo,
- maxVideoReferences,
- values.numFrames
- );
+ // Same project-scoped live read as `setReferences`: the clip field's
+ // adopt and upload paths call this after an await, and a captured
+ // list would clobber a reference added meanwhile. A write whose
+ // project has moved on is dropped.
+ if (referencesRef.current.projectId !== projectId) {
+ return;
+ }
+ const current = referencesRef.current.references;
+ const references = applyReferenceExtendSourceVideo(current, sourceVideo, maxVideoReferences, values.numFrames);
// Unchanged identity with a clip set means the video cap is full and
// no same-clip entry could be adopted. REFUSE the whole drop: patching
@@ -322,7 +325,7 @@ export const VideoWidgetView = () => {
// only ask about the clip currently set, and this drop is a different
// one.) Clearing is never refused -- removing the linked entry cannot
// overflow anything.
- if (sourceVideo && references === values.references) {
+ if (sourceVideo && references === current) {
toaster.create({
description: t('widgets.video.referenceExtendCapFullDescription'),
title: t('widgets.video.referenceExtendCapFull'),
@@ -336,7 +339,7 @@ export const VideoWidgetView = () => {
}
patch({ sourceVideo, ...(sourceVideo ? { firstFrameImage: null } : {}) });
},
- [maxVideoReferences, patch, referenceExtend, t, values.numFrames, values.references]
+ [maxVideoReferences, patch, projectId, referenceExtend, t, values.numFrames]
);
// The single choke point for every list edit the reference field makes — add,
// remove, retrim, reorder — so pinning the continuity anchor here covers all
diff --git a/invokeai/frontend/webv2/src/features/video/ui/seedToggle.browser.test.tsx b/invokeai/frontend/webv2/src/features/video/ui/seedToggle.browser.test.tsx
index a998c18175..3858afcb66 100644
--- a/invokeai/frontend/webv2/src/features/video/ui/seedToggle.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/video/ui/seedToggle.browser.test.tsx
@@ -17,6 +17,8 @@ import { userEvent } from 'vitest/browser';
let container: HTMLDivElement;
let root: Root;
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
beforeEach(() => {
container = document.createElement('div');
document.body.append(container);