Skip to content

fix(hydration): discard the server tail after an @if/@switch arm's last component - #1587

Merged
trueadm merged 2 commits into
mainfrom
claude/musing-swirles-cb5298
Oct 2, 2026
Merged

trueadm merged 2 commits into
mainfrom
claude/musing-swirles-cb5298

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A hydrating @if/@switch arm whose client content is only its own component or branch slots left any server content after its last claimed range on the page, with nothing reported. A minimal repro, with every component framed:

@if (props.server) { <><S /><A /><u>{'tail'}</u></> } @else { <><S /><A /></> }

Server render with server: true, then hydrate without it. On main the result is <s>s</s><b>a</b><u>tail</u>, with no recoverable error and no dev warning, in both dev and prod. #1564's discardArmTail only fires when the cursor rests on an open marker, and here it rests on <u>.

The cursor after an arm body is not a reliable "consumed up to" marker on its own, because clone() leaves it on an adopted root. This change adds a record of where a range claim made directly by the arm parked the cursor:

  • HydrationCapability.parkPast(close, parent) replaces the plain node = getNextSibling(end) advance at three sites: componentSlotLite (after fix(hydration): discard the server tail of an adopted component range #1576's settleClaim), componentSlotImpl, and the end of renderBranchSlot. When parent is the arm being adopted, it also records the parked node and the arm's slot count.
  • renderAdoptedArm(block) wraps the arm's first hydrating renderBlock. It saves and restores the outer arm's record in try/finally, so nested arms and throws are safe. It returns the parked node only when:
    • the arm cloned no template of its own (adopt() seals the record when CURRENT_SCOPE.block is the arm), and
    • no slot registered after the last park (slots only grows). That covers a later component adopting the parked node without a range, whatever path it adopts through.
  • discardArmTail also discards from that node, alongside its existing open-marker rule. It keeps fix(hydration): discard server ranges a shorter @if/@switch arm leaves unclaimed #1564's conventions: stop at nodes recovery rebuilt, code 51 plus one dev "the end of the branch" diagnostic, quiet under staleServerValues. fix(hydration): keep a rebuilt clone where the server node it replaced stood #1561's quiet sweepRebuiltTail still runs first.

Validation

  • pnpm format:check: not run repo-wide; pnpm format:files:check passes on every changed file.
  • pnpm typecheck: not run. Before the merge with main, pnpm typecheck:files passed on runtime.ts, the test and the .tsrx fixture.
  • pnpm test: not run.
  • targeted tests, before the merge with main: tests/hydration/branch-arm-claim-tail.test.ts, 28 cases × dev/prod compile, passed 56/56 across the octane and octane-prod projects.
    • On main at the time, all 20 target cases failed for the intended reason and the 8 controls passed.
    • Each guard was mutation-checked: removing it turns only its own test red. The guards are the template seal, the slot count, staleServerValues, the lite, full and branch park sites, and the fresh-node stop.

Left unverified locally:

  • Machine load meant no local runs after the merge with main, so this head's tests and typecheck are left to CI.
  • The merge changed two things: it folded the discard into fix(hydration): discard server ranges a shorter @if/@switch arm leaves unclaimed #1564's discardArmTail, and it switched the expected recoverable message from a draft code 337 to code 51.
  • A full octane + octane-prod run before the merge crashed early with a Vitest worker EPIPE under load, so it gave no result either way.

Bundle (benchmarks/bundle-size/run-minimal.mjs), measured before the merge:

  • hydrate-root: +712 raw / +209 gzip
  • deferred-hydration: +715 / +217
  • root-static, suspense-transition, base-ui: +2 raw
  • cli-spa-starter, hooks-state, behavior-root: unchanged

No wall-clock measurement was trustworthy at that load. The per-claim cost during hydration is one method call and one field compare.

Risk / follow-ups

  • The in-flight host-tail follow-up to fix(hydration): discard server ranges a shorter @if/@switch arm leaves unclaimed #1564 (fix/branch-arm-host-tails) has these tests, and planned to fold the same parked-claim rule into its renderArm or ship it right after. Whichever lands second should keep a single rule at this call site.
  • Lists, @try and Activity at an arm's top level don't record a park yet. A server tail after them stays as before (a safe miss).
  • Element and text tails after an arm's own template are the host-tail follow-up's territory.

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 control-flow in runtime.ts (cursor parking and arm tail discard), but behavior is narrowly scoped to branch arms and is heavily regression-tested.

Overview
Fixes a silent hydration bug where the server rendered a longer @if / @switch arm than the client (e.g. extra trailing markup after the same framed components) and that leftover DOM stayed on the page with no onRecoverableError or dev warning.

The runtime now tracks where each arm’s last server range claim parks the hydration cursor via parkPast (replacing bare getNextSibling advances after component and branch slots) and renderAdoptedArm for the arm’s first adopting render. discardArmTail is extended to remove unclaimed server nodes from that parked position as well as the existing open-marker case, while still preserving adopted nodes and reporting the mismatch once.

Adds a broad hydration fixture and test suite (branch-arm-claim-tail) covering @switch, nested branches, lazy/pending children, @try, and dormant Hydrate boundaries.

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

trueadm and others added 2 commits October 2, 2026 16:18
…laim

A hydrating @if/@switch arm whose content is its own component or branch
slots left any server content after its last claimed range on the page,
with nothing reported. Each claim now parks the cursor through
HydrationCapability.parkPast, which records where it parked for the arm
being adopted. renderAdoptedArm discards from there to the arm's end,
stopping at nodes recovery rebuilt, and reports it once (new code 337
plus a dev diagnostic at the branch). It leaves the content when the arm
cloned a template of its own, or a later slot registered after the last
claim, because the cursor then rests on adopted roots rather than past
them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fold the parked-claim rule into main's discardArmTail (#1564) instead of
a second discard path: renderAdoptedArm now returns where the arm's last
slot parked the cursor, and discardArmTail also discards from that node.
The report follows #1564's convention (code 51, "the end of the branch"),
so the new code 337 is dropped. The parks keep #1576's settleClaim and
run after #1561's quiet sweepRebuiltTail.

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 18:03
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-size: 26 value(s) increased
  • 🔴 bundle-reachability: 30 value(s) increased

Compares 18fbcb06b with f963557f6 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 6.34 6.05 −4.6% ±98% ⚪ within noise
octane-tsrx replace 9.83 9.36 −4.8% ±64% ⚪ within noise
octane-tsrx add 4.90 5.13 +4.7% ±99% ⚪ within noise
octane-tsrx update 0.60 0.66 +10.0% ±61% ⚪ within noise
octane-tsrx select 0.20 0.25 +25.0% ±67% ⚪ within noise
octane-tsrx swap 0.51 0.62 +21.6% ±54% ⚪ within noise
octane-tsrx remove 0.45 0.46 +2.2% ±56% ⚪ within noise
octane-tsrx runlots 40.47 63.49 +56.9% ±80% ⚪ within noise
octane-tsrx select_lots 0.20 0.24 +20.0% ±74% ⚪ within noise
octane-tsrx clear 38.18 41.14 +7.8% ±18% ⚪ within noise
octane-jsx run 6.04 6.21 +2.8% ±94% ⚪ within noise
octane-jsx replace 12.37 9.98 −19.3% ±90% ⚪ within noise
octane-jsx add 5.27 5.42 +2.8% ±101% ⚪ within noise
octane-jsx update 0.64 0.68 +6.3% ±35% ⚪ within noise
octane-jsx select 0.50 0.53 +6.0% ±73% ⚪ within noise
octane-jsx swap 0.86 0.86 ±0.0% ±68% ⚪ within noise
octane-jsx remove 0.69 0.68 −1.4% ±79% ⚪ within noise
octane-jsx runlots 36.12 35.50 −1.7% ±16% ⚪ within noise
octane-jsx select_lots 4.27 3.80 −11.0% ±104% ⚪ within noise
octane-jsx clear 44.22 41.98 −5.1% ±40% ⚪ within noise

Application bundle size (bytes)

target metric base head Δ
octane-tsrx todo_js_raw 141,632 141,676 +44 (+0.0%) 🔴
octane-tsrx todo_js_gzip 46,754 46,779 +25 (+0.1%) 🔴
octane-tsrx todo_js_brotli 40,931 41,005 +74 (+0.2%) 🔴
octane-tsrx todo_app_gzip 2,476 2,477 +1 (+0.0%) 🔴
octane-tsrx todo_fw_raw 136,522 136,566 +44 (+0.0%) 🔴
octane-tsrx todo_fw_gzip 44,278 44,302 +24 (+0.1%) 🔴
octane-tsrx todo_fw_brotli 38,812 38,886 +74 (+0.2%) 🔴
octane-tsrx chat_js_raw 140,877 140,921 +44 (+0.0%) 🔴
octane-tsrx chat_js_gzip 46,731 46,756 +25 (+0.1%) 🔴
octane-tsrx chat_js_brotli 40,996 41,095 +99 (+0.2%) 🔴
octane-tsrx chat_app_gzip 2,978 2,979 +1 (+0.0%) 🔴
octane-tsrx chat_app_brotli 2,633 2,638 +5 (+0.2%) 🔴
octane-tsrx chat_fw_raw 134,224 134,268 +44 (+0.0%) 🔴
octane-tsrx chat_fw_gzip 43,753 43,777 +24 (+0.1%) 🔴
octane-tsrx chat_fw_brotli 38,363 38,457 +94 (+0.2%) 🔴
octane-tsrx weather_js_raw 177,447 177,493 +46 (+0.0%) 🔴
octane-tsrx weather_js_gzip 58,013 58,032 +19 (+0.0%) 🔴
octane-tsrx weather_js_brotli 50,370 50,491 +121 (+0.2%) 🔴
octane-tsrx weather_app_gzip 9,045 9,044 −1 (−0.0%) 🟢
octane-tsrx weather_app_brotli 7,582 7,590 +8 (+0.1%) 🔴
octane-tsrx weather_fw_raw 150,712 150,758 +46 (+0.0%) 🔴
octane-tsrx weather_fw_gzip 48,968 48,988 +20 (+0.0%) 🔴
octane-tsrx weather_fw_brotli 42,788 42,901 +113 (+0.3%) 🔴
octane-jsx js_raw 225,636 225,638 +2 (+0.0%) 🔴
octane-jsx js_gzip 72,064 72,069 +5 (+0.0%) 🔴
octane-jsx js_brotli 62,396 62,371 −25 (−0.0%) 🟢
octane-jsx app_brotli 2,501 2,500 −1 (−0.0%) 🟢
octane-jsx fw_raw 217,169 217,171 +2 (+0.0%) 🔴
octane-jsx fw_gzip 69,121 69,126 +5 (+0.0%) 🔴
octane-jsx fw_brotli 59,895 59,871 −24 (−0.0%) 🟢

15 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
root-static raw 201,955 201,957 +2 (+0.0%) 🔴
root-static gzip 64,211 64,216 +5 (+0.0%) 🔴
root-static brotli 55,630 55,608 −22 (−0.0%) 🟢
context raw 110,283 110,285 +2 (+0.0%) 🔴
context gzip 36,508 36,512 +4 (+0.0%) 🔴
context brotli 32,306 32,295 −11 (−0.0%) 🟢
hydrate-root raw 164,499 164,937 +438 (+0.3%) 🔴
hydrate-root gzip 52,553 52,665 +112 (+0.2%) 🔴
hydrate-root brotli 45,699 45,842 +143 (+0.3%) 🔴
deferred-hydration raw 227,262 227,702 +440 (+0.2%) 🔴
deferred-hydration gzip 72,179 72,304 +125 (+0.2%) 🔴
deferred-hydration brotli 62,238 62,448 +210 (+0.3%) 🔴
suspense-transition raw 166,795 166,797 +2 (+0.0%) 🔴
suspense-transition gzip 53,606 53,614 +8 (+0.0%) 🔴
suspense-transition brotli 46,785 46,799 +14 (+0.0%) 🔴
binding-base-ui-vite raw 212,145 212,147 +2 (+0.0%) 🔴
binding-base-ui-vite gzip 67,819 67,826 +7 (+0.0%) 🔴
binding-base-ui-vite brotli 58,685 58,729 +44 (+0.1%) 🔴
binding-base-ui-esbuild raw 210,114 210,116 +2 (+0.0%) 🔴
binding-base-ui-esbuild gzip 67,748 67,754 +6 (+0.0%) 🔴
binding-base-ui-esbuild brotli 58,764 58,769 +5 (+0.0%) 🔴
binding-radix-vite raw 214,075 214,077 +2 (+0.0%) 🔴
binding-radix-vite gzip 68,343 68,350 +7 (+0.0%) 🔴
binding-radix-vite brotli 59,287 59,244 −43 (−0.1%) 🟢
binding-radix-esbuild raw 212,003 212,005 +2 (+0.0%) 🔴
binding-radix-esbuild gzip 68,353 68,358 +5 (+0.0%) 🔴
binding-radix-esbuild brotli 59,217 59,250 +33 (+0.1%) 🔴
binding-mantine-hooks-esbuild brotli 29,066 29,072 +6 (+0.0%) 🔴
binding-apollo-client-vite raw 357,197 357,199 +2 (+0.0%) 🔴
binding-apollo-client-vite gzip 111,906 111,911 +5 (+0.0%) 🔴
binding-apollo-client-vite brotli 97,599 97,545 −54 (−0.1%) 🟢
binding-apollo-client-esbuild raw 360,153 360,155 +2 (+0.0%) 🔴
binding-apollo-client-esbuild gzip 114,110 114,114 +4 (+0.0%) 🔴
binding-apollo-client-esbuild brotli 99,359 99,387 +28 (+0.0%) 🔴

71 other measured values are unchanged.

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