Skip to content

Commit 49113eb

Browse files
fix(media-use): clean failed asset reservations (#2627)
* fix(media-use): clean failed asset reservations * test(media-use): pin installer guidance exactly
1 parent 7f76170 commit 49113eb

8 files changed

Lines changed: 306 additions & 95 deletions

File tree

‎skills-manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"files": 10
4747
},
4848
"media-use": {
49-
"hash": "f15fce08a493d70e",
49+
"hash": "5399c0f111ff5619",
5050
"files": 139
5151
},
5252
"motion-graphics": {

‎skills/media-use/scripts/lib/heygen-cli.mjs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import { track } from "./telemetry.mjs";
66
// needs — so anything below this can't authenticate for free usage at all.
77
export const HEYGEN_MIN_VERSION = "0.3.0";
88
// Free-usage path is OAuth (`--oauth` → subscription/free credits); `--api-key`
9-
// bills API credits, so the onboarding steers to OAuth.
9+
// bills API credits, so the onboarding steers to OAuth. Keep pipe-to-shell
10+
// installer text out of the runtime module; the docs are the safer source of
11+
// truth for platform-specific setup.
1012
export const HEYGEN_INSTALL_COMMAND =
11-
"curl -fsSL https://static.heygen.ai/cli/install.sh | bash && heygen auth login --oauth";
13+
"Install the CLI from https://developers.heygen.com/cli, then run: heygen auth login --oauth";
1214
export const HEYGEN_AUTH_COMMAND = "heygen auth login --oauth";
1315
export const HEYGEN_UPDATE_COMMAND = "heygen update";
1416
export const HEYGEN_CLIENT_SOURCE_ARGV = ["--headers", "X-HeyGen-Client-Source: media-use"];
1517

16-
export const HEYGEN_NOT_FOUND_MESSAGE = `media-use: heygen CLI not found — it's the free path for bgm/image/voice/avatar-video. Install: ${HEYGEN_INSTALL_COMMAND}`;
18+
export const HEYGEN_NOT_FOUND_MESSAGE = `media-use: heygen CLI not found — it's the free path for bgm/image/voice/avatar-video. ${HEYGEN_INSTALL_COMMAND}`;
1719
export const HEYGEN_NOT_AUTHENTICATED_MESSAGE = `media-use: heygen CLI not authenticated (free usage) — run: ${HEYGEN_AUTH_COMMAND}`;
1820
export const HEYGEN_OUTDATED_MESSAGE = `media-use: heygen CLI is outdated — run: ${HEYGEN_UPDATE_COMMAND} (need >= v${HEYGEN_MIN_VERSION})`;
1921

‎skills/media-use/scripts/lib/lut-preset-provider.mjs‎

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { existsSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
22
import { join } from "node:path";
3-
import { allocateId } from "./manifest.mjs";
3+
import { withReservedFile, withReservedFileSync } from "./manifest.mjs";
44
import { freezeUrl } from "./freeze.mjs";
55
import { tokenOverlap } from "./match.mjs";
66
import { buildCube } from "./cube-build.mjs";
@@ -183,19 +183,27 @@ export async function freezeLibraryLut(match, { projectDir, type, localOnly = fa
183183
// to deterministic buildCube params when offline (--local-only) or if the
184184
// download/validation fails, so resolution is never blocked on the network.
185185
if (match.url && !localOnly) {
186-
const { id, localPath } = allocateId(projectDir, type, ".cube");
187-
const fullPath = join(projectDir, localPath);
188-
const tmpPath = `${fullPath}.tmp`;
189186
try {
190-
// Download + validate at a .tmp path, then atomically rename. A crash
191-
// (SIGKILL/OOM) between write and validate can't orphan an invalid .cube
192-
// at the final path — only a validated cube is ever renamed into place.
193-
await freezeUrl(match.url, tmpPath);
194-
assertValidCubeFile(tmpPath, `downloaded library LUT ${match.id} failed validation`);
195-
renameSync(tmpPath, fullPath);
196-
return libraryRecord(match, { id, localPath, fullPath, via: "url" });
187+
return await withReservedFile(
188+
projectDir,
189+
type,
190+
".cube",
191+
async ({ id, localPath, fullPath }) => {
192+
const tmpPath = `${fullPath}.tmp`;
193+
try {
194+
// Download + validate at a .tmp path, then atomically rename. A crash
195+
// (SIGKILL/OOM) between write and validate can't orphan an invalid .cube
196+
// at the final path — only a validated cube is ever renamed into place.
197+
await freezeUrl(match.url, tmpPath);
198+
assertValidCubeFile(tmpPath, `downloaded library LUT ${match.id} failed validation`);
199+
renameSync(tmpPath, fullPath);
200+
return libraryRecord(match, { id, localPath, fullPath, via: "url" });
201+
} finally {
202+
rmSync(tmpPath, { force: true });
203+
}
204+
},
205+
);
197206
} catch (err) {
198-
rmSync(tmpPath, { force: true });
199207
if (!match.params) {
200208
throw new Error(`failed to freeze library LUT ${match.id}: ${err.message}`);
201209
}
@@ -204,26 +212,25 @@ export async function freezeLibraryLut(match, { projectDir, type, localOnly = fa
204212
}
205213

206214
if (match.params) {
207-
const { id, localPath } = allocateId(projectDir, type, ".cube");
208-
const fullPath = join(projectDir, localPath);
209-
const tmpPath = `${fullPath}.tmp`;
210-
try {
211-
const cube = buildCube(match.params);
212-
assertValidCubeText(cube, `invalid library LUT ${match.id}`);
213-
// Write + validate at .tmp, then atomic rename — same no-orphan guarantee
214-
// as the url path above.
215-
writeFileSync(tmpPath, cube);
216-
assertValidCubeFile(tmpPath, `invalid frozen LUT ${localPath}`);
217-
renameSync(tmpPath, fullPath);
218-
} catch (err) {
219-
rmSync(tmpPath, { force: true });
220-
throw err;
221-
}
222-
return libraryRecord(match, {
223-
id,
224-
localPath,
225-
fullPath,
226-
via: match.url ? "params-fallback" : "params",
215+
return withReservedFileSync(projectDir, type, ".cube", ({ id, localPath, fullPath }) => {
216+
const tmpPath = `${fullPath}.tmp`;
217+
try {
218+
const cube = buildCube(match.params);
219+
assertValidCubeText(cube, `invalid library LUT ${match.id}`);
220+
// Write + validate at .tmp, then atomic rename — same no-orphan guarantee
221+
// as the url path above.
222+
writeFileSync(tmpPath, cube);
223+
assertValidCubeFile(tmpPath, `invalid frozen LUT ${localPath}`);
224+
renameSync(tmpPath, fullPath);
225+
return libraryRecord(match, {
226+
id,
227+
localPath,
228+
fullPath,
229+
via: match.url ? "params-fallback" : "params",
230+
});
231+
} finally {
232+
rmSync(tmpPath, { force: true });
233+
}
227234
});
228235
}
229236

‎skills/media-use/scripts/lib/lut-preset-provider.test.mjs‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { strict as assert } from "node:assert";
2-
import { mkdtempSync, rmSync, existsSync, readFileSync } from "node:fs";
2+
import { mkdtempSync, rmSync, existsSync, readFileSync, readdirSync } from "node:fs";
33
import { join } from "node:path";
44
import { tmpdir } from "node:os";
55
import { test } from "node:test";
@@ -128,3 +128,33 @@ test("url library entries respect localOnly and freeze through fetch", async ()
128128
rmSync(projectDir, { recursive: true, force: true });
129129
}
130130
});
131+
132+
test("failed URL library freeze releases its reservation", async () => {
133+
const projectDir = mkdtempSync(join(tmpdir(), "mu-lut-failure-"));
134+
const originalFetch = globalThis.fetch;
135+
try {
136+
globalThis.fetch = async () => ({
137+
ok: true,
138+
headers: { get: () => "12" },
139+
body: [Buffer.from("not a cube\n")],
140+
});
141+
await assert.rejects(
142+
freezeLibraryLut(
143+
{
144+
kind: "library",
145+
id: "broken-cdn-look",
146+
description: "Broken CDN look",
147+
tags: ["broken"],
148+
intensity: 1,
149+
url: "https://example.com/broken.cube",
150+
},
151+
{ projectDir, type: "lut" },
152+
),
153+
/failed to freeze library LUT/,
154+
);
155+
assert.deepStrictEqual(readdirSync(join(projectDir, ".media/luts")), []);
156+
} finally {
157+
globalThis.fetch = originalFetch;
158+
rmSync(projectDir, { recursive: true, force: true });
159+
}
160+
});

‎skills/media-use/scripts/lib/manifest.mjs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,40 @@ export function allocateId(projectDir, type, ext) {
190190
return { id, localPath };
191191
});
192192
}
193+
194+
function reservedFile(projectDir, type, ext) {
195+
const allocation = allocateId(projectDir, type, ext);
196+
return { ...allocation, fullPath: join(projectDir, allocation.localPath) };
197+
}
198+
199+
function rollbackReservation(reservation) {
200+
rmSync(reservation.fullPath, { force: true });
201+
}
202+
203+
// A reservation is committed only when populate returns a non-null value.
204+
// Throwing/rejecting or returning null means no usable asset was produced, so
205+
// the placeholder must be released. Keeping this transaction beside allocateId
206+
// prevents individual provider/cache/LUT paths from forgetting the rollback.
207+
export function withReservedFileSync(projectDir, type, ext, populate) {
208+
const reservation = reservedFile(projectDir, type, ext);
209+
try {
210+
const result = populate(reservation);
211+
if (result == null) rollbackReservation(reservation);
212+
return result;
213+
} catch (error) {
214+
rollbackReservation(reservation);
215+
throw error;
216+
}
217+
}
218+
219+
export async function withReservedFile(projectDir, type, ext, populate) {
220+
const reservation = reservedFile(projectDir, type, ext);
221+
try {
222+
const result = await populate(reservation);
223+
if (result == null) rollbackReservation(reservation);
224+
return result;
225+
} catch (error) {
226+
rollbackReservation(reservation);
227+
throw error;
228+
}
229+
}

‎skills/media-use/scripts/lib/manifest.test.mjs‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { strict as assert } from "node:assert";
2-
import { mkdtempSync, rmSync, readFileSync, writeFileSync, mkdirSync, existsSync } from "node:fs";
2+
import {
3+
mkdtempSync,
4+
rmSync,
5+
readFileSync,
6+
writeFileSync,
7+
mkdirSync,
8+
existsSync,
9+
readdirSync,
10+
} from "node:fs";
311
import { join } from "node:path";
412
import { tmpdir } from "node:os";
513
import {
@@ -9,6 +17,7 @@ import {
917
findByEntity,
1018
nextId,
1119
allocateId,
20+
withReservedFileSync,
1221
normalizePrompt,
1322
manifestPath,
1423
mediaDir,
@@ -164,6 +173,38 @@ function runTests() {
164173
cleanup();
165174
});
166175

176+
test("failed reservation rollback preserves another completed reservation", () => {
177+
setup();
178+
const kept = withReservedFileSync(tmp, "bgm", ".wav", (reservation) => {
179+
writeFileSync(reservation.fullPath, "completed asset");
180+
return reservation;
181+
});
182+
183+
assert.throws(
184+
() =>
185+
withReservedFileSync(tmp, "bgm", ".wav", () => {
186+
throw new Error("populate failed");
187+
}),
188+
/populate failed/,
189+
);
190+
191+
assert.equal(kept.id, "bgm_001");
192+
assert.equal(readFileSync(kept.fullPath, "utf8"), "completed asset");
193+
assert.deepStrictEqual(readdirSync(typeDirPath(tmp, "bgm")), ["bgm_001.wav"]);
194+
assert.equal(allocateId(tmp, "bgm", ".wav").id, "bgm_002");
195+
cleanup();
196+
});
197+
198+
test("empty reservation result releases the placeholder", () => {
199+
setup();
200+
assert.equal(
201+
withReservedFileSync(tmp, "image", ".jpg", () => null),
202+
null,
203+
);
204+
assert.deepStrictEqual(readdirSync(typeDirPath(tmp, "image")), []);
205+
cleanup();
206+
});
207+
167208
test("findByEntity matches case-insensitively", () => {
168209
setup();
169210
appendRecord(tmp, makeRecord({ entity: "GitHub", type: "icon" }));

0 commit comments

Comments
 (0)