fix(core): root-cause id-less media wash in timingCompiler getAttr, drop the band-aid (#1792)

* fix(core): root-cause the id-less media wash in getAttr, drop the band-aid

The blank-wash/dropped-audio fix in #1790 added assignMissingMediaIds in the
producer to stamp ids onto id-less timed media. That was a band-aid: the real
cause is timingCompiler's getAttr, whose regex had no name boundary at all, so
getAttr(tag, "id") matched the trailing id="…" inside data-hf-id="…". compileTag
saw a phantom id and skipped its existing hf-video-N/hf-audio-N injection,
leaving the element with no real el.id — which the render pipeline keys off of.

Fix getAttr with the same (?<![\w-]) lookbehind used for the lint readAttr fix.
compileTag's auto-id injection now fires for data-hf-id-only media, in both the
main composition and sub-compositions (parseSubCompositions runs the same
compileTimingAttrs pass), so assignMissingMediaIds is removed entirely.

Extends the regression fixture with a standalone id-less <audio> (the dropped-
audio side, previously untested) and raises minAudioCorrelation to 0.9. Adds a
timingCompiler test for the data-hf-id/id boundary.

* test(producer): use seeded pink noise (not a pure sine) for fixture audio

A continuous sine anti-aligns under the audio cross-correlation (correlation
-1.0 from a sub-period offset). Broadband seeded noise correlates robustly.

* test: cover audio-side id injection via unit test; keep render fixture video-only

The audio render-baseline used synthetic sine/noise, which anti-aligns under
the harness audio cross-correlation (deterministic -1.0). Real audio fixtures
are unaffected. Cover the audio side of the boundary fix with a deterministic
timingCompiler unit test (id-less <audio> gets hf-audio-N) instead, and keep
the render fixture video-only.

* test(producer): regenerate baseline under the root fix (hf-video-N from compileTag)
This commit is contained in:
Miguel Ángel
2026-06-29 19:07:36 -07:00
committed by GitHub
parent faea9f2267
commit 0dfedd111c
4 changed files with 32 additions and 37 deletions
@@ -16,6 +16,29 @@ describe("compileTimingAttrs", () => {
expect(unresolved).toHaveLength(0);
});
it("injects a real id when the element has only data-hf-id (not a phantom match)", () => {
// Regression: getAttr(tag, "id") matched the trailing id="…" inside
// data-hf-id="…" and returned a phantom, so compileTag skipped its
// hf-video-N injection — leaving no real el.id and a blank-wash render.
const html = '<video data-hf-id="hf-bgvideo01" src="a.mp4" data-start="0" data-duration="2">';
const { html: compiled } = compileTimingAttrs(html);
expect(compiled).toContain('id="hf-video-0"');
expect(compiled).toContain('data-hf-id="hf-bgvideo01"');
expect(compiled).toContain('data-end="2"');
});
it("injects a real id on an audio element that has only data-hf-id", () => {
// Audio side of the same bug: the mixer selects `audio[id][src]`, so a
// phantom-id match meant the element was dropped (silent). compileTag must
// inject a real hf-audio-N so the mixer can find it.
const html = '<audio data-hf-id="hf-bgaudio01" src="a.mp3" data-start="0" data-duration="2">';
const { html: compiled } = compileTimingAttrs(html);
expect(compiled).toContain('id="hf-audio-0"');
expect(compiled).toContain('data-hf-id="hf-bgaudio01"');
});
it("leaves data-end unchanged when already present", () => {
const html = '<video id="v1" src="a.mp4" data-start="0" data-end="3">';
const { html: compiled, unresolved } = compileTimingAttrs(html);
+7 -1
View File
@@ -60,7 +60,13 @@ export function shouldClampMediaDuration(declaredDuration: number, maxDuration:
// ── Helpers ──────────────────────────────────────────────────────────────
function getAttr(tag: string, attr: string): string | null {
const match = tag.match(new RegExp(`${attr}=["']([^"']+)["']`));
// `(?<![\w-])` anchors the attribute name to a fresh start. Without it,
// `getAttr(tag, "id")` matches the trailing `id="…"` inside `data-hf-id="…"`
// (and "src" inside `data-src`, etc.) and returns a phantom value. That bug
// made compileTag believe a Studio-stamped `data-hf-id`-only element already
// had an `id`, so it skipped its `hf-video-N` injection — leaving the element
// with no real `el.id`, which the render pipeline keys off of (blank wash).
const match = tag.match(new RegExp(`(?<![\\w-])${attr}=["']([^"']+)["']`));
return match ? (match[1] ?? null) : null;
}
+1 -35
View File
@@ -1360,32 +1360,6 @@ function rewriteUnresolvableGsapToCdn(html: string, projectDir: string): string
* with all media metadata resolved.
*/
// fallow-ignore-next-line complexity
/**
* Every render stage identifies `<video>`/`<audio>` by their real `id`: frame
* extraction keys injected stills as `__render_frame_<id>__`, the runtime
* frame-swap matches on `el.id` (runtime/media.ts), and the audio mixer selects
* `audio[id][src]`. A timed media element with no `id` — e.g. one carrying only
* `data-hf-id`, which is what Studio stamps — has an empty `el.id`, so its
* injected frames never match: the video renders as a blank wash and any
* separate `<audio>` is silently dropped. Assign a stable positional `id` to
* every timed media element missing one, on the same HTML that is parsed for
* media and served to the renderer, so the whole pipeline shares one identity.
* `data-hf-id` is intentionally NOT reused as the id — it is a Studio edit
* handle, not the render identity.
*/
function assignMissingMediaIds(html: string): string {
const { document } = parseHTML(html);
const media = document.querySelectorAll("video[data-start], audio[data-start]");
let seq = 0;
let changed = false;
for (const el of Array.from(media)) {
if (el.getAttribute("id")) continue;
el.setAttribute("id", `hf-media-${seq++}`);
changed = true;
}
return changed ? document.toString() : html;
}
export async function compileForRender(
projectDir: string,
htmlPath: string,
@@ -1510,15 +1484,7 @@ export async function compileForRender(
// Collect assets that resolve outside projectDir (e.g. ../shared-assets/hero.png).
// These can't be served by the file server, so we map them to paths the
// orchestrator will copy into the compiled output directory.
const { html: htmlBeforeMediaIds, externalAssets } = collectExternalAssets(
embeddedHtml,
projectDir,
);
// Give every timed <video>/<audio> a real `id` before any stage parses or
// serves this HTML — id-less media (e.g. carrying only `data-hf-id`) would
// otherwise render as a blank wash with dropped audio. See assignMissingMediaIds.
const html = assignMissingMediaIds(htmlBeforeMediaIds);
const { html, externalAssets } = collectExternalAssets(embeddedHtml, projectDir);
for (const [relPath, absPath] of remoteMediaAssets) {
externalAssets.set(relPath, absPath);
@@ -44,7 +44,7 @@
pipeline could not match its injected frames (keyed on the empty
el.id), so the footage rendered as a blank white/grey wash. The
baseline must show the testsrc2 footage, not a flat fill. -->
<video id="hf-media-0" data-hf-id="hf-bgvideo01" class="clip" src="clip.mp4" muted playsinline data-start="0" data-duration="2" data-track-index="0" data-end="2" data-has-audio="false"></video>
<video data-hf-id="hf-bgvideo01" class="clip" src="clip.mp4" muted playsinline data-start="0" data-duration="2" data-track-index="0" id="hf-video-0" data-end="2" data-has-audio="false"></video>
<div id="caption" class="label clip" data-start="0" data-duration="2" data-track-index="10">
FOOTAGE