From c2937f8b3abd8c76083db6fffa6157a011c4952e Mon Sep 17 00:00:00 2001 From: EtienneLescot Date: Tue, 17 Nov 2026 17:10:39 +0100 Subject: [PATCH 1/7] fix(editor): retry the dimension backfill and give it time on long takes A long fragmented MP4 from the Linux recorder takes over 5 s to reach loadedmetadata (12.7 s cold for an 11 min take), so the on-load probe timed out, the asset was marked probed for the session and the editor stretched the picture. The backfill now waits up to 60 s and retries a failed probe on a later document change, at most three times. Refs #1610 --- src/lib/ai-edition/store/useTimeline.test.ts | 56 ++++++++++++++++++++ src/lib/ai-edition/store/useTimeline.ts | 30 ++++++++--- 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts index 3a8433ce9..57a098551 100644 --- a/src/lib/ai-edition/store/useTimeline.test.ts +++ b/src/lib/ai-edition/store/useTimeline.test.ts @@ -501,6 +501,62 @@ describe("useTimeline backfills missing source dimensions on load", () => { expect(bridgeMocks.save).not.toHaveBeenCalled(); }); + // #1610: a long fragmented-MP4 take outlasted the 5 s probe, and that one failure was final + // for the session, so the editor stretched the picture. The backfill waits longer and + // tries again on a later document change. + it("waits longer than an insert probe and retries a failed probe on a later change", async () => { + probeVideoDimensionsMock.mockResolvedValueOnce(null); + const unprobed = { ...sampleDoc, assets: [{ ...sampleDoc.assets[0], video: undefined }] }; + useProjectStore.setState({ + projectId: "proj_test", + document: unprobed, + revision: 1, + status: "ready", + error: null, + }); + renderTimeline(); + await waitFor(() => expect(probeVideoDimensionsMock).toHaveBeenCalledTimes(1)); + expect(probeVideoDimensionsMock.mock.calls[0][1]).toBeGreaterThanOrEqual(60_000); + await act(async () => { + await new Promise((r) => setTimeout(r, 0)); + }); + expect(bridgeMocks.save).not.toHaveBeenCalled(); + + act(() => { + useProjectStore.setState({ document: { ...unprobed, zoomRanges: [] }, revision: 2 }); + }); + await waitFor(() => expect(bridgeMocks.save).toHaveBeenCalledTimes(1)); + expect(probeVideoDimensionsMock).toHaveBeenCalledTimes(2); + const saved = useProjectStore.getState().document?.assets.find((a) => a.id === "asset_1"); + expect(saved?.video).toMatchObject({ width: 1920, height: 1080 }); + }); + + it("stops probing an unreadable file after a bounded number of attempts", async () => { + probeVideoDimensionsMock.mockResolvedValue(null); + const unprobed = { ...sampleDoc, assets: [{ ...sampleDoc.assets[0], video: undefined }] }; + useProjectStore.setState({ + projectId: "proj_test", + document: unprobed, + revision: 1, + status: "ready", + error: null, + }); + renderTimeline(); + for (let revision = 2; revision < 8; revision++) { + await act(async () => { + await new Promise((r) => setTimeout(r, 0)); + }); + act(() => { + useProjectStore.setState({ document: { ...unprobed, zoomRanges: [] }, revision }); + }); + } + await act(async () => { + await new Promise((r) => setTimeout(r, 0)); + }); + expect(probeVideoDimensionsMock).toHaveBeenCalledTimes(3); + expect(bridgeMocks.save).not.toHaveBeenCalled(); + }); + it("does not re-probe a used asset with no reachable file more than once", async () => { probeVideoDimensionsMock.mockResolvedValue(null); // probe fails (unreadable file) useProjectStore.setState({ diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts index 32408cdb3..c7be8e2a5 100644 --- a/src/lib/ai-edition/store/useTimeline.ts +++ b/src/lib/ai-edition/store/useTimeline.ts @@ -89,6 +89,13 @@ import { useSequentialTimelineOps } from "./useSequentialTimelineOps"; // these 2 s, which is what all five add* used to hardcode. const DEFAULT_NEW_REGION_SEC = 2; +// The on-load dimension backfill runs in the background, so it can wait far longer than an +// insert's 5 s probe. It has to: the Linux recorder writes fragmented MP4 with no index, and +// Chromium walks every fragment before `loadedmetadata`, which takes well past 5 s on a long +// take (#1610). A failure is retried on a later document change, a bounded number of times. +const DIMS_PROBE_TIMEOUT_MS = 60_000; +const DIMS_PROBE_ATTEMPTS = 3; + // NaN-guarded floors. Timeline inputs arrive from drag deltas and persisted // documents, both of which can carry NaN; every action needs the same guard. const finiteSec = (n: number) => (Number.isFinite(n) ? Math.max(0, n) : 0); @@ -257,10 +264,11 @@ export function useTimeline() { // cropped clip could show under ORIGINAL while an un-probed 16:9 sibling was missing entirely. // Probe once on load and persist via saveDocument with `history: false` (a write the user // never made must not be what the next Ctrl+Z reverses), so the fix sticks and every consumer - // agrees without each re-probing on its own (what the export dialog used to do). Attempt each - // asset at most once per session, even on failure, so a file that can't be probed doesn't - // spin the effect on every document change. + // agrees without each re-probing on its own (what the export dialog used to do). A failed + // probe is tried again on a later document change, up to DIMS_PROBE_ATTEMPTS per session, so + // one slow open is not final (#1610) and an unreadable file still doesn't spin the effect. const probedAssetIdsRef = useRef>(new Set()); + const dimsProbeAttemptsRef = useRef>(new Map()); useEffect(() => { if (!document) return; const usedAssetIds = new Set(document.timeline.clips.map((c) => c.assetId)); @@ -278,7 +286,8 @@ export function useTimeline() { (a) => usedAssetIds.has(a.id) && (needsScreen(a) || needsCamera(a)) && - !probedAssetIdsRef.current.has(a.id), + !probedAssetIdsRef.current.has(a.id) && + (dimsProbeAttemptsRef.current.get(a.id) ?? 0) < DIMS_PROBE_ATTEMPTS, ); if (missing.length === 0) return; // No cleanup cancels this. The effect re-runs on EVERY document change, and a fresh @@ -293,18 +302,27 @@ export function useTimeline() { const probed: Record = {}; for (const a of missing) { probedAssetIdsRef.current.add(a.id); + dimsProbeAttemptsRef.current.set(a.id, (dimsProbeAttemptsRef.current.get(a.id) ?? 0) + 1); const entry: { video?: Dims; camera?: Dims } = {}; + let failed = false; if (needsScreen(a)) { - const dims = await probeVideoDimensions(toFileUrl(a.originalPath)); + const dims = await probeVideoDimensions(toFileUrl(a.originalPath), DIMS_PROBE_TIMEOUT_MS); if (dims) entry.video = dims; + else failed = true; } // Probed independently of the screen: one file being unreadable must not cost // the other its dimensions, and a camera-less asset simply skips this. if (a.cameraTrack && needsCamera(a)) { - const dims = await probeVideoDimensions(toFileUrl(a.cameraTrack.sourcePath)); + const dims = await probeVideoDimensions( + toFileUrl(a.cameraTrack.sourcePath), + DIMS_PROBE_TIMEOUT_MS, + ); if (dims) entry.camera = dims; + else failed = true; } if (entry.video || entry.camera) probed[a.id] = entry; + // Unmarked so a later document change retries what is still missing. + if (failed) probedAssetIdsRef.current.delete(a.id); } if (Object.keys(probed).length === 0) return; // The store only takes a document once its save returns, so a write still in flight From 6fb867c038cfd0df09906b98a9624c5a0671fc3f Mon Sep 17 00:00:00 2001 From: EtienneLescot Date: Wed, 18 Nov 2026 17:19:22 +0100 Subject: [PATCH 2/7] fix(editor): count only failed dimension probes and reserve the batch A save deferred by waitForDocumentSaves no longer uses up a probe attempt, and a run started while a batch is still probing no longer picks up a queued asset of that batch. Refs #1610 --- src/lib/ai-edition/store/useTimeline.test.ts | 78 ++++++++++++++++++++ src/lib/ai-edition/store/useTimeline.ts | 13 +++- 2 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts index 57a098551..04765d1c5 100644 --- a/src/lib/ai-edition/store/useTimeline.test.ts +++ b/src/lib/ai-edition/store/useTimeline.test.ts @@ -557,6 +557,84 @@ describe("useTimeline backfills missing source dimensions on load", () => { expect(bridgeMocks.save).not.toHaveBeenCalled(); }); + // A successful probe whose save was deferred is not a failure: it must not use up the + // attempts, or a busy save queue would end the backfill for the session. + it("keeps retrying a successful probe whose save keeps timing out", async () => { + for (let i = 0; i < 3; i++) waitForDocumentSavesMock.mockResolvedValueOnce("timeout"); + const unprobed = { ...sampleDoc, assets: [{ ...sampleDoc.assets[0], video: undefined }] }; + useProjectStore.setState({ + projectId: "proj_test", + document: unprobed, + revision: 1, + status: "ready", + error: null, + }); + renderTimeline(); + for (let revision = 2; revision < 5; revision++) { + await waitFor(() => expect(waitForDocumentSavesMock).toHaveBeenCalledTimes(revision - 1)); + await act(async () => { + await new Promise((r) => setTimeout(r, 0)); + }); + act(() => { + useProjectStore.setState({ document: { ...unprobed, zoomRanges: [] }, revision }); + }); + } + await waitFor(() => expect(bridgeMocks.save).toHaveBeenCalledTimes(1)); + expect(probeVideoDimensionsMock).toHaveBeenCalledTimes(4); + }); + + // A run started while an earlier asset is still probing must not take a later asset of + // the first batch: it would be probed twice. + it("does not probe a queued asset again from a run started while the batch is out", async () => { + let resolveFirst!: (dims: { width: number; height: number }) => void; + probeVideoDimensionsMock.mockImplementation((url: string) => + url.includes("first") + ? new Promise((resolve) => { + resolveFirst = resolve; + }) + : Promise.resolve({ width: 1280, height: 720 }), + ); + const first = { ...sampleDoc.assets[0], originalPath: "/tmp/first.webm", video: undefined }; + const second = { + ...sampleDoc.assets[0], + id: "asset_2", + originalPath: "/tmp/second.webm", + video: undefined, + }; + const twoAssets: AxcutDocument = { + ...sampleDoc, + assets: [first, second], + timeline: { + ...sampleDoc.timeline, + clips: [ + ...sampleDoc.timeline.clips, + { ...sampleDoc.timeline.clips[0], id: "clip_b", assetId: "asset_2" }, + ], + }, + }; + useProjectStore.setState({ + projectId: "proj_test", + document: twoAssets, + revision: 1, + status: "ready", + error: null, + }); + renderTimeline(); + await waitFor(() => expect(probeVideoDimensionsMock).toHaveBeenCalledTimes(1)); + act(() => { + useProjectStore.setState({ document: { ...twoAssets, zoomRanges: [] }, revision: 2 }); + }); + await act(async () => { + resolveFirst({ width: 1920, height: 1080 }); + await new Promise((r) => setTimeout(r, 0)); + }); + await waitFor(() => expect(bridgeMocks.save).toHaveBeenCalledTimes(1)); + const secondCalls = probeVideoDimensionsMock.mock.calls.filter(([url]) => + String(url).includes("second"), + ); + expect(secondCalls).toHaveLength(1); + }); + it("does not re-probe a used asset with no reachable file more than once", async () => { probeVideoDimensionsMock.mockResolvedValue(null); // probe fails (unreadable file) useProjectStore.setState({ diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts index c7be8e2a5..b30cd9c86 100644 --- a/src/lib/ai-edition/store/useTimeline.ts +++ b/src/lib/ai-edition/store/useTimeline.ts @@ -300,9 +300,10 @@ export function useTimeline() { void (async () => { type Dims = { width: number; height: number }; const probed: Record = {}; + // Reserve the whole batch before the first await: a run triggered while an earlier + // asset is still probing must not pick up a later one and probe it twice. + for (const a of missing) probedAssetIdsRef.current.add(a.id); for (const a of missing) { - probedAssetIdsRef.current.add(a.id); - dimsProbeAttemptsRef.current.set(a.id, (dimsProbeAttemptsRef.current.get(a.id) ?? 0) + 1); const entry: { video?: Dims; camera?: Dims } = {}; let failed = false; if (needsScreen(a)) { @@ -321,8 +322,12 @@ export function useTimeline() { else failed = true; } if (entry.video || entry.camera) probed[a.id] = entry; - // Unmarked so a later document change retries what is still missing. - if (failed) probedAssetIdsRef.current.delete(a.id); + // Unmarked so a later document change retries what is still missing. Only a failed + // probe counts toward the limit: a save deferred below must stay retryable. + if (failed) { + probedAssetIdsRef.current.delete(a.id); + dimsProbeAttemptsRef.current.set(a.id, (dimsProbeAttemptsRef.current.get(a.id) ?? 0) + 1); + } } if (Object.keys(probed).length === 0) return; // The store only takes a document once its save returns, so a write still in flight From d192c49c3e3de7d4f7aab059c7c9af3ca4978b51 Mon Sep 17 00:00:00 2001 From: EtienneLescot Date: Wed, 18 Nov 2026 17:34:49 +0100 Subject: [PATCH 3/7] fix(editor): treat a path toFileUrl cannot encode as a failed probe The batch is reserved up front, so a throw from toFileUrl skipped the cleanup and stranded every asset in it. Catch it per track instead. Refs #1610 --- src/lib/ai-edition/store/useTimeline.test.ts | 38 ++++++++++++++++++++ src/lib/ai-edition/store/useTimeline.ts | 16 ++++++--- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts index 04765d1c5..15a64bb3e 100644 --- a/src/lib/ai-edition/store/useTimeline.test.ts +++ b/src/lib/ai-edition/store/useTimeline.test.ts @@ -635,6 +635,44 @@ describe("useTimeline backfills missing source dimensions on load", () => { expect(secondCalls).toHaveLength(1); }); + // toFileUrl throws on a path it cannot encode. That must count as one failed probe, not + // strand the batch: the readable asset after it still gets its dims saved. + it("still saves the rest of the batch when one path cannot be turned into a URL", async () => { + const broken = { + ...sampleDoc.assets[0], + originalPath: "/tmp/bad\uD800.webm", + video: undefined, + }; + const readable = { + ...sampleDoc.assets[0], + id: "asset_2", + originalPath: "/tmp/readable.webm", + video: undefined, + }; + useProjectStore.setState({ + projectId: "proj_test", + document: { + ...sampleDoc, + assets: [broken, readable], + timeline: { + ...sampleDoc.timeline, + clips: [ + ...sampleDoc.timeline.clips, + { ...sampleDoc.timeline.clips[0], id: "clip_b", assetId: "asset_2" }, + ], + }, + }, + revision: 1, + status: "ready", + error: null, + }); + renderTimeline(); + await waitFor(() => expect(bridgeMocks.save).toHaveBeenCalledTimes(1)); + const assets = useProjectStore.getState().document?.assets; + expect(assets?.find((a) => a.id === "asset_2")?.video).toMatchObject({ width: 1920 }); + expect(assets?.find((a) => a.id === "asset_1")?.video).toBeUndefined(); + }); + it("does not re-probe a used asset with no reachable file more than once", async () => { probeVideoDimensionsMock.mockResolvedValue(null); // probe fails (unreadable file) useProjectStore.setState({ diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts index b30cd9c86..99bed57f0 100644 --- a/src/lib/ai-edition/store/useTimeline.ts +++ b/src/lib/ai-edition/store/useTimeline.ts @@ -303,21 +303,27 @@ export function useTimeline() { // Reserve the whole batch before the first await: a run triggered while an earlier // asset is still probing must not pick up a later one and probe it twice. for (const a of missing) probedAssetIdsRef.current.add(a.id); + // A path toFileUrl cannot encode (a lone surrogate throws) is a failed probe, not a + // throw that skips the cleanup below and strands the whole reserved batch. + const probe = async (path: string) => { + try { + return await probeVideoDimensions(toFileUrl(path), DIMS_PROBE_TIMEOUT_MS); + } catch { + return null; + } + }; for (const a of missing) { const entry: { video?: Dims; camera?: Dims } = {}; let failed = false; if (needsScreen(a)) { - const dims = await probeVideoDimensions(toFileUrl(a.originalPath), DIMS_PROBE_TIMEOUT_MS); + const dims = await probe(a.originalPath); if (dims) entry.video = dims; else failed = true; } // Probed independently of the screen: one file being unreadable must not cost // the other its dimensions, and a camera-less asset simply skips this. if (a.cameraTrack && needsCamera(a)) { - const dims = await probeVideoDimensions( - toFileUrl(a.cameraTrack.sourcePath), - DIMS_PROBE_TIMEOUT_MS, - ); + const dims = await probe(a.cameraTrack.sourcePath); if (dims) entry.camera = dims; else failed = true; } From b16fe2570a0f4e4c1148d8d070642d45e96ef1d8 Mon Sep 17 00:00:00 2001 From: EtienneLescot Date: Thu, 19 Nov 2026 19:18:09 +0100 Subject: [PATCH 4/7] fix(editor): serialize the dimension backfill's save so overlapping batches keep both Two batches waiting on the same pending save each re-read the same document, and the second save dropped the first one's dimensions while those assets stayed marked as probed. Chain the wait, re-read and save on a shared promise. Refs #1610 --- src/lib/ai-edition/store/useTimeline.test.ts | 55 +++++++++++++++ src/lib/ai-edition/store/useTimeline.ts | 72 +++++++++++--------- 2 files changed, 95 insertions(+), 32 deletions(-) diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts index 15a64bb3e..74d575488 100644 --- a/src/lib/ai-edition/store/useTimeline.test.ts +++ b/src/lib/ai-edition/store/useTimeline.test.ts @@ -673,6 +673,61 @@ describe("useTimeline backfills missing source dimensions on load", () => { expect(assets?.find((a) => a.id === "asset_1")?.video).toBeUndefined(); }); + // Two batches that wait on the same save must not both build on the same document: the + // second save would drop the first one's dimensions while that asset stays marked probed. + it("keeps both batches' dimensions when their saves would overlap", async () => { + let releaseSaves!: () => void; + const saves = new Promise<"drained">((resolve) => { + releaseSaves = () => resolve("drained"); + }); + waitForDocumentSavesMock.mockImplementationOnce(() => saves); + waitForDocumentSavesMock.mockImplementationOnce(() => saves); + const second = { + ...sampleDoc.assets[0], + id: "asset_2", + originalPath: "/tmp/second.webm", + video: undefined, + }; + const doc: AxcutDocument = { + ...sampleDoc, + assets: [{ ...sampleDoc.assets[0], video: undefined }, second], + }; + useProjectStore.setState({ + projectId: "proj_test", + document: doc, + revision: 1, + status: "ready", + error: null, + }); + renderTimeline(); + await waitFor(() => expect(waitForDocumentSavesMock).toHaveBeenCalledTimes(1)); + // asset_2 enters the timeline while the first batch waits: a second batch probes it. + act(() => { + useProjectStore.setState({ + document: { + ...doc, + timeline: { + ...doc.timeline, + clips: [ + ...doc.timeline.clips, + { ...doc.timeline.clips[0], id: "clip_b", assetId: "asset_2" }, + ], + }, + }, + revision: 2, + }); + }); + await waitFor(() => expect(probeVideoDimensionsMock).toHaveBeenCalledTimes(2)); + await act(async () => { + await new Promise((r) => setTimeout(r, 0)); + releaseSaves(); + }); + await waitFor(() => expect(bridgeMocks.save).toHaveBeenCalledTimes(2)); + const assets = useProjectStore.getState().document?.assets; + expect(assets?.find((a) => a.id === "asset_1")?.video).toMatchObject({ width: 1920 }); + expect(assets?.find((a) => a.id === "asset_2")?.video).toMatchObject({ width: 1920 }); + }); + it("does not re-probe a used asset with no reachable file more than once", async () => { probeVideoDimensionsMock.mockResolvedValue(null); // probe fails (unreadable file) useProjectStore.setState({ diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts index 99bed57f0..6ace93fc8 100644 --- a/src/lib/ai-edition/store/useTimeline.ts +++ b/src/lib/ai-edition/store/useTimeline.ts @@ -269,6 +269,9 @@ export function useTimeline() { // one slow open is not final (#1610) and an unreadable file still doesn't spin the effect. const probedAssetIdsRef = useRef>(new Set()); const dimsProbeAttemptsRef = useRef>(new Map()); + // Batches write one after another: two that waited on the same save would each re-read the + // same document, and the second save would drop the first one's dimensions. + const dimsWriteRef = useRef>(Promise.resolve()); useEffect(() => { if (!document) return; const usedAssetIds = new Set(document.timeline.clips.map((c) => c.assetId)); @@ -336,38 +339,43 @@ export function useTimeline() { } } if (Object.keys(probed).length === 0) return; - // The store only takes a document once its save returns, so a write still in flight - // (the fresh-recording auto-zooms, typically) is invisible here. Building on the store - // before it lands and saving after it would erase it. Wait it out; on a timeout, - // write nothing and let a later run probe again. - if ((await waitForDocumentSaves()) === "timeout") { - for (const id of Object.keys(probed)) probedAssetIdsRef.current.delete(id); - return; - } - // Re-read fresh state so a concurrent edit made while probing isn't stomped. - const current = useProjectStore.getState().document; - if (!current || current.project.id !== originatingProjectId) return; - // `history: false` — see the comment above: a backfill nobody asked for must - // not become the thing the next Ctrl+Z reverses. - await useProjectStore.getState().saveDocument( - { - ...current, - assets: current.assets.map((a) => { - const found = probed[a.id]; - if (!found) return a; - return { - ...a, - ...(found.video - ? { video: { codec: "unknown", fps: 0, ...a.video, ...found.video } } - : {}), - ...(found.camera && a.cameraTrack - ? { cameraTrack: { ...a.cameraTrack, ...found.camera } } - : {}), - }; - }), - }, - { history: false }, - ); + const write = async () => { + // The store only takes a document once its save returns, so a write still in flight + // (the fresh-recording auto-zooms, typically) is invisible here. Building on the store + // before it lands and saving after it would erase it. Wait it out; on a timeout, + // write nothing and let a later run probe again. + if ((await waitForDocumentSaves()) === "timeout") { + for (const id of Object.keys(probed)) probedAssetIdsRef.current.delete(id); + return; + } + // Re-read fresh state so a concurrent edit made while probing isn't stomped. + const current = useProjectStore.getState().document; + if (!current || current.project.id !== originatingProjectId) return; + // `history: false` — see the comment above: a backfill nobody asked for must + // not become the thing the next Ctrl+Z reverses. + await useProjectStore.getState().saveDocument( + { + ...current, + assets: current.assets.map((a) => { + const found = probed[a.id]; + if (!found) return a; + return { + ...a, + ...(found.video + ? { video: { codec: "unknown", fps: 0, ...a.video, ...found.video } } + : {}), + ...(found.camera && a.cameraTrack + ? { cameraTrack: { ...a.cameraTrack, ...found.camera } } + : {}), + }; + }), + }, + { history: false }, + ); + }; + const queued = dimsWriteRef.current.then(write); + dimsWriteRef.current = queued.catch(() => undefined); // a failed write must not stall the next batch + await queued; })(); }, [document]); From 127d0fbb6742e751426b398091a07a4a1af786d6 Mon Sep 17 00:00:00 2001 From: EtienneLescot Date: Thu, 19 Nov 2026 19:30:02 +0100 Subject: [PATCH 5/7] fix(editor): keep the backfill save inside useTimeline for the write audit The named write helper moved the saveDocument call site out of useTimeline, which the document write audit tracks by enclosing function. Chain the write inline instead. Refs #1610 --- src/lib/ai-edition/store/useTimeline.ts | 74 ++++++++++++------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts index 6ace93fc8..9aa09c0dc 100644 --- a/src/lib/ai-edition/store/useTimeline.ts +++ b/src/lib/ai-edition/store/useTimeline.ts @@ -339,43 +339,43 @@ export function useTimeline() { } } if (Object.keys(probed).length === 0) return; - const write = async () => { - // The store only takes a document once its save returns, so a write still in flight - // (the fresh-recording auto-zooms, typically) is invisible here. Building on the store - // before it lands and saving after it would erase it. Wait it out; on a timeout, - // write nothing and let a later run probe again. - if ((await waitForDocumentSaves()) === "timeout") { - for (const id of Object.keys(probed)) probedAssetIdsRef.current.delete(id); - return; - } - // Re-read fresh state so a concurrent edit made while probing isn't stomped. - const current = useProjectStore.getState().document; - if (!current || current.project.id !== originatingProjectId) return; - // `history: false` — see the comment above: a backfill nobody asked for must - // not become the thing the next Ctrl+Z reverses. - await useProjectStore.getState().saveDocument( - { - ...current, - assets: current.assets.map((a) => { - const found = probed[a.id]; - if (!found) return a; - return { - ...a, - ...(found.video - ? { video: { codec: "unknown", fps: 0, ...a.video, ...found.video } } - : {}), - ...(found.camera && a.cameraTrack - ? { cameraTrack: { ...a.cameraTrack, ...found.camera } } - : {}), - }; - }), - }, - { history: false }, - ); - }; - const queued = dimsWriteRef.current.then(write); - dimsWriteRef.current = queued.catch(() => undefined); // a failed write must not stall the next batch - await queued; + dimsWriteRef.current = dimsWriteRef.current + .then(async () => { + // The store only takes a document once its save returns, so a write still in flight + // (the fresh-recording auto-zooms, typically) is invisible here. Building on the store + // before it lands and saving after it would erase it. Wait it out; on a timeout, + // write nothing and let a later run probe again. + if ((await waitForDocumentSaves()) === "timeout") { + for (const id of Object.keys(probed)) probedAssetIdsRef.current.delete(id); + return; + } + // Re-read fresh state so a concurrent edit made while probing isn't stomped. + const current = useProjectStore.getState().document; + if (!current || current.project.id !== originatingProjectId) return; + // `history: false` — see the comment above: a backfill nobody asked for must + // not become the thing the next Ctrl+Z reverses. + await useProjectStore.getState().saveDocument( + { + ...current, + assets: current.assets.map((a) => { + const found = probed[a.id]; + if (!found) return a; + return { + ...a, + ...(found.video + ? { video: { codec: "unknown", fps: 0, ...a.video, ...found.video } } + : {}), + ...(found.camera && a.cameraTrack + ? { cameraTrack: { ...a.cameraTrack, ...found.camera } } + : {}), + }; + }), + }, + { history: false }, + ); + }) + // A failed write must not stall the next batch. + .catch(() => undefined); })(); }, [document]); From 8a4ac49b8352aa4df2b7fff16c92b4b1a9a8af0a Mon Sep 17 00:00:00 2001 From: EtienneLescot Date: Thu, 19 Nov 2026 19:44:07 +0100 Subject: [PATCH 6/7] fix(editor): retry the dimension backfill after a failed save A probe that succeeded but whose save failed stayed reserved for the session, so its dimensions stayed missing until the editor remounted. Release it on a failed save, without counting a probe failure. Refs #1610 --- src/lib/ai-edition/store/useTimeline.test.ts | 23 ++++++++++++++++++++ src/lib/ai-edition/store/useTimeline.ts | 5 ++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts index 74d575488..773a689cc 100644 --- a/src/lib/ai-edition/store/useTimeline.test.ts +++ b/src/lib/ai-edition/store/useTimeline.test.ts @@ -583,6 +583,29 @@ describe("useTimeline backfills missing source dimensions on load", () => { expect(probeVideoDimensionsMock).toHaveBeenCalledTimes(4); }); + it("retries a successful probe whose save failed on a later change", async () => { + bridgeMocks.save.mockResolvedValueOnce({ success: false, error: "disk full" }); + const unprobed = { ...sampleDoc, assets: [{ ...sampleDoc.assets[0], video: undefined }] }; + useProjectStore.setState({ + projectId: "proj_test", + document: unprobed, + revision: 1, + status: "ready", + error: null, + }); + renderTimeline(); + await waitFor(() => expect(bridgeMocks.save).toHaveBeenCalledTimes(1)); + await act(async () => { + await new Promise((r) => setTimeout(r, 0)); + }); + act(() => { + useProjectStore.setState({ document: { ...unprobed, zoomRanges: [] }, revision: 2 }); + }); + await waitFor(() => expect(bridgeMocks.save).toHaveBeenCalledTimes(2)); + const saved = useProjectStore.getState().document?.assets.find((a) => a.id === "asset_1"); + expect(saved?.video).toMatchObject({ width: 1920, height: 1080 }); + }); + // A run started while an earlier asset is still probing must not take a later asset of // the first batch: it would be probed twice. it("does not probe a queued asset again from a run started while the batch is out", async () => { diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts index 9aa09c0dc..13bf51026 100644 --- a/src/lib/ai-edition/store/useTimeline.ts +++ b/src/lib/ai-edition/store/useTimeline.ts @@ -354,7 +354,7 @@ export function useTimeline() { if (!current || current.project.id !== originatingProjectId) return; // `history: false` — see the comment above: a backfill nobody asked for must // not become the thing the next Ctrl+Z reverses. - await useProjectStore.getState().saveDocument( + const saved = await useProjectStore.getState().saveDocument( { ...current, assets: current.assets.map((a) => { @@ -373,6 +373,9 @@ export function useTimeline() { }, { history: false }, ); + // A failed save (disk full, say) leaves the dims unwritten: a later change retries them, + // without counting it as a failed probe. + if (!saved) for (const id of Object.keys(probed)) probedAssetIdsRef.current.delete(id); }) // A failed write must not stall the next batch. .catch(() => undefined); From e8a84fef3a2df49a63a108f76922c7003fa8b678 Mon Sep 17 00:00:00 2001 From: EtienneLescot Date: Thu, 19 Nov 2026 20:00:39 +0100 Subject: [PATCH 7/7] fix(editor): bound the dimension backfill save so a hung save cannot stall the chain The backfill batches write one after another, so a save that never settled held every later batch and kept their assets reserved for the session. Wait on it through saveWithDeadline and release the batch on a timeout, as on a failed save. Refs #1610 --- src/lib/ai-edition/store/useTimeline.test.ts | 40 +++++++++++++++++ src/lib/ai-edition/store/useTimeline.ts | 46 +++++++++++--------- 2 files changed, 65 insertions(+), 21 deletions(-) diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts index 773a689cc..229b8f8f4 100644 --- a/src/lib/ai-edition/store/useTimeline.test.ts +++ b/src/lib/ai-edition/store/useTimeline.test.ts @@ -606,6 +606,46 @@ describe("useTimeline backfills missing source dimensions on load", () => { expect(saved?.video).toMatchObject({ width: 1920, height: 1080 }); }); + // A save that never settles must not hold the batch chain, or the asset stays reserved and + // no later change ever probes it again. + it("releases a batch whose save never settles once the deadline passes", async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }); + let settleHungSave!: () => void; + try { + bridgeMocks.save.mockImplementationOnce( + () => + new Promise((resolve) => { + settleHungSave = () => resolve({ success: false, error: "hung" }); + }), + ); + const unprobed = { ...sampleDoc, assets: [{ ...sampleDoc.assets[0], video: undefined }] }; + useProjectStore.setState({ + projectId: "proj_test", + document: unprobed, + revision: 1, + status: "ready", + error: null, + }); + renderTimeline(); + await waitFor(() => expect(bridgeMocks.save).toHaveBeenCalledTimes(1)); + await act(async () => { + await vi.advanceTimersByTimeAsync(10_000); + }); + act(() => { + useProjectStore.setState({ document: { ...unprobed, zoomRanges: [] }, revision: 2 }); + }); + await waitFor(() => expect(probeVideoDimensionsMock).toHaveBeenCalledTimes(2)); + // Settle it so the store's in-flight count does not leak into the next test. + await act(async () => { + settleHungSave(); + await vi.advanceTimersByTimeAsync(0); + }); + await waitFor(() => expect(bridgeMocks.save).toHaveBeenCalledTimes(2)); + } finally { + vi.useRealTimers(); + } + }); + // A run started while an earlier asset is still probing must not take a later asset of // the first batch: it would be probed twice. it("does not probe a queued asset again from a run started while the batch is out", async () => { diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts index 13bf51026..3171ac416 100644 --- a/src/lib/ai-edition/store/useTimeline.ts +++ b/src/lib/ai-edition/store/useTimeline.ts @@ -354,28 +354,32 @@ export function useTimeline() { if (!current || current.project.id !== originatingProjectId) return; // `history: false` — see the comment above: a backfill nobody asked for must // not become the thing the next Ctrl+Z reverses. - const saved = await useProjectStore.getState().saveDocument( - { - ...current, - assets: current.assets.map((a) => { - const found = probed[a.id]; - if (!found) return a; - return { - ...a, - ...(found.video - ? { video: { codec: "unknown", fps: 0, ...a.video, ...found.video } } - : {}), - ...(found.camera && a.cameraTrack - ? { cameraTrack: { ...a.cameraTrack, ...found.camera } } - : {}), - }; - }), - }, - { history: false }, + const saved = await saveWithDeadline( + useProjectStore.getState().saveDocument( + { + ...current, + assets: current.assets.map((a) => { + const found = probed[a.id]; + if (!found) return a; + return { + ...a, + ...(found.video + ? { video: { codec: "unknown", fps: 0, ...a.video, ...found.video } } + : {}), + ...(found.camera && a.cameraTrack + ? { cameraTrack: { ...a.cameraTrack, ...found.camera } } + : {}), + }; + }), + }, + { history: false }, + ), ); - // A failed save (disk full, say) leaves the dims unwritten: a later change retries them, - // without counting it as a failed probe. - if (!saved) for (const id of Object.keys(probed)) probedAssetIdsRef.current.delete(id); + // A failed save (disk full, say), or one that never settles, leaves the dims unwritten: + // a later change retries them without counting a failed probe. The deadline keeps a + // hung save from stalling every batch queued behind it. + if (saved !== true) + for (const id of Object.keys(probed)) probedAssetIdsRef.current.delete(id); }) // A failed write must not stall the next batch. .catch(() => undefined);