Skip to content

fix(media-use): dither stages its output in a fresh folder beside the output - #8599

Merged
ukimsanov merged 2 commits into
mainfrom
fix/media-use-dither-private-staging
Nov 23, 2026
Merged

ukimsanov merged 2 commits into
mainfrom
fix/media-use-dither-private-staging

Conversation

@ukimsanov

Copy link
Copy Markdown
Collaborator

What

dither.mjs (media-use) now writes its encode into a fresh private folder beside the output, then moves the finished file onto the output path. Before, it wrote to <output name>.part-<process id><ext> next to the output.

Why

That old name could already exist in a project folder someone else made, for example as a link to a file elsewhere on the machine. ffmpeg -y follows a link at its output path, so dithering that project's media would overwrite the file the link points at, and the run would still report success. A folder made fresh for each run can't hold anything beforehand.

Related work

Same approach as transcript-cut.mjs, which already stages its encode in a fresh folder beside the output.

How

  • privateStagingOutput(outPath) makes the folder with mkdtempSync(join(dirname(outPath), ".media-use-dither-")) and returns it with the file path inside it.
  • The image and video paths encode there, renameSync the result onto the output, and remove the folder in finally. The folder sits beside the output, so the rename stays on one drive.
  • For video, the folder is made before either ffmpeg process starts, so a failure to create it leaves no process running.
  • If ffmpeg can't start, the video path now reports it as a normal error ({"ok":false,"error":"spawn ffmpeg ENOENT"}) instead of crashing, and the folder is removed. The removal retries briefly, since on Windows ffmpeg may still hold the file for a moment.

Test plan

  • Unit tests added/updated

  • Manual testing performed

  • Documentation updated (if applicable)

  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

  • Fails without the fix: two new tests in skills/media-use/scripts/dither.test.mjs (image and video). Each starts the script, puts a link at the old <output name>.part-<pid><ext> name for that run, and checks that the link's target is never created, the output is a regular file, and no staging folder is left. The tests also keep a link at the output path itself, which is replaced by the finished file, and its target is never created. Both fail on main and pass here.

  • All media-use tests, run the CI way: 323 pass, 0 fail, 1 skipped.

  • By hand: with ffprobe on the PATH but no ffmpeg, a video run prints the error above and leaves only the input in the folder. Main crashes there.

  • oxlint, oxfmt, lint-skills and the comment ratchet pass.

No visible change

The output file and the script's messages are the same.

… output

The encode used to go to a name built from the output name and the process id, which a folder
could already hold. It now goes into a newly made private folder beside the output, the way
transcript-cut does, and only the finished file is moved onto the output path.
…s no folder behind

A video encode whose ffmpeg can't start now fails through the normal error path, so the staging
folder is removed. Removal retries briefly while ffmpeg still holds the file on Windows. The test
also keeps a link at the output path and shows the script's own error when it fails.

@eddy-heygen eddy-heygen left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Eddy's agent) Reviewed by Eddy's Claude agent at commit 3ae96d6 (full diff read: 3 files, +68/-16)

Verdict: approve

Summary: dither.mjs no longer encodes to a sibling file named after the process id. Both the image and the video path now create a fresh folder beside the output with mkdtempSync, encode to out<ext> inside it, rename that file onto the output, and remove the folder. A name that project content could have placed in advance is no longer written to.

What I checked

  • privateStagingOutput: the folder name ends in a random suffix chosen at creation, and creation fails if the name exists, so nothing can sit at the staging path before the encoder opens it. The folder is in the output's own directory, so the final rename stays on one filesystem.
  • processImage and processVideo both pass staging.file to the encoder and rename only that file. The old temporaryOutput helper has no caller left and is removed.
  • The rename replaces whatever is at the output path itself rather than writing through it; the new tests place a link there too and check its target is not created.
  • Cleanup: both paths remove the staging folder in finally, on success and on failure.
  • Missing encoder binary in the video path: the two once(..., "close") promises reject on the child's error event; the added no-op catch handlers keep that from surfacing as an unhandled rejection before the try block awaits them, so the run ends through the normal error path and the folder is removed. I read this; no test covers it.
  • skills-manifest.json: only the media-use hash changes, and Skills: manifest in sync passes.

Non-blockers

  • A run that is killed (not failed) leaves a .media-use-dither-* folder beside the output. Harmless, but it is a hidden folder in the user's project; a note in the skill's docs or a sweep of old ones at start would cover it.
  • In processVideo the folder is created a few statements before the try. A synchronous throw from spawn in between would skip the cleanup. Moving the try up, or creating the folder as its first statement, closes that.
  • The two new tests are skipped on Windows, so the Windows path is covered by reading only.

CI: 28 passing, 34 skipped, none failing or pending when I looked. I read the Test: skills log: it installs the encoder, and both new tests ran and passed there (image and video), 878 of 879 passing with 1 unrelated skip.

@ukimsanov
ukimsanov added this pull request to the merge queue Nov 23, 2026
Merged via the queue into main with commit 7717567 Nov 23, 2026
62 checks passed
@ukimsanov
ukimsanov deleted the fix/media-use-dither-private-staging branch November 23, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants