fix(output): write atomically so a failed re-run cannot destroy a good file - #22
Merged
Conversation
…d file
Both encoders wrote straight to the final destination, and two paths then
removed it. Under --overwrite, that cost the user a valid output file:
- encodeVideo() ran ffmpeg against the destination and rm'd it on failure.
- The skip-larger branch rm'd the destination and reported
"skipped - output-larger-than-input", whose documented meaning is that
the original was kept and nothing was touched. It had just been deleted.
- encodeImage()'s write() used writeFile on the destination, which
truncates before writing, so an abort mid-write left a truncated file.
Encoding now goes to a hidden staging file beside the destination and is
renamed onto it only after the encode succeeds and clears skip-larger. The
staging name keeps the real extension last so ffmpeg can still infer its
muxer, and the leading dot means discovery's existing dotfile skip stops an
in-flight encode being picked up as input by a concurrent or later run.
mapWithConcurrency now settles every worker before rethrowing. Promise.all
rejected on the first abort and left sibling workers' cleanup unawaited,
which stranded staging files.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
First of three PRs for 2.1.0. The theme across all three is that the tool
should do what its README says it does; this one is the only item that
destroys user data.
The bug
Both encoders wrote straight to the final destination path, and two separate
paths then removed it. With
--overwriteover a directory that already hadoutput from an earlier run, a valid file was lost.
Reproduced on
main@ e5982e6:The run reports
skipped — output-larger-than-input, whose documentedmeaning is that the original was kept and nothing was touched. It had in fact
just deleted the user's previously compressed file. Three routes in:
encodeVideo()targeted the destination andrm'd it in its failure catch.runVideoJob()rm'd the destination outright.encodeImage()'swrite()usedwriteFileon the destination, whichtruncates before it writes — an abort mid-write left a truncated file.
The fix
Encode into a hidden staging file beside the destination,
rename()onto itonly after the encode succeeds and clears skip-larger. Failure, abort and
skip-larger now remove the staging file only; the destination is never touched
by a run that does not successfully replace it.
Two details that matter:
.clip-<uuid>.tmp.mp4).ffmpeg picks its muxer from the output extension, so
clip.mp4.partwouldhave broken every video encode.
in-flight encode, so a concurrent or later run cannot pick one up as input.
mapWithConcurrencynow settles every worker before rethrowing.Promise.allrejected on the first abort and left sibling workers' cleanup unawaited, which
stranded staging files on the way out.
encodeImage's encode-to-buffer design is unchanged — it is what makesskip-larger able to decline without ever putting a worse file on disk.
Verification
Same reproduction on this branch:
192 tests pass (up from 184), covering: a failed encode with
--overwriteleaves the prior output intact, a larger-output skip leaves it intact, and no
staging residue after success, failure or abort.
typecheck/lint/format:check/build/build:mcpall clean.Notes for review
No API, JSON or CLI surface changes —
schemaVersionstays 1. The behaviourchange is that
skipped — output-larger-than-inputis now truthful.🤖 Generated with Claude Code