Skip to content

fix(hydration): rebuild a hole-first fragment over another arm's nodes - #1599

Merged
trueadm merged 2 commits into
mainfrom
claude/ecstatic-williams-2c0985
Oct 2, 2026
Merged

trueadm merged 2 commits into
mainfrom
claude/ecstatic-williams-2c0985

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Hydration now rebuilds a nested fragment that starts with a hole when the server rendered something else there, such as the other @if arm.

A leading hole (a component call, text hole, renderable hole, nested block, or boundary) matched any server node in fragmentRootMatches/lazyFragmentRootMatches. Repro: an @else arm <><Under v="z" /><i>x</i></> hydrated over the server's then-arm <b>…</b><em>e</em>. Hydration adopted the server nodes as the fragment's roots. The static <i>x</i> was never built, and Under's slot took the server <b> as its anchor. After Under rebuilt <u> in place of <b>, the rebuilt-tail sweep removed <em>, so the section read <u>z</u>. Before #1561 the same shape threw NotFoundError synchronously from commitBag, which inserted before the discarded <b>.

  • fragmentRootMatches (parsed templates) and lazyFragmentRootMatches (production, read from the template source) now skip the leading <!> holes. They compare the first static element root with the server node the compiled walk reaches from the cursor, HydrationCapability.sibling(cursor, holes). The lazy path only scans the source, so production hydration still never parses a template on the happy path.
  • On a mismatch, adopt() calls rebuildFragment, which reports once, as it already did for fragments that start with a static root. The development warning names the root that differed: "the client expected a fragment with <i> after its leading holes but the server rendered <em>".
  • The check is skipped under a HYDRATION_RANGE_BOUNDARY passthrough root (the new HydrationCapability.passthroughRoot). There the owner adopts the first server range in the container, so its content can sit one range level off (range-boundary.test.ts, "keeps a multi-root owner…", fails without the gate).

Why rebuild instead of keeping the in-place slot's anchor valid: a valid anchor cannot bring back the fragment's missing static roots. With the rebuild, a component in the hole also mounts inside the fresh clone, so its anchor is a client node, and a later key change remounts it normally.

Validation

  • pnpm format:check (not run; pnpm format:files:check on the changed files passes)
  • pnpm typecheck (not run; pnpm typecheck:files on the changed files, with the .tsrx fixture passed explicitly, passes)
  • pnpm test (not run, because of local machine load)
  • targeted tests:
    • New packages/octane/tests/hydration/arm-hole-first-fragment.test.ts plus a fixture. Every case runs under both compiles and both runtimes (production via vi.stubEnv), 52 tests.
      • Rebuild cases (12): the arm differs at the static root; the arm ends before it; a keyed hole component whose key changes after the rebuild. Each asserts the final DOM, removal of the server nodes, one recoverable error, and the dev warning. All 12 fail on main.
      • Matching controls (40): component, text, renderable, nested-block, @try, and <Suspense> holes first. Each must adopt the server nodes with no report.
      • Mutations: each half of the fix (parsed path, lazy path), broken on its own, fails 6 cases. Skipping the step past the holes fails all 32 controls.
    • packages/octane/tests/hydration passes in octane and octane-prod after merging current main (131 files each). Before the merge, it passed together with the 124 other packages/octane/tests files that call hydrateRoot (253 files per project).
    • Bundle size (benchmarks/bundle-size/run-minimal.mjs, vs main):
      Scenario Raw Gzip Brotli
      hydrate-root +458 +164 +174
      deferred-hydration +458 +175 +111
      root-static, hooks-state (client-only) 0 0 0
    • Runtime cost applies only to hydrating a fragment that starts with a hole: a few sibling() steps whose close-marker lookups are cached and would be done by the adopting slot anyway. No hydration benchmark was run.
    • Not run locally: website/tests/ssr-hydration.e2e.test.ts.

Risk / follow-ups

  • If the sibling claimRoots short-range fix lands, it introduces a field with the same name, passthroughRoot. Whichever PR lands second should merge them into one.

  • Out of scope, and also failing on main: when the server's other arm matches the fragment's static roots, the fragment is correctly adopted, but a component hole whose server node is a bare element still goes wrong:

    • An in-place rebuild's tail sweep removes the adopted static root.
    • A keyed or dynamic-identity call keeps the stale server node, because renderUnframed stops at its own anchor.
    • A hole after a static root renders against the lagging cursor.

    These are being fixed separately.

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 adoption in runtime.ts for nested fragments and @if arms; incorrect logic could cause extra rebuilds or regress range-boundary hydration, though mismatches fail safe via client rebuild.

Overview
Fixes hydration when a nested fragment’s first logical root is a dynamic hole (component, text, block, etc.) but the server HTML came from a different branch (e.g. the other @if arm). Previously those holes matched any server node, so the client adopted the wrong subtree, skipped static roots, and mis-anchored components in the hole.

