Skip to content

fix(core): a held clip's still goes up the moment it comes on screen, not at the copy's next poll - #9115

Merged
miguel-heygen merged 7 commits into
mainfrom
dperf/still-when-due
Nov 30, 2026
Merged

miguel-heygen merged 7 commits into
mainfrom
dperf/still-when-due

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

What changed

A 4K clip held while its lighter copy is made now shows its still (its own frame at the playhead, from the server) on the tick it comes on screen. Before, the still was asked for only when the copy's progress was next checked, every 2 s (5 s while the copy queue is full), so a held clip arriving between checks showed nothing for up to that long.

  • mediaProxy.ts: while a copy is made, stillsWhenDue keeps the still that copy's checks would put up. showStillWhenDue(el, onScreen) puts it up when the clip is on screen. It is the same showStill call, so "due" still means the media sync has the clip inside its window (sourceTimeAtPlayhead), and a clip with a still or poster is left alone.
  • init.ts: the visibility pass calls it next to standInCaptureStill, the capture page's own per-tick still. That pass runs after syncRuntimeMedia in the same tick, so the clip's source time is already known.

What I measured

fails without the fix: packages/core/src/runtime/mediaProxy.test.ts "puts its still up on the tick it comes on screen, not at the copy's next poll". A held clip whose copy is being made, with no poll in between: no still before the clip is inside its window, none while it is off screen, the 5.0 s still once it is on screen. Without the call it fails with expected null to be '5.0'. mediaProxy.test.ts and init.buffering.test.ts pass 3 runs in a row (139 tests). All of src/runtime passes (3,944 tests), and core's tsc --noEmit is clean.

On a Windows laptop (Edge, a 4K H.264 project with 100 clips, first open with an empty copy cache), the same CLI served either main's runtime or this branch's. One clean pair; another broke on page load and is not counted:

undrawn samples with no still held, no file, no still
main 566 276 (49%) 256
this branch 613 8 (1.3%) 4

The still now goes up earlier and stays while the playhead moves on, so its frame trails the playhead further (median 3.2 s vs 1.8 s) until the copy plays.

What I did NOT exercise

  • The per-tick call through init.ts in a unit test: the test calls showStillWhenDue directly. The laptop runs went through the real tick.
  • A scrub or a paused seek onto a held clip: the same pass runs on a seek, but I only measured first opens during playback.
  • Safari and a sealed (null origin) frame: the still goes through the same showStill, unchanged here.

@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.

[P2] packages/core/src/runtime/mediaProxy.ts:640 — The visibility tick can use a still callback after its copy job is obsolete. Replacing a held clip’s source and seeking into its window before the next poll set a poster from the previous source. A quality change similarly set a poster at the old quality. Check the job’s existing live() predicate before requesting its still. Both probes passed with that guard.

The named regression passes and fails when the helper is disabled. It still passes if the visibility-pass call is removed. A paused-seek test through the runtime would cover that wiring.

149 targeted tests passed. The additional checks used mocked copy responses. Native browser timing was outside the test run.

— tai (automated review)

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 275 (base branch 273), smooth 51 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

Both points fixed in c41a44f:

  • The still closure checks its copy job's own live() before asking (mediaProxy.ts), so neither the copy's poll nor the visibility tick puts up a still for a job a new src or a quality change made obsolete.
  • "puts up no still for a copy job a new source or quality made obsolete": each case on its own fails without the guard.
  • "puts its still up on a paused seek into its window, through the runtime's own pass": it boots initSandboxRuntimeModular, seeks to 1 s while paused, and expects the 5.0 s still with no poll in between. It fails with the init.ts call removed. The file's ./bridge mock now keeps the real module and mocks only postRuntimeMessage, which the runtime needs. mediaProxy uses nothing else from it.

mediaProxy.test.ts and init.buffering.test.ts pass 3 runs in a row (141 tests); all of src/runtime passes (3,946), and tsc --noEmit is clean.

More laptop first opens (same CLI, main's runtime vs this branch's):

Still check, second clean pair: no still in 271 of 829 undrawn samples on main (33%), 10 of 518 here (1.9%).

Stall trace, 3 first opens a side, interleaved, 30 s of playback each:

stalled (s) reached (s) waiting on a copy (s) all copies done (s)
main 14.2 / 12.1 / 6.7 12.9 / 15.8 / 21.7 8.9 / 7.5 / 0.3 144 / 149 / 163
this branch 9.4 / 12.3 / 11.4 18.2 / 15.2 / 16.3 4.1 / 5.7 / 5.7 143 / 146 / 159

