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): anchor the linked tail window's start to the clip end

Lincoln Steinafb6899dc2f6
diff --git a/invokeai/frontend/webv2/src/features/video/core/graph.test.ts b/invokeai/frontend/webv2/src/features/video/core/graph.test.ts
index d2f4acbba9..3b3fa42230 100644
--- a/invokeai/frontend/webv2/src/features/video/core/graph.test.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/graph.test.ts
@@ -557,6 +557,30 @@ describe('compileVideoGraph — MiniMax H3 Ref2VA', () => {
);
});
+ it('reference-extend: only the linked tail window is anchored to the clip end', () => {
+ const linked = (clip: Record<string, unknown>, flag = true) => ({
+ clip: { fps: 24, height: 480, numFrames: 402, video_name: 'long.mp4', width: 832, ...clip },
+ conditioning: 'video_audio' as const,
+ ...(flag ? { fromSourceVideo: true } : {}),
+ kind: 'video' as const,
+ });
+ const startOf = (reference: unknown) =>
+ nodeOfType(
+ compileVideoGraph({ ...referenceSettings, references: [reference] } as never, model).backendGraph,
+ 'minimax_h3_video_reference'
+ ).start_frame;
+
+ // A user's own reference keeps an absolute start: their trim is a position,
+ // not a length, and re-anchoring it would drift with the estimate.
+ expect(startOf(linked({ endFrame: 400, startFrame: 260 }, false))).toBe(260);
+ // A start the clip's own beginning clamped stays 0 — true at any real count.
+ expect(startOf(linked({ endFrame: 400, startFrame: 0 }))).toBe(0);
+ // A cutpoint far enough from the end that BOTH bounds keep the estimate.
+ expect(startOf(linked({ endFrame: 300, startFrame: 160 }))).toBe(160);
+ // The tail case: end went negative, so the start follows it.
+ expect(startOf(linked({ endFrame: 400, startFrame: 260 }))).toBe(-142);
+ });
+
it('fl2va graphs are unchanged by the ref2va machinery', () => {
const { backendGraph } = compileVideoGraph(settingsFor(componentSource), componentSource);
@@ -608,7 +632,12 @@ describe('compileVideoGraph — MiniMax H3 Ref2VA', () => {
// The linked reference is an ordinary first reference; the flag never reaches metadata.
const videoReferences = nodesOfType(backendGraph, 'minimax_h3_video_reference');
- expect(videoReferences[0]).toMatchObject({ id: 'reference_1', start_frame: 260 });
+ // Both bounds ride the SAME negative anchor, so the extracted window keeps
+ // its exact length whatever the clip's real frame count turns out to be.
+ // A positive start would have made it `tail + (real - estimate)` frames,
+ // and the overrun is discarded at the seam.
+ expect(videoReferences[0]).toMatchObject({ end_frame: -2, id: 'reference_1', start_frame: -142 });
+ expect((videoReferences[0].end_frame as number) - (videoReferences[0].start_frame as number)).toBe(140);
const metadata = nodeOfType(backendGraph, 'core_metadata');
expect(metadata.generation_mode).toBe('minimax_h3_ref2v');
diff --git a/invokeai/frontend/webv2/src/features/video/core/graph.ts b/invokeai/frontend/webv2/src/features/video/core/graph.ts
index d31ac4bb66..bca9256bad 100644
--- a/invokeai/frontend/webv2/src/features/video/core/graph.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/graph.ts
@@ -66,24 +66,7 @@ const addReferenceNodes = (
): BackendInvocationContract => {
let chain: BackendInvocationContract | null = null;
references.forEach((reference, index) => {
- const node =
- reference.kind === 'image'
- ? addNode(graph, {
- detail: reference.detail,
- id: `reference_${index + 1}`,
- image: toImageField(reference.image),
- type: 'minimax_h3_image_reference',
- })
- : addNode(graph, {
- conditioning: reference.conditioning,
- // Same estimate-overshoot protection as the extend path: tail-window bounds
- // compile as negative indices the backend resolves against the REAL count.
- end_frame: toTailAwareIndex(reference.clip.endFrame, reference.clip.numFrames),
- id: `reference_${index + 1}`,
- start_frame: toTailAwareIndex(reference.clip.startFrame, reference.clip.numFrames),
- type: 'minimax_h3_video_reference',
- video: { video_name: reference.clip.video_name },
- });
+ const node = addReferenceNode(graph, reference, index);
const collect = addNode(graph, { id: `reference_collect_${index + 1}`, type: 'collect' });
if (chain) {
@@ -100,6 +83,64 @@ const addReferenceNodes = (
return chain;
};
+const addReferenceNode = (
+ graph: BackendGraphContract,
+ reference: VideoReferenceItem,
+ index: number
+): BackendInvocationContract => {
+ if (reference.kind === 'image') {
+ return addNode(graph, {
+ detail: reference.detail,
+ id: `reference_${index + 1}`,
+ image: toImageField(reference.image),
+ type: 'minimax_h3_image_reference',
+ });
+ }
+ // Same estimate-overshoot protection as the extend path: tail-window bounds
+ // compile as negative indices the backend resolves against the REAL count.
+ const endFrame = toTailAwareIndex(reference.clip.endFrame, reference.clip.numFrames);
+
+ return addNode(graph, {
+ conditioning: reference.conditioning,
+ end_frame: endFrame,
+ id: `reference_${index + 1}`,
+ start_frame: toReferenceStartIndex(reference, endFrame),
+ type: 'minimax_h3_video_reference',
+ video: { video_name: reference.clip.video_name },
+ });
+};
+
+/**
+ * A video reference's start bound, measured from the same end as its other bound.
+ *
+ * `toTailAwareIndex` sends a near-the-end bound out NEGATIVE, resolved against
+ * the clip's REAL frame count, and leaves anything further back POSITIVE, taken
+ * from the panel's ESTIMATE. That split is right for a hand-picked trim, whose
+ * start is an absolute position the estimate must not drift — but the linked
+ * tail window straddles it: the cutpoint end goes negative while the start,
+ * ~124 frames back, stays positive, so what the backend extracts is
+ * `tail + (real - estimate)` frames rather than `tail`. That length is a budget
+ * the backend enforces by discarding the overrun at the SEAM (see
+ * `deriveReferenceExtendClip`), so it has to survive an inexact estimate.
+ *
+ * The linked entry's start is not an absolute pick — it is defined as
+ * `tail - 1` frames before the cutpoint — so it rides the same negative anchor
+ * and the window keeps its length whatever the real count turns out to be. Two
+ * cases stay absolute: a start the clip's own beginning already clamped to 0
+ * ("sample from the top" is true at any real count), and a cutpoint far enough
+ * from the end that the end bound stayed positive, where both bounds already
+ * share the estimate.
+ */
+const toReferenceStartIndex = (reference: Extract<VideoReferenceItem, { kind: 'video' }>, endIndex: number): number => {
+ const { clip } = reference;
+
+ if (reference.fromSourceVideo !== true || clip.startFrame === 0 || endIndex >= 0) {
+ return toTailAwareIndex(clip.startFrame, clip.numFrames);
+ }
+
+ return endIndex - (clip.endFrame - clip.startFrame);
+};
+
const addPromptAndSeedNodes = (graph: BackendGraphContract) => ({
negativePrompt: addNode(graph, { id: 'negative_prompt', type: 'string' }),
positivePrompt: addNode(graph, { id: 'positive_prompt', type: 'string' }),