Skip to content

fix(hydration): append a rebuilt single root that was its host's last child - #1619

Merged
trueadm merged 8 commits into
mainfrom
fix/rebuilt-last-child-root
Oct 3, 2026
Merged

trueadm merged 8 commits into
mainfrom
fix/rebuilt-last-child-root

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Why

HydrationCapability.rebuiltAt has three possible results:

  • a node: insert before it;
  • null: append, because the replaced server node was its parent's last child;
  • undefined: the root was not rebuilt.

commitBag and createHydrateSlot called it as rebuiltAt(...) ?? block.endMarker, so null fell through to the end marker. For a full single-root slot, the end marker is the slot's anchor: the server node the template walk found, which rebuiltAt had just removed. insertBefore then threw.

Repro (server renders the first arm, client hydrates the second):

@if (server) { <section><hr /><u>{'u'}</u></section> } @else { <section><hr /><Para /></section> }

Changes

  • The hydrating root insert moves into a new HydrationCapability.insertRoot(root, block). It keeps an adopted server root where it stands; a rebuilt root goes in place of the server node it replaced, and is appended when that node was its parent's last child; any other root goes at the block's end. commitBag and createHydrateSlot now call hydration.insertRoot(...) when hydrating, and otherwise just insert at block.endMarker. This replaces rebuiltAt and its ?? block.endMarker fallback.
  • Merged with fix(hydration): remove the server tail after a component root rebuilt over its range #1614: its range-tail removal (rebuiltRange / takeRangeTail) now runs inside insertRoot. When takeRangeTail returns null, the root is appended.
  • Removed Fix hydration mismatch in lite component hosts #1593's if (block instanceof LiteBlockImpl && block.endMarker === cursor) block.endMarker = this.node; from adopt()'s mismatch path. Every slot kind now goes through the shared path.
  • Tests: added FullLast and ConstantOutputLast to tests/hydration/in-place-mismatch-full.test.ts. They run in the dev compile, the prod compile with the dev runtime, and the prod compile with the prod runtime. Each checks:
    • the rebuilt root is appended in place of the <u>, and the <hr> keeps its identity;
    • one recoverable report, plus a dev-only located warning;
    • the arm toggles off and on, then unmount leaves the container empty.

Validation

  • pnpm format:check: not run. pnpm format:files:check passes on the 4 changed files.
  • pnpm typecheck: not run. pnpm typecheck:files passes on runtime.ts, the test and its .tsrx fixture.
  • pnpm test: not run locally; left to CI.
  • Targeted tests:
    • New cases on main: 5 of the 6 fail with NotFoundError. The dev-compile constant-output case passes on main because Fix hydration mismatch in lite component hosts #1593 patched the lite path. All 6 pass with the fix.
    • Mutation check: removing Fix hydration mismatch in lite component hosts #1593's line without the commitBag fix fails 9 tests (lite-host-mismatch and the new cases). The shared path now protects the lite slot.
    • 20 related files pass, 853 tests in octane and 853 in octane-prod: lite-host-mismatch, in-place-mismatch-full, root-suspended-clone-mismatch, arm-component-hole, arm-lite-hole, rebuilt-clone-order, rebuilt-clone-suspended, in-place-fragment-hole, unframed-fragment-root, in-place-component-sibling, adopted-host-component-clone, rebuilt-clone-blocks, component-range-siblings, branch-arm-tail, adopted-component-tail, deopt-host-retry, arm-hole-first-fragment, deferred-hydration, mismatch-structural, hmr.
    • The reviewer's evidence shapes LastChildRebuild, LastChildRebuildState and TextThenLeafRebuild pass in all three modes.
  • Size: node benchmarks/bundle-size/run-minimal.mjs, production raw / gzip / brotli. Last re-check after merging main 0d91b93: root-chained-jsx 85,981 / 28,545 / 25,337 on main → 85,941 / 28,529 / 25,334 here (budget 86,016 / 28,576 / 25,344); --budgets root-static-local root-chained-jsx hooks-state prop-attributes context exits 0. Earlier, against main 62e76bf:
    • root-chained-jsx (budget enforced): 86,002 / 28,544 / 25,337 → 85,962 / 28,528 / 25,335. The first push measured 28,546 B gzip, 2 B over the 28,544 budget, which is why insertRoot exists.
    • --budgets root-static-local root-chained-jsx hooks-state prop-attributes context: all within budget.
    • hydrate-root (not enforced): 169,643 / 53,945 / 47,135 → 169,637 / 53,951 / 47,062.
    • deferred-hydration (not enforced): 233,041 / 73,828 / 63,686 → 233,011 / 73,823 / 63,748.
    • Per function (esbuild minify + gzip): commitBag −49 B raw / −16 B gz; createHydrateSlot −33 / −6; HydrationCapability +41 / +6; componentSlotImpl unchanged.
    • After the insertRoot change, re-ran in-place-mismatch-full and rebuilt-clone-suspended (octane): 28 pass. Typecheck and format clean.
    • After the merge with main 0d91b93: in-place-mismatch-full, rebuilt-root-tail and rebuilt-fragment-suspended-child (octane): 122 pass. Typecheck clean.

