-
Notifications
You must be signed in to change notification settings - Fork 4.4k
fix(skills): retain audio metadata file identity during generation #3778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+182
−5
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { closeSync, ftruncateSync, openSync, readFileSync, writeSync } from "node:fs"; | ||
|
|
||
| // Keep the merge base and output on the same file even if its pathname changes | ||
| // while audio generation runs. The CLI owns this handle until write or exit. | ||
| export function openAudioMeta(path) { | ||
| let fd; | ||
| try { | ||
| fd = openSync(path, "r+"); | ||
| } catch (error) { | ||
| if (error.code !== "ENOENT") throw error; | ||
| } | ||
| let value = {}; | ||
| if (fd !== undefined) { | ||
| try { | ||
| value = JSON.parse(readFileSync(fd, "utf8")); | ||
| } catch (error) { | ||
| closeSync(fd); | ||
| throw error; | ||
| } | ||
| } | ||
| return { | ||
| value, | ||
| write(meta) { | ||
| const bytes = Buffer.from(JSON.stringify(meta, null, 2)); | ||
| // Defer new-file creation until generation succeeds. Never overwrite a | ||
| // file (or follow a link) that appeared since the missing merge base. | ||
| if (fd === undefined) fd = openSync(path, "wx"); | ||
| try { | ||
| let offset = 0; | ||
| while (offset < bytes.length) { | ||
| offset += writeSync(fd, bytes, offset, bytes.length - offset, offset); | ||
| } | ||
| ftruncateSync(fd, bytes.length); | ||
| } finally { | ||
| closeSync(fd); | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { test } from "node:test"; | ||
| import { | ||
| mkdtempSync, | ||
| readFileSync, | ||
| renameSync, | ||
| rmSync, | ||
| statSync, | ||
| symlinkSync, | ||
| writeFileSync, | ||
| } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { spawnSync } from "node:child_process"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { openAudioMeta } from "./audio-meta.mjs"; | ||
|
|
||
| function fixture(t) { | ||
| const dir = mkdtempSync(join(tmpdir(), "audio-meta-")); | ||
| t.after(() => rmSync(dir, { recursive: true, force: true })); | ||
| return dir; | ||
| } | ||
|
|
||
| test("updates the read file from byte zero, truncates, and retains its mode", (t) => { | ||
| const path = join(fixture(t), "meta.json"); | ||
| writeFileSync(path, JSON.stringify({ voices: ["long existing voice metadata"] }), { | ||
| mode: 0o600, | ||
| }); | ||
| const originalMode = statSync(path).mode; | ||
| const handle = openAudioMeta(path); | ||
| assert.deepEqual(handle.value, { voices: ["long existing voice metadata"] }); | ||
| handle.write({ voices: ["声"] }); | ||
| assert.equal(readFileSync(path, "utf8"), JSON.stringify({ voices: ["声"] }, null, 2)); | ||
|
jrusso1020 marked this conversation as resolved.
Dismissed
|
||
| assert.equal(statSync(path).mode, originalMode); | ||
| }); | ||
|
|
||
| test("pathname replacement cannot redirect the existing-file write", (t) => { | ||
| const dir = fixture(t); | ||
| const path = join(dir, "meta.json"); | ||
| const moved = join(dir, "original.json"); | ||
| writeFileSync(path, "{}"); | ||
| const handle = openAudioMeta(path); | ||
| renameSync(path, moved); | ||
| writeFileSync(path, "replacement must survive"); | ||
| handle.write({ voices: [] }); | ||
| assert.equal(readFileSync(path, "utf8"), "replacement must survive"); | ||
| assert.deepEqual(JSON.parse(readFileSync(moved, "utf8")), { voices: [] }); | ||
| }); | ||
|
|
||
| test("existing symlink outputs retain their original target even after retargeting", (t) => { | ||
| const dir = fixture(t); | ||
| const path = join(dir, "meta.json"); | ||
| const target = join(dir, "target.json"); | ||
| const victim = join(dir, "victim.json"); | ||
| writeFileSync(target, "{}"); | ||
| writeFileSync(victim, "untouched"); | ||
| symlinkSync(target, path); | ||
| const handle = openAudioMeta(path); | ||
| rmSync(path); | ||
| symlinkSync(victim, path); | ||
| handle.write({ bgm: null }); | ||
| assert.equal(readFileSync(victim, "utf8"), "untouched"); | ||
| assert.deepEqual(JSON.parse(readFileSync(target, "utf8")), { bgm: null }); | ||
| }); | ||
|
|
||
| test("missing output is created only when saved", (t) => { | ||
| const path = join(fixture(t), "meta.json"); | ||
| const handle = openAudioMeta(path); | ||
| assert.deepEqual(handle.value, {}); | ||
| assert.throws(() => statSync(path), { code: "ENOENT" }); | ||
| handle.write({ sfx: [] }); | ||
| assert.deepEqual(JSON.parse(readFileSync(path, "utf8")), { sfx: [] }); | ||
| }); | ||
|
|
||
| for (const replacement of ["file", "symlink"]) { | ||
| test(`new output does not overwrite a racing ${replacement}`, (t) => { | ||
| const dir = fixture(t); | ||
| const path = join(dir, "meta.json"); | ||
| const victim = join(dir, "victim.json"); | ||
| const handle = openAudioMeta(path); | ||
| writeFileSync(victim, "untouched"); | ||
| if (replacement === "symlink") symlinkSync(victim, path); | ||
| else writeFileSync(path, "untouched"); | ||
| assert.throws(() => handle.write({}), { code: "EEXIST" }); | ||
| assert.equal(readFileSync(path, "utf8"), "untouched"); | ||
| assert.equal(readFileSync(victim, "utf8"), "untouched"); | ||
| }); | ||
| } | ||
|
|
||
| test("malformed merge base remains an error without changing bytes", (t) => { | ||
| const path = join(fixture(t), "meta.json"); | ||
| writeFileSync(path, "bad json"); | ||
| assert.throws(() => openAudioMeta(path), SyntaxError); | ||
| assert.equal(readFileSync(path, "utf8"), "bad json"); | ||
| }); | ||
|
|
||
| test("CLI partial run retains unselected voices, BGM and SFX", (t) => { | ||
| const dir = fixture(t); | ||
| const path = join(dir, "meta.json"); | ||
| const request = join(dir, "request.json"); | ||
| const previous = { | ||
| voices: [{ id: "a", duration_s: 2 }], | ||
| bgm: { path: "bgm.wav" }, | ||
| sfx: [{ name: "click" }], | ||
| tts_provider: "kokoro", | ||
| voice_id: "voice-a", | ||
| }; | ||
| writeFileSync(path, JSON.stringify(previous)); | ||
| writeFileSync(request, "{}"); | ||
| const result = spawnSync( | ||
| process.execPath, | ||
| [ | ||
| fileURLToPath(new URL("../audio.mjs", import.meta.url)), | ||
| "--request", | ||
| request, | ||
| "--hyperframes", | ||
| dir, | ||
| "--out", | ||
| path, | ||
| "--only", | ||
| "none", | ||
| ], | ||
| { | ||
| encoding: "utf8", | ||
| env: { | ||
| ...process.env, | ||
| HEYGEN_CONFIG_DIR: dir, | ||
| HEYGEN_API_KEY: "", | ||
| HYPERFRAMES_API_KEY: "", | ||
| }, | ||
| }, | ||
| ); | ||
| assert.equal(result.status, 0, result.stderr); | ||
| const actual = JSON.parse(readFileSync(path, "utf8")); | ||
| for (const key of Object.keys(previous)) assert.deepEqual(actual[key], previous[key]); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.