Skip to content

refactor: single-source hydration wire protocol literals - #1136

Closed
WebEferen wants to merge 2 commits into
mainfrom
refactor/single-source-protocol-literals
Closed

WebEferen wants to merge 2 commits into
mainfrom
refactor/single-source-protocol-literals

Conversation

@WebEferen

@WebEferen WebEferen commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Every hydration wire literal now has exactly one owning module. Six families of
them were re-typed across two to five files each, so a change on one side of a
hydration boundary could diverge from the other and no test would notice: the
failure only shows up as a mismatch in a real app.

literal owner now was spelled in
[ ] [f [f0 [f1 hydration-markers.ts constants.ts + stream-protocol.ts
data-octane-hydrate-{id,when,id-count,seed} + selector hydration-markers.ts constants.ts + 3 × hydration/* + runtime.ts
useId spelling (: prefix in- base36 :) hydration-markers.ts runtime.ts + runtime.server.ts
[b;…;root, [f0;b;, [f1;b; dom-binding-protocol.ts + runtime.ts + runtime.server.ts
element-scoped ViewTransition style id + body css.ts runtime.ts + runtime.server.ts
10 × Symbol.for registry keys runtime-tags.ts (new) up to 5 files each

hydration-markers.ts is the home for the first three because the pre-root
capture bundle and stream-protocol.ts deliberately avoid constants.ts (it
imports dom-tables.js). That constraint is why the literals were duplicated in
the first place; moving the declarations down to the table-free leaf and
re-exporting them from constants.ts satisfies it without a second spelling.
octane/constants exports the same names and values as before.

Marker payloads stay plain literals rather than deriving the longer ones from
the shorter. A bundler does not constant-fold a cross-module template literal,
so a derived spelling reaches every output as a runtime concat and measured
roughly twice the bundle cost. Downstream prefixes still derive from them.

Validation

  • pnpm format:check
  • pnpm typecheck
  • pnpm test
  • targeted tests: packages/octane/tests/wire-protocol-single-owner.test.ts (new)

Local pnpm test: 2 failed / 67079 passed. Both are the webkit cases in
packages/vite-plugin-octane/tests/production.test.ts, which fail identically
on origin/main on the same machine; origin/main additionally fails one case
in packages/vaul/tests/browser/vaul.browser.test.ts, so this branch is a
strict subset of the base failures here. The suite needs
NODE_OPTIONS=--max-old-space-size=8192 on that machine; at the default heap it
dies with a V8 OOM and reports unrelated flakes on the way down. CI is the
authoritative signal.

Bundle cost, benchmarks/bundle-size/run-minimal.mjs on two worktrees of the
same machine (origin/main @ 9307ab2 vs this branch):

scenario base raw this raw Δ raw Δ gzip
capture-only 10229 10361 +132 (+1.29%) +47
cli-spa-starter 142492 142586 +94 (+0.07%) +29
deferred-hydration 307471 307597 +126 (+0.04%) +43
hydrate-root 270453 270568 +115 (+0.04%) +60
root-static, hooks-state, binding-hooks (see note) (see note) +36 (+0.02%) +19 to +30
root-static-specialized, server-hooks, binding-vanilla, binding-aria (see note) (see note) 0 0 to +4

Two of that benchmark's assertions already fail identically on pristine
origin/main here (capture-only measures 10229 against a 3776 budget, and
cli-spa-starter reports styled: false), so I made them non-fatal
symmetrically in both worktrees to collect every scenario. That harness edit is
not part of this diff.

Risk / follow-ups

Bugbot flagged this Medium Risk for the silent-hydration-mismatch class, which
is the right class to worry about. Here is what it actually reduces to.

Every value is pinned, not merely relocated. No literal changed spelling in
this diff, and the new suite asserts each one against its written-out form:
'[', ']', '[f', '[f0', '[f1', '[b;', bindingRootMarker('v7') === '[b;v7;root', formatUseId('r3-', 35) === ':r3-in-z:', the four
data-octane-hydrate-* names and the selector, both halves of the ViewTransition
stylesheet, and tag === Symbol.for(key) for all ten registry keys. A typo made
while moving a declaration fails the suite instead of reaching a hydration
boundary. I confirmed that by breaking one value per family on purpose and
watching the suite go red.

The one measured cost is bundle size: +132 raw / +47 gzip bytes (+1.29%) on
the pre-root capture entry, +0.07% or less on every other scenario, and exactly
zero on server-hooks, binding-vanilla and binding-aria-vite, which is the
evidence that the new tag leaf still tree-shakes. Full table above. I measured
three shapes of the change and shipped the cheapest one that still keeps a
single owner per literal.

The behavior change is unreachable from this repo. The only in-repo producer
of a kind: 'view' handoff range is currentRanges() in
dom-binding-program.ts, which sets view: definition.view.id from
CompiledBindingProgram.id, typed readonly id: string. So range.view is
always a string for a view range, and the new range.view === undefined arm
cannot be taken. The type cannot express that because BindingHandoffRange is a
flat interface rather than a discriminated union, and external adapters
implement it, so narrowing it is a contract change and deliberately not done
here. For a malformed external lease the previous code compared against
"[b;undefined;root", which only a view stamped with the literal id
undefined could match, so the two agree everywhere except a doubly
pathological case, where rejecting (and re-rendering) is the safe direction.

/* @__PURE__ */ on Symbol.for is existing practice, not something this PR
introduces.
main already uses it in six places, including the cross-adapter
keys octane.binding-handoff and octane.control-handoff. Dropping an
unregistered key is unobservable because Symbol.for creates on read: a bundle
that needs a tag calls for it itself rather than relying on another module
having registered it first.

Not covered, and not exposure this diff creates

Both of these are duplication that already exists on main. This diff neither
improves nor worsens them; they are listed so the remaining surface is explicit.

  • src/compiler/* ships as verbatim JavaScript and can only import plain-JS
    leaves (it already imports dom-tables.js, event-names.js,
    sanitize-url.js). So compile.js still mints its own [b; markers, and
    __oct_loc, octane-frag, __octanePermanentStatic and
    __octaneNativeChangeDiagnostic stay duplicated against the runtime. Sharing
    them needs the leaf converted to .js plus .d.ts; the new suite scopes
    itself to packages/octane/src outside compiler/ and says so.
  • Octane-RPC-Outcome is written in packages/app-core/src/server/rpc.js and
    read in three packages/octane/src modules. app-core declares octane as a
    peer dependency and that file imports nothing from octane today, so
    single-sourcing the header would mean adding a wire constant to octane's
    public export surface and introducing the first octane import into that
    module. That is an API decision rather than a mechanical move, which is why it
    is out of scope here rather than a boundary this diff leaves broken.

Provenance

  • An agent produced this diff (agent-authored)

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Changes span client/SSR hydration markers, deferred-boundary attributes, and shared Symbol tags—any typo would surface as silent hydration mismatches, though values are unchanged and a new scan test guards duplicate spellings.

Overview
Consolidates hydration and cross-runtime wire literals so SSR, the client runtime, pre-root capture, and React/universal layers import the same definitions instead of re-typing strings and Symbol.for keys in multiple files.

New or expanded owners: hydration-markers.ts (block/@for comment payloads, deferred <Hydrate> attribute names and selector, shared formatUseId), dom-binding-protocol.ts (binding comment prefixes, bindingRootMarker, isForBindingOpenComment), css.ts (element-scoped <ViewTransition> style id/CSS), and runtime-tags.ts (Octane/React registry tags). constants.ts still exposes the public API via re-exports; capture/stream code imports the table-free leaves directly.

Call sites in runtime.ts, runtime.server.ts, stream-protocol.ts, hydration capture, and React/universal code switch to those imports. wire-protocol-single-owner.test.ts scans packages/octane/src and fails if any wire literal is spelled twice in source.

Behavior: adopting a presentation view when the hydration range has no view id now fails immediately (range.view === undefined) instead of building a comparison against "[b;undefined;root".

Reviewed by Cursor Bugbot for commit f043e9c. Bugbot is set up for automated code reviews on this repo. Configure here.

The @for arm markers, presentation-binding comment prefixes,
deferred-boundary attribute names, useId spelling, element-scoped
ViewTransition stylesheet, and the cross-realm Symbol.for tags were each
re-typed in two to five modules. A change on one side of a hydration
boundary could diverge from the other with no test noticing.

Give each one an owning module: hydration-markers.js for the payloads,
attributes and useId (it stays off the dom-tables.js graph so the pre-root
capture bundle can share them), dom-binding-protocol.js for the binding
marker producer next to its parser, css.js for the ViewTransition
stylesheet, and a new runtime-tags.js for the registry keys.
octane/constants re-exports the same names and values.

Payloads stay plain literals: a bundler does not constant-fold a
cross-module template literal, and a derived spelling measured roughly
twice the bundle cost on the pre-root capture entry.

Adopting a presentation view whose range carries no view id is now
rejected outright instead of compared against "[b;undefined;root".
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent refactor PR: internal restructuring, no behavior change labels Sep 18, 2026
@WebEferen
WebEferen marked this pull request as ready for review September 19, 2026 00:26
@WebEferen
WebEferen marked this pull request as draft September 19, 2026 00:32
@WebEferen
WebEferen marked this pull request as ready for review September 19, 2026 00:51
@WebEferen WebEferen closed this Sep 20, 2026
@WebEferen

Copy link
Copy Markdown
Contributor Author

Closing because #1169 carried the work I've done here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent refactor PR: internal restructuring, no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant