Skip to content

feat(core): give the preview parent a typed editing port to prepare for an opaque frame - #7556

Merged
jrusso1020 merged 4 commits into
mainfrom
feat/runtime-editing-port
Nov 11, 2026
Merged

jrusso1020 merged 4 commits into
mainfrom
feat/runtime-editing-port

Conversation

@jrusso1020

@jrusso1020 jrusso1020 commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Prepares Studio's main preview to run in an opaque origin. Studio reads the preview frame's window and document directly today, and an opaque frame does not allow that. This PR adds the runtime half of the replacement: the runtime hands its parent a MessagePort and answers typed, validated requests on it. No Studio code changes yet.

What

  • packages/core/src/runtime/editingPort.ts, exported as @hyperframes/core/runtime/editing-port and added to the files tsc emits. Both sides import their wire types from this one module, so they cannot drift apart. It holds:
    • the versioned protocol (v: 1);
    • parseEditingRequest, which turns any unusable request into a typed error (malformed, unsupported-version, unknown-kind, invalid-params) and never throws;
    • createEditingPortClient, the parent side. It is promise-based, with request ids, per-request timeouts and dispose.
  • packages/core/src/runtime/editingAgent.ts is the runtime side, installed next to the control bridge in init.ts and removed on teardown.
    • When the parent posts editing-connect, the agent transfers one port to window.parent.
    • A connect from anything other than the parent is ignored and does not use up the handoff. That includes the page itself, another frame, and a top-level page, where the page is its own parent.
    • After the handoff the agent stops listening, so the port cannot be requested again or replaced.
  • Request kinds in v1: hello, runtime-snapshot, wait-for-seek and set-loop-start. A handler that throws, or returns a result that cannot be cloned, gets a failed reply.

Why

The playback and timeline hooks (useTimelinePlayer, previewMessageRouter, timelineSyncHydration, useShadowPreviewReload) read runtime state off the frame's window: __clipManifest, __clipTree, __player, __hfStartupError, __hfWaitForSeekCompletion and __hf.setLoopStart. The v1 kinds answer exactly those reads, so those hooks can move to the port next.

Play, pause, seek, rate, mute, volume and play range already have messages on the existing control protocol (bridge.ts), and <hyperframes-player> already drives an opaque frame with them. This PR does not duplicate them. The next PR switches Studio's transport to those messages.

Related work

Refs #7522 (the hover preview frame is opaque) and #7553 (fonts and modules in Studio's own preview frames).

How

Design notes and what is deferred
  • Handoff. The parent asks for the port right away and again when the runtime posts ready, so a client created before the runtime boots still connects. The agent posts the port with target origin "*", like every other runtime post in bridge.ts, because the frame is not told its parent's origin. On the parent side, the client takes a port only from its own iframe's contentWindow.
  • One handoff per document load. A reloaded frame runs a new runtime with its own handoff. A second client for the same frame document, such as a React StrictMode remount, gets no port. Studio should create one client per frame load.
  • Replies are data from the frame. The client checks each reply's envelope and maps it to a result or an EditingPortError. Callers still validate result payloads.
  • Agent reads. The agent reads the same window globals Studio reads today, so moving a hook to the port does not change what it sees.
  • Bundle size. The runtime IIFE gets only the agent and the validator. The client is tree-shaken out of it.
  • Producer size budget. The producer ships the runtime, so its packed tarball grew to 878,763 bytes, over its 870,000 budget. This PR raises that budget to 880,000 in scripts/package-size-budgets.json.
  • Deferred to later PRs:
    • Studio transport moves to the existing control and state messages.
    • Scene swap (__hfSwapScenes, with a cancel request).
    • Keyboard shortcut forwarding from the frame, which is a pushed event, not a request.
    • The timeline's DOM fallbacks and sub-composition child rows (parseTimelineFromDOM, collectHostDomChildren), which move into the runtime as a snapshot request.
    • Element rects, computed styles and attributes.
    • DOM patches (attribute, style, text).
    • GSAP timeline reads and patches.
    • Console and error forwarding, to replace useConsoleErrorCapture patching the frame's console.
    • Capture and pick helpers.
    • Manual-edit gesture state, which moves to the parent along with the gesture code.