fragmentRootMatches and lazyFragmentRootMatches now walk past leading <!> holes and compare the first static element to the server node at hydration.sibling(cursor, holeCount). On mismatch, hydration rebuilds the fragment (one recoverable error #51), same as for a static-first fragment. Dev warnings call out the expected static root after leading holes vs what the server had.

HydrationCapability.passthroughRoot gates this check for HYDRATION_RANGE_BOUNDARY passthrough roots so range-boundary adoption behavior is unchanged.

Adds a changeset, fixture, and broad hydrateRoot tests (mismatch rebuilds + matching adoption controls across dev/prod compile and runtime).

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

trueadm and others added 2 commits October 2, 2026 19:57
A nested fragment whose first root is a hole (a component call, text hole,
renderable hole, nested block or boundary) matched any server node, so when
the server rendered another @if arm there, hydration adopted that arm's nodes
as the fragment's roots. Its static roots were never built, a component in the
hole took a server node as its anchor, and the rebuilt root's tail sweep then
removed the server node that stood in for them.

fragmentRootMatches and lazyFragmentRootMatches now compare the first static
element root after the leading holes with the server node the compiled walk
reaches from the cursor (HydrationCapability.sibling); the lazy path scans the
template source, so production hydration still never parses on the happy
path. A mismatch rebuilds the fragment on the client with one report, as for a
fragment that starts with a static root, and the development warning names
that root. A HYDRATION_RANGE_BOUNDARY passthrough root, whose owner adopts one
range level off, keeps the previous behavior (passthroughRoot).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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:03
@trueadm
trueadm marked this pull request as ready for review October 2, 2026 19:03
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Benchmark report

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

Compares 53db43656 with 55c45ec1d 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 4.51 4.91 +8.9% ±95% ⚪ within noise
octane-tsrx replace 8.20 10.21 +24.5% ±47% ⚪ within noise
octane-tsrx add 4.08 5.30 +29.9% ±69% ⚪ within noise
octane-tsrx update 0.46 0.51 +10.9% ±37% ⚪ within noise
octane-tsrx select 0.21 0.22 +4.8% ±64% ⚪ within noise
octane-tsrx swap 0.39 0.43 +10.3% ±60% ⚪ within noise
octane-tsrx remove 0.35 0.38 +8.6% ±59% ⚪ within noise
octane-tsrx runlots 36.26 39.78 +9.7% ±21% ⚪ within noise
octane-tsrx select_lots 0.19 0.22 +15.8% ±64% ⚪ within noise
octane-tsrx clear 36.10 38.45 +6.5% ±26% ⚪ within noise
octane-jsx run 5.09 6.59 +29.5% ±109% ⚪ within noise
octane-jsx replace 9.06 8.76 −3.3% ±57% ⚪ within noise
octane-jsx add 3.78 4.45 +17.7% ±79% ⚪ within noise
octane-jsx update 0.74 0.80 +8.1% ±72% ⚪ within noise
octane-jsx select 0.38 0.55 +44.7% ±64% ⚪ within noise
octane-jsx swap 0.85 0.78 −8.2% ±76% ⚪ within noise
octane-jsx remove 0.54 0.61 +13.0% ±28% ⚪ within noise
octane-jsx runlots 34.08 34.14 +0.2% ±18% ⚪ within noise
octane-jsx select_lots 2.85 3.40 +19.3% ±51% ⚪ within noise
octane-jsx clear 36.70 38.30 +4.4% ±29% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

target metric base head Δ
hydrate-root raw 165,126 165,584 +458 (+0.3%) 🔴
hydrate-root gzip 52,726 52,881 +155 (+0.3%) 🔴
hydrate-root brotli 45,882 46,042 +160 (+0.3%) 🔴
deferred-hydration raw 227,932 228,390 +458 (+0.2%) 🔴
deferred-hydration gzip 72,362 72,527 +165 (+0.2%) 🔴
deferred-hydration brotli 62,452 62,528 +76 (+0.1%) 🔴
binding-base-ui-esbuild gzip 67,772 67,771 −1 (−0.0%) 🟢
binding-base-ui-esbuild brotli 58,752 58,780 +28 (+0.0%) 🔴
binding-aria-esbuild gzip 3,363 3,361 −2 (−0.1%) 🟢
binding-aria-esbuild brotli 3,013 3,012 −1 (−0.0%) 🟢
binding-radix-esbuild gzip 68,374 68,373 −1 (−0.0%) 🟢
binding-radix-esbuild brotli 59,318 59,272 −46 (−0.1%) 🟢

93 other measured values are unchanged.

@trueadm
trueadm merged commit 6959fa0 into main Oct 2, 2026
34 of 60 checks passed
@trueadm
trueadm deleted the claude/ecstatic-williams-2c0985 branch October 2, 2026 19:22
trueadm added a commit that referenced this pull request Oct 2, 2026
Resolve runtime.ts against #1585 and #1599.

#1585 reports a mismatched range once across sibling components: the
discard runs before the report, which is skipped when the discard stopped
at a range end an earlier recovery already reported (firstAtRangeEnd).
renderUnframed keeps that order with this branch's discardInPlace, which
returns where it stopped. placeRebuilt's in-place discard also records a
range end it reaches (reachedRangeEnd), as its other discard does.

#1599 names the static root after a fragment's leading holes in the
rebuilt-fragment warning. That warning now lives in placeRebuilt, so it
takes the template from the rebuilt record and keeps the lite call's site.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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