Skip to content

Commit c171e1c

Browse files
committed
fix(cloud): accept a legacy plan's audio artifact name for one release
Review raised a rolling-deploy window I had flagged but left undecided: `plan` and `assemble` are separate invocations bridged by object storage, so a pre-rollout planner can be paired with a post-rollout assembler. Both readers locate the artifact by existence alone, which makes that pairing a silently muted video rather than an error. That is reachable enough to be worth two lines, so reads now accept the old name while writes only ever emit the new one. Give the fallback one owner (`resolvePlanAudioPath` / `isPlanAudioArtifactPath`) rather than four call sites, marked for deletion one release out. Also fixes a hole in the first pass of this: the plan-v2 materializer matched either name but then joined the CURRENT one, so a legacy plan resolved to a path that was never written. It now joins the artifact's own name. Review nits in the same pass: correct the pad-branch docstring, which still described a concat-copy shape the pad branch stopped using when it moved to apad + re-encode, and fix the Windows fixture's stale `.aac` output extension so it cannot model a shape that reintroduces the priming delay.
1 parent 927f44e commit c171e1c

8 files changed

Lines changed: 120 additions & 21 deletions

File tree

‎packages/aws-lambda/src/handler.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import {
2424
listPlanV2ArtifactsForTarget,
2525
materializePlanV2Target,
2626
plan,
27+
isPlanAudioArtifactPath,
2728
PLAN_AUDIO_RELATIVE_PATH,
29+
resolvePlanAudioPath,
2830
planV2WithPublisher,
2931
type PlanResult,
3032
type PlanV2Artifact,
@@ -393,7 +395,7 @@ async function handlePlanV2(
393395
Width: manifest.width,
394396
Height: manifest.height,
395397
Format: manifest.format,
396-
HasAudio: manifest.artifacts.some((artifact) => artifact.path === PLAN_AUDIO_RELATIVE_PATH),
398+
HasAudio: manifest.artifacts.some((artifact) => isPlanAudioArtifactPath(artifact.path)),
397399
AudioS3Uri: null,
398400
FfmpegVersion: manifest.ffmpegVersion,
399401
ProducerVersion: manifest.producerVersion,
@@ -571,7 +573,7 @@ async function handleAssemble(
571573

572574
let audioPath: string | null = null;
573575
if (event.AudioS3Uri) {
574-
audioPath = join(planDir, PLAN_AUDIO_RELATIVE_PATH);
576+
audioPath = resolvePlanAudioPath(planDir) ?? join(planDir, PLAN_AUDIO_RELATIVE_PATH);
575577
await downloadS3ObjectToFile(s3, event.AudioS3Uri, audioPath);
576578
}
577579

@@ -619,9 +621,7 @@ async function handleAssembleV2(
619621
const planDir = await downloadAndMaterializePlanV2(s3, event, { role: "assembler" }, work);
620622
// `downloadAndMaterializePlanV2` materializes atomically. Audio is
621623
// assembler-only and lives at the familiar v1-compatible location.
622-
const audioPath = existsSync(join(planDir, PLAN_AUDIO_RELATIVE_PATH))
623-
? join(planDir, PLAN_AUDIO_RELATIVE_PATH)
624-
: null;
624+
const audioPath = resolvePlanAudioPath(planDir);
625625
const chunkPaths = await downloadChunkObjects(s3, event.ChunkS3Uris, work, event.Format);
626626
const finalOutput =
627627
event.Format === "png-sequence"

‎packages/gcp-cloud-run/src/server.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ import {
3232
listPlanV2ArtifactsForTarget,
3333
materializePlanV2Target,
3434
plan,
35+
isPlanAudioArtifactPath,
3536
PLAN_AUDIO_RELATIVE_PATH,
37+
resolvePlanAudioPath,
3638
planV2WithPublisher,
3739
type PlanResult,
3840
type PlanV2Artifact,
@@ -398,7 +400,7 @@ async function handlePlanV2(
398400
Width: manifest.width,
399401
Height: manifest.height,
400402
Format: manifest.format,
401-
HasAudio: manifest.artifacts.some((artifact) => artifact.path === PLAN_AUDIO_RELATIVE_PATH),
403+
HasAudio: manifest.artifacts.some((artifact) => isPlanAudioArtifactPath(artifact.path)),
402404
AudioGcsUri: null,
403405
FfmpegVersion: manifest.ffmpegVersion,
404406
ProducerVersion: manifest.producerVersion,
@@ -568,7 +570,7 @@ async function handleAssemble(
568570
// only for backward compatibility with an older Plan that uploaded it
569571
// standalone.
570572
let audioPath: string | null = null;
571-
const planAudio = join(planDir, PLAN_AUDIO_RELATIVE_PATH);
573+
const planAudio = resolvePlanAudioPath(planDir) ?? join(planDir, PLAN_AUDIO_RELATIVE_PATH);
572574
if (existsSync(planAudio) && statSync(planAudio).size > 0) {
573575
audioPath = planAudio;
574576
} else if (event.AudioGcsUri) {
@@ -620,9 +622,7 @@ async function handleAssembleV2(
620622
const work = mkdtempSync(join(deps?.tmpRoot ?? tmpdir(), "hf-cr-assemble-v2-"));
621623
try {
622624
const planDir = await downloadAndMaterializePlanV2(storage, event, { role: "assembler" }, work);
623-
const audioPath = existsSync(join(planDir, PLAN_AUDIO_RELATIVE_PATH))
624-
? join(planDir, PLAN_AUDIO_RELATIVE_PATH)
625-
: null;
625+
const audioPath = resolvePlanAudioPath(planDir);
626626
const chunkPaths = await downloadChunkObjects(storage, event.ChunkGcsUris, work, event.Format);
627627
const finalOutput =
628628
event.Format === "png-sequence"

‎packages/producer/src/distributed.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,12 @@ export { PlanVideosMetadataError, type DistributedFormat } from "./services/dist
154154
// extension selects the container, so they must read it from here rather than
155155
// restate it: a literal that drifts from the writer's is a silently missing
156156
// audio track, not a loud failure.
157-
export { PLAN_AUDIO_RELATIVE_PATH } from "./services/distributed/shared.js";
157+
export {
158+
isPlanAudioArtifactPath,
159+
PLAN_AUDIO_LEGACY_RELATIVE_PATH,
160+
PLAN_AUDIO_RELATIVE_PATH,
161+
resolvePlanAudioPath,
162+
} from "./services/distributed/shared.js";
158163

159164
// ── Plan-time shared types from `freezePlan` ───────────────────────────────
160165
// Re-exported so adopters that deserialize a planDir's `meta/encoder.json`
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { afterEach, describe, expect, it } from "vitest";
2+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import {
6+
isPlanAudioArtifactPath,
7+
PLAN_AUDIO_LEGACY_RELATIVE_PATH,
8+
PLAN_AUDIO_RELATIVE_PATH,
9+
resolvePlanAudioPath,
10+
} from "./shared.js";
11+
12+
/**
13+
* `plan` and `assemble` are separate invocations bridged by object storage, so a
14+
* rolling deploy can pair a pre-rollout planner with a post-rollout assembler.
15+
* Both readers locate the artifact by existence alone, which would make that
16+
* pairing a silently muted video rather than an error.
17+
*
18+
* Delete this file, the legacy constant, and the fallback branch one release
19+
* after the container change ships.
20+
*/
21+
describe("plan audio artifact compatibility", () => {
22+
const dirs: string[] = [];
23+
24+
afterEach(() => {
25+
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
26+
});
27+
28+
const planDir = (files: string[]): string => {
29+
const dir = mkdtempSync(join(tmpdir(), "hf-plan-audio-"));
30+
dirs.push(dir);
31+
for (const name of files) writeFileSync(join(dir, name), "audio");
32+
return dir;
33+
};
34+
35+
it("prefers the current name", () => {
36+
const dir = planDir([PLAN_AUDIO_RELATIVE_PATH, PLAN_AUDIO_LEGACY_RELATIVE_PATH]);
37+
expect(resolvePlanAudioPath(dir)).toBe(join(dir, PLAN_AUDIO_RELATIVE_PATH));
38+
});
39+
40+
it("falls back to a legacy plan's name so a rolling deploy keeps its audio", () => {
41+
const dir = planDir([PLAN_AUDIO_LEGACY_RELATIVE_PATH]);
42+
expect(resolvePlanAudioPath(dir)).toBe(join(dir, PLAN_AUDIO_LEGACY_RELATIVE_PATH));
43+
});
44+
45+
it("reports no audio when the plan carries neither name", () => {
46+
expect(resolvePlanAudioPath(planDir([]))).toBeNull();
47+
});
48+
49+
it("recognises both names as the audio artifact, and nothing else", () => {
50+
expect(isPlanAudioArtifactPath(PLAN_AUDIO_RELATIVE_PATH)).toBe(true);
51+
expect(isPlanAudioArtifactPath(PLAN_AUDIO_LEGACY_RELATIVE_PATH)).toBe(true);
52+
expect(isPlanAudioArtifactPath("plan.json")).toBe(false);
53+
expect(isPlanAudioArtifactPath("meta/chunks.json")).toBe(false);
54+
});
55+
56+
it("writes only the current name", () => {
57+
expect(PLAN_AUDIO_RELATIVE_PATH).not.toBe(PLAN_AUDIO_LEGACY_RELATIVE_PATH);
58+
expect(PLAN_AUDIO_RELATIVE_PATH.endsWith(".m4a")).toBe(true);
59+
});
60+
});

‎packages/producer/src/services/distributed/planV2.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import {
4747
import { PLAN_V2_INTEGRITY_UNRECOVERABLE, PlanV2IntegrityError } from "./planV2Errors.js";
4848
import { planV2BlobPath } from "./planV2Layout.js";
4949
import {
50-
PLAN_AUDIO_RELATIVE_PATH,
50+
isPlanAudioArtifactPath,
5151
PLAN_VIDEOS_META_RELATIVE_PATH,
5252
type DistributedFormat,
5353
parsePlanVideosJson as parseSharedPlanVideosJson,
@@ -283,7 +283,7 @@ function artifactTargets(
283283
path: string,
284284
videoDependencies: ReadonlyMap<string, readonly number[]> | null,
285285
): Pick<PlanV2Artifact, "chunks" | "assembler"> {
286-
if (path === PLAN_AUDIO_RELATIVE_PATH) return { chunks: [], assembler: true };
286+
if (isPlanAudioArtifactPath(path)) return { chunks: [], assembler: true };
287287
if (path === "plan.json" || path === "meta/chunks.json" || path === "meta/encoder.json") {
288288
return { chunks: "all", assembler: true };
289289
}
@@ -898,6 +898,7 @@ export function materializePlanV2Target(
898898
}
899899
const manifest = readPlanV2Manifest(planV2Dir);
900900
const artifacts = listPlanV2ArtifactsForTarget(manifest, target);
901+
const audioArtifact = artifacts.find((artifact) => isPlanAudioArtifactPath(artifact.path));
901902
const verified = artifacts.map((artifact) => ({
902903
artifact,
903904
sourcePath: verifyBlob(planV2Dir, artifact),
@@ -933,10 +934,14 @@ export function materializePlanV2Target(
933934
sourcePlanV1Hash: getPlanV2ExecutionPlanHash(manifest),
934935
artifactCount: artifacts.length,
935936
sizeBytes: artifacts.reduce((sum, artifact) => sum + artifact.sizeBytes, 0),
937+
// Join the artifact's OWN name, not the current constant: a plan written
938+
// before the container change carries the legacy name, and materializing it
939+
// under the new one would point at a file that was never written.
936940
audioPath:
937-
target.role === "assembler" &&
938-
artifacts.some((artifact) => artifact.path === PLAN_AUDIO_RELATIVE_PATH)
939-
? join(destinationDir, PLAN_AUDIO_RELATIVE_PATH)
941+
target.role === "assembler"
942+
? audioArtifact
943+
? join(destinationDir, audioArtifact.path)
944+
: null
940945
: null,
941946
};
942947
}

‎packages/producer/src/services/distributed/shared.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,35 @@ export const PLAN_VIDEOS_META_RELATIVE_PATH = "meta/videos.json";
4848
*/
4949
export const PLAN_AUDIO_RELATIVE_PATH = MIXED_AUDIO_FILENAME;
5050

51+
/**
52+
* Name the audio artifact carried before it moved to an MP4-family container.
53+
*
54+
* COMPATIBILITY SHIM, remove one release after the container change ships.
55+
* `plan` and `assemble` are separate invocations bridged by object storage, so
56+
* a rolling deploy can pair a pre-rollout planner with a post-rollout
57+
* assembler. Both readers locate the artifact by existence alone, which makes
58+
* that pairing a silently muted video rather than an error, so reads accept the
59+
* old name for one release while writes only ever emit the new one.
60+
*/
61+
export const PLAN_AUDIO_LEGACY_RELATIVE_PATH = "audio.aac";
62+
63+
/** True for either the current or the legacy plan-audio artifact name. */
64+
export function isPlanAudioArtifactPath(path: string): boolean {
65+
return path === PLAN_AUDIO_RELATIVE_PATH || path === PLAN_AUDIO_LEGACY_RELATIVE_PATH;
66+
}
67+
68+
/**
69+
* Locate a plan's audio artifact on disk, preferring the current name and
70+
* falling back to the legacy one. Returns `null` when the plan has no audio.
71+
*/
72+
export function resolvePlanAudioPath(planDir: string): string | null {
73+
for (const name of [PLAN_AUDIO_RELATIVE_PATH, PLAN_AUDIO_LEGACY_RELATIVE_PATH]) {
74+
const candidate = join(planDir, name);
75+
if (existsSync(candidate)) return candidate;
76+
}
77+
return null;
78+
}
79+
5180
/**
5281
* On-disk shape of `<planDir>/meta/videos.json`. The engine's
5382
* `ExtractedFrames` shape carries an absolute `outputDir`, a `framePaths`

‎packages/producer/src/services/render/audioPadTrim.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ describe("buildPadTrimAudioArgs", () => {
9898
// portable finite-padding shape used by the main audio mixer.
9999
const winPlan = buildPadTrimAudioPlan(
100100
"C:\\Users\\alice\\AppData\\Local\\Temp\\hf-render-abc\\audio.m4a",
101-
"C:\\Users\\alice\\AppData\\Local\\Temp\\hf-render-abc\\audio-padded.aac",
101+
"C:\\Users\\alice\\AppData\\Local\\Temp\\hf-render-abc\\audio-padded.m4a",
102102
4.0,
103103
5.0,
104104
);

‎packages/producer/src/services/render/audioPadTrim.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ export interface PadTrimAudioPlan {
109109
* sequence that materializes it. Exported separately so unit tests can pin
110110
* every branch without spawning ffmpeg.
111111
*
112-
* - `sourceDuration < targetDuration` → generate only the missing silence
113-
* tail, then concat-copy the source AAC plus that tail. This avoids
114-
* re-encoding the already mixed audio; the pad branch remains the
115-
* inverse of trim instead of becoming a second full-source AAC encode.
112+
* - `sourceDuration < targetDuration` → pad with `apad` to the exact target
113+
* and re-encode AAC. This decodes and re-encodes the already mixed audio;
114+
* an earlier concat-copy shape avoided that but could not produce a
115+
* portable result on the bundled Windows FFmpeg builds.
116116
* - `sourceDuration > targetDuration` → filter to the exact target and
117117
* re-encode AAC so packet padding cannot outlast the video.
118118
* - `|Δ| < AUDIO_DURATION_TOLERANCE_SECONDS` → no-op `copy`, but we still

0 commit comments

Comments
 (0)