From b54e4981f2f710e928215e3138e1fa075b1d1108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Fri, 13 Nov 2026 03:09:18 +0000 Subject: [PATCH] fix(engine): keep media ids that differ only by case in separate folders (#7635) * fix(engine): keep media ids that differ only by case in separate folders macOS and Windows disks ignore case, and APFS ignores Unicode normalization, but mediaIdPathSegment passed plain ids through as written. Two clips such as `s12-vB1` and `s12-vb1` therefore shared one `__hyperframes_video_frames` folder: whichever was staged first kept it, and the other clip read its frames. A longer clip then failed mid-render with ENOENT on a frame past the shorter clip's end; a same-length pair rendered the wrong footage silently. Only a lowercase, NFC-normalized id now maps to itself. Any other spelling gets the existing readable~digest form, so every per-id path (frame folders, the tail probe, audio work files, distributed plans) stays distinct on a case-insensitive disk. * fix(engine): name the case-folding check instead of commenting it Keeps mediaPathSegment.ts at or under its comment share at the merge base. --- .../engine/src/utils/mediaPathSegment.test.ts | 16 ++++++++ packages/engine/src/utils/mediaPathSegment.ts | 11 +++++- .../src/services/renderOrchestrator.test.ts | 38 +++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/packages/engine/src/utils/mediaPathSegment.test.ts b/packages/engine/src/utils/mediaPathSegment.test.ts index c35e64c71c..8557b26d19 100644 --- a/packages/engine/src/utils/mediaPathSegment.test.ts +++ b/packages/engine/src/utils/mediaPathSegment.test.ts @@ -24,6 +24,22 @@ describe("mediaIdPathSegment", () => { }, ); + it.each([ + ["s12-vb1", "s12-vB1"], + ["clip", "CLIP"], + ["Intro", "INTRO"], + ["caf\u00e9", "cafe\u0301"], + ])("never lets %j and %j share a folder on a case-insensitive disk", (a, b) => { + const fold = (segment: string) => segment.normalize("NFC").toLowerCase(); + expect(fold(mediaIdPathSegment(a))).not.toBe(fold(mediaIdPathSegment(b))); + }); + + it("names an id with capitals by a digest of its exact text", () => { + const segment = mediaIdPathSegment("s12-vB1"); + expect(segment).toMatch(/^s12-vB1~[0-9a-f]{12}$/); + expect(mediaIdPathSegment("s12-Vb1")).not.toBe(segment); + }); + it("keeps distinct unsafe ids apart", () => { expect(mediaIdPathSegment("a/b")).not.toBe(mediaIdPathSegment("a\\b")); expect(mediaIdPathSegment("a/b")).not.toBe("a_b"); diff --git a/packages/engine/src/utils/mediaPathSegment.ts b/packages/engine/src/utils/mediaPathSegment.ts index fa793d92fb..230e741fe9 100644 --- a/packages/engine/src/utils/mediaPathSegment.ts +++ b/packages/engine/src/utils/mediaPathSegment.ts @@ -10,12 +10,21 @@ const READABLE_MAX_BYTES = 64; * form plus a digest of the whole id, so the two namespaces never meet. */ export function mediaIdPathSegment(id: string): string { - if (PLAIN_SEGMENT.test(id) && !GENERATED_SUFFIX.test(id)) return id; + if ( + PLAIN_SEGMENT.test(id) && + !GENERATED_SUFFIX.test(id) && + survivesCaseAndUnicodeFoldingOnMacAndWindowsDisks(id) + ) + return id; const readable = truncateUtf8(id.replace(/[/\\:]/g, "_"), READABLE_MAX_BYTES); const digest = createHash("sha256").update(id).digest("hex").slice(0, 12); return `${readable}~${digest}`; } +function survivesCaseAndUnicodeFoldingOnMacAndWindowsDisks(id: string): boolean { + return id === id.toLowerCase() && id === id.normalize("NFC"); +} + function truncateUtf8(text: string, maxBytes: number): string { let bytes = 0; let out = ""; diff --git a/packages/producer/src/services/renderOrchestrator.test.ts b/packages/producer/src/services/renderOrchestrator.test.ts index 73cdb828e1..15cf12a8cb 100644 --- a/packages/producer/src/services/renderOrchestrator.test.ts +++ b/packages/producer/src/services/renderOrchestrator.test.ts @@ -1097,6 +1097,44 @@ describe("materializeExtractedFramesForCompiledDir", () => { expect(copies).toEqual([{ src: outputDir, dest: linkPath }]); expect(extracted.framePaths.get(0)).toBe(win32.join(linkPath, "frame_000001.jpg")); }); + + it("gives ids that differ only by case their own frames on a case-insensitive disk", () => { + // macOS and Windows disks fold case: `s12-vB1` and `s12-vb1` named as-is are one folder. + const entries = new Map(); + const fold = (path: string) => path.toLowerCase(); + const clip = (videoId: string, cacheDir: string, frameCount: number) => ({ + videoId, + outputDir: cacheDir, + framePaths: new Map( + Array.from({ length: frameCount }, (_, i): [number, string] => [ + i, + join(cacheDir, `frame_${String(i + 1).padStart(5, "0")}.jpg`), + ]), + ), + }); + const short = clip("s12-vB1", "/cache/hook", 64); + const long = clip("s12-vb1", "/cache/sparks", 66); + + materializeExtractedFramesForCompiledDir([short, long], "/job/compiled", { + fileSystem: { + existsSync: (path) => entries.has(fold(String(path))), + mkdirSync: () => undefined, + symlinkSync: (target, path) => { + if (entries.has(fold(String(path)))) { + throw Object.assign(new Error("EEXIST"), { code: "EEXIST" }); + } + entries.set(fold(String(path)), String(target)); + }, + cpSync: () => { + throw new Error("a symlink is expected"); + }, + }, + }); + + const targetOf = (framePath: string) => entries.get(fold(join(framePath, ".."))); + expect(targetOf(long.framePaths.get(64)!)).toBe("/cache/sparks"); + expect(targetOf(short.framePaths.get(63)!)).toBe("/cache/hook"); + }); }); describe("writeCompiledArtifacts — external assets on Windows drive-letter paths (GH #321)", () => {