Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(timeline): re-collect auto-zoom suggestions when the clips move underneath

超超超超超级喜欢你的达妮娅f7258ae7420a
diff --git a/src/components/ai-edition/recordingImport.ts b/src/components/ai-edition/recordingImport.ts
index cb5457e0f..2c22d61fb 100644
--- a/src/components/ai-edition/recordingImport.ts
+++ b/src/components/ai-edition/recordingImport.ts
@@ -26,6 +26,7 @@ import {
} from "@/lib/ai-edition/store/projectStore";
import {
appendAutoZoomSuggestions,
+ clipExtentSignature,
collectAutoZoomSuggestionsForDocument,
} from "@/lib/ai-edition/timeline/apply-auto-zooms";
import { nativeBridgeClient } from "@/native/client";
@@ -110,15 +111,6 @@ async function readAutoZoomPref(): Promise<boolean> {
}
}
-function clipExtentSignature(document: AxcutDocument): string {
- return document.timeline.clips
- .map(
- (clip) =>
- `${clip.id}:${clip.assetId}:${clip.sourceStartSec}:${clip.sourceEndSec ?? ""}:${clip.timelineStartSec}:${clip.timelineEndSec}`,
- )
- .join("|");
-}
-
function pendingFreshRecordingAsset(document: AxcutDocument) {
if (pendingFreshRecordingAutoZoomPath) {
return document.assets.find(
diff --git a/src/components/ai-edition/v4/V4Timeline.tsx b/src/components/ai-edition/v4/V4Timeline.tsx
index 8dd426376..cd2edb07f 100644
--- a/src/components/ai-edition/v4/V4Timeline.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.tsx
@@ -52,7 +52,7 @@ import { useTimelineTranscriptGate } from "@/lib/ai-edition/store/transcriptionS
import { useChatPromptBus } from "@/lib/ai-edition/store/useChatPromptBus";
import { useEditorSettings } from "@/lib/ai-edition/store/useEditorSettings";
import type { useTimeline } from "@/lib/ai-edition/store/useTimeline";
-import { collectAutoZoomSuggestionsForDocument } from "@/lib/ai-edition/timeline/apply-auto-zooms";
+import { collectAutoZoomSuggestionsForLatestDocument } from "@/lib/ai-edition/timeline/apply-auto-zooms";
import { hasAnyClipWithCamera } from "@/lib/ai-edition/timeline/camera";
import { formatSec } from "@/lib/ai-edition/timeline/format";
import {
@@ -1488,9 +1488,16 @@ export function V4Timeline({
}
setAutoBusy(true);
try {
- const suggestions = await collectAutoZoomSuggestionsForDocument(document, (videoPath) =>
- nativeBridgeClient.cursor.getTelemetry(videoPath),
+ // Collected against the document as it is now, and again if the clips moved
+ // while the telemetry was being read: the suggestions carry timeline spans and
+ // `addZoomsBulk` anchors them against whatever the store holds at write time,
+ // so a trim or a reorder during that multi-second wait would land them on
+ // different media.
+ const collected = await collectAutoZoomSuggestionsForLatestDocument(
+ () => useProjectStore.getState().document,
+ (videoPath) => nativeBridgeClient.cursor.getTelemetry(videoPath),
);
+ const suggestions = collected?.suggestions ?? [];
if (suggestions.length === 0) {
toast.info(t("toolbar.noAutoZoomMoments"), {
description: t("toolbar.noAutoZoomMomentsDescription"),
diff --git a/src/lib/ai-edition/timeline/apply-auto-zooms.test.ts b/src/lib/ai-edition/timeline/apply-auto-zooms.test.ts
index c00124f19..484c79fc9 100644
--- a/src/lib/ai-edition/timeline/apply-auto-zooms.test.ts
+++ b/src/lib/ai-edition/timeline/apply-auto-zooms.test.ts
@@ -1,9 +1,10 @@
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
import type { CursorTelemetryPoint } from "@/components/video-editor/types";
import { type AxcutDocument, createEmptyDocument } from "../schema";
import {
appendAutoZoomSuggestions,
collectAutoZoomSuggestionsForDocument,
+ collectAutoZoomSuggestionsForLatestDocument,
} from "./apply-auto-zooms";
function dwell(
@@ -110,3 +111,52 @@ describe("appendAutoZoomSuggestions", () => {
});
});
});
+
+describe("collectAutoZoomSuggestionsForLatestDocument", () => {
+ const telemetry: CursorTelemetryPoint[] = [0, 600, 1200, 1800].map((timeMs) => ({
+ timeMs,
+ cx: 0.5,
+ cy: 0.5,
+ }));
+
+ it("collects once when the clips stand still", async () => {
+ const doc = documentWithClip();
+ const getTelemetry = vi.fn(async () => telemetry);
+
+ const out = await collectAutoZoomSuggestionsForLatestDocument(() => doc, getTelemetry);
+
+ expect(out?.document).toBe(doc);
+ expect(getTelemetry).toHaveBeenCalledTimes(1);
+ });
+
+ // THE case: the telemetry read takes seconds, and the suggestions carry timeline
+ // spans that `appendAutoZoomSuggestions` will anchor against whatever the store
+ // holds at write time. A trim landing in between makes the first collection
+ // describe a timeline that no longer exists.
+ it("collects again when the clips moved during the read", async () => {
+ const before = documentWithClip();
+ const after: AxcutDocument = {
+ ...before,
+ timeline: {
+ ...before.timeline,
+ clips: before.timeline.clips.map((clip) => ({ ...clip, sourceEndSec: 4 })),
+ },
+ };
+ let current = before;
+ const getTelemetry = vi.fn(async () => {
+ current = after;
+ return telemetry;
+ });
+
+ const out = await collectAutoZoomSuggestionsForLatestDocument(() => current, getTelemetry);
+
+ expect(out?.document).toBe(after);
+ expect(getTelemetry.mock.calls.length).toBeGreaterThan(1);
+ });
+
+ it("gives up when the project is gone", async () => {
+ const getTelemetry = vi.fn(async () => telemetry);
+ expect(await collectAutoZoomSuggestionsForLatestDocument(() => null, getTelemetry)).toBeNull();
+ expect(getTelemetry).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/lib/ai-edition/timeline/apply-auto-zooms.ts b/src/lib/ai-edition/timeline/apply-auto-zooms.ts
index f1f1c82c0..3a6c5fac0 100644
--- a/src/lib/ai-edition/timeline/apply-auto-zooms.ts
+++ b/src/lib/ai-edition/timeline/apply-auto-zooms.ts
@@ -39,6 +39,52 @@ export async function collectAutoZoomSuggestionsForDocument(
return perSource.flat();
}
+/**
+ * The clip geometry the suggestions were built against, as a comparable string.
+ *
+ * Suggestions carry TIMELINE spans, and `appendAutoZoomSuggestions` anchors them
+ * against whatever clips the document holds when the write happens. Collecting
+ * takes a multi-second telemetry round trip, so those two documents are not
+ * necessarily the same one — and if a clip was trimmed, moved, added, removed or
+ * reordered in between, the spans land on different media, or on nothing.
+ */
+export function clipExtentSignature(document: AxcutDocument): string {
+ return document.timeline.clips
+ .map(
+ (clip) =>
+ `${clip.id}:${clip.assetId}:${clip.sourceStartSec}:${clip.sourceEndSec ?? ""}:${clip.timelineStartSec}:${clip.timelineEndSec}`,
+ )
+ .join("|");
+}
+
+/**
+ * Collect against the document as it is NOW, and collect again if the clips moved
+ * while the telemetry was being read.
+ *
+ * One retry, not a loop: a user who keeps editing through the wait will keep
+ * invalidating it, and the honest answer there is the write-time guards, not
+ * spinning here. Returns the suggestions together with the document they were
+ * built from, so the caller can tell what they describe.
+ */
+export async function collectAutoZoomSuggestionsForLatestDocument(
+ readDocument: () => AxcutDocument | null,
+ getTelemetry: (videoPath: string) => Promise<CursorTelemetryPoint[] | null | undefined>,
+): Promise<{ document: AxcutDocument; suggestions: AutoZoomSuggestion[] } | null> {
+ const start = readDocument();
+ if (!start) return null;
+ const startSignature = clipExtentSignature(start);
+ const suggestions = await collectAutoZoomSuggestionsForDocument(start, getTelemetry);
+ const latest = readDocument();
+ if (!latest) return null;
+ if (clipExtentSignature(latest) === startSignature) {
+ return { document: latest, suggestions };
+ }
+ return {
+ document: latest,
+ suggestions: await collectAutoZoomSuggestionsForDocument(latest, getTelemetry),
+ };
+}
+
export function appendAutoZoomSuggestions(
document: AxcutDocument,
suggestions: AutoZoomSuggestion[],