The first pair, which I did not count, had a branch run stuck at 0.9 s with clips waiting on copies that had no data yet. That run had no stall trace, so I can't name its cause. Its still samples show no held clip among the waiting ones, and held clips are the only ones this change touches (a poster). The same wait shows up on main in the trace above, and none of the six traced runs stuck at the start.

@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 request-time guard fixes the earlier source and quality cases. The new paused-seek test also fails if the visibility hook is removed.

[P2] packages/core/src/runtime/mediaProxy.ts:443 — A sealed preview can still install an obsolete poster after its fetch resolves. I changed the source or quality while a still was pending, then resolved the request before the next copy poll. Both cases installed the old poster. Recheck liveness before applying the response.

[P2] packages/core/src/runtime/mediaProxy.ts:658 — An obsolete copy's cleanup can clear a replacement copy's pending still. In the overlap probe the replacement response arrived and the poster stayed blank. Scope pending stills and cleanup to their copy job.

151 targeted tests passed. The three additional probes used mocked fetches. Native browser timing and the cause of the laptop stall remain unverified.

— tai (automated review)

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Both fixed at the root in a7eead8: a still, and a hold, now belong to the copy job for the clip's current source.

  • stillOwners records which job a clip's still is for (keyed by that job's source check). It replaces the per-clip "a still was asked" set and the sealed fetch's hold token.
  • A sealed still's answer goes up only if its job still owns the still and the clip still has that job's source (your first finding).
  • dropStill(el, owner) drops only that job's own still, so an obsolete job's cleanup leaves a replacement's pending still alone (your second finding).
  • The overlap probe also showed the old job's cleanup ending the new job's hold (endCopyHold): it put a src back, and the new job stopped and dropped its own still. A job now leaves a hold alone when the clip is held for another source.
  • A held clip with no src now matches only the job copying the source it is held for, not every job on that element. Before, the old job's check passed again once the new job held the clip, and the two took the still from each other.

On a quality change I kept one thing deliberately, and it's open for you to disagree. A still from a job a quality change made obsolete may still go up. It's the same frame of the same file, only drawn for a different box. The replacement job starts only after the old one settles at its next poll, so refusing it leaves the clip blank for up to that poll, which is what this PR removes. main's own test "drops its still once its file draws again, after a copy that started with no src (origin null)" pins that a held clip keeps a still across a level drop. It failed when I refused these stills, and passes here. So the still's check is the source only, while the copy job's own liveness still includes the level.

Tests in mediaProxy.test.ts:

  • "puts up no sealed still that answers after the clip got a new source": fails on c41a44f, and without the answer-time check.
  • "keeps a replacement copy's pending still through the old copy's cleanup": fails on c41a44f, without the hold guard, and without the held-source match.
  • "asks no still for a copy job whose clip has a new source": the request-time source case, carried over from c41a44f.

mediaProxy.test.ts and init.buffering.test.ts pass 3 runs in a row (143 tests); all of src/runtime passes (3,948), and tsc --noEmit is clean.

@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.

Keeping a same-source still across a quality change makes sense. That case passed, along with the new stale-response and stale-cleanup tests.

[P2] packages/core/src/runtime/mediaProxy.ts:445 — A pending sealed still is discarded after its own proxy URL is assigned but before the copy first draws. A deferred-response probe left the poster blank in that interval. The prior head displayed the still until loadeddata. Keep the still eligible through its own copy's load.

[P2] packages/core/src/runtime/mediaProxy.ts:758 — A job that started without a src still matches a different source held without a src. Null attribute equality succeeds before the held-source check. In an overlap probe, the old job installed its proxy over the replacement. Require a matching held source for this branch.

153 targeted runtime tests passed. Both remaining findings reproduced with mocked responses. Native browser timing was outside this round’s validation.

— tai (automated review)

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Both round 3 findings fixed at 8ef5c24 (I've taken this branch over from its author).

mediaProxy.ts:445, a pending sealed still dropped once its own copy's URL is set. When the still's fetch answers, it now checks that the clip is still on this job's source, or is showing this job's copy (src is that copy's URL). So a still that answers while the copy loads goes up, and copyLoadEnds still takes it down at loadeddata, error, abort or emptied. A still for a clip that moved to another source is still refused, because neither check passes.

mediaProxy.ts:758, a job started with no src matched another source held with no src. forThisSource no longer lets two empty src values match before the held-source check. With no src, a held clip matches only the source this job started for. A clip that isn't held matches only a job that also started with no src, so a clip whose file comes from a child <source> tag keeps its copy.

Tests, each red on a7eead8 and green here. With either fix reverted alone, only its own test fails:

  • "puts up a sealed still that answers while its own copy loads, and takes it down once the copy draws": your deferred-response probe. Copy served, src is the copy, then the still answers. Before the fix the poster stayed null.
  • "loads no copy from a job started on a held clip once the clip holds a new source": your overlap probe. On a7eead8 the old job's copy was installed over /b.mp4.

sealedHeld() gains a finish() that makes the copy ready. Also run: mediaProxy, init and media test files 3 times each, 615 passed every time; core tsc clean; comment ratchet passes. main is merged in.

One thing your overlap probe also showed: swapToProxy called directly on a clip with no src copies the page's own URL, since currentSrcValue is empty. No caller in the runtime gets there: lightenIfHeavy returns while a copy is requested, and the other paths need a codec entry or source facts read from src. So I've left it out of this PR.

@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.

P2 · Wait for the sealed still to finish decoding in the regression test. At packages/core/src/runtime/mediaProxy.test.ts:2019, advancing the fake clock 10 ms can leave jsdom's FileReader callbacks pending on the real event loop. The exact head failed once in the combined runtime run with a null poster, then passed five combined reruns. Await the reader's completion or mock its completion explicitly before asserting the poster. Keep the loadeddata assertion to verify cleanup.

The two previous source-ownership findings are addressed. Both regressions fail when their respective fixes are reverted, and the PR body names the required regression. Validation used mocked runtime tests.

— tai (automated review)

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Fixed at d3e4d36, for every sealed-still test in the file, not just the new one.

The cause: dataUrlOf decodes the still with FileReader, and jsdom's reader finishes on the real event loop. Each sealed-still test answered the still, stepped the fake clock 10 ms and hoped the decode had landed. A test that expects a poster could then fail, as you saw. A test that expects none could pass without checking anything, because the decode might not have run yet.

I ran a probe under fake timers. The response's and the Blob's arrayBuffer() both finish on the fake clock's first step; only FileReader's onload waits for real time. So the test file now stands in a FileReader (set in the top-level beforeEach) that reads the Blob with arrayBuffer() and gives the same data:<type>;base64,... result. Every still now decodes within a fake clock step. The production code is unchanged.

Proof that no test waits on real time now: with every 10 ms step after an answered still cut to 0,

  • with this reader: 125 of 125 pass;
  • with jsdom's reader: 9 sealed-still tests fail, several of them already on main, and yours at line 2019 among them.

The loadeddata cleanup assertion stays. mediaProxy, init and media test files: 615 passed, 3 runs out of 3. core tsc is clean, and the comment ratchet passes.

@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 sealed-still tests now use an asynchronous reader stub that completes within the fake clock step and is restored after each test. The late-still case still verifies that the poster shows during its own copy load and clears on loadeddata. The source-ownership regression also passes.

This head adds test code only. Both targeted runtime suites passed five consecutive runs (145 tests per run), and the PR description names the required regression. Validation used mocked runtime tests.

— tai (automated review)

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

The clip hold rules are now written down in one place, at ca148bb: packages/core/src/runtime/README.md, "Hold rules". It is one table of who takes each hold and what ends it, plus the copy job's rules. holdForCopy's doc line points to it. Docs and one comment line only; no code changes.

Each finding on this PR, against those rules:

Round Finding Rule that settles it
2 a still or hold from a job for an older source acting on the clip's new one The job that owns it (owner is the job for the clip's current source), Still (only the owning job puts one up) and End of the job (a job whose clip holds another source leaves that hold alone)
2, pushback a same-source still kept across a quality change The job that owns it depends on the source, not the quality level, so the still stays with the source
3 a pending sealed still dropped once its own copy's URL was set Still: it stays up while its own copy loads; the job takes it down when that load ends
3 a job started with no src claiming another held source The job that owns it: with no src, a held clip belongs only to the job for the source it holds
4 the poster assertion raced jsdom's FileReader test-only, no rule

Any future change to how a clip is held or released updates that table in the same PR.

@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 delta from the approved commit adds the runtime hold rules and a source comment pointing to them. I checked the documented ownership and still lifetime against the runtime. The production behavior and tests match the approved commit, and the PR body still names the regression. The previous review ran both targeted suites five times. This round verified the documentation delta.

— tai (automated review)

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 30, 2026
Merged via the queue into main with commit 426cdc0 Nov 30, 2026
109 checks passed
@miguel-heygen
miguel-heygen deleted the dperf/still-when-due branch November 30, 2026 04:25
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