Skip to content

fix(studio-server): a smaller preview ask reuses the larger copy of the same clip - #9135

Open
miguel-heygen wants to merge 3 commits into
mainfrom
fix/one-preview-copy-per-clip
Open

miguel-heygen wants to merge 3 commits into
mainfrom
fix/one-preview-copy-per-clip

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

What

A boxed preview copy ask is now served by a copy of the same clip that is already made or being made at a size at least as large. A new copy starts only when the ask needs a larger one.

Why

An editor that swaps two preview frames on every edit can end up with the frames asking for the copy one rung apart (here 724x430 and 608x362, both shown at 603x339). Each rung had its own cache entry, so every swap started a second transcode of the same clip. On a 4K 10-bit clip that is a full extra transcode per size while the person edits, and it competes with the edit's own save for the machine. Measured on six hover-deletes with such a clip on the timeline: 7 copy pieces before, 4 after (one copy, every piece once).

Related work

Refs the preview proxy box ladder (previewProxyBox.ts), which already rounds nearby boxes to shared rungs; this extends the sharing across rungs.

How

getCanonicalProxyCachePath is the one lookup every copy ask (resolve, progress, retry) goes through. For a boxed ask whose own copy is neither on disk nor in flight, it returns the smallest copy of the same source version and variant that this process was asked for, is at least the asked width and height, and is made or in flight. The family key is the unboxed fast key, so an edited source never reuses a stale copy. A larger copy that failed is neither on disk nor in flight, so the ask makes its own.

Limits: the record is per process, so after a restart a larger copy on disk is reused only once it is asked for again; two sizes asked within the same few-millisecond lookup can still both start.

Test plan

  • Unit tests added/updated: proxyTranscoder.test.ts "serves a smaller preview ask from the copy a larger ask is making or made, and copies again only for a larger one". fails without the fix: expected [ … ] to have a length of 1 but got 2 (two transcodes for 724x430 then 608x362).
  • src/helpers/proxyTranscoder*, src/helpers/proxyCache*, src/routes/preview*: 29 files, 586 tests, 3 runs in a row green; tsc --noEmit, oxlint, oxfmt, comment ratchet clean.
  • Manual testing performed: an editor build patched with this change, six deletes beside a 4K 10-bit clip, ffmpeg processes sampled every 0.5 s: both frames' asks joined one copy (same piece names for all four pieces).
  • Documentation updated (if applicable)
  • Comments follow CONTRIBUTING.md "Comments"

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed at 3fe4adf. Requesting one change.

A smaller ask can write its own small copy under the larger copy's name.
getCanonicalProxyCachePath now hands a smaller ask the larger copy's path when that copy is in flight (proxyTranscoder.ts:263, :281). resolveProxy uses that path only after more awaits (the on-disk check in lookUpCopy, then its turn in reachQueueInAskOrder), and then does inFlight.get(cachePath) at :1323. If the larger job's last caller leaves in that gap, leave() forgets and aborts the job at once (:438), nothing is remembered as failed, and the smaller ask starts a new job with cachePath = the larger name and box = its own smaller box (:1337 to :1344). The result is a 608x362 copy stored as the 724x430 file, with the content index pointing there too, and every later 724x430 ask is served it from disk for as long as that source version lives. Before this change the same gap only cost a duplicate copy of the right size.

The trigger is the pattern in the description: a frame swap aborts one frame's request while the other frame asks one rung apart. The window is a few file stats wide, so it is rare, but the wrong-size file stays.

Suggested fix, small: when a new job starts, transcode with the box that names the path (the lookup already holds it in sized), or re-check at :1323 that a borrowed path is still in flight or on disk and fall back to the ask's own path when it is not. A test that aborts the larger ask's only caller between the smaller ask's lookup and its turn would pin it.

What holds: the family key is the unboxed fast key with the variant and time limit, so an edited source or another variant never shares; a failed larger copy is not reused; progress and retry go through the same lookup; a larger job finishing in the same gap is safe because transcodeToCache returns the existing file (:1158). The body carries the fails without the fix: line and the named test is in the diff.

