Skip to content

feat(studio): playback and timeline reads go over the preview's message channel - #7561

Merged
jrusso1020 merged 9 commits into
mainfrom
feat/studio-playback-over-bridge
Nov 12, 2026
Merged

jrusso1020 merged 9 commits into
mainfrom
feat/studio-playback-over-bridge

Conversation

@jrusso1020

@jrusso1020 jrusso1020 commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Prepares Studio's preview to run on an opaque origin, where Studio cannot read the frame's window. This is the first slice of moving Studio's playback and timeline readers onto the runtime's message channel (#7556). It also fixes the one-port-per-page-load limit in that channel.

Same-origin paths are unchanged. Every preview today is same-origin, and Studio still reads those frames directly, exactly as on main, so pairing this Studio with any runtime version changes nothing a user sees. Some hosts can pair it with a runtime older than #7556, which has no editing port. The port and the control bridge are used only for an opaque frame, one whose document Studio cannot read. Nothing flips sandbox-origin.

What

  • Fresh port on a later connect (packages/core/src/runtime/editingAgent.ts). The agent handed its port over once per page load. A parent that disposed its client and made a new one for the same frame got no port, so every request timed out. This happens on a React StrictMode remount, or a remount that keeps the iframe. Now each valid connect from window.parent gets a fresh port. Earlier ports stay open, because the runtime posts ready before it installs the agent, so a client can connect twice and take either port. A disposed client closes its own end, and the agent closes every port on teardown. A connect from any other sender is still ignored.

  • player/lib/previewChannel.ts (new).

    • One editing-port client per frame load. The frame's next load disposes it, and the next call opens a client for the new runtime.
    • previewFrameOpaque(iframe): true only when reading the frame's window.document throws, as it does across origins. A same-origin window always counts as scriptable, even one that reports a null contentDocument. A frame with no window is not opaque, so it keeps its old no-op path.
    • setFrameLoopStart: on a non-opaque frame, calls the runtime's __hf.setLoopStart directly, as before. On an opaque frame, it sends the loop start over the port, only when the value changes.
    • Two more port reads for opaque frames: seek painted, and the clip manifest. The manifest reply is shape-checked before use.
    • The seek-painted wait first gives the port 1 s to arrive (AGENT_PROBE_MS). A runtime without the editing agent never sends one, so a reload is not held for the full 15 s budget.
  • player/lib/bridgePlayback.ts (new). For opaque frames only: a PlaybackAdapter that sends play, pause and seek as control-bridge messages (bridge.ts). It takes time, play state and duration from the state and timeline messages that frame's runtime posts. It keeps a seek until a state confirms it, with a 500 ms limit, so a state posted before the runtime handled the seek cannot move the playhead back between quick frame steps. The confirming frame is the one the runtime lands the seek on: the frame at or before the target, using the post's fps, or within half a frame at 1 fps when a post carries no fps.

    It takes posts only from a frame Player registered with watchPreviewFrame (called before the frame loads, so the boot-time timeline is not missed), and only from an origin that frame can have. That is null for an opaque sandbox or a data: URL, the parent's origin for srcdoc, about: or no src, and otherwise the origin of its src. A post from any other window or origin is dropped, never stored.

  • Readers moved:
    Each reader keeps its direct path for a same-origin frame and gets a port or bridge path for an opaque one:

    • useTimelinePlayer setLoopStart, through setFrameLoopStart.
    • previewMessageRouter: the state-tick manifest recovery. An opaque frame is asked over the port, one request at a time.
    • playbackAdapterResolution: the new resolveFramePlaybackAdapter. A non-opaque frame gets the same direct adapter useTimelinePlayer.getAdapter built before.
    • useShadowPreviewReload: the restore-seek wait.
    • sceneSwap sceneSwapFor: an opaque frame gets no in-place scene swap, so a reload falls back to a full shadow reload. On a same-origin frame it reads __hfSwapScenes as before.

Why

Before the main preview frame can go opaque, every Studio reader that reaches into the frame has to move onto a message channel. This PR covers the playback transport and the runtime-state reads in the playback and timeline hooks.

Related work

Refs #7556 (the runtime's editing port) and #7522 (the hover preview frame is opaque).

How

Claude Opus 5.5

Port vs bridge. Play, pause and seek go over the existing control bridge, as <hyperframes-player> already does for an opaque frame. Reads that have no bridge message go over the editing port: loop start, seek painted, and the manifest.

Why same-origin keeps the direct adapter. The direct adapter answers getTime() synchronously, every animation frame. It also wraps a document that runs longer than its adapter (static seek), and it falls back to __timelines when a composition has no __player. The bridge adapter can only report the last state the runtime posted, and the bridge's seek takes no keepPlaying or settled option. Using it for same-origin previews would change playback timing for every user today. So the bridge adapter is used only when the frame cannot be scripted, and the switch happens when the frame goes opaque.

Port only for opaque frames. A host can pair this Studio with a runtime older than #7556, which has no editing agent. A port-first read would never get an answer there. For the loop start, that means the runtime's own loop wrap would not know where to land. So every reader, not only the playback adapter, keeps the direct same-origin path from main, and the unchanged main test for the loop start (useTimelinePlayer.seek.test.ts) still passes. The port gets real traffic once the frame goes opaque.

Per frame load.

  • The editing client is cached per iframe element and disposed on that element's next load. With the agent fix above, a client created after a dispose still gets a port.
  • The bridge mirror is keyed by the frame's window, which stays the same across loads. Its listener is installed when the module loads, because a runtime posts its first timeline while it boots, before the frame's load. A listener added later would never see the duration.

Replies are frame data.

  • The manifest reply must have a clips array and a finite durationInFrames.
  • The mirror accepts only finite, non-negative times and durations, and only messages that pass the Studio protocol check.

Same-origin direct paths kept on purpose. These are main's reads, now behind previewFrameOpaque, and they go away when the frame flips (paths under packages/studio/src/player/):

  • lib/previewChannel.ts:35 (setFrameLoopStart)
  • hooks/previewMessageRouter.ts:114
  • hooks/playbackAdapterResolution.ts:45,64,109
  • hooks/useShadowPreviewReload.ts:40

Deferred. These direct reads remain and have no opaque path yet, because the runtime cannot supply the data or because this PR keeps to its size. On a same-origin frame they behave as before:

  • hooks/timelineSyncHydration.ts:202 (safeContentDocument), :446 (__clipManifest in hydrateTimelineFromPreview), and the DOM normalisation and DOM-parse fallbacks there. These need a runtime snapshot of the DOM rows.
  • hooks/useTimelineSyncCallbacks.ts:184 (__clipTree parent map; the port already answers clipParents) and :221-222 (enrichMissingCompositions).
  • hooks/previewMessageRouter.ts:155: the DOM-parse fallback when a timeline message produced no rows.
  • hooks/useTimelinePlayer.ts:183 (__timelines timeScale in applyPlaybackRate) and :502 (gesture check before a scene swap).
  • hooks/usePlaybackKeyboard.ts:205-206: keyboard listeners inside the frame. These need the runtime to forward keys.
  • sceneSwap.ts:58,118: the stale-scene meta write, and the in-place swap (__hfSwapScenes), which an opaque frame skips for a full reload. The swap needs a port request with cancel.
  • hooks/useShadowPreviewReload.ts:46,53-55,168,191: the reload-failure diagnostics and the live gesture checks.
  • The bridge seek gaining keepPlaying/settled, so that a loop wrap over the bridge does not pause and resume.
  • The bridge mirror is keyed by the frame's window, so on an opaque frame it outlives a reload that reuses the same iframe (the Player's retry). Until the new runtime posts its timeline, the adapter can report the old document's duration.

Before

Before: on main, Studio's adapter lookup on an opaque preview frame throws SecurityError, the clip stays at 0s, and a second editing client for the same frame times out

After

After: the same opaque frame seeks to 1.5s, plays and pauses over the control bridge, with a 2s duration from the runtime's timeline message. A remounted client gets a port, and a same-origin frame still gets the runtime's own player

Both are real Chrome 153 runs. The composition and runtime sit in a sandbox="allow-scripts" frame, driven by this PR's Studio modules bundled for the browser. For Before, the runtime was built from main and the adapter lookup is main's resolvePlaybackAdapter path.

Test plan

  • Unit tests added/updated:

    • editingAgent.test.ts: each later connect from the parent gets a fresh port and the earlier port keeps answering; dispose closes every port handed over; another frame's connect after the handoff leaves the parent's port alone.
    • editingPort.test.ts, with the real agent and the real client: queued deliveries in three orders (ready before either port, ready between the two connects, the first port before ready). Each ends with a working port. The first two orders fail with an agent that closes the previous port on each connect.
    • editingPort.test.ts: a new client for the same frame is served after the first is disposed.
    • previewChannel.test.ts, bridge adapter, two cases that fail on c7b73fd4 and pass now:
      • After seek(4.01), the runtime's 30 fps post of 4.0 (still playing) is taken at once.
      • A post from an unregistered window, and posts from a registered frame with an origin it cannot have, are ignored.
    • previewChannel.test.ts, origins: registered frames loading srcdoc, about:blank, a data: URL or another origin are accepted only from their own origin.
    • previewChannel.test.ts, 8 tests: which frames count as opaque (including one with no window); one client per frame load; on a same-origin frame, the loop start goes to the runtime's own hook with no port, and on an opaque frame it goes over the port only on change; the seek-painted wait; manifest shape checks; the bridge adapter's control messages on a frame whose window throws on any read; state and timeline mirrored from that frame only.
    • previewMessageRouter.manifest.test.ts: a same-origin frame's manifest is read straight off it, at once, with no port. An opaque frame's comes from the port, one request at a time.
    • useTimelinePlayer.seek.test.ts is identical to main, and its loop-start test still passes.
  • Mutation probes for this revision: going back to the contentDocument === null test for opaque fails previewChannel.test.ts and the existing useDomEditPreviewSync.test.ts, whose fake same-origin frame has no contentDocument. In addition: the agent from the previous head (closing the earlier port) and the agent from main (one handoff per load) each fail the new agent and port tests; removing the 1 s agent probe, the seek confirmation, or the opaque guard in sceneSwapFor each fails its test.

  • Mutation probes, earlier revisions. Each change below was reverted on its own, and the named test failed: the agent's close-and-rehand (and, separately, restoring its once-only handoff), the per-load client reset, the loop-start dedupe, the manifest shape check, the bridge branch in resolveFramePlaybackAdapter, the mirror's time guard, and the router's one-at-a-time guard. For both the loop start and the router, forcing the port path for every frame fails the same-origin test (for the loop start, main's seek test as well), and forcing the direct path fails the opaque test. Treating a windowless frame as opaque also fails a test.

  • Studio src/player plus the 6 other test files that mount the player: the full Studio suite, 745 files and 8,496 tests, passed. Core editingAgent, editingPort and bridge: 70 passed. tsc --noEmit (studio), oxlint, oxfmt, fallow, the comment ratchet and citation checks, check:package-cycles, check:package-subpaths and check:studio-copy-dashes all passed.

  • Manual testing performed: the real-Chrome run above.

  • Same-origin UX parity in a real Studio session. The Studio dev server ran the studio-open fixture (12 scenes, 60 s) in headless Chrome 153, with the same scripted journey on three setups:

    • (a) base ef5cdeb0;
    • (b) this PR's head;
    • (c) this PR's Studio paired with a runtime built without the editing agent, which stands in for a host on an older runtime. A connect probe got no port from it; the same probe got one on (a), which shows the probe works.

    Results came from the runtime's own state posts and Studio's buttons, and the three runs matched:

    • Play: about 1.5 s of playback. Pause: time held still.
    • Keyboard seek: 3 × Shift+Right moved exactly 30 frames.
    • Ruler scrub: 10 drag steps landed on 11 distinct times, with no playback during the drag.
    • In/out loop: I/O set a 2 s range (28.6 to 30.6 s) and turned loop on. Playback wrapped twice in 5 s and stayed inside the range.
    • Reload: a project file edit loaded a new preview document. It kept the playhead (29.57 s), and Play then advanced about 1.5 s in the new document.
    • Each run had no console error except the same four 404s.
  • Opaque-preview parity in a real Studio session. The same journey ran on this head with every <hyperframes-player> Studio created set to sandbox-origin="opaque", a scratch patch applied in the page and not committed. The frame's sandbox was allow-scripts and its contentDocument read as null, so the bridge adapter and the port reads ran. Compared with the same-origin run on the same head:

    • Play, pause, keyboard seek (30 frames), ruler scrub (12 vs 11 distinct times), in/out loop (2 wraps within 28.6 to 30.65 s), playhead kept across a reload (29.53 s), and play after reload: all matched.
    • Different: the reload is promoted after about 9 s, against about 0.5 s same-origin. The cause is outside the Studio readers changed here. In an opaque frame, <hyperframes-player> cannot scan assets and waits for the runtime's assets-ready, and the hidden reload frame posted that after about 8.3 s (0.3 s same-origin). The previous preview stays on screen meanwhile. This has to be fixed before any preview is switched to opaque.
    • Before this commit, the opaque run also showed two defects that are now fixed here. A reload threw on __hfSwapScenes. Quick frame steps lost a step (20 of 30 frames) to a stale state post.
  • Not exercised: the Desktop app's browser view. Desktop picks this up only on its next @hyperframes pin bump, and parity gets checked there.

  • Comments follow CONTRIBUTING.md "Comments".

— Rames Jusso

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 267 (base branch 267), smooth 107 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 (8)

@jrusso1020
jrusso1020 force-pushed the feat/studio-playback-over-bridge branch from cc4741d to 3a44017 Compare November 11, 2026 23:33

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

Verdict: REQUEST CHANGES · 1 blocker · 1 important · 0 nits · head 3a440172

  • important: the UX-parity run only covers frames Studio can script. The dev server's preview is same-origin, so it never reaches the bridge adapter or the port reads. The opaque path is tested only in jsdom, with a harness that never closes ports, which is how the blocker below got past it. Please add a parity run with an opaque preview, e.g. sandbox without allow-same-origin.
Scope
  • Read all 12 files. Required CI is green on this head.
  • The CodeQL origin alert at bridgePlayback.ts:40 is a false positive: the mirror is keyed by event.source, so a window can only write its own entry.
  • Not posted (medium confidence): a retry reload reuses the same iframe (Player.tsx:238). The mirror is keyed by window and survives that reload, so initializeAdapter can take the fast path with the old document's duration before the new runtime is listening.

— Genesis

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

Verdict: APPROVE · 2 resolved, 0 open, 1 new (important, opaque path only) · 3a44017 → c7b73fd

— Genesis

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

Verdict: APPROVE · 1 resolved, 0 open, 0 new · c7b73fd → c8f421e

— Genesis

@jrusso1020
jrusso1020 added this pull request to the merge queue Nov 12, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 12, 2026
jrusso1020 and others added 8 commits November 12, 2026 08:57
…ng port

A parent that disposes its editing client and creates a new one for the same
frame (a remount that keeps the iframe) used to wait forever: the runtime
handed its port over once per page load. Each valid connect from the parent
now closes the previous port and hands over a new one. Connects from any
other sender are still ignored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ge channel

Prepares Studio's preview to run on an opaque origin, where the parent cannot
read the frame's window.

- previewChannel: one editing-port client per frame load, plus the loop start,
  seek-painted and clip-manifest reads over it.
- bridgePlayback: a playback adapter that sends play, pause and seek as
  control-bridge messages and mirrors the runtime's state and timeline posts.
  It is used only for a frame Studio cannot script; a same-origin frame keeps
  the direct adapter.
- The loop start and the state-tick manifest recovery now always use the
  editing port. The shadow reload's seek-painted wait uses it for a frame
  Studio cannot script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hare

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… Studio can script

A host can pair this Studio with a runtime older than the editing port. The
loop start and the state-tick manifest recovery now read a same-origin frame
directly, exactly as before; only a frame Studio cannot script uses the port.
previewFrameOpaque replaces previewFrameScriptable and treats a frame with no
window as not opaque, so a detached frame keeps its old no-op path too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… on an opaque preview

The runtime posts ready before it installs its editing agent, so a client can
connect twice and receive two ports. Closing the earlier port on each connect
left a client that took the first one with a dead channel for the rest of the
frame load. The agent now keeps earlier ports open (a disposed client closes
its own end) and closes all of them on teardown.

For an opaque preview:
- the restore-seek wait gives up after a 1 s probe when no editing port
  arrives, so a runtime without the agent no longer holds a reload for 15 s;
- the bridge playback mirror keeps a seek until the runtime confirms it, so
  a state posted before the seek cannot undo a quick frame step;
- sceneSwapFor returns no swap instead of reading the frame's window, so a
  reload falls back to a full reload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
previewFrameOpaque treated any frame whose contentDocument was null as opaque.
A same-origin window can still report a null contentDocument (an iframe that is
not in the document), and that sent it down the opaque path. A frame is opaque
now only when reading its window's document throws, as it does across origins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t frame precision

The bridge playback mirror took a state or timeline post from any window and
made a mirror for it. It now takes a post only from a frame Player registered
(watchPreviewFrame, called before the frame loads, so the boot-time timeline
is not missed), and only when the post's origin is one that frame can have:
null for an opaque sandbox or a data: URL, the parent's origin for srcdoc,
about: or no src, else the origin of its src.

A pending seek now counts as confirmed by the frame the runtime lands it on,
the frame at or before it, rather than by a 1 ms match. An off-grid seek used
to drop every state post for 500 ms, freezing the playhead and its play state.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the document

Main's test harness now attaches its fake iframe, and happy-dom then fires
that frame's about:blank load on its own, which ends the editing client's
frame load mid-test. These tests build their own detached frame instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jrusso1020
jrusso1020 force-pushed the feat/studio-playback-over-bridge branch from c8f421e to 3d69078 Compare November 12, 2026 09:02

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

Verdict: REQUEST CHANGES · 1 blocker · 0 important · 0 nits · c8f421e → 3d69078

The rebased patch is byte-identical to the approved one apart from a hunk offset, and detachedFakeIframe is correct. However, the required Tests on windows-latest check (studio-1) is red on this head because a sibling test hits the same attached-iframe race. ↓ inline

— Genesis

…test

The manifest router test had the same race: the attached harness frame's own
about:blank load ended the editing client before the port answered, which
lost on the slower Windows runner. The harness now exports
detachedFakeIframe, which makeFakeIframe builds on, and both opaque-frame
test files use it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

Verdict: APPROVE · 1 resolved, 0 open, 0 new · 3d69078 → e50ce91

— Genesis

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