Skip to content

fix(hydration): replace exactly the server node at a component hole in another arm's adopted nodes - #1603

Merged
trueadm merged 6 commits into
mainfrom
claude/musing-thompson-d5d423
Oct 2, 2026
Merged

trueadm merged 6 commits into
mainfrom
claude/musing-thompson-d5d423

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

When the server rendered another @if/@switch arm whose static roots match the client arm's, the client adopts the server arm's nodes through its template. The server node at a component call's <!> hole is then an element of the other arm, not the component's <!--[--> range. Main already renders a single-root call in place of that node (#1602). This PR fixes the calls that cannot render in place of a single server node, in both compiles:

  • Keyed or dynamic calls, and components that render several roots, a fragment, or nothing. These go through the missing-range (unframed) path, which took the lagging cursor as the stale node and stopped its discard at the anchor. At a hole before the shared root, the anchor is the stale node, so the other arm's element stayed on screen. At a hole after an adopted root, the cursor still rests on that root, so the discard deleted it (new KeyedLast case).
    • HydrationCapability.renderUnframed: when a claim's anchor is an element or text node and not the claiming block's end marker, that anchor is the server node at the call's position. The discard starts at the anchor, whatever node the cursor rests on, and stops right after it, because the template claims the nodes that follow. Doing this when the claim settles, rather than inline in componentSlotImpl, keeps it out of bundles that never hydrate. It composes with fix(hydration): adopt an unframed fragment component and continue after its last root #1597: the in-place adoption that renderUnframed now attempts also uses that node, so a matching body adopts it, and a mismatching one discards only it.
  • Development-compile (lite) calls. renderLiteInPlace rendered such a call in place with LiteBlockImpl.endMarker set to the server element, which only works for a single-root body:
    • With two roots, the first rebuilt over the end marker and the second rebuilt over the adopted <i> after the hole.
    • A fragment or an empty body left the stale element on screen.
    • A positional lite call whose callee lacks the compiler's $$singleRoot stamp now mounts unframed (mountUnframedLite, with that node as the claim's anchor), matching componentSlotImpl. The branch is in the cold helper, so ordinary lite dispatch is unchanged.

Earlier revisions of this PR carried a slotCursor/positional design and an adopt() guard change. Main's #1593, #1584 and #1602 superseded both, and the merge now takes main's versions. The PR's diff against main is a small runtime.ts change, two test files with fixtures, and one changeset.

Validation

  • pnpm format:check (not run; pnpm format:files:check is clean on the changed files)
  • pnpm typecheck (not run; pnpm typecheck:files is clean on runtime.ts, both tests and both .tsrx fixtures)
  • pnpm test (not run in full)
  • targeted tests:
    • arm-component-hole.test.ts and arm-lite-hole.test.ts: dev/prod compile × dev/prod runtime (vi.stubEnv('NODE_ENV', 'production')).
    • On main's runtime alone, 20 of their tests fail: Keyed, KeyedLast and Dynamic, plus PairArm, FragArm and EmptyArm, in every mode. The single-root, in-host and control cases pass there.
    • Each piece was broken on purpose, and a test fails every time:
    • After the final merge, all of packages/octane/tests/hydration/ passes in octane and octane-prod: 280 file runs, 13,364 tests.

Risk / follow-ups

  • An unstamped single-root lite callee at a positional hole now rebuilds (recoverable error 55) instead of adopting a matching server node. Prod already does the same for calls it cannot prove single-root.

  • The renderUnframed change applies to every anchored claim at an element or text node, not only in @if arms. The full hydration directory covers that in both projects.

  • No test covers two of its guards:

  • Bundle (run-minimal.mjs, against main at 3ec43da):

    Scenario raw gzip brotli
    root-static 0 0 0
    context 0 0 0
    hydrate-root +81 +26 +45
    deferred-hydration +81 +37 +7

    The first version of this fix added +65 raw to every component-slot bundle, as the bot's report showed. hydrate-root was already over its committed budget on main.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


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
Touches core hydration DOM adoption/discard logic in runtime.ts; behavior changes for all anchored unframed claims at element/text nodes, though the hydration suite is expanded to cover it.

Overview
Fixes client hydration when an @if/@switch arm reuses the server’s other arm’s static DOM but places a component call in a template hole where the server left that arm’s own element (not a <!--[--> component range).

HydrationCapability.renderUnframed now treats a claim anchored on an element/text node (the hole’s walk result, not the block end marker) as positional: discard starts at that node regardless of cursor position, so keyed/dynamic/multi-root/empty calls replace only the mismatched server node and do not delete adopted siblings before/after the hole.

renderLiteInPlace routes the same positional-hole case for dev-compile lite calls that are not stamped $$singleRoot through mountUnframedLite instead of in-place lite rendering, which previously corrupted multi-root, fragment, or empty bodies and could rebuild over adopted nodes.

Adds arm-component-hole and arm-lite-hole fixtures/tests (dev/prod compile × runtime), asserting DOM cleanup, single onRecoverableError reports, and stable updates when switching arms. Patch changeset for octane.

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

trueadm and others added 2 commits October 2, 2026 20:32
…n another arm's adopted nodes

When the server rendered another @if/@switch arm whose static roots match
the client arm's, the client adopts the server arm's nodes through its
template, and the server node at a component call's <!> hole is an element
of the other arm rather than the component's range.

- HydrationCapability.slotCursor: an element or text anchor is the server
  node at the call's position; the cursor moves to it and the call's first
  render remembers it (positional). renderInPlace drops the rebuilt tail for
  it, and renderUnframed discards only that node.
- Lite (development compile) calls: a positional call whose callee is not
  stamped $$singleRoot renders unframed in that node's place
  (mountUnframedLite with an anchor) instead of in place, where a body of
  several roots, a fragment, or nothing lost the adopted node after the hole
  or left the stale element on screen.
- adopt()'s single-root mismatch guard compares the cursor's parent with
  CURRENT_SCOPE.block.parentNode, the parent commitBag inserts into, which
  for a lite component is its host rather than CURRENT_BLOCK's parent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n-d5d423

# Conflicts:
#	packages/octane/src/runtime.ts
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 2, 2026
@github-actions
github-actions Bot marked this pull request as draft October 2, 2026 19:35
…n-d5d423

# Conflicts:
#	packages/octane/src/runtime.ts
@trueadm
trueadm marked this pull request as ready for review October 2, 2026 20:07
…n-d5d423

# Conflicts:
#	packages/octane/src/runtime.ts
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-reachability: 6 value(s) increased

Compares 6bb20ac94 with bc58f0a65 on the same runner. Timing verdicts require a change beyond the combined margin of error (at least 5%) and one 0.1ms timer tick, with the fastest sample also moving beyond that margin. Workflow run

js-framework: 1k/10k row operations (ms, lower is better)

All 20 timed operations (20 within noise)
target operation base head Δ noise
octane-tsrx run 3.58 4.62 +29.1% ±41% ⚪ within noise
octane-tsrx replace 8.33 8.78 +5.4% ±20% ⚪ within noise
octane-tsrx add 4.04 3.67 −9.2% ±33% ⚪ within noise
octane-tsrx update 0.53 0.41 −22.6% ±56% ⚪ within noise
octane-tsrx select 0.19 0.21 +10.5% ±63% ⚪ within noise
octane-tsrx swap 0.38 0.37 −2.6% ±33% ⚪ within noise
octane-tsrx remove 0.45 0.45 +0.0% ±49% ⚪ within noise
octane-tsrx runlots 31.27 33.77 +8.0% ±17% ⚪ within noise
octane-tsrx select_lots 0.23 0.28 +21.7% ±47% ⚪ within noise
octane-tsrx clear 46.56 45.36 −2.6% ±14% ⚪ within noise
octane-jsx run 5.32 4.64 −12.8% ±105% ⚪ within noise
octane-jsx replace 9.70 8.71 −10.2% ±25% ⚪ within noise
octane-jsx add 3.74 3.53 −5.6% ±26% ⚪ within noise
octane-jsx update 0.70 0.58 −17.1% ±40% ⚪ within noise
octane-jsx select 0.45 0.37 −17.8% ±41% ⚪ within noise
octane-jsx swap 0.72 0.66 −8.3% ±21% ⚪ within noise
octane-jsx remove 0.70 0.66 −5.7% ±34% ⚪ within noise
octane-jsx runlots 30.82 30.10 −2.3% ±11% ⚪ within noise
octane-jsx select_lots 2.55 2.75 +7.8% ±33% ⚪ within noise
octane-jsx clear 51.20 47.54 −7.1% ±30% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

target metric base head Δ
hydrate-root raw 169,255 169,328 +73 (+0.0%) 🔴
hydrate-root gzip 53,845 53,864 +19 (+0.0%) 🔴
hydrate-root brotli 46,972 47,026 +54 (+0.1%) 🔴
deferred-hydration raw 232,397 232,470 +73 (+0.0%) 🔴
deferred-hydration gzip 73,659 73,688 +29 (+0.0%) 🔴
deferred-hydration brotli 63,469 63,555 +86 (+0.1%) 🔴

99 other measured values are unchanged.

trueadm and others added 2 commits October 2, 2026 21:31
The benchmark report showed +65 raw bytes in every bundle with a component
slot, including ones that never hydrate (root-static, context, js-framework),
because componentSlotImpl resolved the positional anchor inline. Resolve it
when renderUnframed settles the claim instead: an element or text anchor
that is not the claiming block's end marker is the server node at the call's
position, so the discard starts there and stops after it. Non-hydrating
bundles are unchanged from main; hydrate-root and deferred-hydration grow by
81 raw bytes. The lite helper passes the node as the claim's anchor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n-d5d423

# Conflicts:
#	packages/octane/src/runtime.ts
@trueadm
trueadm merged commit 6ea582e into main Oct 2, 2026
31 checks passed
@trueadm
trueadm deleted the claude/musing-thompson-d5d423 branch October 2, 2026 21:29
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 fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant