Skip to content

fix(hydration): recover silently and take client values under stale captures - #1636

Merged
trueadm merged 3 commits into
mainfrom
fix/stale-hydration-recovery
Oct 3, 2026
Merged

trueadm merged 3 commits into
mainfrom
fix/stale-hydration-recovery

Conversation

@trueadm

@trueadm trueadm commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This fixes findings B1 and B2 from the 2026-10-02 review of PRs merged into main.

If a dormant <Hydrate> boundary's props change before it activates, the server HTML is older than the client's state. Activation should then repair it silently (docs/deferred-hydration.md, "Boundary ownership"). Two things went wrong instead.

B1: recovery still reported. Each recovery site checked staleServerValues on its own, and several never checked it. These sites still called onRecoverableError in dev and prod, and the first and third also logged a dev warning:

  • a renderable hole whose server text became a component (code 55);
  • a createElement child list that shrank (code 62);
  • a dynamic host tag that changed (code 51).

#1595's statement that "every other site already uses reportStructural" was wrong: only two sites did.

B2: old server values stayed. Under the same stale captures, suppressHydrationWarning and dangerouslySetInnerHTML kept the server's older value after activation. For example, <em title="old">old</em> stayed next to <i>new</i>, and an <article> kept <b>old</b>. dangerouslySetInnerHTML also logged a false dev warning.

Changes (runtime.ts):

  • One stale gate. noteRecoverableHydrationError returns when currentHydration is stale. So does logHydrationMismatch, the single DEV-only funnel for structural, value and kept-value warnings. All 14 inline !this.staleServerValues checks are deleted, including recordTextMismatch's. reportStructural() now returns void, because warnings are gated centrally.
  • One keep-server predicate. HydrationCapability.keepsServerValue(el) is true when the element is suppressed, not freshly rebuilt, and not stale. It decides in htext, both branches of htextSwap, allowAttribute, allowClass and applyStyle, and it replaces the separate fresh-clone checks that fix(hydration): apply a rebuilt clone's attributes without a value report #1536 and fix(hydration): show suppressed text inside a rebuilt clone #1538 added.
  • Stale HTML is replaced in place. HydrationCapability.repairHTML keeps the server's dangerouslySetInnerHTML on a mismatch, as React does, unless captures are stale. Then it replaces the server's children in place after save(el), as text repairs do, so a discarded @try arm attempt restores them for its retry. The client write path wasn't usable: it defers removing the old children to the root's commit, which would still run after a rollback. (Second commit, from review.)
  • With unchanged captures, mismatches are still reported and suppressed values are still kept, as before.

Other changes: docs/deferred-hydration.md now covers suppressed and dangerouslySetInnerHTML values, and there is a changeset.

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 runtime, test and fixture passes.
  • pnpm test: not run, to limit machine load. CI runs it.
  • targeted tests:
    • New file: tests/hydration/stale-capture-recovery.test.ts with its .tsrx fixture.
      • It failed on main: all 10 changed-captures cases failed in dev and prod compiles.
      • The 10 unchanged-captures controls pass on main and with this change.
      • I broke each part of the fix and a test failed every time: each gate, the stale and fresh conditions in keepsServerValue, and the setHTML stale write.
      • Two cases added from review, each failing on the first commit in both compiles: a pending stale @try arm keeps the server HTML (fails without save), and a suppressed host whose server text node is missing gets the client text under stale captures, while an unchanged-captures control still gets an empty node.
    • I temporarily instrumented all 33 converted report and warning sites to log if their own capability's staleness ever differed from currentHydration's. Across 37 related hydration files in both projects, it never did.
    • On main 11a9da9: 54 related hydration and value-path files, including the test files from fix(hydration): remove the server tail after a component root rebuilt over its range #1614/fix(hydration): keep a hookless component's signal level below its arms and rows #1616/fix(hydration): append a rebuilt single root that was its host's last child #1619/fix(suspense): keep a pending boundary's query$ loads across parent renders #1620/fix(hydration): adopt a descriptor list again from its first server item when a deferred retry refills it #1624, all pass in --project octane and --project octane-prod, 2037 tests each.
      • The first commit's run had one timeout under load in each project: native-condition-demand (dev) and split-template-consumers (prod). Both pass when re-run alone, twice each. The run after the review fixes was fully green.
      • After moving the stale HTML write into repairHTML, I re-ran the new file and five dangerouslySetInnerHTML and script suites in both projects (94 tests each).
    • Bundle size (benchmarks/bundle-size/run-minimal.mjs, main vs. this change):
      • hydrate-root: −38 B raw, +36 B gzip, −36 B brotli.
      • deferred-hydration: −38 B raw, +19 B gzip, −174 B brotli.
      • The first commit alone was −220 B raw and −28 B gzip. The in-place HTML repair adds back about 180 B raw.
      • Every client-only scenario, including the five CI enforces, is byte-identical.
      • JSX/de-opt bundles: −6 B raw (Vite) and −16 B raw (esbuild), with gzip and brotli within ±30 B.
      • The changed code runs only when hydration finds a mismatch, so there is no new per-node cost.
    • pnpm sync produces no changes.

Risk / follow-ups

  • B4 is not changed. These are the HYDRATION_REBUILT short-circuits in reportRebuiltRange and discardListArm.
    • Removing them still gave exactly one report in the review's suggested case: a <Hydrate> activation that discards a branch, list or empty branch, commits, then suspends in a later sibling. On resume the branch updates rather than re-hydrating.
    • The only path left unexercised is a re-activation after discardHydratePresentation. The central gate does not cover retries, so the short-circuits stay.
  • Native-adoption recovery reports run after currentHydration is restored, so they are now silenced only when the outer hydration is stale. Before, they were never silenced. These were not among the review's seven sites.
  • A separate bug, not touched here: when hydration keeps a mismatched server dangerouslySetInnerHTML, the next render with the same __html overwrites it. React keeps it until the value changes. Tracked in a separate session.

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 central hydration mismatch reporting and value-repair paths used in dev and prod; incorrect gating could hide real mismatches or change DOM after deferred boundary activation.

Overview
When a dormant <Hydrate> boundary activates after its captures changed, the server HTML is intentionally stale. This PR makes that path quiet and consistent: recovery still runs, but onRecoverableError and dev hydration warnings are suppressed via central checks on currentHydration.staleServerValues in noteRecoverableHydrationError and logHydrationMismatch, replacing scattered per-site guards.

Under the same stale captures flag, activation now applies the client’s presentation instead of keeping older server output: keepsServerValue treats suppressHydrationWarning like a normal mismatch for text, attributes, class, and style, and repairHTML replaces mismatched dangerouslySetInnerHTML in place (with save for rollback). Unchanged captures still report mismatches and still keep suppressed / innerHTML server values as before.

Docs and a changeset describe the behavior; new stale-capture-recovery fixture tests compare dormant vs plain trees in dev and prod compiles.

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

…aptures

When a dormant <Hydrate> boundary's captures change before it activates, the
server HTML predates the client's state, so activation is meant to repair it
without onRecoverableError or a dev warning. Each recovery site tested
staleServerValues itself, and several never did: a renderable hole whose
server text became a component (code 55), a shrinking createElement child list
(62), and a changed dynamic host tag (51) still reported in dev and prod.

Gate once instead: noteRecoverableHydrationError and logHydrationMismatch
return when the current hydration is stale, and every inline
!this.staleServerValues check is gone. reportStructural no longer returns
whether it reported, since warnings gate centrally.

Under the same stale captures, suppressHydrationWarning and
dangerouslySetInnerHTML kept the server's older value after activation, and
dangerouslySetInnerHTML also logged a false dev warning. One predicate,
HydrationCapability.keepsServerValue (suppressed, not freshly rebuilt, not
stale), now decides at every text, attribute, class, and style site, folding in
the fresh-clone checks from #1536 and #1538. setHTML writes the client's HTML
when stale. Unchanged captures still report and keep server values as before.

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 3, 2026
@github-actions
github-actions Bot marked this pull request as draft October 3, 2026 07:35

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

Fix All in Cursor

Bugbot Autofix prepared fixes for both issues found in the latest run.

  • ✅ Fixed: Stale HTML write skips hydration rollback
    • Added hydration.save(el) before the stale-capture path falls through to the general innerHTML write, so a speculative attempt that rolls back restores the original server content.
  • ✅ Fixed: Suppressed stale swap keeps empty text
    • Replaced isHydrationSuppressed(host) with this.keepsServerValue(host) in htextSwap's structural arm so stale captures install the client text instead of an empty tracking node.

Create PR

Or push these changes by commenting:

@cursor push 91aa3f553c
Preview (91aa3f553c)
diff --git a/packages/octane/src/runtime.ts b/packages/octane/src/runtime.ts
--- a/packages/octane/src/runtime.ts
+++ b/packages/octane/src/runtime.ts
@@ -21504,13 +21504,13 @@
 			return posNode as Text;
 		}
 		const host = (STAGED_DOM?.view(posNode) ?? posNode)?.parentNode ?? null;
-		const suppressed = isHydrationSuppressed(host);
+		const keeps = this.keepsServerValue(host);
 		// A non-empty client text binding where the server has no text node is a
 		// structural mismatch, just like an extra client element. Build the client
 		// text so recovery succeeds, but publish the normal dev diagnostic. A
 		// suppressed host keeps the absent server value by installing only an empty
 		// tracking node; later real commits can update that node normally.
-		if (text !== '' && !suppressed) {
+		if (text !== '' && !keeps) {
 			if (host !== null) this.save(host);
 			noteRecoverableHydrationError(() => new Error(formatClientError(54)));
 			if (process.env.NODE_ENV !== 'production') {
@@ -21521,7 +21521,7 @@
 				);
 			}
 		}
-		const created = (STAGED_DOM?.view(document) ?? document).createTextNode(suppressed ? '' : text);
+		const created = (STAGED_DOM?.view(document) ?? document).createTextNode(keeps ? '' : text);
 		if (posNode !== null && (STAGED_DOM?.view(posNode) ?? posNode).parentNode !== null) {
 			domNode((STAGED_DOM?.view(posNode) ?? posNode).parentNode!).insertBefore(created, posNode);
 		}
@@ -24117,7 +24117,7 @@
 		}
 		// React keeps the server's HTML on a mismatch. Server HTML that predates
 		// the client's state (staleServerValues) is not worth keeping: write the
-		// client's below.
+		// client's below, after recording the parent so rollback can restore it.
 		if (!hydration.staleServerValues) {
 			if (process.env.NODE_ENV !== 'production' && !hydration.keepsServerValue(el))
 				warnHydrationKeptServerValue(
@@ -24128,6 +24128,7 @@
 				);
 			return;
 		}
+		hydration.save(el);
 	}
 	if (el.localName === 'script') {
 		setScriptText(el, next);

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit af7724c. Configure here.

Comment thread packages/octane/src/runtime.ts Resolved
Comment thread packages/octane/src/runtime.ts Resolved
@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-size: 4 value(s) increased
  • 🔴 bundle-reachability: 8 value(s) increased

Compares 950ef0b0b with 47ed32caa 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.23 3.29 +1.9% ±10% ⚪ within noise
octane-tsrx replace 7.23 6.82 −5.7% ±35% ⚪ within noise
octane-tsrx add 3.21 3.22 +0.3% ±9% ⚪ within noise
octane-tsrx update 0.31 0.31 −0.0% ±46% ⚪ within noise
octane-tsrx select 0.11 0.11 −0.0% ±112% ⚪ within noise
octane-tsrx swap 0.28 0.28 −0.0% ±42% ⚪ within noise
octane-tsrx remove 0.35 0.33 −5.7% ±51% ⚪ within noise
octane-tsrx runlots 29.07 28.36 −2.4% ±29% ⚪ within noise
octane-tsrx select_lots 0.21 0.22 +4.8% ±64% ⚪ within noise
octane-tsrx clear 36.64 36.82 +0.5% ±7% ⚪ within noise
octane-jsx run 3.38 3.42 +1.2% ±8% ⚪ within noise
octane-jsx replace 6.25 6.42 +2.7% ±8% ⚪ within noise
octane-jsx add 3.00 3.05 +1.7% ±8% ⚪ within noise
octane-jsx update 0.46 0.55 +19.6% ±25% ⚪ within noise
octane-jsx select 0.32 0.35 +9.4% ±46% ⚪ within noise
octane-jsx swap 0.60 0.63 +5.0% ±25% ⚪ within noise
octane-jsx remove 0.52 0.52 −0.0% ±35% ⚪ within noise
octane-jsx runlots 27.47 27.57 +0.4% ±16% ⚪ within noise
octane-jsx select_lots 2.20 2.38 +8.2% ±19% ⚪ within noise
octane-jsx clear 38.45 39.08 +1.6% ±7% ⚪ within noise

Application bundle size (bytes)

target metric base head Δ
octane-jsx js_raw 226,884 226,878 −6 (−0.0%) 🟢
octane-jsx js_brotli 62,735 62,837 +102 (+0.2%) 🔴
octane-jsx app_gzip 2,936 2,935 −1 (−0.0%) 🟢
octane-jsx app_brotli 2,497 2,515 +18 (+0.7%) 🔴
octane-jsx fw_raw 218,425 218,419 −6 (−0.0%) 🟢
octane-jsx fw_gzip 69,491 69,492 +1 (+0.0%) 🔴
octane-jsx fw_brotli 60,238 60,322 +84 (+0.1%) 🔴

38 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
root-static raw 203,121 203,115 −6 (−0.0%) 🟢
root-static gzip 64,616 64,600 −16 (−0.0%) 🟢
root-static brotli 55,909 56,002 +93 (+0.2%) 🔴
hydrate-root raw 173,260 173,220 −40 (−0.0%) 🟢
hydrate-root gzip 54,883 54,922 +39 (+0.1%) 🔴
hydrate-root brotli 47,892 47,935 +43 (+0.1%) 🔴
deferred-hydration raw 236,589 236,549 −40 (−0.0%) 🟢
deferred-hydration gzip 74,764 74,805 +41 (+0.1%) 🔴
deferred-hydration brotli 64,526 64,510 −16 (−0.0%) 🟢
binding-base-ui-vite raw 213,352 213,346 −6 (−0.0%) 🟢
binding-base-ui-vite gzip 68,215 68,212 −3 (−0.0%) 🟢
binding-base-ui-vite brotli 59,116 59,198 +82 (+0.1%) 🔴
binding-base-ui-esbuild raw 211,307 211,291 −16 (−0.0%) 🟢
binding-base-ui-esbuild gzip 68,148 68,152 +4 (+0.0%) 🔴
binding-base-ui-esbuild brotli 59,119 59,056 −63 (−0.1%) 🟢
binding-radix-vite raw 215,284 215,278 −6 (−0.0%) 🟢
binding-radix-vite gzip 68,739 68,730 −9 (−0.0%) 🟢
binding-radix-vite brotli 59,636 59,655 +19 (+0.0%) 🔴
binding-radix-esbuild raw 213,199 213,183 −16 (−0.0%) 🟢
binding-radix-esbuild gzip 68,734 68,730 −4 (−0.0%) 🟢
binding-radix-esbuild brotli 59,795 59,650 −145 (−0.2%) 🟢
binding-apollo-client-vite raw 358,345 358,339 −6 (−0.0%) 🟢
binding-apollo-client-vite brotli 97,970 98,019 +49 (+0.1%) 🔴
binding-apollo-client-esbuild raw 361,292 361,276 −16 (−0.0%) 🟢
binding-apollo-client-esbuild gzip 114,485 114,476 −9 (−0.0%) 🟢
binding-apollo-client-esbuild brotli 99,713 99,710 −3 (−0.0%) 🟢

79 other measured values are unchanged.

… suppressed lost text node

Under stale captures setHTML wrote the client's HTML through the client write
path, whose removal of the server's children is deferred to the root's
commit. A resolved @Try arm that wrote it and then suspended therefore showed
the client's HTML beside server content while pending, unlike text, and a
naive save() would leave the article empty once the deferred removal ran.
HydrationCapability.repairHTML now replaces the server's children in place
after save(el), as text repairs do, so the discarded attempt restores them for
its retry. It is a method, so bundles that never hydrate do not retain it.

htextSwap's branch for a server that rendered no text node still decided
with isHydrationSuppressed, so a suppressed host under stale captures got an
empty node instead of the client's text. It now uses keepsServerValue, and
saves the host before inserting non-empty client text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm marked this pull request as ready for review October 3, 2026 07:57
…ecovery

# Conflicts:
#	packages/octane/src/runtime.ts
@trueadm
trueadm merged commit 1d33932 into main Oct 3, 2026
30 checks passed
@trueadm
trueadm deleted the fix/stale-hydration-recovery branch October 3, 2026 11:03
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