Read: proxyTranscoder.ts lookup, queue, join/leave, transcodeToCache and resolveProxy sections whole, the rest of the 1,551-line file as hunks; the new test whole. Nothing run here (no installed dependencies on this box); the finding is traced by reading, not reproduced. Checks were pending at review time.

Nit, no action needed: boxedCopiesAsked is never trimmed; it grows by one small entry per source version and rung.

Need: yes · Size: fits (+63) · Reuse: uses the existing lookup and in-flight map · Simpler: no · Structure: fine once the borrowed path carries its own box
Risk: wrong-size copy kept under a larger name in the abort gap above.

— Rames

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

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

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

Quarantined, measured but not gated (0)

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Valid, thanks. Fixed at 3cc5d76: the lookup now returns the box its path belongs to (findCopy returns { path, box }), and resolveProxy starts any job with that box. A smaller ask that finds its borrowed job already dropped now remakes the larger copy under the larger name, so a name always holds its own size.

Red first: "makes a borrowed copy at its own size when the larger job it borrowed is dropped before the smaller ask joins it" drops the 724x430 job's last caller exactly when the 608x362 ask checks the borrowed path. On 3fe4adf the second ffmpeg ran max(608/iw\,362/ih) under the 724 name; now it runs at 724. Proxy and preview route suites: 29 files, 587 tests, 3 runs green.

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed at 3cc5d76 (re-review of the delta since 3fe4adf, read whole; nothing run locally).

Prior point: resolved. A borrowed copy path now carries the size of the copy it names (findCopy returns { path, box }, and resolveProxy hands copyBox to transcodeToCache), so a smaller ask that outlives the larger job it borrowed makes the file at the size its name says. The new test "makes a borrowed copy at its own size when the larger job it borrowed is dropped before the smaller ask joins it" covers exactly that gap.

One thing left, caused by this change: Build is red at this head on bun run verify:packed-manifests:

@hyperframes/studio-server unpacked size 787666 bytes is over its budget of 779727 by more than 1%.

It was green at 3fe4adf and on main, so the added code in this delta is what crossed the line. Either trim, or raise the entry in scripts/package-size-budgets.json with the reason in the PR body, as the check asks. I approve as soon as Build is green; no other code change needed from my side.

Need: yes · Size: fits (2 files) · Reuse: yes, joins the existing in-flight map · Simpler: no · Structure: fine
Risk: low once the budget check passes.

— Rames

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Size budget, the reason for raising it (8e3f2fb): @hyperframes/studio-server unpacked was 787,419 bytes at 3fe4adf, 105 bytes under the 1% line of its 779,727 budget. The borrowed-box fix adds 247 bytes of code to proxyTranscoder (787,666). Nothing else entered the tarball: same file list at both heads, no test, fixture or map. The budget now matches the measured size, as earlier raises did; packed (203,775) stays within its budget.

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed at 8e3f2fb. Delta since 3cc5d76 compared by bytes: one line, scripts/package-size-budgets.json raises the @hyperframes/studio-server unpacked budget from 779727 to 787666 (the measured size, +7939 bytes, about 1%). Code and tests are unchanged from the head I read whole. Nothing run locally; checks are still pending at this head.

The raise is reasonable for the code this adds. One body-only thing left: the budget check asks for the reason for a raise to be in the PR, and the body does not mention it yet. Add one line (what grew and why it is not trimmable) and I approve on this same head, no new commit needed.

Need: yes · Size: fits · Reuse: yes · Simpler: no · Structure: fine
Risk: low.

— Rames

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Rames, the budget reason, here because PR bodies in this repo stay frozen: the borrowed-box fix adds 247 bytes of real code (the path's box carried from the lookup to the job), and 3fe4adf already sat 105 bytes under the 1% line; nothing else entered the tarball (same 65 files, one chunk +247 bytes), so there is nothing to trim but the fix itself.

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