Skip to content

fix(hydration): mount blocks inside a rebuilt clone as client content - #1552

Merged
trueadm merged 3 commits into
mainfrom
fix/rebuilt-clone-blocks
Oct 2, 2026
Merged

trueadm merged 3 commits into
mainfrom
fix/rebuilt-clone-blocks

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

When a hydrating template's root does not match the server node, adopt() reports the mismatch and returns a fresh client clone. The hydration cursor still points at the server output that follows the mismatch. The @if, @switch, @for, @try, <Activity>, and <ErrorBoundary> blocks inside that fresh clone found no server range of their own, mounted on the client-mount path, and kept hydrating against that stale cursor. With this change they mount as client content, the same way component and child slots inside a rebuilt clone already do.

Why

Before this change, a block inside a rebuilt clone (dev and prod unless noted):

  • logged a second, false structural diagnostic for the same recovery (dev). For example: the client expected <s> but the server rendered the end of the parent block, or for @for, the client expected another list item but the server rendered nothing.
  • threw NotFoundError for an @for with an @empty arm, or when a server sibling followed the rebuilt element.
  • adopted a following server element whose tag matched an arm's template into the rebuilt element (wrong content). An @for could claim the following server list range, rendering its items outside the rebuilt element.
  • made the rebuilt element disappear from the final DOM when a server sibling followed it.
  • had a use() inside the arm read the following server sibling's positional seed, showing that sibling's value permanently.

onRecoverableError still fired once, because recoverable reports coalesce per root per microtask.

Changes

  • HydrationCapability.inFreshRange(anchor, parent): whether a slot sits inside a client-built replacement.
  • ifBlock, switchBlock, forBlock, tryBlock, errorBlock, and activityBlock ask only in their existing "no server range found" branch. SSR always frames these blocks while hydrating, so that branch is off the adoption path. If the slot is inside a rebuilt clone, the call re-enters under hydration.suspend. The re-entry through the returned-output preamble is safe because the reset handler clears slots[0].
  • forBlock's sole-hole branch now requires the cursor to sit in domParent before adopting its range. resolveOpen already applies this rule for the other blocks. It is the one change on the adoption path: one parentNode read per sole-hole @for mount while hydrating.
  • Regression suite tests/hydration/rebuilt-clone-blocks.test.ts, run with dev and prod compiles. It covers each block, the crash, both foreign-claim cases, adopted-sibling identity, and the seed case. Controls confirm a genuine @if or @for mismatch inside an adopted element still reports.

Validation

  • pnpm format:check (not run repo-wide; pnpm format:files:check on the changed files passes)
  • pnpm typecheck (not run repo-wide; pnpm typecheck:files on the changed files, with the .tsrx fixture passed explicitly, passes)
  • pnpm test (not run; see the targeted runs below)
  • targeted tests:
    • Before merging main: vitest run --project octane --project octane-prod passed 35,858/35,859. The one failure was a 5 s timeout in behavior-root.test.ts at load average ~40; that file passed 414/414 in two isolated reruns.
    • After merging main (load average ~65): the full suite exceeded the 10-minute local run limit, so I ran tests/hydration/ in both projects instead. 11,452/11,517 passed. All 65 failures were 5 s timeouts or knock-on errors from them ("receiver already installed", "wrong authority"), spread over 18 files. Rerunning those 18 files with --maxWorkers=4 left 14 failures in 2 files (signal-instance-identity, text-sibling-hydrate), and both files then passed 117/117 with --testTimeout=60000.
    • rebuilt-clone-blocks.test.ts: 26/26. Against the unpatched runtime, 16 fail (all 10 new dev cases; in prod, the @empty crash, both foreign-claim cases, adopted-sibling identity, and the seed case). The 4 controls pass either way.
    • pnpm sync leaves the tree clean.
  • Bundle size: the PR bench reports +141 to +309 B raw (+50 to +85 B gzip, about 0.1–0.2%) on every application bundle, including client-only ones, because ifBlock, forBlock, and tryBlock always ship and each gains a fresh-host re-entry. The reachability bundles grow by +17 to +41 B gzip (hydrate-root, deferred-hydration, suspense-transition, cli-spa-starter). The minimal signal-free budgets that CI enforces are byte-identical (benchmarks/bundle-size/run-minimal.mjs --budgets ... locally). No wall-clock benchmark: the only adoption-path change is the single parentNode read above, which is far below benchmark noise, and the bench's 20 js-framework timings are all within noise.