Risk / follow-ups

  • createHydrateSlot changes only in form. A <Hydrate> wrapper always sits inside a framed range, so its server node is never its parent's last child. Probes of a last-child <Hydrate> rebuild give the same DOM before and after the change.
  • S-1 (deferred, tracked separately). After a single-root rebuild, the slot's state.anchor still points at the removed server node. A later identity swap inside a committed Suspense boundary throws. Today that requires reassigning the live binding of an imported (cross-module) component.
    • The suggested fix, pointing the anchor at the rebuilt root's next sibling, does not cover fragment @if arms: hydration replaces the arm's server markers with fresh ones.
    • Reading the anchor from the live root at swap time fixes every shape, but costs about +24 B gz in client-only bundles.
    • Left out of this PR; tracked as its own follow-up task.
  • Neighbours fix(hydration): remove the server tail after a component root rebuilt over its range #1614 and fix(hydration): discard the server tail after a last call adopted in place #1615 edit adopt() and settleClaim near the removed line, so a merge conflict there is possible. No logic overlaps.

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 commit logic (insertRoot, commitBag, createHydrateSlot); incorrect insertion could affect mismatch recovery beyond the fixed last-child case.

Overview
Fixes hydrateRoot failing with NotFoundError and an empty container when hydration rebuilds a single-root component over a mismatched server node and that call is the last child of its host (no trailing sibling).

Root insertion during hydration is centralized in HydrationCapability.insertRoot(root, block), replacing rebuiltAt and the ?? block.endMarker pattern in commitBag and createHydrateSlot. Rebuilt roots are inserted before the following server sibling when one exists, or appended when the replaced node was the parent's last child. The previous lite-only endMarker tweak in adopt() is removed so all slot kinds share this path.

Adds FullLast / ConstantOutputLast fixtures and tests across dev/prod compile and runtime modes.

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

… child

A single-root component call that is its host element's last child, hydrated
over a different server element, threw NotFoundError and left the container
empty (any production compile; a development compile when the component has
hooks). rebuiltAt returns null to append when the replaced node was the
last child, and `?? block.endMarker` turned that into the block's end
marker, which for a full single-root slot is the server node rebuiltAt had
just removed.

rebuiltAt now takes the block and falls back to its end marker itself, so
null only ever means "append". commitBag and createHydrateSlot use it, and
#1593's lite-only endMarker rewrite in adopt() is removed: the shared path
now covers the lite slot too.

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 23:41
@trueadm
trueadm marked this pull request as ready for review October 2, 2026 23:47
…ability.insertRoot

The rebuiltAt fallback added 2 B gzip to client-only bundles (commitBag),
pushing root-chained-jsx past its committed budget. Move the hydrating
branch of the root insert into HydrationCapability.insertRoot, which keeps
an adopted server root in place and puts a rebuilt one where rebuiltAt did.
commitBag and createHydrateSlot now only insert at the block's end on the
client path.

root-chained-jsx (production): 86002/28544/25337 on main -> 85962/28528/25335.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

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

Compares 0d91b934a with d1db0cabb 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.44 3.90 −12.2% ±57% ⚪ within noise
octane-tsrx replace 8.15 7.77 −4.7% ±11% ⚪ within noise
octane-tsrx add 3.96 3.71 −6.3% ±21% ⚪ within noise
octane-tsrx update 0.37 0.39 +5.4% ±48% ⚪ within noise
octane-tsrx select 0.15 0.16 +6.7% ±90% ⚪ within noise
octane-tsrx swap 0.37 0.32 −13.5% ±68% ⚪ within noise
octane-tsrx remove 0.34 0.33 −2.9% ±48% ⚪ within noise
octane-tsrx runlots 37.32 35.67 −4.4% ±11% ⚪ within noise
octane-tsrx select_lots 0.21 0.18 −14.3% ±56% ⚪ within noise
octane-tsrx clear 37.58 38.06 +1.3% ±15% ⚪ within noise
octane-jsx run 4.04 3.88 −4.0% ±8% ⚪ within noise
octane-jsx replace 7.66 7.00 −8.6% ±13% ⚪ within noise
octane-jsx add 3.53 3.64 +3.1% ±25% ⚪ within noise
octane-jsx update 0.58 0.57 −1.7% ±40% ⚪ within noise
octane-jsx select 0.39 0.31 −20.5% ±46% ⚪ within noise
octane-jsx swap 0.71 0.58 −18.3% ±46% ⚪ within noise
octane-jsx remove 0.57 0.54 −5.3% ±41% ⚪ within noise
octane-jsx runlots 32.74 32.43 −0.9% ±20% ⚪ within noise
octane-jsx select_lots 3.02 2.76 −8.6% ±22% ⚪ within noise
octane-jsx clear 36.76 36.01 −2.0% ±21% ⚪ within noise

Application bundle size (bytes)

target metric base head Δ
octane-tsrx js_raw 126,848 126,808 −40 (−0.0%) 🟢
octane-tsrx js_gzip 41,990 41,978 −12 (−0.0%) 🟢
octane-tsrx js_brotli 37,019 37,006 −13 (−0.0%) 🟢
octane-tsrx app_gzip 2,564 2,561 −3 (−0.1%) 🟢
octane-tsrx fw_raw 119,336 119,296 −40 (−0.0%) 🟢
octane-tsrx fw_gzip 39,426 39,417 −9 (−0.0%) 🟢
octane-tsrx fw_brotli 34,791 34,778 −13 (−0.0%) 🟢
octane-tsrx todo_js_raw 142,235 142,195 −40 (−0.0%) 🟢
octane-tsrx todo_js_gzip 47,001 46,991 −10 (−0.0%) 🟢
octane-tsrx todo_js_brotli 41,211 41,187 −24 (−0.1%) 🟢
octane-tsrx todo_app_gzip 2,476 2,475 −1 (−0.0%) 🟢
octane-tsrx todo_app_brotli 2,120 2,116 −4 (−0.2%) 🟢
octane-tsrx todo_fw_raw 137,125 137,085 −40 (−0.0%) 🟢
octane-tsrx todo_fw_gzip 44,525 44,516 −9 (−0.0%) 🟢
octane-tsrx todo_fw_brotli 39,091 39,071 −20 (−0.1%) 🟢
octane-tsrx chat_js_raw 141,444 141,404 −40 (−0.0%) 🟢
octane-tsrx chat_js_gzip 46,947 46,938 −9 (−0.0%) 🟢
octane-tsrx chat_js_brotli 41,283 41,265 −18 (−0.0%) 🟢
octane-tsrx chat_app_gzip 2,979 2,978 −1 (−0.0%) 🟢
octane-tsrx chat_app_brotli 2,639 2,636 −3 (−0.1%) 🟢
octane-tsrx chat_fw_raw 134,791 134,751 −40 (−0.0%) 🟢
octane-tsrx chat_fw_gzip 43,968 43,960 −8 (−0.0%) 🟢
octane-tsrx chat_fw_brotli 38,644 38,629 −15 (−0.0%) 🟢
octane-tsrx weather_js_raw 178,410 178,370 −40 (−0.0%) 🟢
octane-tsrx weather_js_gzip 58,357 58,349 −8 (−0.0%) 🟢
octane-tsrx weather_js_brotli 50,832 50,823 −9 (−0.0%) 🟢
octane-tsrx weather_app_gzip 9,046 9,045 −1 (−0.0%) 🟢
octane-tsrx weather_app_brotli 7,578 7,581 +3 (+0.0%) 🔴
octane-tsrx weather_fw_raw 151,675 151,635 −40 (−0.0%) 🟢
octane-tsrx weather_fw_gzip 49,311 49,304 −7 (−0.0%) 🟢
octane-tsrx weather_fw_brotli 43,254 43,242 −12 (−0.0%) 🟢
octane-jsx js_raw 226,723 226,683 −40 (−0.0%) 🟢
octane-jsx js_gzip 72,373 72,364 −9 (−0.0%) 🟢
octane-jsx js_brotli 62,608 62,571 −37 (−0.1%) 🟢
octane-jsx app_brotli 2,495 2,497 +2 (+0.1%) 🔴
octane-jsx fw_raw 218,264 218,224 −40 (−0.0%) 🟢
octane-jsx fw_gzip 69,438 69,429 −9 (−0.0%) 🟢
octane-jsx fw_brotli 60,113 60,074 −39 (−0.1%) 🟢

