Skip to content

fix(hydration): resume a suspended child inside a rebuilt component fragment - #1598

Merged
trueadm merged 7 commits into
mainfrom
claude/intelligent-lumiere-f12800
Oct 2, 2026
Merged

trueadm merged 7 commits into
mainfrom
claude/intelligent-lumiere-f12800

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A preserved <Hydrate> activation that suspends on a child inside a hydrated component fragment now resumes correctly. Four fixes:

  • Resume cursor: after the resume, the components that follow a lite component adopt their own server ranges. Before, they removed and duplicated server nodes, even when the server HTML matched the client exactly.
  • Client-built children: a child that mounted inside content hydration rebuilt on the client resumes as a client render. Before, it could adopt a server node outside its rebuilt parent.
  • Range size: a component fragment that holds more roots than its server range does is rebuilt and reported once, instead of being adopted over another component's content.
  • Suspended claims: a component that suspends before its template renders, for example one that calls use() in setup, claims its range again when it resumes. Its fragment is rebuilt and reported once, as when its data is ready. Cursor Bugbot flagged this.

Original repro (now the HoleFirstBranch shape): the server renders <><Bold /><Em /></>; the client renders <><FragChild leaf /><Em /></>, where FragChild is <><Under leaf /><i>x</i></> and Under calls use(leaf). Resolving leaf threw NotFoundError. It now reads <u>z</u><i>x</i><em>e</em>, keeps the server <em>, and reports the mismatch once.

Why

  1. A resumed lite component left the cursor stale. componentSlotLite moved the hydration cursor past its adopted range only on its first render. When the activation resumed and replayed the arm, the next sibling (<Em />) found the cursor inside the earlier component's range and misadopted. It removed the server <i> and added a second <em>, even when the server rendered exactly what the client did.
  2. A client-built child resumed with hydration active. A child that first mounted under HydrationCapability.suspend was completed at the activation's saved cursor with hydration active. When that cursor stood on a server node left after the replaced one, the child adopted it, outside its rebuilt parent, and its own static roots were never created. Cursor Bugbot flagged this, and RebuiltRootBranch reproduces it.
  3. A fragment could be adopted over another component's content. fix(hydration): rebuild a hole-first fragment over another arm's nodes #1599, now on main, rebuilds a hole-first fragment when a static element root after its leading holes doesn't match. A fragment with no such root, such as a component call followed by text, still matched any server node, and its text was never created.

Changes

All in packages/octane/src/runtime.ts:

  • componentSlotLite: on a hydrating replay, the cursor now moves past the scope's adopted lite range, as componentSlotImpl already does for its frame.
  • HydrationCapability.suspend records the SuspenseException that leaves a client-built subtree (freshSuspension). A preserved activation stores whether its source suspended there, and the resume completes that source under suspend(). It still restores the saved cursor for the ancestors' replay. The match is on the exception object, which is allocated per throw, not on the promise, so another suspension on a shared promise can't match.
  • claimRoots now returns whether the claimed component range holds all of the fragment template's roots. Every fragment-root hole renders at least one server node (a range, text, or <!---->), so adopt() rebuilds a fragment whose roots outnumber its range. It adds no work when the server matches, because claimRoots already walked those roots. Under a HYDRATION_RANGE_BOUNDARY passthrough root (passthroughRoot, added by fix(hydration): rebuild a hole-first fragment over another arm's nodes #1599), ranges can sit one level off, so the old tolerance stays; range-boundary.test.ts covers that.
  • renderClaimed records the block and its slot when the claimed render suspends. renderSuspended, added by fix(hydration): adopt a component hole's own server node in a template adopted in place #1602, resumes a source with a recorded claim through renderClaimed again, so its template's range check and the leftover-content cleanup still run.

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 runtime.ts, the test, and the .tsrx fixture passes)
  • pnpm test (not run as a whole; see the targeted runs below)
  • targeted tests:
    • New packages/octane/tests/hydration/rebuilt-fragment-suspended-child.test.ts, in dev and prod compiles. Four fragment shapes (the hole first, static first, a two-root leaf, a hole followed by text), each suspended then resolved, already resolved, and server-matching. Two more cases compare a resume with the already-resolved run: a child of a rebuilt root, and a component that suspends before its template. Reverting the cursor fix fails 16 tests, the client-built resume fix 2, the range check 4, and the claim record 2.
    • On the final head, packages/octane/tests/hydration in octane and octane-prod: 280 files, 13,228 tests passed.
    • Earlier revision, before merging main: the full octane and octane-prod projects passed, 1347 files and 37,338 tests.
    • benchmarks/bundle-size/run-minimal.mjs, without the client-built resume fix, against current main: hydrate-root +38 B raw / +11 B gzip, deferred-hydration +38 B raw / +14 B gzip, all other fixtures unchanged. An earlier revision with that fix measured +145 / +235 B raw. No timing benchmark was run. The matching path does no extra DOM reads, and replays add one WeakMap lookup per lite slot.

Risk / follow-ups

  • While the child is pending, the server content the rebuild replaced is already gone. The rebuilt fragment's other nodes and the server's following siblings stay. Keeping the server content until the leaf commits would need speculative rollback for <Hydrate> activations.
  • Pre-existing, also with no suspension: when a single root such as <p> is rebuilt over a server node, the rest of that server range is left on screen (RebuiltRootBranch keeps the server <em>tail</em>). This PR's test compares the resumed output with the already-resolved output instead of pinning that tail. It is tracked 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

High Risk
Changes core hydration cursor, claim, and preserved-activation resume logic in runtime.ts, where subtle bugs can corrupt DOM or duplicate mismatch reporting.

Overview
Fixes <Hydrate> activations that suspend on a child inside a hydrated component fragment and then resume when async data arrives.

Resume replay: lite component slots now advance the hydration cursor past their adopted server range on replay, not only on first adoption. Following siblings no longer mis-adopt inside the earlier component’s range and corrupt DOM when server HTML already matched.

Client-built subtrees: preserved activations record when the suspended source mounted under HydrationCapability.suspend, and resume that leaf as a client render so it does not steal server nodes outside its rebuilt parent. renderClaimed suspensions stash their claim in suspendedClaims and renderSuspended re-enters renderClaimed on resume.

Fragment range fit: claimRoots reports whether the server range covers all fragment roots; fragments with extra roots (e.g. component + trailing text) rebuild and emit a single mismatch instead of adopting over another component’s markup—including when suspension happens before the fragment hydrates.

Adds a patch changeset and hydration tests covering several fragment shapes, rebuilt roots, and setup-before-template suspend.

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

trueadm and others added 2 commits October 2, 2026 19:57
…ragment

A preserved <Hydrate> activation crashed with NotFoundError when it resumed
a child that suspended inside a component fragment. The fragment had
adopted, or should have rebuilt, another component's server range.

A fragment whose first root is a hole matches any server node, so
hydration adopted it over another component's content. The hole's
single-root component then received the server node as its insertion
anchor and discarded that node during mismatch recovery. The fragment's
static roots were never created. claimRoots now reports a claimed range
that ends before the template's roots do. Every fragment-root hole renders
at least one server node, so adopt() rebuilds such a fragment. Passthrough
roots, which adopt ranges one level off, keep the tolerant behavior.

On resume, a replayed lite component now moves the cursor past its adopted
range, so the siblings after it adopt their own server ranges. Before this,
even a resume whose server HTML matched discarded and duplicated server
nodes. A suspension thrown out of a client-built subtree is recorded, and
its source resumes as a client render instead of adopting at the saved
cursor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…miere-f12800

# Conflicts:
#	packages/octane/src/runtime.ts
@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:02
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

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

Compares 3ec43daa4 with 160802f66 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.51 3.45 −1.7% ±14% ⚪ within noise
octane-tsrx replace 8.05 8.12 +0.9% ±14% ⚪ within noise
octane-tsrx add 3.47 3.50 +0.9% ±11% ⚪ within noise
octane-tsrx update 0.47 0.44 −6.4% ±63% ⚪ within noise
octane-tsrx select 0.26 0.18 −30.8% ±56% ⚪ within noise
octane-tsrx swap 0.43 0.39 −9.3% ±52% ⚪ within noise
octane-tsrx remove 0.54 0.42 −22.2% ±75% ⚪ within noise
octane-tsrx runlots 31.96 30.03 −6.0% ±20% ⚪ within noise
octane-tsrx select_lots 0.24 0.24 +0.0% ±42% ⚪ within noise
octane-tsrx clear 43.74 42.94 −1.8% ±16% ⚪ within noise
octane-jsx run 3.57 4.31 +20.7% ±27% ⚪ within noise
octane-jsx replace 8.09 8.69 +7.4% ±26% ⚪ within noise
octane-jsx add 3.31 3.28 −0.9% ±18% ⚪ within noise
octane-jsx update 0.67 0.66 −1.5% ±21% ⚪ within noise
octane-jsx select 0.57 0.50 −12.3% ±80% ⚪ within noise
octane-jsx swap 0.74 0.72 −2.7% ±15% ⚪ within noise
octane-jsx remove 0.61 0.69 +13.1% ±47% ⚪ within noise
octane-jsx runlots 34.90 33.44 −4.2% ±31% ⚪ within noise
octane-jsx select_lots 2.77 2.77 +0.0% ±26% ⚪ within noise
octane-jsx clear 45.03 44.44 −1.3% ±8% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

target metric base head Δ
hydrate-root raw 167,251 167,567 +316 (+0.2%) 🔴
hydrate-root gzip 53,308 53,392 +84 (+0.2%) 🔴
hydrate-root brotli 46,419 46,520 +101 (+0.2%) 🔴
deferred-hydration raw 230,392 230,798 +406 (+0.2%) 🔴
deferred-hydration gzip 73,105 73,210 +105 (+0.1%) 🔴
deferred-hydration brotli 63,041 63,155 +114 (+0.2%) 🔴
binding-aria-esbuild gzip 3,363 3,361 −2 (−0.1%) 🟢

98 other measured values are unchanged.

@trueadm
trueadm marked this pull request as ready for review October 2, 2026 19:22

@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.

Stale Bugbot comment from a previous run.

Comment thread packages/octane/src/runtime.ts Resolved
trueadm and others added 2 commits October 2, 2026 20:56
A child that mounted inside content hydration rebuilt on the client, then
suspended, resumed with hydration active at the activation's saved cursor.
When that cursor stood on a server node left after the replaced one, such as
the rest of a range after a rebuilt single root, the child adopted that node
outside its rebuilt parent. Its own static roots were never created, and the
mismatch was reported a second time.

HydrationCapability.suspend now records the SuspenseException that leaves a
client-built subtree. A preserved activation stores whether its source
suspended there, and the resume renders that source under suspend(), while
still restoring the cursor for its ancestors' replay. The match is on the
exception object, allocated per throw, so another suspension on a shared
promise cannot match.

Reported by Cursor Bugbot on #1598.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…miere-f12800

# Conflicts:
#	packages/octane/src/runtime.ts

@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 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Range check skipped on resume remount
    • Added this.claimFrom !== null guard so the cursor !== this.claimFrom short-circuit only fires when claimFrom is actively set, ensuring claimRoots always runs its range-size check when claimFrom has been cleared by endClaim after a suspension.

Create PR

Or push these changes by commenting:

@cursor push 7f4cb37909
Preview (7f4cb37909)
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
@@ -20475,7 +20475,11 @@
 				// Roots that outnumber what the server rendered there, such as holes
 				// with no static element root after them, belong to other content,
 				// and only a rebuild creates them.
-				if (cursor !== this.claimFrom || this.claimRoots(cursor, template ?? lazy!) || claimsRoot)
+				if (
+					(this.claimFrom !== null && cursor !== this.claimFrom) ||
+					this.claimRoots(cursor, template ?? lazy!) ||
+					claimsRoot
+				)
 					return { __oct_vfrag: true, firstChild: cursor } as unknown as Node;
 			}
 			return this.rebuildFragment(template ?? resolveLazyTemplate(lazy!), cursor, loc);

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

Reviewed by Cursor Bugbot for commit 7e61cda. Configure here.

Comment thread packages/octane/src/runtime.ts Resolved
@cursor

cursor Bot commented Oct 2, 2026

Copy link
Copy Markdown

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Rebuilt fragment resume hydrates again
    • Re-applied the freshSuspension tracking mechanism lost during merge of PR fix(hydration): rebuild a hole-first fragment over another arm's nodes #1599: suspend() records SuspenseExceptions on freshSuspension, preserveSuspendedHydrateActivation sets a fresh flag on the activation, and the resume path wraps renderBlock in suspend() when fresh is true so the client-built leaf does not adopt server nodes.

Create PR

Or push these changes by commenting:

@cursor push 591f3a44c9
Preview (591f3a44c9)
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
@@ -14778,6 +14778,8 @@
 	capture: OffscreenCapture;
 	source: Block | null;
 	cursor: Node | null;
+	/** The source mounted inside client-built DOM, which it resumes as. */
+	fresh: boolean;
 }
 
 // Allocate bookkeeping only when an already-visible SSR arm really suspends.
@@ -14929,8 +14931,9 @@
 function preserveSuspendedHydrateActivation(
 	state: HydrateSlot,
 	hydration: HydrationCapability,
-	thenable: TrackedThenable<unknown>,
+	suspension: SuspenseException,
 ): void {
+	const thenable = suspension.thenable;
 	const suspendedBlock = findSuspendedHydrateBlock(state.block, thenable);
 	const activation: PreservedHydrateActivation = {
 		hydration,
@@ -14939,7 +14942,9 @@
 		capture: WIP_CAPTURE!,
 		source: suspendedBlock === state.block ? null : suspendedBlock,
 		cursor: hydration.resumeAt(),
+		fresh: hydration.freshSuspension === suspension,
 	};
+	hydration.freshSuspension = null;
 	const activations = (preservedHydrateActivations ??= new WeakMap());
 	if (!activations.has(state)) preservedHydrateActivationCount++;
 	activations.set(state, activation);
@@ -15129,7 +15134,7 @@
 			// The SSR arm is already the visible content. Let its adopted try block
 			// remain connected and retry adoption in the same hydration capability
 			// once application data settles, without ever mounting a cloned fallback.
-			preserveSuspendedHydrateActivation(state, hydration, error.thenable);
+			preserveSuspendedHydrateActivation(state, hydration, error);
 			return;
 		}
 		state.hydrated = true;
@@ -15893,12 +15898,16 @@
 			// ranges on an ordinary parent replay. Complete the actually suspended
 			// leaf at its saved cursor first, then let its ancestors reconcile the
 			// already-adopted siblings without treating their range as fresh DOM.
+			// A leaf that mounted inside a mismatch's client-built replacement has
+			// no server DOM to adopt and completes as a client render.
+			const source = preserved.source;
 			hydration.node = preserved.cursor;
 			try {
-				renderBlock(preserved.source);
+				if (preserved.fresh) hydration.suspend(() => renderBlock(source));
+				else renderBlock(source);
 			} catch (error) {
 				if (!isSuspenseException(error)) throw error;
-				preserveSuspendedHydrateActivation(state, hydration, error.thenable);
+				preserveSuspendedHydrateActivation(state, hydration, error);
 				return;
 			}
 		}
@@ -18858,6 +18867,8 @@
 	 * does not claim. Undefined until the template adopts claimFrom.
 	 */
 	private claimEnd: Node | null | undefined = undefined;
+	/** The last suspension thrown out of a client-built subtree (suspend). */
+	freshSuspension: SuspenseException | null = null;
 	/** Skip component-frame adoption until the declared container owner. */
 	passthroughRanges = false;
 	/**
@@ -18992,6 +19003,9 @@
 		const previousNative = setNativeAdoptionResolver(null);
 		try {
 			return fn();
+		} catch (error) {
+			if (isSuspenseException(error)) this.freshSuspension = error;
+			throw error;
 		} finally {
 			setNativeAdoptionResolver(previousNative);
 			this.depth--;

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

…r resumes

renderClaimed opens a claim on the server range a component adopted, so its
template can check that the range holds all of its roots and settle what is
left after them. A component that suspends before its template renders, such
as one that calls use() in setup, closed that claim on the throw. Its resume
then rendered the same block with no claim open. The template was adopted
over another component's nodes, and each root reported its own mismatch,
instead of the fragment being rebuilt once, as it is when the data is ready.

renderClaimed now records the block and its slot when its render suspends,
and renderSuspended renders a resumed source with a recorded claim through
renderClaimed again.

Reported by Cursor Bugbot on #1598.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm merged commit 5e586e2 into main Oct 2, 2026
32 checks passed
@trueadm
trueadm deleted the claude/intelligent-lumiere-f12800 branch October 2, 2026 21:28
trueadm added a commit that referenced this pull request Oct 3, 2026
… over its range (#1614)

* fix(hydration): remove the server tail after a component root rebuilt over its range

When a range's own single-root template did not match the range's first
server node, adopt() rebuilt the root, but only a successful adoption
recorded where the range's content ends (claimRoots). settleClaim then fell
back to its last-slot heuristic and left the rest of the server range on
screen, unreported.

The single-root mismatch path now records the claim when it rebuilds the
open claim's first node, under claimRoots' existing owner check, and
settleClaim removes that tail with sweepRebuiltTail, without a second
report. A lite component replayed by a <Hydrate> resume (#1598) sweeps the
same tail before stepping past its range.

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

* fix(hydration): sweep a resumed lite range only when its whole content was rebuilt

The <Hydrate> resume path swept the rebuilt root's server tail whenever one
was pending. When the rebuilt root came from a call in a fragment's leading
hole, that tail was the fragment's own adopted static root, so the resume
deleted it. The single-root mismatch path now records the scope whose
range's whole content the rebuilt root is (claimRoots' owner check), and a
lite replay sweeps only for that scope.

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

* fix(hydration): sweep a resumed rebuilt root's tail by range, in full component slots too

A <Hydrate> resume of a wrapper whose only call inherits its range left the
rebuilt root's server tail. The rebuild recorded the inheriting call's
scope, but the wrapper's slot is what completes on the resume, and that
wrapper renders in a full component slot, which never swept. The rebuild
now records the start marker of the range whose whole content it is
(rebuiltRange), and both the lite replay and componentSlotImpl's
post-render step sweep when their range starts there.

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

* fix(hydration): take a rebuilt root's range tail when the root commits

A <Hydrate> resume completes a range through whichever slot owns it, and
each slot kind needed its own sweep of a rebuilt root's server tail. The
root that is its range's whole content now takes the rest of that range
when it commits (rebuiltAt), while the cursor still rests right after the
node it replaces, so every slot kind and every resume path is covered.
The sweeps in settleClaim, componentSlotLite's replay and componentSlotImpl
are gone.

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

* fix(hydration): keep a markerless branch's rebuilt root out of the enclosing range

#1622 lets claimRoots accept a markerless branch's claim (claimOwner), which
has no open marker of its own. A rebuilt root that is such a branch's
content recorded the node before it as its range's start, which can be an
enclosing range's open marker. The commit-time take then removed the nodes
after the branch up to that range's end, including a later call's server
node (NotFoundError). Only a claim on a marker range records rebuiltRange.

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

* test(hydration): cover a rebuilt root whose component suspends before it commits

A <Hydrate> resume claims the range again when the component that rebuilt
its single root suspended before committing it (a use() in setup, or in the
template after the clone), so the root still takes its range's tail when
the resume commits it. Pin that for lite and full slots.

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

---------

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