Skip to content

fix(core): the preview runtime's editing port ignores later changes to MessagePort and window.parent - #8963

Open
miguel-heygen wants to merge 3 commits into
mainfrom
fix/editing-port-native-methods
Open

miguel-heygen wants to merge 3 commits into
mainfrom
fix/editing-port-native-methods

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

What

The preview runtime's editing port keeps using the browser's own MessagePort methods and the frame's original parent, even when a page script later changes MessagePort.prototype or assigns window.parent.

Why

The runtime evaluates before the page's own scripts, but it set the port's onmessage and called postMessage and close through MessagePort.prototype at call time. So a page script that patched those methods saw every port the runtime opened, and could post on it as if it were the runtime. installEditingAgent also read window.parent when it installed, which happens after the page's scripts run, and a page can assign that property. canvasKeys.ts already read postMessage early; the other port users did not.

How

  • New runtime/runtimePort.ts reads MessageChannel, postMessage, close and the onmessage setter from a probe channel when the runtime first evaluates. It exposes createRuntimeChannel, postOnPort, onPortMessage and closePort. Those use the stored methods for any browser port and fall back to the object's own methods for a stand-in object, as in tests.
  • The editing agent, watchGeometry, textEdit, stageAct, pausedFrames and canvasKeys use them for every port call. canvasKeys drops its own copy of the same lookup.
  • installEditingAgent defaults its parent to window.parent as read when the module evaluated.

No visible change

The editing protocol and every message are unchanged. Studio works the same as before.

Test plan

  • Unit tests added: editingAgent.portCapture.test.ts. "keeps its port from a composition that patched MessagePort before Studio connected" patches onmessage and postMessage after the runtime loads, then connects and talks to the agent. It checks that the patches saw no runtime port and that a Delete posted through anything they saw never reaches the parent. "hands its port to the parent it loaded with, not one a composition put on window.parent" assigns window.parent before install and checks that the port is never handed to it.
  • fails without the fix: both tests in editingAgent.portCapture.test.ts fail with the six runtime files at main, and pass with this change.
  • Core runtime suites: 177 files, 3,889 tests, 3 runs in a row. test:hyperframe-runtime-ci exits 0. tsc --noEmit is clean. The Studio sealed-keys, sealed, preview-channel, stage-act and dom-edit-session suites pass against the rebuilt core (112 tests).
  • Comments follow CONTRIBUTING.md "Comments".

…o MessagePort and window.parent

Port calls use the browser's own methods, read when the runtime first evaluates.
The agent hands its port to the parent the frame loaded with.
@miguel-heygen miguel-heygen added the needs-jerrai-review Request a review from Jerrai. Removed after the review posts; re-add to request another. label Nov 27, 2026

@jerry-heygen jerry-heygen left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Changes requested at 66b24c03. Capturing the native port methods is the right direction, but the helper still depends on mutable page-realm operations when it uses those methods after composition code has run. The privileged port path should use only operations captured at runtime startup and should not fall back to page-controlled methods on a real port. Please add a regression for that boundary, not only for direct replacement of MessagePort methods.

I verified all eight effective changes against the current test merge. This is a source finding; I did not run a browser reproduction.

— Jerrai (pr-review)

@jerry-heygen jerry-heygen removed the needs-jerrai-review Request a review from Jerrai. Removed after the review posts; re-add to request another. label Nov 27, 2026
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 273 (base branch 271), smooth 66 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-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

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

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

Quarantined, measured but not gated (0)

… use built-ins read at load

Covers the channel's port getters, MessageEvent data and the Maps that hold ports.
Native calls go through Reflect.apply read at load.
@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

3bebf863 widens this. The description says a later change "never sees a runtime port"; that overstates it. This is a best-effort change.

Covered now, all read when the runtime first evaluates and called through a Reflect.apply read at the same time:

  • MessagePort postMessage, close and the onmessage setter.
  • The channel's port1/port2 getters. Node keeps them as the channel's own data, and that is read directly.
  • MessageEvent's data getter, for messages arriving on a runtime port.
  • The containers that hold ports: the agent's open ports and the key-watch and geometry-watch maps now go through portMap(), whose Map methods were read at load.

Not covered: a built-in the runtime reaches with a port by some other route after load. Page script shares this realm, so the frame side cannot fully close that. The checks that matter for what Studio does with frame messages belong on the Studio side.

Test added: editingAgent.portCapture.test.ts > "keeps its port from a composition that patched the channel's port getters, Set.add or Map.set". It fails at 66b24c03 and passes here. Core runtime: 3,890 tests; tsc --noEmit clean.

@jerry-heygen jerry-heygen left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Changes requested at 3bebf863. The captured invocation, channel accessors, event-data read, and port collections address most of my previous review. One part remains: the production helpers still use the stand-in path if native capture or port recognition fails. In an editing session that means the code can silently fall back to the page’s methods instead of stopping the connection. Please keep the stand-in adapter explicit for tests and fail closed for Studio’s browser channel if the native methods aren’t available. This doesn’t require making the shared realm a security boundary.

The hosted runtime suite passed at this head. The separate CLI shard failed in received-record fixture cleanup (ENOTEMPTY); that is not the reason for this hold. I did not run a browser reproduction.

— Jerrai (pr-review)

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