Repository navigation
fix(hydration): resume a suspended child inside a rebuilt component fragment - #1598
Conversation
…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
Benchmark report
Compares js-framework: 1k/10k row operations (ms, lower is better)All 20 timed operations (20 within noise)
Application bundle size (bytes)No changes across 45 measured values. Public-import reachability bundles (bytes)
98 other measured values are unchanged. |
…miere-f12800 # Conflicts: # packages/octane/src/runtime.ts
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
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Range check skipped on resume remount
- Added
this.claimFrom !== nullguard so thecursor !== this.claimFromshort-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.
- Added
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.
|
Bugbot Autofix prepared a fix for the issue found in the latest run.
Or push these changes by commenting: 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]>
… 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]>


Summary
A preserved
<Hydrate>activation that suspends on a child inside a hydrated component fragment now resumes correctly. Four fixes: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
HoleFirstBranchshape): the server renders<><Bold /><Em /></>; the client renders<><FragChild leaf /><Em /></>, whereFragChildis<><Under leaf /><i>x</i></>andUndercallsuse(leaf). ResolvingleafthrewNotFoundError. It now reads<u>z</u><i>x</i><em>e</em>, keeps the server<em>, and reports the mismatch once.Why
componentSlotLitemoved 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.HydrationCapability.suspendwas 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, andRebuiltRootBranchreproduces it.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, ascomponentSlotImplalready does for its frame.HydrationCapability.suspendrecords theSuspenseExceptionthat leaves a client-built subtree (freshSuspension). A preserved activation stores whether its source suspended there, and the resume completes that source undersuspend(). 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.claimRootsnow 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<!---->), soadopt()rebuilds a fragment whose roots outnumber its range. It adds no work when the server matches, becauseclaimRootsalready walked those roots. Under aHYDRATION_RANGE_BOUNDARYpassthrough 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.tscovers that.renderClaimedrecords 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 throughrenderClaimedagain, so its template's range check and the leftover-content cleanup still run.Validation
pnpm format:check(not run repo-wide;pnpm format:files:checkon the changed files passes)pnpm typecheck(not run repo-wide;pnpm typecheck:filesonruntime.ts, the test, and the.tsrxfixture passes)pnpm test(not run as a whole; see the targeted runs below)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.packages/octane/tests/hydrationinoctaneandoctane-prod: 280 files, 13,228 tests passed.main: the fulloctaneandoctane-prodprojects passed, 1347 files and 37,338 tests.benchmarks/bundle-size/run-minimal.mjs, without the client-built resume fix, against currentmain: 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
<Hydrate>activations.<p>is rebuilt over a server node, the rest of that server range is left on screen (RebuiltRootBranchkeeps 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
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith 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.renderClaimedsuspensions stash their claim insuspendedClaimsandrenderSuspendedre-entersrenderClaimedon resume.Fragment range fit:
claimRootsreports 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.