Test plan

  • Unit tests added: editingAgent.test.ts and editingPort.test.ts, 28 tests.
    • The agent answers each kind from runtime state.
    • 16 malformed requests each get a typed error, and none of them runs a handler.
    • The port goes to the parent once.
    • A connect from the page itself, a sibling frame, a null source, the wrong version, a top-level page, or after dispose gets no port.
    • The client round-trips with the agent.
    • The client takes a port only from its own frame.
    • Error replies and malformed replies map to typed errors.
    • Requests time out before and after the handoff.
    • dispose rejects pending and later requests and closes the port, including when it runs before the handoff, with no unhandled rejection.
  • Mutation-probed. Each of these reverts made a test fail:
    • the parent-source check;
    • the top-level guard;
    • the single handoff;
    • the connect version check;
    • the set-loop-start params check;
    • the empty-params check;
    • the request version check;
    • looking up kinds with in, which lets __proto__ and toString through;
    • the client's own-frame check;
    • the client timeout;
    • the rejection handler on ready.
  • Headless Chrome 153 run, using the real runtime IIFE in a sandbox="allow-scripts" srcdoc frame. self.origin was "null" and contentDocument was null.
    • One handoff; hello, runtime-snapshot, set-loop-start and wait-for-seek all answered.
    • The composition's own script posted editing-connect to itself twice and received no port.
    • A second parent connect produced no second port.
  • src/runtime vitest: 131 files, 3043 tests.
  • Core test:hyperframe-runtime-ci, typecheck, check:package-subpaths, check:package-cycles, fallow audit. The built dist/runtime/editingPort.js imports under Node.
  • Manual testing in Studio. No Studio code consumes the port yet.
  • Comments follow CONTRIBUTING.md "Comments".

— Rames Jusso

🤖 Generated with Claude Code

jrusso1020 and others added 4 commits November 11, 2026 16:15
…or an opaque frame

Studio reads the preview frame's window and document directly, which stops
working once the preview runs in an opaque origin. This adds the runtime half
of the replacement, with no Studio consumer changed yet:

- runtime/editingPort.ts: a versioned request/reply protocol, a validator
  that turns any bad request into a typed error, and a promise-based parent
  client with request ids, timeouts and dispose. Exported as
  @hyperframes/core/runtime/editing-port.
- runtime/editingAgent.ts: on the parent's connect, the runtime hands
  window.parent a MessagePort once and answers hello, runtime-snapshot,
  wait-for-seek and set-loop-start on it. A connect from anything but the
  parent is ignored and does not use up the handoff.

Playback transport stays on the existing control protocol.

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

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

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

The producer ships the preview runtime, which now carries the editing agent
and its request validator. The packed tarball measured 878,763 bytes in CI
against an 870,000 budget.

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

Copy link
Copy Markdown
Contributor

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

@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 · 0 blocker · 0 important · 1 nit · head 9e8d6d78

The agent hands the port only to the parent, and a malformed request never reaches a handler. I checked both in real Chromium, not just with the synthesized MessageEvents. ↓ one inline nit about re-handoff.

Scope and probe

Read end to end: editingAgent.ts, editingPort.ts, the init.ts hook, and the subpath/tsconfig/budget changes.

Probe: an esbuild IIFE of both modules, run in chromium-1237. The parent was on 127.0.0.1 with a sandbox="allow-scripts" (opaque) preview frame and a sibling frame on localhost.

  • The sibling's connect and the frame's own self-post connect were both ignored, and neither used up the handoff. The parent's client then got the port; the sibling and the frame got none.
  • hello, runtime-snapshot (with clipParents [["b","a"]]) and set-loop-start round-tripped. seconds: -3 came back as invalid-params.
  • A second parent connect after the handoff produced no second port (1 port total).
  • On a top-level page, a self-posted connect produced no handoff.

The parser hands each handler a freshly built params object. Kind lookup uses === against the list, so __proto__ and toString can't match. The ready post happens synchronously in installRuntimeControlBridge, before the agent is installed in the same task, so a connect sent in reply to ready can't arrive before the listener exists.

The "*" handoff gives any embedder a port, but its data is roughly what the bridge's timeline post already sends to any parent. No prior reviews. CI is green on this head; Player Safari perf was still running.

— Claude

if (!isPlainRecord(data) || data.source !== "hf-parent" || data.type !== EDITING_CONNECT)
return;
if (data.v !== EDITING_PROTOCOL_VERSION) return;
window.removeEventListener("message", onMessage);

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.

nit: with one handoff per document, a client that is disposed and recreated for the same frame load never gets a port, and every request times out. That's what happens when a Studio component remounts while the iframe stays (I reproduced it: dispose(), then a new client for the same frame, then hello → timeout). The parent-only guarantee comes from the source check, not from limiting it to one handoff. On a later parent connect you could close the old port and hand over a new one, or at least spell out the one-client-per-load contract for the consumer PRs.

@jrusso1020
jrusso1020 added this pull request to the merge queue Nov 11, 2026
Merged via the queue into main with commit ef5cdeb Nov 11, 2026
97 of 98 checks passed
@jrusso1020
jrusso1020 deleted the feat/runtime-editing-port branch November 11, 2026 17:11
@terencecho terencecho mentioned this pull request Nov 11, 2026
3 of 4 tasks
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.

1 participant