Skip to content

fix(cli): the project server leaves out pictures the run itself saved - #9086

Open
miguel-heygen wants to merge 13 commits into
mainfrom
fix/project-server-skips-this-runs-pictures
Open

miguel-heygen wants to merge 13 commits into
mainfrom
fix/project-server-skips-this-runs-pictures

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

What

The server that loads a project for snapshot, keyframes --shot, check and the other page commands no longer serves pictures the same run saved into the project folder.

Why

snapshot -o <a folder in the project> and keyframes --shot <a file in the project> save their pictures while the composition is still open. The project server then served them as if they were the project's own assets, so a page could load the run's output while the run was still making it. What a frame shows must not depend on frames saved before it.

Related work

Stacked on #9079, which touches the same lines of motionShot.ts.

How

  • utils/savedThisRun.ts remembers each file the process saved, by device, inode and birth time rather than by path. A link, another letter case or a later file at the same path is told apart correctly.
  • The two writers that save pictures report to it: writeCaptureFileSync (frames, crops, reference frames, contact sheets) and the one saveShot in motionShot.ts. Saving and remembering are one synchronous step, so no request is answered between them.
  • staticProjectServer.ts leaves such a file out, next to the rule that leaves out .hyperframes.

Every command that loads a page and saves a picture, run through the built CLI on main (ef1d5db) with a page that keeps asking for the files:

Command Page could load it on main With this change
snapshot -o out yes no
snapshot -o out --zoom yes no
keyframes --shot yes no
compare --out (saved after its pages close) no no
check --snapshots (saved under .hyperframes) no no
Studio's thumbnail route, from the page being drawn (opaque origin, its request fails) no untouched

snapshot with no -o saves under .hyperframes, which the server already left out. grade-compare loads a temporary folder of its own, read and not run.

A picture that an earlier run left in the project is an ordinary project file, and still served: compositions use such files as assets.

Test plan

fails without the fix: packages/cli/src/utils/savedThisRun.test.ts ("never serves a picture this run saved into the project", "refuses the picture under any other name for the same file", "stops serving an asset once this run saves its picture over it"). The other two rows pin what must still be served: "keeps serving every file this run did not save" and "serves a file another program puts where this run saved one". The filmstrip row of screenshotsOutsidePage.chromium.test.ts now also asserts that the shot is remembered.

  • Unit tests added/updated. Six deliberate breaks (server, writer, shot, by path instead of file, birth time ignored, everything refused) each turn a row red.
  • Manual testing performed: the table above, before and after, pictures saved in every case that saved one before.
  • Documentation updated: none needed.
  • Comments follow CONTRIBUTING.md "Comments".

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes requested at e058db00.

  • [P1] Authorize and serve the same open file. packages/cli/src/utils/staticProjectServer.ts:419 checks a pathname and opens it separately for the response. If the file changes during that request, the identity checked can differ from the file served. Check the identity on the handle used for the response. The current overwrite test covers sequential requests.
  • [P2] Handle lookup errors in the request. packages/cli/src/utils/savedThisRun.ts:7 can throw on filesystem errors such as an inaccessible file. Once an output has been registered, that exception can leave the request unanswered and exit the CLI. Handle lookup failures without serving an unverified file or crashing the command.

The Linux CLI build, five focused tests, and the existing Chromium keyframes filmstrip test passed. The findings are from source inspection. #9079 is approved and remains open. The stacked head has no Linux CLI or Windows CI result, so those checks are still needed on the retargeted head.

— tai (automated review)

Base automatically changed from fix/check-compares-screenshots-outside-page to main November 29, 2026 10:55
…ips-this-runs-pictures

# Conflicts:
#	packages/cli/src/commands/motionShot.ts
#	packages/cli/src/utils/screenshotsOutsidePage.chromium.test.ts
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 275 (base branch 273), smooth 55 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Newly passing (2)

  • seqscaletwice-scale-px-r0-root-z100
  • seqscaletwice-scale-px-r0-nested-z100

Not banked yet, reported and not gated (bank a full-grid run's baseline.json) (2)

  • seqscaletwice-scale-px-r0-root-z100
  • seqscaletwice-scale-px-r0-nested-z100

Quarantined, measured but not gated (0)

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Answering the review at e058db0; new head 4e525e6, now on main with main merged in (#9079 landed, so the diff is this change only).

  • P1, check and serve the same open file: fixed. A request opens the file once. The same handle is checked, sized, read and streamed (withOpenFile and serveProjectFile in staticProjectServer.ts). New row "serves the file it checked when this run saves over it during the request": on e058db0 it fails, here the response holds the project's own picture. The media-copy route had the same gap across its transcode, so it looks again just before it answers ("leaves out a copy of what this run saved while the copy was made").
  • P2, lookup errors: fixed. A file that cannot be looked up is not served, and an open or read error answers 500 and leaves the server running. Rows: "answers, without serving it, a file it cannot look up", "answers for a file it may not open", "refuses a picture it saved but could not look up". The first two never got an answer on e058db0; a PNG the process may not open went unanswered on main as well.
  • A folder or a pipe named like an asset now answers 404; the open is non-blocking. Every handle is closed, streamed or not ("closes every handle it opened").

Run at bce0595, which differs from this head only in one test: lint, format, tsc, the Fallow audit, pre-push and the CLI tests that cover a changed file, all green except snapshotHdrFrame.test.ts, which is red at the merge-base on that machine too. Eleven deliberate breaks of the server, the lookup and the writer each turn a row red. The built CLI was probed again at 0ad4f1b: the page still loads none of the saved pictures, and every picture is still saved. The later commits were not probed that way: one moves the last step of the copy route into a function, the others are the merge and that test.

Windows CI at 4b58b8b had one red row, the P1 one: the simulated overwrite was not answered with a picture. I read that as Windows refusing to replace the open file; the row now records whether the overwrite was refused and expects the project's own picture either way. I have not run it on Windows outside CI.

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The direct-file path now checks and serves the same handle, and the targeted lookup and open failure tests pass. The proxy path still needs the same guarantees.

  • P2 — Bind proxy generation to the authorized source. In packages/cli/src/utils/staticProjectServer.ts:352, the codec probe, transcode, and final authorization can observe different file versions. Keep one authorized source identity through the pipeline, or use an approved immutable input for both the probe and transcode.
  • P2 — Check the opened proxy copy before streaming. In packages/cli/src/utils/staticProjectServer.ts:312, apply the saved-file and regular-file checks to the opened copy itself before serving it. The source check covers a different file.

41 targeted tests passed on Linux. The proxy findings are from source inspection. Real transcoding and other platforms were unverified locally. The PR now targets main and the CLI and Windows checks are running.

— tai (automated review)

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Answering the review at 4e525e6; new head da46a6e. Both findings were right. The first one was worse than a late check. With real ffmpeg, on 4e525e6: a clip is replaced while its copy is made, the response is 404, but the copy stays in the cache under the original's key. With the original back in place, the next request got the other picture.

  • Bind the copy to the checked source: fixed in the pipeline. The server hands resolveProxy the stat of the handle it checked (checkedSource). Device, inode, size, modified time and change time must match at the lookup, again once the cache key is known, when joining a copy in progress, when reusing one by content, and right before a copy is published. Otherwise the request fails and nothing is kept. The pieces a stopped copy leaves behind are named by that version too, so a piece read from another file is never continued from. The codec probe sits between the first check and the lookup, so a swap there is refused as well.
  • Check the opened copy: fixed. The saved-file and regular-file checks moved into withOpenFile, the one place this server opens a project file or a copy.

What changes for every user of the pipeline (Studio preview, play, publish, the pre-warm): a source whose times or size change while its copy is made now fails that one request (502 from the routes) and is copied again at the next one. A change of attributes counts too, since change time moves. The failure is not remembered.

Nine new rows. "keeps no copy made while the file was moved away and back, and makes the next one", "neither copies nor serves a file in any state but the one the caller checked", "does not join a copy being made from another state of the file", "does not file a finished copy under a file that changed while its content was read", "makes every piece again once the file was moved away and back", "copies only the file it checked, when another takes its name during the codec probe" and the two "answers a copy that is ..." rows each failed on 4e525e6's code, run before the interface was simplified. "does not serve a finished copy for a file that changed while it was looked up" was added after that and was shown red by removing its check. Thirteen deliberate breaks each turn a row red; so do the ten earlier ones that still apply (the eleventh is now one of the thirteen).

Real transcoding, through the real server with a 40 s ProRes clip, read back as colours: untouched, 200 and the right picture; replaced by a file this run saved, 502, no copy left, then the right picture once the original is back; moved away and back, 502, no copy left, then the right picture. The built CLI was probed again with this code: no page loads a saved picture, and every picture is still saved.

Run at da46a6e: lint, format, tsc in both packages, the Fallow audit, pre-push, the named tests three times, and the tests that cover a changed file, green except snapshotHdrFrame.test.ts, red at the merge-base on that machine too. The whole studio-server suite (2785 tests) and the whole CLI suite (10374 passed) were run one revision before the interface was simplified. Five CLI rows were red there (downloadStall.process, studioServer.previewAssets, snapshotHdrFrame), and are red the same way on that machine with 4e525e6's version of the three changed source files.

Package size: this head packs to 787,454 bytes unpacked with the CI command; the limit with its 1% tolerance is 787,524. No budget was raised. resolveProxy's doc comment is shorter for that reason.

Windows, which the previous comment left unconfirmed: CI at 4e525e6 passed, and on a Windows 11 machine replacing an open file is refused (EPERM). There, on macOS and on Linux, moving a file away and back changes its version, and the path and the handle report the same one. Not proven: the copy rows on Windows outside CI, Studio's preview in a browser, and a change within the filesystem's own time tick (a few milliseconds on Linux), which no version of this kind can see.

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

New head 11d1b07: one test edited, no source change since da46a6e.

Windows CI at da46a6e had one red row, the new "copies only the file it checked, when another takes its name during the codec probe". Its mocked probe replaced the clip synchronously, while the server still held the handle it had checked, and Windows refuses that. The row now replaces it one step later, after the handle is closed. Refusing the replacement on Linux in the same way gives the same assertion failure with da46a6e's test and passes with this one. It passed three times here, and still fails when the server does not pass the checked file on. Not run on Windows outside CI.

Correction to the previous comment: it lists Windows as unproven for the copy rows "outside CI"; inside CI this row had not passed yet either.

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The test now waits for the checked handle to close. The opened-copy checks also reject saved pictures and directories. Two source-identity issues remain.

  • P2 — Keep the verified source through the proxy pipeline. At packages/studio-server/src/helpers/proxyTranscoder.ts:1185, FFmpeg still opens a pathname independently of the verified source. The later check guards publication, but the probe, hash, and encoder still lack a shared source. Use the verified handle or one verified immutable input throughout.
  • P2 — Include the verified source version in fast cache lookups. At packages/studio-server/src/helpers/proxyCacheKey.ts:31, the key uses pathname, modification time, and size for source identity. A same-size replacement with a preserved timestamp can receive the previous file’s proxy. Include the verified version before accepting a fast hit.

39 targeted tests passed on Linux. The two findings are from source inspection. Real FFmpeg and local Windows execution were unverified. Windows verification and the Linux CLI shards passed in CI.

— tai (automated review)

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Both points are fixed at c26b775 (main merged in first, at 3247ab9).

  • One checked input through the whole copy (proxyTranscoder.ts, proxyCacheKey.ts): the copy step opens the source once, checks that the open file is the version the caller checked, then copies its bytes to a private file in the cache folder and hashes them in that same read. It checks the open file again after the read. The codec probe, the filter probe, every piece and the sound pass all read that private file, never the project path, and it is removed when the copy settles. The content key is the hash of those bytes, so copies already on disk are still found. The re-checks of the path before and after FFmpeg are gone: nothing reads the path after the check.
  • The fast key names the checked file (fastCopyKey): it now takes the checked version (device, inode, size, and modified and changed times to the nanosecond) instead of statting the path again. A different file of the same size on a drive that keeps times to the second no longer gets the old copy. Because two versions now have two keys, a second ask for another version makes its own copy instead of joining one in progress, so the check for that case is removed.

fails without the fix: packages/studio-server/src/helpers/proxyTranscoder.test.ts "converts the bytes it checked when another file takes the name during the copy" and "gives another file of the same size its own copy on a drive that keeps times to the second" both fail on the previous head's code with main merged, and pass here 3 runs in a row. Breaking the fix turns exactly one red each: handing FFmpeg the project path again breaks the first, and dropping the inode from the version breaks the second.

  • Two tests changed with the behaviour. "keeps no copy made while the file was moved away and back" became the first test above. "does not join a copy being made from another state" became "makes its own copy for another state of the file instead of joining one in progress". Main's change-time key had already turned that second test red on the merge alone. The test spy now records what each FFmpeg call's input held, since the input is the private file and no longer named after the clip.
  • cacheReadStall.test.ts: the content-key stall test now runs the checked copy, which reads and writes without holding the thread, and expects the same key as before.
  • studio-server: 2,790 tests pass (6 skipped), and the type check is clean. CLI staticProjectServer and savedThisRun tests pass, 86 of 86. The FFmpeg-backed tests ran with real FFmpeg on Linux.
  • Size: studio-server unpacked is 788,260 bytes, 1.1% over its old budget of 779,727 (the check allows 1%), so the budget is raised to that in its own commit. The reason is the copy step above; packed stays inside its budget.

Costs and limits:

  • Every new copy now reads and writes the source once more before FFmpeg starts, and needs that much free space in the cache folder while it runs. A copied project found by content now pays that write too.
  • Cache cleanup removes temp files an hour old, so a single copy taking over an hour fails and the next ask makes it again.
  • Stills (resolveStill) still read the path; the project server never asks for them with a checked source.
  • Not run: Windows and macOS locally (CI runs Windows), and Studio's dev server.

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Head is now b2e6896: one more commit after c26b775 that only trims doc comments in proxyCacheKey.ts. No code changed.

This branch has not been deployed

No deployments
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