Risk / follow-ups

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 for all major control-flow blocks; behavior changes only when slots sit inside client-rebuilt subtrees, but mistakes could affect DOM correctness or mismatch diagnostics.

Overview
When hydration rebuilds a subtree after a structural mismatch, the cursor can still sit on server siblings that follow the replacement. Control-flow blocks inside that fresh clone (@if, @switch, @for, @try, <Activity>, <ErrorBoundary>) used to keep adopting/hydrating from that stale cursor when they had no server range of their own.

This PR adds HydrationCapability.inFreshRange and, in each block’s “no server range” path, re-enters via hydration.suspend so those slots mount as client content (matching component/child slots in rebuilt clones). @for also requires the hydration cursor to live under domParent before sole-hole range adoption, avoiding cross-range claims.

Regression tests cover false second mismatches, foreign server nodes/list ranges, sibling adoption, use() seeds, and unchanged reporting for real mismatches inside adopted elements.

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

trueadm and others added 2 commits October 2, 2026 08:44
When a template's root does not match the server node, hydration reports
the mismatch and clones the template fresh on the client. The cursor
still points at the server output that follows the mismatch, but the
@if, @switch, @for, @Try, Activity, and ErrorBoundary blocks inside the
fresh clone kept hydrating against it. Development builds logged a
second, false structural mismatch for the same recovery; an @for with an
@empty arm threw; and a block could take a following server element,
list range, or use() seed as its own, so the rebuilt element showed the
wrong content or went missing.

Each of those blocks now checks, only once it has found no server range,
whether it sits inside a client-built replacement, and if so mounts with
hydration suspended, as component and child slots already do. A sole-hole
@for also requires the cursor to sit in its own parent before adopting
it, the rule resolveOpen applies to the other blocks. A block mismatch
inside an adopted server element still reports.

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
@trueadm
trueadm marked this pull request as ready for review October 2, 2026 08:36
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🟡 js-framework: 1 timed operation(s) possibly slower
  • 🔴 bundle-size: 26 value(s) increased
  • 🔴 bundle-reachability: 15 value(s) increased

Compares 5f354d23e with e63cb2d51 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)

target operation base head Δ noise
octane-jsx replace 9.51 7.69 −19.1% ±14% 🟢 faster
octane-jsx remove 0.57 0.83 +45.6% ±42% 🟡 slower
All 20 timed operations (18 within noise)
target operation base head Δ noise
octane-tsrx run 3.48 3.72 +6.9% ±29% ⚪ within noise
octane-tsrx replace 8.32 8.09 −2.8% ±40% ⚪ within noise
octane-tsrx add 3.57 3.77 +5.6% ±32% ⚪ within noise
octane-tsrx update 0.45 0.44 −2.2% ±71% ⚪ within noise
octane-tsrx select 0.22 0.20 −9.1% ±62% ⚪ within noise
octane-tsrx swap 0.38 0.45 +18.4% ±63% ⚪ within noise
octane-tsrx remove 0.40 0.41 +2.5% ±42% ⚪ within noise
octane-tsrx runlots 34.53 34.06 −1.4% ±31% ⚪ within noise
octane-tsrx select_lots 0.28 0.29 +3.6% ±41% ⚪ within noise
octane-tsrx clear 46.90 48.46 +3.3% ±20% ⚪ within noise
octane-jsx run 3.76 4.28 +13.8% ±37% ⚪ within noise
octane-jsx replace 9.51 7.69 −19.1% ±14% 🟢 faster
octane-jsx add 4.13 3.40 −17.7% ±32% ⚪ within noise
octane-jsx update 0.81 0.92 +13.6% ±29% ⚪ within noise
octane-jsx select 0.53 0.75 +41.5% ±45% ⚪ within noise
octane-jsx swap 0.78 1.00 +28.2% ±45% ⚪ within noise
octane-jsx remove 0.57 0.83 +45.6% ±42% 🟡 slower
octane-jsx runlots 31.24 30.21 −3.3% ±17% ⚪ within noise
octane-jsx select_lots 2.77 3.09 +11.6% ±47% ⚪ within noise
octane-jsx clear 50.32 48.74 −3.1% ±15% ⚪ within noise

Application bundle size (bytes)

target metric base head Δ
octane-tsrx js_raw 125,830 125,955 +125 (+0.1%) 🔴
octane-tsrx js_gzip 41,583 41,627 +44 (+0.1%) 🔴
octane-tsrx js_brotli 36,652 36,666 +14 (+0.0%) 🔴
octane-tsrx app_gzip 2,563 2,562 −1 (−0.0%) 🟢
octane-tsrx fw_raw 118,318 118,443 +125 (+0.1%) 🔴
octane-tsrx fw_gzip 39,020 39,065 +45 (+0.1%) 🔴
octane-tsrx fw_brotli 34,428 34,442 +14 (+0.0%) 🔴
octane-tsrx todo_js_raw 140,074 140,284 +210 (+0.1%) 🔴
octane-tsrx todo_js_gzip 46,202 46,258 +56 (+0.1%) 🔴
octane-tsrx todo_js_brotli 40,507 40,614 +107 (+0.3%) 🔴
octane-tsrx todo_app_gzip 2,475 2,476 +1 (+0.0%) 🔴
octane-tsrx todo_app_brotli 2,120 2,118 −2 (−0.1%) 🟢
octane-tsrx todo_fw_raw 134,964 135,174 +210 (+0.2%) 🔴
octane-tsrx todo_fw_gzip 43,727 43,782 +55 (+0.1%) 🔴
octane-tsrx todo_fw_brotli 38,387 38,496 +109 (+0.3%) 🔴
octane-tsrx chat_js_raw 139,319 139,529 +210 (+0.2%) 🔴
octane-tsrx chat_js_gzip 46,170 46,215 +45 (+0.1%) 🔴
octane-tsrx chat_js_brotli 40,574 40,600 +26 (+0.1%) 🔴
octane-tsrx chat_app_gzip 2,979 2,978 −1 (−0.0%) 🟢
octane-tsrx chat_app_brotli 2,636 2,638 +2 (+0.1%) 🔴
octane-tsrx chat_fw_raw 132,666 132,876 +210 (+0.2%) 🔴
octane-tsrx chat_fw_gzip 43,191 43,237 +46 (+0.1%) 🔴
octane-tsrx chat_fw_brotli 37,938 37,962 +24 (+0.1%) 🔴
octane-tsrx weather_js_raw 175,134 175,427 +293 (+0.2%) 🔴
octane-tsrx weather_js_gzip 57,142 57,226 +84 (+0.1%) 🔴
octane-tsrx weather_js_brotli 49,728 49,743 +15 (+0.0%) 🔴
octane-tsrx weather_app_brotli 7,607 7,603 −4 (−0.1%) 🟢
octane-tsrx weather_fw_raw 148,399 148,692 +293 (+0.2%) 🔴
octane-tsrx weather_fw_gzip 48,098 48,182 +84 (+0.2%) 🔴
octane-tsrx weather_fw_brotli 42,121 42,140 +19 (+0.0%) 🔴

15 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
cli-spa-starter raw 115,639 115,764 +125 (+0.1%) 🔴
cli-spa-starter gzip 39,002 39,029 +27 (+0.1%) 🔴
cli-spa-starter brotli 34,103 34,092 −11 (−0.0%) 🟢
hydrate-root raw 156,619 156,700 +81 (+0.1%) 🔴
hydrate-root gzip 50,209 50,226 +17 (+0.0%) 🔴
hydrate-root brotli 43,760 43,789 +29 (+0.1%) 🔴
deferred-hydration raw 218,779 218,947 +168 (+0.1%) 🔴
deferred-hydration gzip 69,631 69,672 +41 (+0.1%) 🔴
deferred-hydration brotli 60,127 60,176 +49 (+0.1%) 🔴
suspense-transition raw 165,315 165,402 +87 (+0.1%) 🔴
suspense-transition gzip 53,028 53,060 +32 (+0.1%) 🔴
suspense-transition brotli 46,256 46,319 +63 (+0.1%) 🔴
binding-base-ui-esbuild brotli 58,195 58,214 +19 (+0.0%) 🔴
binding-radix-esbuild brotli 58,686 58,726 +40 (+0.1%) 🔴
binding-mantine-hooks-esbuild gzip 32,533 32,534 +1 (+0.0%) 🔴
binding-mantine-hooks-esbuild brotli 28,883 28,887 +4 (+0.0%) 🔴

89 other measured values are unchanged.

CI job durations

  • 🟡 1 job(s) slower than every recent main run: validate publishable packages
  • 🟢 3 job(s) faster than every recent main run: website integration suite, test shard (Node 24, 4/4), React parity shard (3/4)

Compares the CI run for 94ebe326c (run) with the last 5 successful main push runs. A job is only slower or faster outside the whole main range, by at least 10% and 15s. Job time excludes runner setup.

job main median main range this PR Δ
wall clock (first job start to last job end) 13m 51s 12m 39s–17m 36s 12m 40s −1m 11s (−9%) ⚪ within noise
total job time 155m 53s 140m 36s–177m 43s 137m 55s −17m 58s (−12%) 🟢 faster
validate publishable packages 4m 29s 3m 55s–5m 00s 5m 07s +38s (+14%) 🟡 slower
website integration suite 6m 21s 5m 46s–7m 00s 5m 35s −46s (−12%) 🟢 faster
test shard (Node 24, 4/4) 11m 19s 11m 03s–14m 12s 10m 01s −1m 18s (−11%) 🟢 faster
React parity shard (3/4) 11m 51s 10m 08s–12m 43s 9m 48s −2m 03s (−17%) 🟢 faster
All 26 timed jobs
job main median main range this PR Δ
validate publishable packages 4m 29s 3m 55s–5m 00s 5m 07s +38s (+14%) 🟡 slower
example apps (1/3) 3m 14s 3m 10s–4m 01s 3m 19s +5s (+3%) ⚪ within noise
example apps (2/3) 3m 43s 3m 38s–4m 40s 3m 48s +5s (+2%) ⚪ within noise
classify changeset release 13s 13s–15s 14s +1s (+8%) ⚪ within noise
React parity shard (1/4) 11m 04s 10m 51s–12m 30s 11m 05s +1s (+0%) ⚪ within noise
CI provenance 3s 3s–4s 4s +1s (+33%) ⚪ within noise
typecheck 3s 3s–4s 3s ±0s (±0%) ⚪ within noise
examples 3s 3s–3s 3s ±0s (±0%) ⚪ within noise
lint 3s 3s–4s 3s ±0s (±0%) ⚪ within noise
test (24) 3s 3s–4s 3s ±0s (±0%) ⚪ within noise
React parity checks 12s 11s–15s 11s −1s (−8%) ⚪ within noise
Lynx compatibility 1m 38s 1m 32s–2m 07s 1m 35s −3s (−3%) ⚪ within noise
example apps (3/3) 3m 41s 3m 19s–4m 16s 3m 34s −7s (−3%) ⚪ within noise
Three compatibility 2m 56s 2m 38s–3m 48s 2m 44s −12s (−7%) ⚪ within noise
heavy integration (package builds, eval corpus, runtime loaders, Astro) 5m 44s 5m 27s–6m 31s 5m 24s −20s (−6%) ⚪ within noise
React parity shard (2/4) 10m 46s 9m 52s–11m 44s 10m 20s −26s (−4%) ⚪ within noise
test shard (Node 24, 3/4) 10m 06s 9m 24s–11m 36s 9m 34s −32s (−5%) ⚪ within noise
lint checks 8m 32s 7m 52s–10m 19s 7m 58s −34s (−7%) ⚪ within noise
React parity shard (4/4) 11m 23s 10m 43s–13m 16s 10m 48s −35s (−5%) ⚪ within noise
website integration suite 6m 21s 5m 46s–7m 00s 5m 35s −46s (−12%) 🟢 faster
heavy integration (browser - chromium) 12m 43s 10m 58s–16m 52s 11m 52s −51s (−7%) ⚪ within noise
test shard (Node 24, 1/4) 12m 46s 10m 36s–13m 13s 11m 47s −59s (−8%) ⚪ within noise
test shard (Node 24, 4/4) 11m 19s 11m 03s–14m 12s 10m 01s −1m 18s (−11%) 🟢 faster
test shard (Node 24, 2/4) 12m 22s 10m 38s–14m 29s 10m 45s −1m 37s (−13%) ⚪ within noise
React parity shard (3/4) 11m 51s 10m 08s–12m 43s 9m 48s −2m 03s (−17%) 🟢 faster
typecheck checks 8m 30s 1m 56s–11m 03s 2m 10s −6m 20s (−75%) ⚪ within noise

The staged-DOM lint requires a native read to go through a classified
receiver. Use the same domNode(hydration.node) idiom childSlot already
uses for its cursor-parent check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm merged commit b58c783 into main Oct 2, 2026
31 checks passed
@trueadm
trueadm deleted the fix/rebuilt-clone-blocks branch October 2, 2026 11:01
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