7 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
cli-spa-starter raw 116,540 116,500 −40 (−0.0%) 🟢
cli-spa-starter gzip 39,367 39,360 −7 (−0.0%) 🟢
cli-spa-starter brotli 34,355 34,350 −5 (−0.0%) 🟢
root-static-specialized raw 77,066 77,026 −40 (−0.1%) 🟢
root-static-specialized gzip 25,582 25,574 −8 (−0.0%) 🟢
root-static-specialized brotli 22,826 22,778 −48 (−0.2%) 🟢
root-chained-jsx raw 85,981 85,941 −40 (−0.0%) 🟢
root-chained-jsx gzip 28,545 28,529 −16 (−0.1%) 🟢
root-chained-jsx brotli 25,337 25,334 −3 (−0.0%) 🟢
root-static raw 203,051 203,011 −40 (−0.0%) 🟢
root-static gzip 64,566 64,555 −11 (−0.0%) 🟢
root-static brotli 55,912 55,975 +63 (+0.1%) 🔴
root-static-local raw 77,069 77,029 −40 (−0.1%) 🟢
root-static-local gzip 25,569 25,560 −9 (−0.0%) 🟢
root-static-local brotli 22,785 22,776 −9 (−0.0%) 🟢
hooks-state raw 89,230 89,190 −40 (−0.0%) 🟢
hooks-state gzip 29,641 29,633 −8 (−0.0%) 🟢
hooks-state brotli 26,413 26,389 −24 (−0.1%) 🟢
prop-attributes raw 97,433 97,393 −40 (−0.0%) 🟢
prop-attributes gzip 32,397 32,385 −12 (−0.0%) 🟢
prop-attributes brotli 28,730 28,676 −54 (−0.2%) 🟢
context raw 110,838 110,798 −40 (−0.0%) 🟢
context gzip 36,709 36,702 −7 (−0.0%) 🟢
context brotli 32,487 32,499 +12 (+0.0%) 🔴
hydrate-root raw 172,212 172,211 −1 (−0.0%) 🟢
hydrate-root gzip 54,603 54,604 +1 (+0.0%) 🔴
hydrate-root brotli 47,653 47,637 −16 (−0.0%) 🟢
deferred-hydration raw 235,621 235,596 −25 (−0.0%) 🟢
deferred-hydration gzip 74,502 74,505 +3 (+0.0%) 🔴
deferred-hydration brotli 64,283 64,266 −17 (−0.0%) 🟢
suspense-transition raw 166,782 166,742 −40 (−0.0%) 🟢
suspense-transition gzip 53,587 53,582 −5 (−0.0%) 🟢
suspense-transition brotli 46,860 46,878 +18 (+0.0%) 🔴
component-owned-effects raw 81,678 81,638 −40 (−0.0%) 🟢
component-owned-effects gzip 26,939 26,931 −8 (−0.0%) 🟢
component-owned-effects brotli 23,990 23,981 −9 (−0.0%) 🟢
binding-hooks raw 87,743 87,703 −40 (−0.0%) 🟢
binding-hooks gzip 29,343 29,336 −7 (−0.0%) 🟢
binding-hooks brotli 26,141 26,130 −11 (−0.0%) 🟢
binding-mantine-hooks-vite raw 98,068 98,028 −40 (−0.0%) 🟢
binding-mantine-hooks-vite gzip 32,612 32,600 −12 (−0.0%) 🟢
binding-mantine-hooks-vite brotli 28,939 28,917 −22 (−0.1%) 🟢
binding-mantine-hooks-esbuild raw 97,662 97,622 −40 (−0.0%) 🟢
binding-mantine-hooks-esbuild gzip 32,792 32,786 −6 (−0.0%) 🟢
binding-mantine-hooks-esbuild brotli 29,084 29,112 +28 (+0.1%) 🔴
binding-apollo-client-vite raw 358,297 358,257 −40 (−0.0%) 🟢
binding-apollo-client-vite gzip 112,261 112,250 −11 (−0.0%) 🟢
binding-apollo-client-vite brotli 97,937 97,959 +22 (+0.0%) 🔴
binding-apollo-client-esbuild raw 361,246 361,206 −40 (−0.0%) 🟢
binding-apollo-client-esbuild gzip 114,470 114,458 −12 (−0.0%) 🟢
binding-apollo-client-esbuild brotli 99,711 99,680 −31 (−0.0%) 🟢
binding-usehooks-ts-vite raw 98,701 98,661 −40 (−0.0%) 🟢
binding-usehooks-ts-vite gzip 32,770 32,764 −6 (−0.0%) 🟢
binding-usehooks-ts-vite brotli 29,133 29,090 −43 (−0.1%) 🟢
binding-usehooks-ts-esbuild raw 98,284 98,244 −40 (−0.0%) 🟢
binding-usehooks-ts-esbuild gzip 32,939 32,930 −9 (−0.0%) 🟢
binding-usehooks-ts-esbuild brotli 29,236 29,231 −5 (−0.0%) 🟢

48 other measured values are unchanged.

trueadm and others added 6 commits October 3, 2026 01:09
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